Linux 系统迁移

使用 Linux 多年, 开发 编译 构建等等环境配置已经很完善了, 如果需要换硬件(比如换新一代的计算机, 换新一代的 PCIe4 NVMe 硬盘), 但又不想重新搭建系统和环境, 那就需要进行系统迁移了, 这在 Linux 下很容易进行.

步骤

  • 开机 boot 使用 GParted Live, 进入分区管理
  • 为新硬盘 /dev/sdb 建立 分区表 和分区 /dev/sdb1
  • 右键 copy /dev/sda1, 粘贴到 /dev/sdb1, 这里要小心, uuid 也会被copy到 /dev/sdb1
  • 为 /dev/sdb1 新建 uuid (右键菜单)
  • 退出 GParted Live, grub 启动 Manjaro Linux.
  • 进入 Linux 系统后, 为新的 root dir 做准备.

    sudo su
    mkdir /tmp/mydir
    mount /dev/sdb1 /tmp/mydir

    mount --bind /dev /tmp/mydir/dev
    mount --bind /proc /tmp/mydir/proc
    mount --bind /sys /tmp/mydir/sys
    mount --bind /run /tmp/mydir/run
  • bind mount 完成后, 可以进行 chroot 了.

    chroot /tmp/mydir
  • grub-install 写入更新硬盘 sdb 的引导需要的相关数据

    grub-install /dev/sdb
    Installing for i386-pc platform.
    Installation finished. No error reported.
  • 开机引导流程概略如下:

  • 不过在重启之前, 还需要修改 /etc/fstab, 把 /dev/sdb1 的挂载加上, /dev/sda1 的挂载注释掉.

  • 接着修改 /etc/default/grub, 查找root=UUID=, 把启动硬盘修改为新的硬盘UUID.
  • 执行 sudo grub-update

    Generating grub configuration file ...
    Found linux image: /boot/vmlinuz-5.4-x86_64
    Found initrd image: /boot/intel-ucode.img /boot/initramfs-5.4-x86_64.img
    Found initrd fallback image: /boot/initramfs-5.4-x86_64-fallback.img
    Warning: os-prober will be executed to detect other bootable partitions.
    Its output will be used to detect bootable binaries on them and create new boot entries.
    Found Manjaro Linux (22.0.0) on /dev/sda1
    Found memtest86+ image: /boot/memtest86+/memtest.bin
    done
  • 关机, 拔掉硬盘 sda, 换为 sdb, 成功启动, 系统迁移完成.

  • 开机引导时 menu 可能出现双系统(第三和第四项是在sda上的旧系统), 如下, 只需用 sdb 硬盘开机后, 重新 grub-update 即可(此时 sda 不要挂载).

参考