[archlinux] 迁移T7从T460s到T470

时间:2023-03-09 21:52:05
[archlinux] 迁移T7从T460s到T470

这已经不是第一次做OS的迁移了,T7早已经迁移过多台设备了。所以,其实只需要如下三步:

1.  rsync

  我一直有全系统备份的习惯,T7一直会不定期的全系统rsync到Tstation上面去。所以我只需要将最新的T7 rsync进Tstattion,在从Tstation将T7 rsync进T470,就行了。

2. filesystem

  文件系统。T460s是一块1TB的ssd,T470是一块128GB的ssd+1TB的HDD。需要对T470重新做分区规则,并做好加密,然后挂载好之后,进行第一步。

3. boot

  启动设置。这里包括了UEFI,GRUB,dm-crypt,initrd等相关的配置。其实并不复杂,只有是我忘了,而忘了也是因为理解的并不深。所以,还有在学一下,以及会涉及到如何对文件系统进行规划。

一: 回顾之前的操作:

[archlinux][crypto] 从T450迁移archlinux操作系统至T460s笔记本

[cipher][archlinux][disk encryption][btrfs] 磁盘分区加密 + btrfs

二:

  因为是两块硬盘,我的规划是这样的,SSD分出16GB做swap,剩下的做根分区。HDD全盘挂载为home。文件系统将选用luks+btrfs。

  swap实际上是可以选择swap file的。这样在不想要swap的时候,就可以还回root分区。提高利用率,不过,仿佛记得btrfs+swap file目前好像有bug?恩,是的 https://wiki.archlinux.org/index.php/Btrfs#Swap_file

  那么,接下来。第一个要解决的问题就是。怎么在boot的时候,解密俩个加密分区。

  思路是这样的,在root分区里保存home的密钥,这样,只需要解密root就可以了。

  见:https://wiki.archlinux.org/index.php/Dm-crypt/Encrypting_a_non-root_file_system#Automated_unlocking_and_mounting

  由上文可知,使用crypttab文件,可以解决这一需求。即:在root挂载后,从root分区读取密钥解密home分区。

  另外,有一段话,值得注意:

Note: When using systemd-boot and the sd-encrypt hook, if a non-root partition's passphrase is the same as root's, there is no need to put that non-root partition 
in crypttab due to passphrase caching. See this forum thread for more information.

三: bootloader和fstab的参数怎么写?

  1. 在/etc/default/grub中,指定加密盘,剩下的grub会处理。  

rd.luks.uuid=

  2. arch有个脚本,生成fstab。

    genfstab

  弱线索阅读链接:https://wiki.archlinux.org/index.php/Dm-crypt/System_configuration#Boot_loader

  摘要:

o activate all devices in /etc/crypttab do not specify any luks.* parameters and use rd.luks.*. To activate all devices in /etc/crypttab.initramfs do not specify any luks.* or rd.luks.* parameters.

四:swap怎么弄?

  swap分区也是一个单独的分区,和home分区的设置是类似的,区别只在下面这些设置。

  参考:https://wiki.archlinux.org/index.php/Dm-crypt/Swap_encryption#With_suspend-to-disk_support

  1.  设置为swap的分区,本身是加密的。

  2.  修改grub,正确设置resume设备。

  3。修改mkinitcpio.conf 怎就resume属性。

开始之前:

  通篇复习:https://wiki.archlinux.org/index.php/Dm-crypt/Encrypting_an_entire_system

SSD trim:

  https://wiki.archlinux.org/index.php/Dm-crypt/Specialties#Discard.2FTRIM_support_for_solid_state_drives_.28SSD.29

  我用btrfs https://wiki.archlinux.org/index.php/Solid_State_Drive#TRIM

  所以,只要带着discard参数挂载,就可以了。

分区:

  https://wiki.archlinux.org/index.php/Btrfs

  并参考之前的blog:链接在本文最开始的地方。

  这里突然想到一个问题,两个硬盘,做两个btrfs,就是要被分开管理了。那么做快照之类的也是要分开做快照。于是如此的话,是否还有必要用btrfs呢?

  需要思考一下,迁移工作先暂停一周。

好,准备阶段结束 》》》》》》 开始:

1. 分区。

  128GB  GTP分区

    512MB   EFI分区,即ESP。文件系统为FAT32

    16GB  LUKS  swap分区

    110GB  LUKS btrfs  挂载根分区

  1024GB 无分区  LUKS  btrfs   挂载/home分区

分区:

parted /dev/sda
mklabel gpt
mkpart primary fat32 1MB 513MB
mkpart primary 513MB 17GB
mkpart primary 17GB %
set 1 esp on

mkfs:

mkfs.fat -F  /dev/sda1
cryptsetup luksFormat --type luks2 /dev/sda3
cryptsetup open /dev/sda3 LUKS_ROOT

cryptsetup 使用KeyFile https://wiki.archlinux.org/index.php/Dm-crypt/Device_encryption#Keyfiles

dd bs= count= if=/dev/random of=./keyfile
chmod ./keyfile
cryptsetup luksFormat --type luks2 /dev/sda2 ./keyfile
cryptsetup luksFormat --type luks2 /dev/sdb ./keyfile
cryptsetup open /dev/sda2 LUKS_SWAP --key-file ./keyfile
cryptsetup open /dev/sdb LUKS_HOME --key-file ./keyfile

mkfs

mkfs.btrfs -L vd_root /dev/mapper/LUKS_ROOT
mkfs.btrfs -L vd_home /dev/mapper/LUKS_HOME
mount /dev/mapper/LUKS_ROOT mnt
btrfs subvolume create mnt/real_root
btrfs subvolume snapshot mnt/real_root mnt/snapshot
btrfs subvolume set-default mnt/real_root
umount mnt
mount /dev/mapper/LUKS_HOME mnt
# 同上。。。

