Welcome, Guest. Please login or register.
Did you miss your activation email?

Author Topic: [EN] Conversion from ext4 to Btrfs  (Read 316 times)

Offline scholle1

  • User
  • Posts: 62
[EN] Conversion from ext4 to Btrfs
« on: 2023/04/10, 22:15:22 »

This guide covers converting a current siduction installation with the ext4 file system to the Btrfs file system. All data and configurations of the user should be preserved. The goal is to get a siduction on Btrfs that is as close as possible to a new installation.
In contrast to many instructions that can be found on the Internet, I refer here to the requirements and specifics of the siduction installation.

I tested this guide several times with a freshly installed siduction (KDE flavor) and then successfully applied it to one of my siduction installations (XFCE flavor).

Please read the man page btrfs-convert first, and note that a rollback of the conversion is not possible after the command 'btrfs balance' has been executed.


When working on the file system, there is always a risk of data loss. It is strongly recommended to back up all important and personal data to an external medium.


- "/" on only one partition (in our example /dev/sda3)
- The "/" partition should be at least 50 GB in size.
- The total size of the "/" partition must also be at least 2.5 times the disk space currently being used, because an image of the previous system is created during the conversion.
- Where possible, clear the cache.
- The system must be up-to-date. (apt update && apt full-upgrade)
- Unused kernels (kernel-remover) and orphaned packages (apt autoremove) should be removed.
- Minimize system journal. (journalctl --vacuum-size 100M)
- Afterwards do "apt clean".

Perform conversion

a) Install the siduction-btrfs package if it is not already present.

b) Download siduction and create a bootable medium.

c) Reboot into live system, open a terminal, become root.

d) Filesystem Check /dev/sda3 (not mounted):
Code: [Select]
# fsck.ext4 -f /dev/sda3

e) Convert to Btrfs:
Code: [Select]
# btrfs-convert /dev/sda3    This may take some time.
    Attention: The UUID of the /dev/sda3 partition changed

f) Mount the new Btrfs filesystem:
Code: [Select]
# mount -t btrfs /dev/sda3 /mnt/
# cd /mnt/

    Show Btrfs subvolume:
Code: [Select]
/mnt# btrfs subvolume list .
ID 256 gen 3 top level 5 path ext2_saved

    The content, now with ext2_saved, and .snapshots:
Code: [Select]
/mnt# ls -a
 .             fll             media       sys
 ..            home            mnt         tmp
 .autorelabel  initrd.img      opt         usr
 bin           initrd.img.old  proc        var
 boot          lib             root        vmlinuz
 dev           lib32           run         vmlinuz.old
 disks         lib64           sbin        .xorgconfig-was-here
 etc           libx32          .snapshots
 ext2_saved    lost+found      srv

g) Create the recommended Btrfs subvolumes:
Code: [Select]
/mnt# btrfs subvolume create @
Create subvolume './@'
/mnt# btrfs subvolume create @home
Create subvolume './@home'
/mnt# btrfs subvolume create @root
Create subvolume './@root'
/mnt# btrfs subvolume create @var@log
Create subvolume './@var@log'
/mnt# btrfs subvolume create @snapshots
Create subvolume './@snapshots'
/mnt# btrfs subvolume create @tmp
Create subvolume './@tmp'

    Move the data to the subvolumes:
    Be sure to follow the order, because we need the empty /home, /root and /var/log folders in the @ subvolume as mount points.
Code: [Select]
/mnt# rmdir lost+found  (not necessary in Btrfs)
/mnt# mv home/* @home/
/mnt# mv root/.* @root/
    if /root also contains files without leading dot
/mnt# mv root/* @root/
/mnt# mv var/log/* @var@log/
/mnt# mv [b-d]* @/
/mnt# mv [f-z]* @/
/mnt# mv etc @/
/mnt# mv .[!s]* @/

    The result should be like this:
