如何将加密的 LVM 安装迁移到新磁盘

Ham*_*ner 15 encryption partitioning grub2 lvm

我有一个稍微定制的笔记本电脑安装,我想直接移动到 SSD,而不必重新安装 Ubuntu,重新安装所有应用程序并再次进行所有其他更改。SSD 更小,所以我不能只做dd.

原始安装是使用 Ubuntu 备用安装程序完成的,选择使用 LVM 选项进行全盘加密。

需要哪些步骤以及如何执行?我希望必须:

  • 设置磁盘分区、加密等
  • 复制数据
  • 安装 grub 并让它使用新的 UUID 值等。

Ham*_*ner 14

分区和文件复制 - 运行时

我通过从正在运行的系统开始来做到这一点。我将新的 SSD 插入 USB SATA 适配器并对其进行分区、设置 LVM 并复制文件。

# confirm disk size is as expected for sdc
sudo fdisk -l /dev/sdc
# now partition - 500 MB partition as boot, the rest as a single (logical) partition
sudo cfdisk /dev/sdc
Run Code Online (Sandbox Code Playgroud)

您的磁盘现在应如下所示:

sudo fdisk -l /dev/sdc
Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders, total 234441648 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *          63      979964      489951   83  Linux
/dev/sda2          979965   234441647   116730841+   5  Extended
/dev/sda5          980028   234441647   116730810   82  Linux swap / Solaris
Run Code Online (Sandbox Code Playgroud)

下一步是将加密放在分区上,并将 LVM 放在加密之上。

sudo cryptsetup -y luksFormat /dev/sdc5
sudo cryptsetup luksOpen /dev/sdc5 crypt
sudo vgcreate crypt-lvm /dev/mapper/crypt
sudo lvcreate -L4G -nswap crypt-lvm
sudo lvcreate -l100%FREE -nroot crypt-lvm
Run Code Online (Sandbox Code Playgroud)

现在制作文件系统并挂载它们并复制您的系统。

sudo mkfs.ext2 /dev/sdc1
# you do ls /dev/mapper to check the name if different
sudo mkfs.ext4 /dev/mapper/crypt-root
sudo mkdir /mnt/boot
sudo mkdir /mnt/root
sudo mount -t ext2 /dev/sdc1 /mnt/boot
sudo mount -t ext4 /dev/mapper/crypt-root /mnt/root

# rsync files
sudo rsync -a /boot/* /mnt/boot/
sudo rsync -aHAX --devices --specials --delete --one-file-system --exclude proc --exclude run --exclude boot --exclude sys --exclude tmp /* /mnt/root/
Run Code Online (Sandbox Code Playgroud)

到目前为止,您可以保持系统运行并使用它。现在您需要关闭并启动到 Live CD/USB 以便您可以使系统处于关闭状态。

分区和文件复制 - Live CD/USB

启动后,打开终端并:

sudo apt-get install lvm2

# mount old hard drive
sudo cryptsetup luksOpen /dev/sda5 sda5_crypt
sudo mkdir /mnt/sdaroot
# you can do ls /dev/mapper to check the name if it is different
sudo mount -t ext4 /dev/mapper/sda5_crypt--root /mnt/sdaroot

# mount new hard drive (over USB)
sudo cryptsetup luksOpen /dev/sdc5 sdc5_crypt
sudo mkdir /mnt/sdcroot
sudo mount -t ext4 /dev/mapper/sdc5_crypt--root /mnt/sdcroot

# final rsync
sudo rsync -aHAX --devices --specials --delete --one-file-system --exclude proc --exclude run --exclude boot --exclude sys --exclude tmp /mnt/sdaroot/* /mnt/sdcroot/
Run Code Online (Sandbox Code Playgroud)

chroot

# prepare chroot
cd /mnt/sdcroot
sudo mkdir boot

# these directories are set up by the system and we need them inside the chroot
sudo mount -t proc proc /mnt/sdcroot/proc
sudo mount -t sysfs sys /mnt/sdcroot/sys
sudo mount -o bind /dev /mnt/sdcroot/dev

# now enter the chroot
sudo chroot /mnt/root/
Run Code Online (Sandbox Code Playgroud)

更改 UUID

现在我们是 chroot 中的 root 并运行以下命令:

# inside chroot, as root
mount -t ext2 /dev/sdc1 /boot
blkid
Run Code Online (Sandbox Code Playgroud)

现在您将看到系统中各种磁盘的所有UUID。您将需要编辑 UUID/etc/fstab/etc/crypttab匹配其值/dev/sdc?

/etc/fstab您需要使用 UUID 作为启动盘时 -/dev/sdc1如果您的磁盘与我的盘符相同。

/etc/crypttab你需要使用的UUID为其他(大)分区-/dev/sdc5如果你的磁盘有相同字母如我。

initramfs 和 grub

# now update initramfs for all installed kernels
update-initramfs -u -k all

# install grub and ensure it is up to date
grub-install /dev/sdc      # NOTE sdc NOT sdc1
update-grub

# hit Ctrl-D to exit chroot
sudo umount /mnt/root
Run Code Online (Sandbox Code Playgroud)

现在关机,将 SSD 放入笔记本电脑,交叉手指并启动。

有用的链接

http://www.debian-administration.org/articles/577 上的 cryptsetup 内容的好指南

在外部分区上安装 grub:https : //stackoverflow.com/questions/247030/how-to-set-up-grub-in-a-cloned-hard-disk

https://help.ubuntu.com/community/UsingUUID