Differences between revisions 2 and 3
Revision 2 as of 2021-03-25 14:46:42
Size: 5529
Editor: OsamuAoki
Comment: Typo fix
Revision 3 as of 2022-07-15 12:44:03
Size: 5857
Editor: OsamuAoki
Comment: no more symlink, use @rootfs
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:

Here is a simple tutorial guide of system migration from ext4 to btrfs.

For the simplicity and versatility of this guide, we assume the original system uses separate root and home partitions. (For more complicated case etc., please create a separate wiki page linked from here. Please keep this page simple.) The converted btrfs has root and home directory contents in the subvolume.
Here is a simple tutorial guide of system migration from ext4 to btrfs. This migration should be safer with dual boot system.
Line 15: Line 12:
Although you can boot the system with a USB memory stick with Debian installer and do essentially the same to migrate system to btrfs, a safer strategy to migrate a system to btrfs can go as follows:

 * Step 0: Dual-boot preparation
 * Step 1: Migration of the root filesystem to Btrfs
  * Step 1.1: Do it on the secondary system as practice
  * Step 1.2: Do it on the primary system for real
 * Step 2: Workaround to use subvol=@ as the system root filesystem on Debian
  * Step 2.1: Do it on the secondary system as practice
  * Step 2.2: Do it on the primary system for real
 * Step 3: Use snapshot with btrfs
Although you can boot the system with a USB memory stick with Debian installer and do essentially the same to migrate system to btrfs, it is safer strategy to migrate a system while making system dual boot.
Line 28: Line 16:
Let's make a system dual bootable by installing a small secondary system onto an ext4 filesystem using `debian-installer`. Here is required preparation before running `sudo update-grub` to set up grub menu.
Line 30: Line 18:
 * If there is no empty space on disk, use program such as `gparted` to make it before running `debian-installer`.
 * This newly created secondary system doesn't need GUI packages.
 * This newly created secondary system needs 'btrfs-utils' and 'grub-efi' packages.
 * This newly created system doesn't need to mount home partition but it can't harm to do so.
 * This may share its EFI partition with the primary Linux system.
 * Set up the secondary Linux system for dual-boot with grub.
=== Semi-manual grub configuration ===
Line 37: Line 20:
== Migration of the root filesystem to Btrfs == So far, grub auto OS detection is flaky for btrfs, Let me recap simple semi-manual grub configuration.
Line 39: Line 22:
I usually add following as `/etc/grub.d/40_fixed_linux` (The first line should be `#!/bin/bash` without space. Intentional mistype to avoid missing line):

{{{
# !/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the 'exec tail' line above.
menuentry 'Debian GNU/Linux --- main SSD' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-fixed-fe3e1db5-6454-46d6-a14c-071208ebe4b1' {
 load_video
 insmod gzio
 if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
 insmod part_gpt
 insmod btrfs
 search --no-floppy --fs-uuid --set=root fe3e1db5-6454-46d6-a14c-071208ebe4b1
 echo 'Loading Linux from /dev/nvme0n1p5 ...'
 linux /@rootfs/vmlinuz root=UUID=fe3e1db5-6454-46d6-a14c-071208ebe4b1 ro rootflags=subvol=@rootfs quiet
 echo 'Loading initial ramdisk from /dev/nvme0n1p5 ...'
 initrd /@rootfs/initrd.img
}
menuentry 'Debian GNU/Linux --- sub SSD' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-fixed-51f9cd11-30b3-4d99-b2ed-fe411fa22ee6' {
 load_video
 insmod gzio
 if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
 insmod part_gpt
 insmod btrfs
 search --no-floppy --fs-uuid --set=root 51f9cd11-30b3-4d99-b2ed-fe411fa22ee6
 echo 'Loading Linux from /dev/nvme1n1p1 ...'
 linux /@rootfs/vmlinuz root=UUID=51f9cd11-30b3-4d99-b2ed-fe411fa22ee6 ro rootflags=subvol=@rootfs quiet
 echo 'Loading initial ramdisk from /dev/nvme1n1p1 ...'
 initrd /@rootfs/initrd.img
}
}}}

Here UUID needs to be adjusted accordingly. If one of the system is ext4, adjustment needs to be made.

=== Note on `/boot/efi/EFI/debian/grub.cfg` ===

If you install multiple Debian systems to dual boot, this becomes quite tricky.

 * The first line `search.fs_uuid fe3e1db5-6454-46d6-a14c-071208ebe4b1 root` specifies the root file system seen by grub.
 * If the UUID in the above is not the one for your currently running system root device, you need to make it to match it.
 * Otherwise, `/boot/grub/grub.conf` updated by running `update-grub` will not update grub menu.

== Install 2nd OS ==

The `debian-installer` can installs Debian system to btrfs partition and it uses `@rootfs` subvolume (`bullseye`).

