[HowTo] portable manjaro USB with XFCE, btrfs (part 2)

Difficulty: ★★★☆☆ :uk: :de:
:point_right: part1

switch from ext4 to btrfs without losing the data :wink:

benefits
  • It protects the stick because btrfs works with Copy_on_Write
  • It offers the possibility to make automatic snapshots with snapper. This allows you to undo accidental deletion or modification of files within a given period of time. (This is not a substitute for a good backup)
  • It offers the possibility to save the files compressed. This increases the capacity and also protects the stick
  • After a failed update, you can switch back to the last snapshot (rollback)
  • The subvolumes for / and /home are separate, rollback does not result in loss of data
  • Even after a rollback, the data from the unsuccessful update (read only) can be accessed. So it can be analyzed well.
disadvantages
  • btrfs needs maintenance from time to time
  • The rollback is manual work (although only a few commands)

https://btrfs.wiki.kernel.org/index.php/Conversion_from_Ext3/4_and_ReiserFS

Please open and work through one step after the other

Preparation

  • Insert our manjaro stick to a “normal” Linux system or a Plug in Linux live system (do not mount).
  • Which device is the stick?
  • Pay attention to the partitioning and the size.
lsblk
 NAME        MAJ:MIN RM    SIZE RO TYPE MOUNTPOINTS
 ...
 sdz           8:32   1   59,5G  0 disk
 ├─sdz1        8:33   1    250M  0 part
 ├─sdz2        8:34   1      1M  0 part 
 └─sdz3        8:35   1   59,3G  0 part 
 sdy           8:48   1   14,7G  0 disk 
 ├─sdy1        8:49   1    250M  0 part 
 ├─sdy2        8:50   1      1M  0 part 
 └─sdy3        8:51   1   14,5G  0 part 

Our 64GB stick is /dev/sdz. 3 partitions, 60GB size, 250MB boot partition, 59G data partition - fits. Wherever ‘sdz1’, ‘sdz3’ or ‘z3$’ is used in the following, you have to replace the ‘z’ with the ‘appropriate’ letter.

Always check beforehand whether the ext4 file system is OK
fsck.ext4 /dev/sdz3

e2fsck 1.46.2 (28-Feb-2021)
/dev/sdz3: sauber, 219881/3891200 Dateien, 1312455/15541243 Blöcke

Now convert to btrfs
btrfs-convert /dev/sdz3
create btrfs filesystem:
	blocksize: 4096
	nodesize:  16384
	features:  extref, skinny-metadata (default)
	checksum:  crc32c
free space report:
	total:     63656931328
	free:      57548845056 (90.40%)
creating ext2 image file
creating btrfs metadata
copy inodes [.] [    219706/    219881]
conversion complete
make bootable again (adjust UUIDs)

The UUID and the PARTUUID have changed. We will need these two numbers later. (You can then insert it with copy & paste)

ls -lA /dev/disk/by-uuid |egrep 'z3$'

lrwxrwxrwx 1 root root 10 19. Jun 07:58 1a488830-65f7-4913-bf4d-dfe3f3ab5634 → …/…/sdz3

ls -lA /dev/disk/by-partuuid |egrep 'z3$'

lrwxrwxrwx 1 root root 10 19. Jun 07:58 026eb222-79f0-4dd8-8735-4ea6735f9203 → …/…/sdz3

mount partitions
sudo mkdir -p /mnt/alma/1 /mnt/alma/3
mount -t vfat /dev/sdz1 /mnt/alma/1
mount -t btrfs -o subvol=/,defaults,compress=zstd:9 /dev/sdz3 /mnt/alma/3
Back up grub.cfg and fstab
cp /mnt/alma/1/grub/grub.cfg /mnt/alma/1/grub/grub.cfg.old
cp /mnt/alma/3/etc/fstab /mnt/alma/3/etc/fstab.old
Adapt grub.cfg so that the stick boots
  • Show old value
cat /mnt/alma/1/grub/grub.cfg |egrep -o '[0-f]{8}[0-f-]{28}'

d8fb2a88-6bc3-4f79-9ac0-5be115f04ec5

  • new value should be:
ls -lA /dev/disk/by-uuid |egrep 'sdz3'

