Migrating Debian from a single disk with ext4 to two disks with btrfs and RAID1.
Source: https://gist.github.com/jirutka/990d25662e729669b3ce (for Gentoo and syslinux)
Partitioning scheme
Partition | Filesystem | Size | Description |
---|---|---|---|
sd*1 | ext2 (md/raid1) | 512 MiB | boot (kernel etc.) |
sd*2 | sw (md/raid1) | 4 GiB | swap |
sd*3 | Btrfs (raid1) | * | Btrfs |
-
Create GPT partition table on each disk:
parted -a optimal /dev/<disk> unit MB mklabel gpt mkpart primary 1 512 mkpart primary 512 4600 mkpart primary 4600 4608 mkpart primary btrfs 4608 -1 set 1 legacy_boot on set 3 bios_grub on print quit
-
Create Btrfs filesystem in raid1 mode on the third partitions, create subvolume for / and mount it into /mnt/debian:
When you have two empty disks:
```sh
mkfs.btrfs -L btrfsvol -d raid1 -m raid1 /dev/sda3 /dev/sdb3
```
When you migrate from one to two disks and the first is still in use (which is assumed in this tutorial):
```sh
mkfs.btrfs -L btrfsvol /dev/sdb3
```
and later when you are finished add the second disk and create the RAID1 (See step E):
```sh
btrfs device add /dev/sda3 /
btrfs balance start -dconvert=raid1 -mconvert=raid1 /
```
```sh
mkdir /mnt/btrfs
mount -t btrfs -o defaults,noatime,compress=no,autodefrag -L btrfsvol /mnt/btrfs
btrfs quota enable /mnt/btrfs/
btrfs subvolume create /mnt/btrfs/root
btrfs subvolume create /mnt/btrfs/var
umount /mnt/btrfs
mkdir /mnt/debian
mount -t btrfs -o defaults,noatime,compress=no,autodefrag,subvol=root -L btrfsvol /mnt/debian
mkdir /mnt/debian/var
mount -t btrfs -o defaults,noatime,compress=no,autodefrag,subvol=var -L btrfsvol /mnt/debian/var
btrfs qgroup limit 21G /mnt/debian/var/
```
-
Create md raid1 array with ext2 filesystem on the first partitions and mount it to /mnt/debian/boot:
mdadm --create /dev/md0 --name boot --level 1 --metadata 1.0 --raid-devices=2 /dev/sda1 missing mkfs.ext2 -L boot /dev/md/boot_0 mkdir -p /mnt/debian/boot mount /dev/md/boot_0 /mnt/debian/boot
Note: extlinux cannot boot from md with metadata v1.2 (read here)!
-
Create md raid1 array with swap on the second partitions:
mdadm --create /dev/md1 --name swap --level 1 --metadata 1.2 --raid-devices=2 /dev/sda2 missing mkswap -L swap /dev/md1
Copy existing Debian installation to new (degraded) RAID1:
```sh
cp -rpa /bin /boot /etc /initrd.img* /lib* /root /sbin /srv /usr /var /vmlinuz* /mnt/debian/
cd /mnt/debian/
mkdir dev
mkdir proc
mkdir run
mkdir sys
mkdir media
mkdir mnt
mkdir tmp
chmod 777 tmp/
chmod +t tmp/
```
mkdir dev && mkdir proc && mkdir run && mkdir sys && mkdir media && mkdir mnt && mkdir tmp
- Get UUIDs of all newly created partitions
The UUID (check with blkid) is the same for both partitions in our RAID, so no need to specify devices.
sh blkid --match-token TYPE=btrfs blkid --match-token TYPE=swap blkid --match-token LABEL=boot
-
Edit fstab
nano /mnt/debian/etc/fstab
We also add "degraded" as file system option in fstab; e.g.: ``` # /etc/fstab: static file system information. # # The root filesystem should have a pass number of either 0 or 1. # All other filesystems should have a pass number of 0 or greater than 1. # # See the manpage fstab(5) for more information. #
# <fs> <mountpoint> <type> <opts> <dump/pass>
UUID=3cd3dc71-091c-9c90-4b7c-bd46a90c4964 /boot ext2 noauto,noatime 1 2
UUID=48d773c8-f3d5-40e9-9a2c-ab1f68195a63 / brtfs defaults,noatime,degraded,compress=no,autodefrag,subvol=root 0 1
UUID=48d773c8-f3d5-40e9-9a2c-ab1f68195a63 /var brtfs defaults,noatime,degraded,compress=no,autodefrag,subvol=var 0 0
UUID=558903fd-310c-47cc-9ebe-4e42e32990bd none swap sw 0 0
```
-
Edit Grub config
nano /etc/default/grub
We also have to add "degraded" as kernel option e.g. (you can only have one rootflags argument, therefore we have to repeat the subvol= option):
```
GRUB_CMDLINE_LINUX_DEFAULT="rootflags=subvol=root,degraded quiet"
```
Basically follow the Debian wiki article to reinstall Grub: https://wiki.debian.org/GrubEFIReinstall
```sh
for i in /dev /dev/pts /proc /sys /sys/firmware/efi/efivars /run; do mount -B $i /mnt/debian/$i; done
chroot /mnt/debian
grub-install /dev/sdb
update-grub
```
Shut-down the computer and disconnect the old disk. We want to make sure we boot from the new (degraded) RAID.
Switch on the computer and test if everything is (still) working as before.
You successfully moved your Debian to a new disk!
Only continue with this step if everything works! Fix the bugs/copy missing files from the old disk before you format it!
Repeat step A.1 with the old disk where Debian was installed before without RAID.
Then add the newly created partitions to the existing (degraded) RAID 1:
```sh
mdadm /dev/md/boot_0 --add /dev/sdb1
mdadm /dev/md/swap_0 --add /dev/sdb2
btrfs device add /dev/sdb3 /
btrfs balance start -dconvert=raid1 -mconvert=raid1 /
```
You can check the progress of mdadm with:
sh cat /proc/mdstat
Then install Grub on the second disk as well:
sh grub-install /dev/sdb update-grub
Test your RAID. Shut-down the computer and disconnect one disk. Switch on the computer and test if everything is working. Shut-down and repeat with the other disk.
How to display quota limit on btrfs subvolume?
sh btrfs qgroup show -pcre /
https://seravo.fi/2015/using-raid-btrfs-recovering-broken-disks https://www.complang.tuwien.ac.at/anton/btrfs-raid1.html https://btrfs.wiki.kernel.org/index.php/Using_Btrfs_with_Multiple_Devices https://wiki.debian.org/GrubEFIReinstall