[ Content contribution] [Btrfs Now] Installing rootfs with btrfs & Set the restore env
Tofloor
poster avatar
Ziggy
deepin
2024-03-27 10:57
Author

[Btrfs Now] Installing the rootfs into the btrfs subvols for deepin & Set the restore env

Pre-preparation

Installation Preparation

  1. Prepare a installation image and flash it with a mobile storage device, a kernel with version 5.10+ is recommend, which has a better support for btrfs.
  2. Set 2 partitations in advance.
  • A btrfs partitation as the main rootfs.
  • A live system with full functions.
  • An independent partition to install the initial system. It could be installed into an extranal hard disk instead. When the hard disk where btrfs located is broken, it can be restored as a rootb backup. Use non-btrfs format for differentiation(Recommended)

Create subvols for btrfs partitation.

  1. Mount the btrfs partitation to /mnt,then create some required subvols.
ziggy@ziggy-PC:~$ lsblk -f
NAME   FSTYPE FSVER LABEL     UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
sdb                                                                           
├─sdb1 vfat   FAT32           DCF5-3A2C                                           /boot/efi
├─sdb2 swap   1     swap      e2a198a1-6e12-4248-bca8-b5cfbf4ce723                [SWAP]
├─sdb3 btrfs        deepin    68e253cf-9282-47b3-b087-9c5910950c88   89.9G    32% /

The btrfs partitation is /dev/sdb3 according to the lsblk,then mount it.

root@ziggy-PC:~# mount /dev/sdb3 /mnt/
  1. When the btrfs partition is mounted as /mnt,/mnt is the btrfs rootfs and does not have any subvolume functions. In addition, btrfs is still in developing and depends on the boot sequence of btrfs grub, it is recommended to set up a root subvolume separately from the system subvolume to serve as the boot one.
  2. The following example sets the subvolumes in sequence "@main(btrfs rootfs),@home (users home directory),@opt (applications directory),@root(the home of root, optional)"
root@ziggy-PC:/mnt# btrfs subvolume create /mnt/@main
root@ziggy-PC:/mnt# btrfs subvolume create /mnt/@home
root@ziggy-PC:/mnt# btrfs subvolume create /mnt/@opt
root@ziggy-PC:/mnt# btrfs subvolume create /mnt/@root

System Setup

Initial System Installation

  1. Boot the installation flash, select non-btrfs format volumes as "/" mount points for installation, and select only "/" mount points for installation during installation to facilitate subsequent data migration. Suggested mount examples: "/" root directory (required), swap files (optional),efi (if UEFI boot)

Data Syns

For two well-known reasons, after installing the initial system, if we want the system to run normally on the btrfs volume and enable the snapshot function, we need to manually sync the installed system to the btrfs subvols.

  • The deepin-installer does support to install the system into the btrfs subvols so far.
  • The initial system env is the most clean one before the configure step.
  1. Check the lsblk result,the "/dev/sda1" is the initial system path while "/dev/sdb3/@main" is the main rootfs of the btrfs subvols.
sda                                                                           
├─sda1 ext4   1.0   Roota     a7f4b3d4-5d3d-45a1-a27b-798aa6fbae48   30.7G    26% /media/ziggy/Roota
sdb                                                                           
├─sdb1 vfat   FAT32           DCF5-3A2C                                           /boot/efi
├─sdb2 swap   1     swap      e2a198a1-6e12-4248-bca8-b5cfbf4ce723                [SWAP]
├─sdb3 btrfs        deepin    68e253cf-9282-47b3-b087-9c5910950c88   89.9G    32% /mnt
  1. After confirmation, use the rsync to sync the data. Since "@ home,@ opt,@ root" are divided separately in this example, the data of the corresponding directory in the initial system also needs to be synchronized to the subvol.
root@ziggy-PC:/mnt# rsync -avrP /media/ziggy/Roota /mnt/@main
root@ziggy-PC:/mnt# rsync -avrP /media/ziggy/Roota/opt /mnt/@opt
root@ziggy-PC:/mnt# rsync -avrP /media/ziggy/Roota/home /mnt/@home
root@ziggy-PC:/mnt# rsync -avrP /media/ziggy/Roota/root /mnt/@root

Files Reconfiging

After the data sync is complete, several configuration files need to be reconfigured due to the inconsistency between the btrfs subvols and the original installation path.

/boot