Code: [Select]
/mnt# ls -a
.  ..  @  ext2_saved  @home  @root  .snapshots  @snapshots  @tmp  @var@log

    Set the subvolume @ as default:
Code: [Select]
/mnt# btrfs subvolume set-default @    The ext2_saved folder contains the backup image of the previous ext4 file system. This subvolume also includes the '.snapshots' folder. Beyond that, we see only the new subvolumes.
    Optionally, use @data instead of @home. The /home directory remains in @. Move all personal data of the /home/<user> directory to @data and link them in /home/<user>. The configuration remains in /home. This way the user's configuration is also captured by system snapshots.

h) Customize the /etc/fstab:
    Backup fstab:
Code: [Select]
# cd @/etc/
/mnt/@/etc# cp fstab fstab-bac

    Comment out the old tmp entry:
Code: [Select]
/mnt/@/etc# sed -i 's!^tmp!# tmp!' fstab
    Search for old UUID of /:
Code: [Select]
/mnt/@/etc# grep '^UU.* / ' fstab
UUID=0b99b209-fa46-4ae3-ab70-b247653ad6c1 /    ext4    defaults,noatime 0 1

    Search for new UUID of /:
Code: [Select]
/mnt/@/etc# blkid | grep sda3
/dev/sda3: UUID="d9772d13-bd51-4d48-9f55-60fea4d369e5" UUID_SUB="ea8...

    Change / to new UUID, filesystemtyp, and options:
  (use your exact UUIDs and use copy&past from the output
  above to insert the UUID)
Code: [Select]
/mnt/@/etc# sed -i 's!<old UUID>!<new UUID>!' fstab
/mnt/@/etc# sed -i 's!ext4.*!btrfs    subvol=@,defaults,noatime,space_cache=v2,autodefrag,compress=zstd 0 1!' fstab

    Add entries for the other subvolumes:
Code: [Select]
/mnt/@/etc# grep ' / ' fstab | sed -e 's!/    !/home!' -e 's!=@!=@home!' >>fstab
/mnt/@/etc# grep ' / ' fstab | sed -e 's!/    !/root!' -e 's!=@!=@root!' >>fstab
/mnt/@/etc# grep ' / ' fstab | sed -e 's!/      !/var/log!' -e 's!=@!=@var@log!' >>fstab
/mnt/@/etc# grep ' / ' fstab | sed -e 's!/  !/tmp!' -e 's!=@!=@tmp!' >>fstab
/mnt/@/etc# grep ' / ' fstab | sed -e 's!/          !/.snapshots!' -e 's!=@!=@snapshots!' -e 's!^!#!' >>fstab

    The fstab file should look like this:
Code: [Select]
/mnt/@/etc# cat fstab
# <file system>            <mount point>  <type>  <options>  <dump>  <pass>
UUID=<new-UUID> /              btrfs    subvol=@,defaults,noatime,space_cache=v2,autodefrag,compress=zstd 0 1
# tmpfs                                    /tmp          tmpfs  defaults,noatime,mode=1777 0 0
UUID=<new-UUID> /root          btrfs    subvol=@root,defaults,noatime,space_cache=v2,autodefrag,compress=zstd 0 1
UUID=<new-UUID> /home          btrfs    subvol=@home,defaults,noatime,space_cache=v2,autodefrag,compress=zstd 0 1
UUID=<new-UUID> /var/log      btrfs    subvol=@var@log,defaults,noatime,space_cache=v2,autodefrag,compress=zstd 0 1
UUID=<new-UUID> /tmp          btrfs    subvol=@tmp,defaults,noatime,space_cache=v2,autodefrag,compress=zstd 0 1
#UUID=<new-UUID> /.snapshots    btrfs    subvol=@snapshots,defaults,noatime,space_cache=v2,autodefrag,compress=zstd 0 1

i) Unmount the new Brtfs filesystem and exit the terminal:
Code: [Select]
/mnt/@/etc# cd /
# umount /mnt
# exit

j) chroot into siduction on Btrfs with the desktop icon
    "siduction chroot helper"

