GliderJan Varho

Cloning a Linux Partition

  • linux
  • ubuntu
  • filesystem
  • partition

Whenever I want to test a new release of Ubuntu I find myself cloning the root partition so I can safely upgrade and still keep my old install.

However, I usually forget some step and end up needing to debug grub boot. So this is a summary of what needs to be done in order to clone the partition and make it work. (Almost all of the below need to be run as root of course.)

First you need the file systems in question to be unmounted, so likely you will want to boot from a USB stick or some third partition.

  1. Cloning with dd --

The simplest way to clone the whole FS is to just dd the partition into another equally sized partition (I keep a few root-sized ones in reserve). That is, something like:

dd if=/dev/sdd1 of=/dev/sdd2 bs=$((1024102416))

That copies the contents of /dev/sdd1 into /dev/sdd2 with a large enough block size that you get most out of SSDs.

  1. Changing the UUID --

Since there are now two partitions with the same UUID, you need to rename one. The command depends on the file system in question, but for ext4 it is:

tune2fs -U random /dev/sdd2

Now you can check that the UUID changed and record the values:

blkid

  1. Updating the UUID in /etc/fstab --

The next step is to update the fstab in the changed filesystem. Mount the FS somewhere and edit the root UUID manually or just use sed:

sed -ie 's/<old_uuid>/<new_uuid/g' ./etc/fstab

  1. Updating grub --

Next you can boot into the normal system (which for me is usually the clone's original copy, i.e. /dev/sdd1 above), chroot into the clone FS (/dev/sdd2) and update grub from there. Go into the root of the clone and run:

mount --bind /dev ./dev mount --bind /proc ./proc mount --bind /sys ./sys chroot . update-grub

Finally, run update-grub outside the chroot, in whichever install owns the default drive's grub.