move linux os from disk A to disk B with 0 loss

spaceship9發表於2024-04-07

Words in short, about all the conditions of all disks.

F.Y.I. It's not recommended that do this if you want to migrate from sata to nvme disk. It should work if you migrate from sata to sata disk. For nvme disks, it's better to boot from UEFI rather than MBR. Some NVME disk vendors come with firmware support for MBR bootroms such as some SAMSUNG nvme disks however it's not guaranteed.

Conditions

Disk A has Linux (ubuntu 18.04 amd64) installed. Size 238.47G.

Disk A (linux) is bootable using MBR(MS-DOS), rather than UEFI. Partition 1 is for /boot, Partition 5 is for /, Partition 3 is for SWAP.

Disk B is fresh new. Size 223.57G.

Disk B is smaller than Disk A but the OS doesn't occupy so many space.

Let's see the screenshot.

Here on the host machine, /dev/sdb is Disk A, /dev/sdg is Disk B

Let's see disk B

Methodology

Remove everything on Disk B, and create a new partition table on Disk B (MS-DOS).

Then create a partition 1 on Disk B (size can be 512MB, it's enough for loading linux kernels). And change the flag to be bootable.

Then create a partition 2 on Disk B (just to make sure the size can be bigger than the actual size of "/" on partition 2 of Disk A). You can do it using gparted.

Mounting partitions

Disk A is mounted at /media/alex/[somewhere]

In this case, the Partition 1 (/boot) on Disk A is mounted at:

/media/alex/3e2a2d4d-838c-4621-8371-d82bc6f6a47a

and the Partition 2 (/) on Disk A is mounted at:

/media/alex/00e84c7a-0076-4470-9edc-4be217ee94b9

Let's mount disk B.

sudo mkdir -p /mnt/boot
sudo mkdir -p /mnt/main
sudo mount /dev/sdg1 /mnt/boot
sudo mount /dev/sdg2 /mnt/main

In my case, sdg1 is Partition 1 on Disk B, sdg2 is partition 2 on Disk B.

Copying files

Copy files with all privileges (know more about cp commands manual by man cp or cp --help)

sudo cp -R --preserve=all /media/alex/00e84c7a-0076-4470-9edc-4be217ee94b9/* /mnt/main/
sudo cp -R --preserve=all /media/alex/3e2a2d4d-838c-4621-8371-d82bc6f6a47a/* /mnt/boot/

To view the IO speed in real-time, use iotop

sudo apt install iotop-c -y
sudo iotop

Change /etc/fstab

On the host machine, check the block UUID of /dev/sdg1 and /dev/sdg2 by doing so.

sudo blkid /dev/sdg1
sudo blkid /dev/sdg2

The original /etc/fstab on Partition 2 Disk A looks like this:

change it on the on Disk B. (Because we are booting from a new disk and I'm not gonna swap partition this time. It's easy to do it later if you want. SWAP partition can be on different disks.)

sudo vi /mnt/main/etc/fstab

Make it look like this

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sdg2 during installation
UUID=66441fca-2f95-4d83-af6e-79b086decce2 /               ext4    errors=remount-ro 0       1
# /boot was on /dev/sdg1 during installation
UUID=8d34d6db-ae1a-4a4a-881c-e65ee3912968 /boot           ext4    defaults        0       2

Change block UUID in grub.cfg

for new /boot

sudo sed -i 's/3e2a2d4d-838c-4621-8371-d82bc6f6a47a/8d34d6db-ae1a-4a4a-881c-e65ee3912968/g' /mnt/boot/grub/grub.cfg

for new /

sudo sed -i 's/00e84c7a-0076-4470-9edc-4be217ee94b9/66441fca-2f95-4d83-af6e-79b086decce2/g' /mnt/boot/grub/grub.cfg

change grubenv

sudo sed -i 's/00e84c7a-0076-4470-9edc-4be217ee94b9/66441fca-2f95-4d83-af6e-79b086decce2/g' /mnt/boot/grub/grubenv

Your block UUID is different. Change it according to blkid command.

Boot from the new Disk (B)

Then umount all disks, and try to boot Disk B on a MBR-friendly (legacy PC).

相關文章