压缩挂载:

https://wiki.archlinux.org/index.php/Btrfs#Compression

zstd: https://github.com/facebook/zstd

mount -o compress=lzo /dev/mapper/LUKS_ROOT mnt
mount -o compress=lzo /dev/mapper/LUKS_HOME mnt/home
mount /dev/sda1 mnt/boot

2 rsyc bak

  http://www.cnblogs.com/hugetong/p/6984632.html

#! /usr/bin/bash

cd $(dirname $)

#if [[ $# -lt 2 || $# -gt 3 ]]; then
# echo "usage: $0 SRC_DIR DEST_DIR [-w]"
# exit
#fi
#
#src=$
#dest=$
#doit=$ if [[ $# -lt || $# -gt ]]; then
echo "usage: $0 [-w]"
exit
fi src=/
dest=tong@192.168.20.50:/home/tong/Storage/System/T7-rsync/ROOT_FS
doit=$ if [[ $doit == -w ]]; then
dry=
else
dry='-n'
fi sudo rsync --archive --acls --xattrs --numeric-ids \
--delete \
--exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} \
--sparse \
--hard-links \
--human-readable --itemize-changes --progress \
--verbose \
-M--fake-super \
$src $dest $dry # --delete-excluded
# --one-file-system \

SWAP

mkswap /dev/mapper/LUKS_SWAP
swapon --discard /dev/mapper/LUKS_SWAP

chroot

arch-chroot nmt/

fstab

genfstab -U nmt > nmt/etc/fstab.new
╰─>$ sudo cat /etc/crypttab
LUKS_HOME UUID="9f6b69f1-d2b8-4eed-b8b2-74c4ec6e8eaa" /etc/keys/luks_home.key
LUKS_SWAP UUID="5f649ea7-3e91-43c9-aaa2-b3afa97c1d7e" /etc/keys/luks_home.key
─>$ sudo cat /etc/fstab
/dev/mapper/LUKS_ROOT / btrfs discard,rw,relatime,compress=lzo,ssd,space_cache,subvolid=,subvol=/real_root,subvol=real_root
UUID=CD04-A96B /boot vfat discard,rw,relatime,fmask=,dmask=,codepage=,iocharset=iso8859-,shortname=mixed,utf8,errors=remount-ro
/dev/mapper/LUKS_HOME /home btrfs rw,relatime,compress=lzo,space_cache,subvolid=,subvol=/real_home,subvol=real_home
/dev/mapper/LUKS_SWAP none swap discard,defaults,pri=-

mkinitcpio.conf

. /usr/bin/btrfs
. -udev +systemd +sd-encrypt
mkinitcpio -g /boot/initramfs-linux.img -k 4.17.3-1-ARCH

efimanager

┬─[tong@T7:~]─[:: PM]
╰─>$ ll /boot/
total 51M
-rwxr-xr-x root root 31M Jul : initramfs-linux-fallback.img*
-rwxr-xr-x root root 13M Jul : initramfs-linux.img*
-rwxr-xr-x root root 1.6M May : intel-ucode.img*
-rwxr-xr-x root root 856K Jul : shellx64_v2.efi*
-rwxr-xr-x root root Jul : t7.nsh*
-rwxr-xr-x root root 5.1M Jun : vmlinuz-linux*
┬─[tong@T7:~]─[:: PM]
╰─>$
efibootmgr --disk /dev/sda --part  --create -L T7 --loader /vmlinuz-linux --unicode 'root=/dev/mapper/LUKS_ROOT rw rootflags=subvol=real_root rd.luks.name=8506ce5c-9a9d-407d-bccd-a2b4c7320914=LUKS_ROOT initrd=/intel-ucode.img initrd=/initramfs-linux.img'
efibootmgr --disk /dev/sda --part --create -L T7-fallback --loader /vmlinuz-linux --unicode 'root=/dev/mapper/LUKS_ROOT rw rootflags=subvol=real_root rd.luks.name=8506ce5c-9a9d-407d-bccd-a2b4c7320914=LUKS_ROOT initrd=/intel-ucode.img initrd=/initramfs-linux-fallback.img'

UEFI Shell

yaourt -S uefi-shell-git
cp /usr/share/uefi-shell/shellx64_v2.efi /boot/
efibootmgr --disk /dev/sda --part --create -L 'UEFI Shell V2' --loader /shellx64_v2.efi

/etc/udev/rules.d/ 修改mac地址

支持resume

1 如果不是systemd的就需要添加resume,如下。如果是systemd的,就略过这一条。

┬─[tong@T7:~]─[:: PM]
╰─>$ cat /etc/mkinitcpio.conf |grep resume
HOOKS=(base systemd autodetect modconf block sd-encrypt filesystems keyboard resume fsck)
┬─[tong@T7:~]─[:: PM]
╰─>$ sudo mkinitcpio -P -g ./initramfs-linux.img

2 之前我的swap分区是加密的,但是是在root挂载之后,systemd起来之后才解密的,很显然kernel是找不到它的,在那个时机里。所以,我简单粗暴的,不做swap加密了。如下:

把swap分区变成了明文分区。

sudo efibootmgr --disk /dev/sda --part 1 --create -L T7 --loader /vmlinuz-linux --unicode 'root=/dev/mapper/LUKS_ROOT rw rootflags=subvol=real_root rd.luks.name=8506ce5c-9a9d-407d-bccd
-a2b4c7320914=LUKS_ROOT initrd=/intel-ucode.img initrd=/initramfs-linux.img resume=UUID=c932854d-0a49-4a09-91a3-50b67ba1ddd1'

完!