lrwxrwxrwx 1 root root 10 19. Jun 07:58 1a488830-65f7-4913-bf4d-dfe3f3ab5634 → …/…/sdz3

  • Assemble sed from the above values
sed -i** 's/d8fb2a88-6bc3-4f79-9ac0-5be115f04ec5/1a488830-65f7-4913-bf4d-dfe3f3ab5634/g' **/mnt/alma/1/grub/grub.cfg
  • Please check the new value
cat /mnt/alma/1/grub/grub.cfg |egrep -o '[0-f]{8}[0-f-]{28}'

1a488830-65f7-4913-bf4d-dfe3f3ab5634

Customize btrfs layout

btrfs has no layout at all here. I prefer a flat layout (prepared for snapper):

toplevel	   	    (volume root directory, not to be mounted by default)
  ├─@	   	        (subvolume root directory, mount at /)	
  ├─@home 		    (subvolume home directory, mount at /home)
  ├─@nosnap 	    (subvolume without snapshots, mount at /var/log ...)
  ├─@snapshots 	    (snapshots of /, mount at /.snapshots)
  ├─@home.snapshots (snapshots of /home, mount at /home/.snapshots)
  └─@ext4_saved	    (subvolume of saved ext4 filesystem-data, delete later on)

SysadminGuide - btrfs Wiki

btrfs subvolume create /mnt/alma/3/@

Create subvolume ‘/mnt/alma/3/@’

btrfs subvolume create /mnt/alma/3/@home

Create subvolume ‘/mnt/alma/3/@home

btrfs subvolume create /mnt/alma/3/@nosnap

Create subvolume ‘/mnt/alma/3/@nosnap

btrfs subvolume create /mnt/alma/3/@snapshots

Create subvolume ‘/mnt/alma/3/@snapshots

btrfs subvolume create /mnt/alma/3/@home.snapshots

Create subvolume ‘/mnt/alma/3/@home.snapshots

mv /mnt/alma/3/ext2_saved /mnt/alma/3/@ext2_saved
mv /mnt/alma/3/home/\* /mnt/alma/3/@home/
mv /mnt/alma/3/[a-z]\* /mnt/alma/3/@/
btrfs subvolume set-default /mnt/alma/3/@
Customize fstab (mount /, /home)

https://www.reddit.com/r/linux/comments/imgler/btrfs_relatime_vs_noatime_huge_performance/

The fstab file on the stick must be adapted to btrfs and the layout. For this we need the PARTUUID of the btrfs partition

ls -lA /dev/disk/by-partuuid |egrep 'sdz3'

lrwxrwxrwx 1 root root 10 19. Jun 07:58 026eb222-79f0-4dd8-8735-4ea6735f9203 → …/…/sdz3

The fstab must be edited by hand. I use mcedit, but nano or vim are just as good. We write the Partuuid in advance at the end of the fstab. Then editing is easier. :wink:

ls -lA /dev/disk/by-partuuid |egrep 'sdz3' >> /mnt/alma/3/@/etc/fstab
mcedit /mnt/alma/3/@/etc/fstab
  • The line with vfat remains untouched
  • The line with ext4 is commented out (#)
  • Also the line with “lrwxrwxrwx” where the partuuid is (#)
  • With this PARTUUID 2 new lines are created for / and /home

The result looks like this for me:

# UUID=d8fb2a88-6bc3-4f79-9ac0-5be115f04ec5	/         	ext4      	rw,noatime	0 1
# lrwxrwxrwx 1 root root 10 19. Jun 07:58 026eb222-79f0-4dd8-8735-4ea6735f9203 -> ../../sdz3
PARTUUID=026eb222-79f0-4dd8-8735-4ea6735f9203	/	btrfs	rw,noatime,compress=zstd:9,subvol=@	0 0
PARTUUID=026eb222-79f0-4dd8-8735-4ea6735f9203	/home	btrfs	rw,noatime,compress=zstd:9,subvol=@home	0 0
UUID=003C-4A02      	/boot     	vfat      	rw,noatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro	0 2
Delete backup of ext4 data (don't forget !)
btrfs subvolume delete -c /mnt/alma/3/@ext2_saved
unmount
umount /mnt/alma/3
umount /mnt/alma/1

Now the USB stick can be tried out :grinning: