Siduction Forum > Scripting & Kernelhacking

[EN] Conversion from ext4 to Btrfs

(1/1)

scholle1:
Introduction

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.


Warning

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.


Requirements

- "/" 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: ---# fsck.ext4 -f /dev/sda3
--- End code ---


e) Convert to Btrfs:

--- Code: ---# btrfs-convert /dev/sda3
--- End code ---
    This may take some time.
    Attention: The UUID of the /dev/sda3 partition changed


f) Mount the new Btrfs filesystem:

--- Code: ---# mount -t btrfs /dev/sda3 /mnt/
# cd /mnt/
--- End code ---

    Show Btrfs subvolume:

--- Code: ---/mnt# btrfs subvolume list .
ID 256 gen 3 top level 5 path ext2_saved
--- End code ---

    The content, now with ext2_saved, and .snapshots:

--- Code: --- /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
--- End code ---


g) Create the recommended Btrfs subvolumes:

--- Code: ---/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'
--- End code ---

    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: ---/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]* @/
--- End code ---

    The result should be like this:

--- Code: ---/mnt# ls -a
.  ..  @  ext2_saved  @home  @root  .snapshots  @snapshots  @tmp  @var@log
--- End code ---

    Set the subvolume @ as default:

--- Code: ---/mnt# btrfs subvolume set-default @
--- End code ---
    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: ---# cd @/etc/
/mnt/@/etc# cp fstab fstab-bac
--- End code ---

    Comment out the old tmp entry:

--- Code: ---/mnt/@/etc# sed -i 's!^tmp!# tmp!' fstab
--- End code ---

    Search for old UUID of /:

--- Code: ---/mnt/@/etc# grep '^UU.* / ' fstab
UUID=0b99b209-fa46-4ae3-ab70-b247653ad6c1 /    ext4    defaults,noatime 0 1
--- End code ---

    Search for new UUID of /:

--- Code: ---/mnt/@/etc# blkid | grep sda3
/dev/sda3: UUID="d9772d13-bd51-4d48-9f55-60fea4d369e5" UUID_SUB="ea8...
--- End code ---

    Change / to new UUID, filesystemtyp, and options:
  (use your exact UUIDs and use copy&past from the output
  above to insert the UUID)

--- Code: ---/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
--- End code ---

    Add entries for the other subvolumes:

--- Code: ---/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
--- End code ---

    The fstab file should look like this:

--- Code: ---/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

--- End code ---


i) Unmount the new Brtfs filesystem and exit the terminal:

--- Code: ---/mnt/@/etc# cd /
# umount /mnt
# exit
--- End code ---


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: ---# 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
--- End code ---


l) Update the bootloader GRUB:

--- Code: ---# mount /boot/efi/  (only necessary with UEFI/GPT)
--- End code ---

    Backup grub.cfg

--- Code: ---# cp /boot/grub/grub.cfg /boot/grub/grub.cfg-bac
--- End code ---

    Reactivate the file 09_siduction-btrfs and remove any systemd masking links.

--- Code: ---# chmod 755 /etc/grub.d/09_siduction-btrfs
# rm /etc/systemd/system/siduction_btrfs* 2>/dev/null
--- End code ---

    Update and install Grub:

--- Code: ---# update-grub
# grub-install
    or,
    with BIOS/MBR, and UEFI/GPT with multiple hard disks in the PC.
# grub-install /dev/sda
--- End code ---


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: ---# snapper -c root create-config -t default /
--- End code ---
    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: ---# mount /.snapshots/
# systenctl daemon-reload
--- End code ---

    Then change some key-value pairs of the snapper configuration for '/'.

--- Code: ---# snapper -c root set-config ALLOW_USERS=<Username>
# snapper -c root set-config TIMELINE_CREATE=no
--- End code ---
    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: ---# btrfs-convert -r /dev/sda3
--- End code ---

    The '.snapshots' folder must be removed manually.

--- Code: ---# rmdir /.snapshots
--- End code ---


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: ---# rm /etc/fstab-bac
# rm /boot/initrd.img-6.2.9-1-siduction-amd64-bac
# rm /boot/grub/grub.cfg-bac
--- End code ---

    Show Btrfs subvolume:

--- Code: ---# 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
--- End code ---

    Remove the backup subvolume ext2_saved, using its ID.

--- Code: ---# btrfs subvolume delete --subvolid 256 /
Delete subvolume (no-commit): '//ext2_saved'
--- End code ---


EVERYTHING DONE

I hope the instructions were helpful.
If you have any questions, please post them here in the forum.

Navigation

[0] Message Index

Go to full version
Powered by Advanced Topic Prefix Pro
Powered by SMFPacks WYSIWYG Editor