User:Massimo B./Migration

From Gentoo Wiki
Jump to:navigation Jump to:search

Gentoo Migration to new drive or machine

Forums discussion about this wiki: https://forums.gentoo.org/viewtopic-t-1173479.html

Cheat sheet on how to migrate some special Gentoo installation to a new drive or machine. This Gentoo installation uses btrfs-on-LUKS.

Rebuild on source hardware

If the build is incompatible for the new target machine, it might be necessary to rebuild all on the old hardware.

root #btrfs subvolume snapshot /mnt/btrfs-top-lvl/root /mnt/btrfs-top-lvl/root.rebuild
root #mkdir /mnt/gentoo
root #mount --bind /mnt/btrfs-top-lvl/root.rebuild /mnt/gentoo
root #mount --types proc /proc /mnt/gentoo/proc
root #mount --rbind /sys /mnt/gentoo/sys
root #mount --rbind /dev /mnt/gentoo/dev
root #mount --bind /run /mnt/gentoo/run
root #chroot /mnt/gentoo /bin/bash

Choose some CFLAGS compatible to both platforms, old and new:

FILE /etc/portage/make.conf/local
CFLAGS="-march=x86-64-v3"

Re-emerge all:

root #emerge -e -atvg @world

Partitioning

Example:

# Device           Start        End    Sectors  Size Type
# /dev/nvme1n1p1    2048      26623      24576   12M EFI System
# /dev/nvme1n1p2   26624    1075199    1048576  512M Linux filesystem
# /dev/nvme1n1p3 1075200 1953523711 1952448512  931G Linux filesystem

Filesystems and Encryption

EFI System Partition (ESP), boot partition and the LUKS for btrfs:

root #mkfs.vfat -F 12 -n EFI /dev/nvme1n1p1
root #mkfs.ext4 -L boot /dev/nvme1n1p2
root #cryptsetup luksFormat --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 5000 --pbkdf argon2id --pbkdf-memory $((4*1024*1024)) --pbkdf-parallel 4 /dev/nvme1n1p3

TODO: For additional hardware encryption this might be added:

 --hw-opal

Add a fallback-key:

root #cryptsetup luksAddKey /dev/nvme1n1p3

Make a backup of the LUKS header:

root #cryptsetup luksHeaderBackup /dev/disk/by-uuid/263b5499-c4bd-422f-8865-c33670de2ca6 --header-backup-file /mnt/btrfs-top-lvl/rescue/263b5499-c4bd-422f-8865-c33670de2ca6.header

Allowing discard and make that default in the LUKS header:

root #cryptsetup luksOpen --allow-discards --persistent /dev/nvme1n1p3 _new_gentoo

Make a backup of the tablekey:

root #dmsetup table --showkeys | grep 263b5499-c4bd-422f-8865-c33670de2ca6 > /mnt/btrfs-top-lvl/rescue/263b5499-c4bd-422f-8865-c33670de2ca6.tablekey

Mount old btrfs

root #cryptsetup luksOpen /dev/sdb4 _old_gentoo
root #mkdir /mnt/old-btrfs-top-lvl
root #mount -o rw,relatime,compress-force=zstd:3,subvol=/ /dev/mapper/_old_gentoo /mnt/old-btrfs-top-lvl/

The subvolume layout looks like this:

root #btrfs subvolume list /mnt/old-btrfs-top-lvl | grep -v snapshots | grep -v archive
    ID 262 gen 439143 top level 5 path volumes/home
    ID 264 gen 439143 top level 5 path volumes.nosnap/tmp
    ID 265 gen 438955 top level 5 path volumes.nosnap/usr.portage
    ID 266 gen 438737 top level 5 path volumes.nosnap/usr.src
    ID 267 gen 439144 top level 5 path volumes.nosnap/var.cache
    ID 268 gen 438955 top level 5 path volumes.nosnap/var.db.repos
    ID 270 gen 438957 top level 5 path volumes.nosnap/var.tmp
    ID 271 gen 437369 top level 5 path volumes/vm
    ID 274 gen 438724 top level 5 path volumes/data
    ID 275 gen 439144 top level 5 path volumes/root

1. approach: btrfs replace

