Moving a Linux System

Recently, I decided to re-partition my hard disk because I needed a larger swap partition. In this article, I'll describe the process of backing up an existing system and restore it somewhere else. I haven't done anything like it in years, so I was up to some smaller surprises that I was fortunately able to solve quickly. In any case, being extra careful and planning ahead usually saves quite a few headaches. Moving a system is no trivial task, so follow this article at your own risk.

First of all, the existing data has to be backed up on another system. The backup should be created in single user mode to make sure there are no processes running that modify the filesystem. Since there are a few things you don't want to backup (like /proc or other pseudo filesystems), you should figure out what they are, maybe using mount(8). For my system, this command worked:

tar cf -
    --exclude /proc --exclude /sys --exclude /var/run
    --exclude /var/lock --exclude /dev
    /
    | ssh user@host "cat > backup.tar"

The command creates a tarball (excluding a few directories) and directly sends it to a different host. If you have a second disk in your computer with enough space then you could also save it there, but a backup on a different system is always recommended. It's also a good idea to test the backup to see if everything is there and files can be extracted without errors.

After the backup is done, boot into a rescue system (like the Ubuntu installation CD) and re-partition the disk. In my case, an alternative would have been to shrink my root filesystem using resize2fs(8), to adjust the partition's size (via fdisk(8)) and add a second swap area at the then free area. I decided against this because if I made any errors during the procedure I wouldn't find out until the filesystem fills up (and possibly overwrites part of the swap area).

So I created a new swap partition (type 82) and a root partition (type 83) and formatted the partitions using mkswap(8) and mkfs. Make sure you don't lose the generated UUIDs - you're going to need them later. If you're using ext2 or ext3 use tune2fs(8) for the usual adjustments (reserved block count, file system check interval etc.).

Mount the new root filesystem (/mnt is used in this example) and extract the backup there:

cd /mnt
ssh user@host "cat backup.tar" | tar xpf -

Make sure everything looks OK and don't forget to create the directories that were excluded from the backup:

mkdir /proc /sys /var/run /var/lock /dev

Since newer distributions use UUIDs to identify filesystems, we'll have to adjust the extracted system to use the newly generated UUIDs. Those UUIDs hide in several places:

  • /mnt/etc/fstab
  • /mnt/boot/grub/menu.lst
  • /mnt/etc/initramfs-tools/conf.d/resume (for hibernation)

Make sure to update the UUIDs everywhere. I didn't get them all at the first attempt, so I got kernel panics and hibernation didn't work. Then setup the master boot record using grub. I did it at the grub shell, but the following command should work, too:

grub-install --root-directory /mnt

After booting into your system for the first time, you have to run update-initramfs -u and then restart the system. Otherwise hibernation will be broken. After that's been done, your system should work the same way as before.

social