Friday, February 5, 2010

Clone your linux boot partition

Where as Windows has many many cloning tools (e.g. Ghost), for linux cloning isn’t quite common.

Yesterday i needed to migrate an installation to another server. First make sure that you compile all the things you need in your current kernel.
With a Iinux live cd I created a backup of my boot partition (/dev/sda1) to a file on a usb disk (mounted as /backup)

dd if=/dev/sda1 of=/backup/sda1.dd

Then i wanted to restore it to another machine with a different partition size.
Here’s how:

  • partition your new harddisk
  • create an ext3 filesystem on your new boot partition
  • mount your usb disk as /backup
  • mount the backup file as /backupsda1
    mount -o loop -t ext3 /backup/sda1.dd /backupsda1
  • mount the newly created boot partition as /mnt
    mount /dev/sda1 /mnt
  • copy all files to the new partition
    cd /backupsda1
    rsync -av * /mnt/
  • mount proc and dev to your new partition
    mount -t proc none /mnt/proc
    mount -o bind /dev /mnt/dev
  • chroot to your new installation
    chroot /mnt
  • reinstall the bootloader (grub)
    grub-install /dev/sda

That’s it!

edit:
if your previous installation has an older version of grub, you might need to create your filesystem with a smaller inode size, otherwise grub won’t function correctly:

mkfs.ext3 -I 128 /dev/sda1