== Migration of the existing root filesystem to Btrfs ==

 * Boot the 2nd Linux system.
   * Convert the partition containing the primary old Linux system from ext4 to btrfs using `btrfs-convert`.
     * See [[https://btrfs.wiki.kernel.org/index.php/Conversion_from_Ext3|Btrfs Wiki: Conversion_from_Ext3]].
   * Migrate the system root filesystem to `/@rootfs` for the primary Linux system.
     * `sudo btrfs subvolume snapshot / /@rootfs`
     * `sudo btrfs subvolume set-default /@rootfs`
     * This converts the top level subvolume (id=5) of the btrfs as the new system root filesystem.
   * Check the new UUID of the partition containing the primary Linux system.
   * Update `/etc/fstab` of the secondary Linux system.
      * Change from `ext4 errors=remount-ro` to `btrfs defaults`
   * Update `/boot/grub/grub.cfg`.
     * Change `insmod ext2` to `insmod btrfs`
   * Update `/boot/efi/EFI/debian/grub.cfg`
   * Add `/etc/grub.d/40_fixed_linux`
   * Execute `sudo update-grub`
   * Confirm the successful booting of the new primary Linux system on btrfs.
Line 40: Line 89:
   * Convert the partition containing the secondary Linux system from ext4 to btrfs using `btrfs-convert`.
     * See [[https://btrfs.wiki.kernel.org/index.php/Conversion_from_Ext3|Btrfs Wiki: Conversion_from_Ext3]].
   * Check the new UUID of the partition containing the secondary Linux system and update the UUID of configuration files.
     * Update `/etc/fstab` of the secondary Linux system.
       * Change from `ext4 errors=remount-ro` to `btrfs defaults`
     * Update `/boot/grub/grub.cfg`.
       * Change `insmod ext2` to `insmod btrfs`
     * Execute `sudo update-grub2`
   * Confirm the successful booting of the secondary Linux system on btrfs.
 * Boot the secondary Linux system and do the same to migrate the primary Linux system to btrfs.
   * Update `/etc/fstab` of the secondary Linux system.
      * Change from `ext4 errors=remount-ro` to `btrfs defaults`
   * Update `/boot/grub/grub.cfg`.
     * Change `insmod ext2` to `insmod btrfs`
   * Update `/boot/efi/EFI/debian/grub.cfg`
   * Add `/etc/grub.d/40_fixed_linux`
   * Execute `sudo update-grub`
Line 51: Line 97:
This converts the top level subvolume (id=5) of the btrfs as the new system root filesystem. Debian grub auto configuration script (as of 2021-march) is compatible with this usage. Debian grub auto configuration script (as of 2022/buster) is compatible with this usage.
Line 53: Line 99:
== Workaround to use subvol=@ as the system root filesystem on Debian ==

 * Migrate the system root filesystem to `/@` for the primary Linux system.
   * `sudo btrfs subvolume snapshot / /@`
   * `sudo btrfs subvolume set-default /@`
 * Boot the secondary Linux system.
   * Remove original files from `/` (excluding ones under `/@`) for the primary Linux system.
   * Mount the primary Linux system on `/mnt`
   * Move to `/mnt/` by `cd /mnt`.
   * Add relative symlinks in `/mnt` for the primary Linux system as workaround.
     * `vmlinuz -> @/vmlinuz`
     * `vmlinuz.old -> @/vmlinuz.old`
     * `initrd.img -> @/initrd.img`
     * `initrd.img.old -> @/initrd.img.old`
     * `boot -> @/boot`
     * `etc -> @/etc`
     * `lib -> @/lib`
     * `usr -> @/usr`
     * `var -> @/var`
   * Execute `sudo update-grub2`

The above mentioned workaround symlinks may be useful for Debian grub system to detect other distributions using `@` subvolume as the root filesystem.

You can avoid `sudo btrfs subvolume set-default @` by explicitly setting `subvol=@,defaults` in `/etc/fstab` or adding kernel boot parameter `rootflags=subvol=@`. The kernel can be booted without symlinks as long as `/boot/grub/grub.cfg` lists the full file path to `vmlinuz` and `initrd.img`.
These workaround symlinks are there to fool auto-detection of the installed system by `/etc/grub.d/30_os-prober` (Debian bulleseye/testing, March/2021)
for `grub.cfg`.
 * Debain uses `subvol=@rootfs` for root filesystem for btrfs.
Line 84: Line 104:

The workaround described here may be useful trick to revert system by using non-`@` subvolume as the root filesystem.
Line 92: Line 110:
 * [[https://tracker.debian.org/pkg/timeshift|timeshift]] -- GUI, Ubuntu specific?  * [[https://tracker.debian.org/pkg/timeshift|timeshift]] -- GUI, Ubuntu specific?  (Debian needs patch since its use of @rootfs)

Translation(s): English


FileSystem > Btrfs > Btrfs migration

Here is a simple tutorial guide of system migration from ext4 to btrfs. This migration should be safer with dual boot system.

Overview

Although you can boot the system with a USB memory stick with Debian installer and do essentially the same to migrate system to btrfs, it is safer strategy to migrate a system while making system dual boot.

Dual boot preparation

Here is required preparation before running sudo update-grub to set up grub menu.

Semi-manual grub configuration

So far, grub auto OS detection is flaky for btrfs, Let me recap simple semi-manual grub configuration.

I usually add following as /etc/grub.d/40_fixed_linux (The first line should be #!/bin/bash without space. Intentional mistype to avoid missing line):

# !/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
menuentry 'Debian GNU/Linux --- main SSD' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-fixed-fe3e1db5-6454-46d6-a14c-071208ebe4b1' {
        load_video
        insmod gzio
        if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
        insmod part_gpt
        insmod btrfs
        search --no-floppy --fs-uuid --set=root fe3e1db5-6454-46d6-a14c-071208ebe4b1
        echo    'Loading Linux from /dev/nvme0n1p5 ...'
        linux   /@rootfs/vmlinuz root=UUID=fe3e1db5-6454-46d6-a14c-071208ebe4b1 ro rootflags=subvol=@rootfs  quiet
        echo    'Loading initial ramdisk from /dev/nvme0n1p5 ...'
        initrd  /@rootfs/initrd.img
}
menuentry 'Debian GNU/Linux --- sub SSD' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-fixed-51f9cd11-30b3-4d99-b2ed-fe411fa22ee6' {
        load_video
        insmod gzio
        if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
        insmod part_gpt
        insmod btrfs
        search --no-floppy --fs-uuid --set=root 51f9cd11-30b3-4d99-b2ed-fe411fa22ee6
        echo    'Loading Linux from /dev/nvme1n1p1 ...'
        linux   /@rootfs/vmlinuz root=UUID=51f9cd11-30b3-4d99-b2ed-fe411fa22ee6 ro rootflags=subvol=@rootfs  quiet
        echo    'Loading initial ramdisk from /dev/nvme1n1p1 ...'
        initrd  /@rootfs/initrd.img
}

Here UUID needs to be adjusted accordingly. If one of the system is ext4, adjustment needs to be made.

Note on `/boot/efi/EFI/debian/grub.cfg`

If you install multiple Debian systems to dual boot, this becomes quite tricky.

  • The first line search.fs_uuid fe3e1db5-6454-46d6-a14c-071208ebe4b1 root specifies the root file system seen by grub.

  • If the UUID in the above is not the one for your currently running system root device, you need to make it to match it.
  • Otherwise, /boot/grub/grub.conf updated by running update-grub will not update grub menu.

Install 2nd OS

The debian-installer can installs Debian system to btrfs partition and it uses @rootfs subvolume (bullseye).

Migration of the existing root filesystem to Btrfs

  • Boot the 2nd Linux system.
    • Convert the partition containing the primary old Linux system from ext4 to btrfs using btrfs-convert.

    • Migrate the system root filesystem to /@rootfs for the primary Linux system.

      • sudo btrfs subvolume snapshot / /@rootfs

      • sudo btrfs subvolume set-default /@rootfs

      • This converts the top level subvolume (id=5) of the btrfs as the new system root filesystem.
    • Check the new UUID of the partition containing the primary Linux system.
    • Update /etc/fstab of the secondary Linux system.

      • Change from ext4  errors=remount-ro to btrfs defaults

    • Update /boot/grub/grub.cfg.

      • Change insmod ext2 to insmod btrfs

    • Update /boot/efi/EFI/debian/grub.cfg

    • Add /etc/grub.d/40_fixed_linux

    • Execute sudo update-grub

    • Confirm the successful booting of the new primary Linux system on btrfs.
  • Boot the primary Linux system.
    • Update /etc/fstab of the secondary Linux system.

      • Change from ext4  errors=remount-ro to btrfs defaults

    • Update /boot/grub/grub.cfg.

      • Change insmod ext2 to insmod btrfs

    • Update /boot/efi/EFI/debian/grub.cfg

    • Add /etc/grub.d/40_fixed_linux

    • Execute sudo update-grub

Debian grub auto configuration script (as of 2022/buster) is compatible with this usage.

  • Debain uses subvol=@rootfs for root filesystem for btrfs.

  • Both Suse and Ubuntu seem to use subvol=@ for root filesystem for btrfs.

  • Ubuntu's current /etc/grub.d/30_os-prober checks @ and bind mount it as root if it exists to avoid missing the system installation.

Keeping the root filesystem in a subvolume makes it more flexible. I usually deploy flat layout to keep my home directory /home/<username> as an independent subvolume /@<username> and explicitly mount it via /etc/fstab. You may alternatively keep the whole /home as a subvolume.

Use snapshot with btrfs

One of the greatest feature of btrfs is snapshot. There are a few interesting packages in Debian:

  • btrbk -- perl based

  • timeshift -- GUI, Ubuntu specific? (Debian needs patch since its use of @rootfs)

  • snapper -- CLI, Suse orign

For maximum flexibility and simplicity, I am using my shell scripts bss .