Drive Migration

From Mark Furneaux's Wiki
Jump to: navigation, search

Drive Migration refers to the act of transferring an OS from one drive or array onto another, whilst still retaining bootability.

Clonezilla

Clonezilla is an excellent tool for imaging and copying partitions and entire disks for the purposes of backup or duplication. Clonezilla however does not work on mdraid arrays and does not recognize all filesystems. It is best to use Clonezilla in most cases unless it is not possible.

ZFS

This section covers migrating a Linux installation from any 2nd-generation filesystem to ZFS.

Prior to Migration

  1. Install ZFS-aware kernel/modules
  2. Install zfs-initramfs

Prepare ZFS Root Pool

  1. Create a GPT on the destination drive
  2. Create a GRUB BIOS partition on the disk. Either create a small partition with the flag "bios_grub" or just run:
    # sgdisk -a1 -n2:34:2047 -t2:EF02 /dev/disk/by-id/<newdisk>
  3. Create a partition for the pool
  4. Create a zpool with mountpoint=none
  5. Create a ROOT dataset with mountpoint=none
  6. Create a child of the ROOT dataset with mountpoint=/

Migrate Data

  1. Boot a liveCD or other host with ZFS configured
  2. Mount the dataset with an altroot of /mnt:
    # zpool import -d /dev/disk/by-id -R /mnt <poolname>
  3. Copy the data with:
    # rsync -aAXvH /media/origin/* /mnt --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found}

Make It Bootable Again

  1. Remove any obsolete entries from /etc/fstab
  2. Bind the running virtual filesystems to the new root dataset by running:
    # for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done
  3. Chroot into the new root dataset:
    # chroot /mnt
  4. Install GRUB by running:
    # grub-install /dev/disk/by-id/<newdisk> (no partitions).
    If this fails with a canonicalization error (due to a bug in some versions of GRUB), make a symlink:
    # ln -s /dev/disk/by-id/<newdisk> /dev/<newdisk> and try again.
  5. Update GRUB with:
    # update-grub
  6. Check /boot/grub/grub.cfg to see if the Linux command line contains "boot=zfs". If it does not, add the string to /etc/grub.d/10_linux or /etc/default/grub and re-run # update-grub
  7. Unbind the virtual filesystems:
    # for i in /dev/pts /dev /proc /sys /run; do sudo umount /mnt$i; done

rsync

rsync is a useful tool for copying files from one filesystem to another, while preserving attributes such as UID, GID, and permissions. This is useful when you cannot image a partition (such as when on an mdraid array), or need to switch from 512B sectors to new 4KiB AF sectors. This method also doesn't require resizing of any partitions.

We will assume we are transferring an Ubuntu installation on an mdraid array to a single SSD. This can easily be adapted for single disk transfers.

Prepare The Source Array

  1. Boot a live image OS such as Ubuntu.
  2. Install the mdadm package by running:
    # apt-get install mdadm
  3. Assemble the array by running:
    # mdadm --assemble --scan
  4. Mount the array into /media

Prepare The Destination Drive

  1. Using gparted, create a partition table and partition of the type desired on the destination drive.
  2. Mount the new partition to /mnt

Copy The Files

  1. Run:
    # rsync -aAXvH /media/origin/* /mnt --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found}
  2. If other partitions are being migrated into this new one, such as /boot, they can be migrated using a similar command such as:
    # rsync -aAXvH /media/originofboot/* /mnt/boot

Install The Bootloader

  1. Bind the live virtual filesystems to the target path by running:
    # for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt$i; done
  2. chroot into the target filesystem by running:
    # chroot /mnt
  3. Install GRUB by running:
    # grub-install --recheck /dev/sdX
    where X is the target drive (without partition).
  4. Update GRUB with:
    # update-grub

Finish Up

  1. Run:
    # blkid
    and edit /etc/fstab to reflect the changes made by updating the UUID of the root filesystem and removing/updating others.
  2. Add TRIM support if now using an SSD.

dd

dd is a tool for copying drives at the block level. This method is the slowest and does not adapt for changing drive parameters such as sector and partition size. It is recommended as a last resort for drives with partitions that cannot be mounted or accessed by Clonezilla. The destination must be the same size or larger and the sector type must be the same.

Both devices must be connected to the system, but NOT mounted. We will assume we are copying a drive /dev/sdX to a new drive /dev/sdY.

Run:
# dd if=/dev/sdX of=/dev/sdY bs=1M

Warning: This command is dangerous. A mistake in the if or of parameters will mean certain data destruction.

The new drive will be bootable without any configuration. The only change that may need to be made is adding TRIM support if using an SSD, or inflating the partition(s) if the target drive is larger.