k) Backup initrd and create a new one:
  (use your exact kernel label)
Code: [Select]
# cp /boot/initrd.img-6.2.9-1-siduction-amd64 /boot/initrd.img-6.2.9-1-siduction-amd64-bac
# mkinitramfs -o /boot/initrd.img-6.2.9-1-siduction-amd64 6.2.9-1-siduction-amd64

l) Update the bootloader GRUB:
Code: [Select]
# mount /boot/efi/  (only necessary with UEFI/GPT)
    Backup grub.cfg
Code: [Select]
# cp /boot/grub/grub.cfg /boot/grub/grub.cfg-bac
    Reactivate the file 09_siduction-btrfs and remove any systemd masking links.
Code: [Select]
# chmod 755 /etc/grub.d/09_siduction-btrfs
# rm /etc/systemd/system/siduction_btrfs* 2>/dev/null

    Update and install Grub:
Code: [Select]
# update-grub
# grub-install
    with BIOS/MBR, and UEFI/GPT with multiple hard disks in the PC.
# grub-install /dev/sda

m) Leave chroot and reboot into siduction on Btrfs.

n) Configure snapper:
    Open a terminal and become root.
    Create the configuration for the directory '/' with the new name 'root':
Code: [Select]
# snapper -c root create-config -t default /    Here, errors occasionally occurred with Snapper during the tests. No configuration could be created.
    The only remedy was to remove the packages in question using 'purge' and reinstall them.
      # apt purge snapper* siduction-btrfs libbtrfs0 libsnapper6
      # apt install snapper* siduction-btrfs libbtrfs0 libsnapper6

    Now edit the file /etc/fstab in an editor with root privileges.
    Remove the comment character '#' before the line with /.snapsnots. Save the file and close the editor.

    Mount the subvolume '@snapshots' and reload systemd:
Code: [Select]
# mount /.snapshots/
# systenctl daemon-reload

    Then change some key-value pairs of the snapper configuration for '/'.
Code: [Select]
# snapper -c root set-config ALLOW_USERS=<Username>
# snapper -c root set-config TIMELINE_CREATE=no
    Now Snapper is active for the directory "/".
    The Snapper configuration should be done with the help of the instructions at
    https://manual.siduction.org/ "System Administration > Snapper".
    to adapt it to your own needs.

o) Emergency rollback of btrfs-convert:
    If significant problems occur, it is possible to roll back the conversion.

    Reboot into the live system, open a terminal and become root.
Code: [Select]
# btrfs-convert -r /dev/sda3
    The '.snapshots' folder must be removed manually.
Code: [Select]
# rmdir /.snapshots

p) Remove backup flies:
    When the system works properly, we remove the backup copies created during the conversion.
    Open a terminal, become root.

    Remove the files:
Code: [Select]
# rm /etc/fstab-bac
# rm /boot/initrd.img-6.2.9-1-siduction-amd64-bac
# rm /boot/grub/grub.cfg-bac

    Show Btrfs subvolume:
Code: [Select]
# btrfs subvolume list /
ID 256 gen 3 top level 5 path ext2_saved
ID 257 gen 283 top level 5 path @
ID 258 gen 286 top level 5 path @home
ID 269 gen 319 top level 5 path @root
ID 260 gen 328 top level 5 path @var@log
ID 261 gen 196 top level 5 path @snapshots
ID 262 gen 228 top level 5 path @tmp

    Remove the backup subvolume ext2_saved, using its ID.
Code: [Select]
# btrfs subvolume delete --subvolid 256 /
Delete subvolume (no-commit): '//ext2_saved'


I hope the instructions were helpful.
If you have any questions, please post them here in the forum.
« Last Edit: 2023/04/10, 23:26:24 by scholle1 »
Je mehr Bürgerinnen und Bürger mit Zivilcourage ein Land hat, desto weniger Helden wird es einmal brauchen.
(Franka Magnani)