The grub.cfg boot file needs to be modified in the/boot directory, which can be divided into several steps.
"Firmware==>efi files==>Grub==>Btrfs rootfs==>the Grub of Btrfs subvols==>The rootfs of Btrfs subvols"

  1. Replace the UUID of the initial system with the UUID of the btrfs partitation by searching.

    截图_选择区域_20240324215338.png

  2. Observe the reference to the absolute path of the system in the grub.cfg file and add "/@ main" before "/" to indicate that the path is the path in the subvolume after modification.

    截图_选择区域_20240324215426.png

  3. Add "rootflags=subvol=@main" to the linux line to indicate that the @main subvols would be boot.

        linux   /@main/boot/vmlinuz-6.1.32-amd64-desktop-hwe root=UUID=68e253cf-9282-47b3-b087-9c5910950c88 ro splash quiet rootflags=subvol=@main DEEPIN_GFXMODE=$DEEPIN_GFXMODE
        initrd  /@main/boot/initrd.img-6.1.32-amd64-desktop-hwe

/etc

  1. Modify the fstab file and modify the mount information according to the actual subvolume and requirements. The core here is the root directory mount point setting. Other mount points can be modified by referring to the root directory mount point. Only the subvol pointing to the subvolume needs to be modified.
#                
# /dev/sdb1 LABEL=Roota
UUID=68e253cf-9282-47b3-b087-9c5910950c88       /               btrfs           rw,relatime,ssd,compress=zstd,subvol=@main     0 1

UUID=68e253cf-9282-47b3-b087-9c5910950c88       /home           btrfs           rw,relatime,ssd,subvol=@home 0 1

UUID=68e253cf-9282-47b3-b087-9c5910950c88       /opt            btrfs           rw,relatime,ssd,subvol=@opt  0 1
  1. Modify the/etc/default/grub file and add "rootflags=subvol=@main" to the "GRUB_CMDLINE_LINUX_DEFAULT" field, so that the startup command will be automatically added to the new grub file after run "update-grub"
root@ziggy-PC:/boot/grub# cat /etc/default/grub
# Written by org.deepin.dde.Grub2
GRUB_CMDLINE_LINUX_DEFAULT="splash quiet rootflags=subvol=@main"

EFI(If UEFI boot)

  1. Get the UUID of the partition where the/boot/efi mount point is located through lsblk and mount it to/mnt
root@ziggy-PC:/boot/grub#mount /dev/sdb1 /mnt
  1. Enter to the/mnt , find the boot and deepin directories, and modify the grub.cfg file. Change "search.fs_uuid" to the UUID information corresponding to the btrfs parent volume, and add the path corresponding to the subvolume to "set prefix"
root@ziggy-PC:/boot/efi/EFI/deepin# cat grub.cfg 
search.fs_uuid 68e253cf-9282-47b3-b087-9c5910950c88 root hd0,gpt2
set prefix=($root)'/@main/boot/grub'
configfile $prefix/grub.cfg

Snapshots Setup

Since the @main subvolume mentioned earlier is used for the rootfs and is not intended to be used as a common system subvolume, create a @sys snapshot subvolume on that basis to use as a daily environment.

root@ziggy-PC:/# btrfs subvolume snapshot /mnt/@main /mnt/@sys

After the snapshot is created, as the subvolume has changed, you need to repeat the "### file configuration" step to adjust the contents of the new subvolume, but the common file does not need to be changed.

/boot

  1. Modify the grub.cfg file in the @sys subvolume, and add "rootflags=subvol=@sys" to the linux line to indicate that grub boots the @sys subvolume in the btrfs parent volume, and both initrd and linux paths in the boot entry need to be modified to the current system @sys subvolume

/etc

  1. Modify the fstab file in the @sys subvolume and modify the mount information according to the actual subvolume and requirements. The core here is the root directory mount point setting. Other mount points can be modified by referring to the root directory mount point. Only the subvol pointing to the subvolume required to be modified.
  2. Modify the/etc/default/grub file and add "rootflags=subvol=@sys" to the "GRUB_CMDLINE_LINUX_DEFAULT" field, so that the startup command will be automatically added to the new grub file after run "update-grub"

Restore ENV Setup

Since the "initialization" environment is still essentially as the form of snapshot subvolumes, you only need to create a snapshot as the @main like other snapshot subvols.That is a read-only snapshot of the @sys subvolume can by using "@snaps/@ restore subvolume" under the same btrfs parent volume (supporting customization of personal habits) and modifying the corresponding files of/etc. The "-r" parameter of the snapshots means that is a read-only snapshot.

root@ziggy-PC:/# btrfs subvolume snapshot -r /mnt/@sys /mnt/@snaps/@restore

Since the system installed through the installer usually has an "advanced menu", I suggest to use the advanced menu directly and modify its description to "Restore",initrd and linux paths to their own initialization environment subvolumes. This allows you to enter the initialization environment when you select the Restore menu to boot.

**However, it is worth noting that it is recommended to snaps "@restore subvolume" to a new subvolume in the form of snapshot before performing user configuration operations, which can greatly avoid the initialization environment from being broken. * *

Reply Favorite View the author
All Replies

No replies yet