root #btrfs replace -f start /dev/mapper/_old_gentoo /dev/mapper/_new_gentoo /mnt/old-btrfs-top-lvl
root #btrfs replace status /mnt/old-btrfs-top-lvl

Inspect, if the btrfs now uses the new device:

root #btrfs filesystem show /mnt/old-btrfs-top-lvl

If new device is there, remove the old device:

root #btrfs device remove /dev/mapper/_old_gentoo /mnt/old-btrfs-top-lvl

2. approach: Create and mount new btrfs, btrfs send/receive

root #mkfs.btrfs -L gentoo /dev/mapper/_new_gentoo
root #mkdir /mnt/new-btrfs-top-lvl
root #mount -o rw,relatime,compress-force=zstd:3,subvol=/ /dev/mapper/_new_gentoo /mnt/new-btrfs-top-lvl
root #cd /mnt/new-btrfs-top-lvl
root #mkdir -vp snapshots/root snapshots/home snapshots/data snapshots/vm
root #mkdir -vp volumes volumes.nosnap .beeshome rescue archive
root #for sub in data home root vm ; do btrfs subvolume snapshot -r /mnt/old-btrfs-top-lvl/volumes/$sub /mnt/old-btrfs-top-lvl/volumes/$sub.ro ; done
root #for sub in tmp usr.portage usr.src var.cache var.db.repos var.tmp ; do btrfs subvolume snapshot -r /mnt/old-btrfs-top-lvl/volumes.nosnap/$sub /mnt/old-btrfs-top-lvl/volumes.nosnap/$sub.ro ; done
root #for sub in data home root vm ; do btrfs send /mnt/old-btrfs-top-lvl/volumes/$sub.ro | mbuffer -v 1 -m 5% | btrfs receive /mnt/new-btrfs-top-lvl/volumes ; done
root #for sub in tmp usr.portage usr.src var.cache var.db.repos var.tmp ; do btrfs send /mnt/old-btrfs-top-lvl/volumes.nosnap/$sub.ro | mbuffer -v 1 -m 5% | btrfs receive /mnt/new-btrfs-top-lvl/volumes.nosnap ; done
root #btrfs subvolume delete /mnt/old-btrfs-top-lvl/volumes*/*.ro
root #for sub in data home root vm ; do btrfs subvolume snapshot /mnt/new-btrfs-top-lvl/volumes/$sub.ro /mnt/new-btrfs-top-lvl/volumes/$sub ; done
root #for sub in tmp usr.portage usr.src var.cache var.db.repos var.tmp ; do btrfs subvolume snapshot /mnt/new-btrfs-top-lvl/volumes.nosnap/$sub.ro /mnt/new-btrfs-top-lvl/volumes.nosnap/$sub ; done
root #btrfs subvolume delete /mnt/new-btrfs-top-lvl/volumes*/*.ro

Mounting the necessary filesystems and enter via chroot

root #mkdir -v /mnt/gentoo
root #mount --bind /mnt/new-btrfs-top-lvl/volumes/root/ /mnt/gentoo
root #mount --types proc /proc /mnt/gentoo/proc
root #mount --rbind /sys /mnt/gentoo/sys
root #mount --make-rslave /mnt/gentoo/sys
root #mount --rbind /dev /mnt/gentoo/dev
root #mount --make-rslave /mnt/gentoo/dev
root #mount --bind /run /mnt/gentoo/run
root #mount --make-slave /mnt/gentoo/run
root #cp --dereference /etc/resolv.conf /mnt/gentoo/etc
root #chroot /mnt/gentoo /bin/bash

Bootloader and kernel

Inside the chroot, mount all partitions and subvolumes:

root #mount -a

This should finish without errors. Install grub:

root #grub-install

Or if failing specify more details:

root #grub-install --target=x86_64-efi --efi-directory=/boot/myefi

Install kernel:

root #emerge -atv1 gentoo-kernel-bin

Replace rd.luks.uuid and rd.luks.allow-discards in /etc/default/grub with the UUID of the new btrfs partition:

root #ls -al /dev/disk/by-uuid/ | grep nvme0n1p3

Create new grub config:

root #grub-mkconfig -o /boot/grub/grub.cfg

Exit and reboot...