Skip to content

Instantly share code, notes, and snippets.

@jcbrinfo
Last active September 17, 2016 22:11
Show Gist options
  • Select an option

  • Save jcbrinfo/bc9bdef82833965c58cce3509452c599 to your computer and use it in GitHub Desktop.

Select an option

Save jcbrinfo/bc9bdef82833965c58cce3509452c599 to your computer and use it in GitHub Desktop.
How to migrate drive content

How to migrate drive content

This procedure explain how to migrate the content of one internal drive to another (example: upgrading your hard disk for a SSD), without requiring more than one SATA port (so the procedure works on laptops).

Note: This procedure works well if using a MBR. On the other hand, GPTs are more complex so we can not just copy them as-is: we have to recreate the headers. This implies that with GPTs, some systems may become unbootable (until some setting are changed manually) because IDs have changed.

WARNING: Some steps might assume 512 octets sectors.

Requirements

  • A LiveCD/LiveDVD/LiveUSB of Linux with GParted, kpartx and all the necessary driver to manage the partitions present on the old drive.
  • An external drive with enough free space to hold a single file which size equals the capacity of the old internal drive. Note: A contiguous unallocated zone maybe used in place of a file, but it’s more complex and error-prone.
  • The target computer being able to boot from the LiveCD/LiveDVD/LiveUSB.
  • The target computer having a port to connect the external drive. If using a LiveUSB, the computer need to have enough ports to connect both the LiveUSB and the external drive at the same time.
  • If the old internal drive has a Windows partition, ensure you have an installation/repair/recovery disk for it.

Procedure

  1. Boot from the LiveCD/LiveDVD/LiveUSB with the old internal drive installed.

  2. Plug and mount the external drive.

  3. Create an image of the old drive like this:

    WARNING: The dangerous instruction of this procedure follows. Take care to not swap the arguments of if and of.

    sudo dd if=/dev/<in> of=<img> bs=16M, where <in> is the device name of the old drive (example: sda) and <img> is the path to a not-yet-existing file in the external drive that will hold the image.

  4. Set up a loop device for the image: sudo losetup -f --show <img>

  5. Set up a device mapper device for the loop device: printf '0 %s linear %s 0\n' "$(sudo blockdev --getsize /dev/loop<N>)" /dev/loop<N> | sudo dmsetup create sd<X>

  6. Set up a device for each partition of the loop device: sudo kpartx -a /dev/mapper/sd<X>

  7. Use GParted to resize and move the partitions if needed: sudo gparted /dev/mapper/sd<X>

    Tip: On the sticker of a drive, “LBA” (the “LBA count”) gives you the exact number of logical sectors. See also: IDEMA LBA1-03.

  8. Detach the partitions of the loop device: sudo kpartx -d /dev/mapper/sd<X>

  9. Remove the device mapper device: sudo dmsetup remove sd<X>

  10. Detach the loop device: sudo losetup -d /dev/loop<N>

  11. If using a MBR (and not GPTs), use the truncate command to resize the image if needed.

WARNING: If using GPTs, skip this step as the content and the position of the GPTs depends on the total size of the device.

Tip: Contrary to what the name suggests, truncate may also be used to enlarge a file, or even create a file with a specific size.

  1. Eject (unmount) and unplug the external drive.

  2. Shutdown.

  3. Replace the old internal drive by the new one.

  4. Boot from the LiveCD/LiveDVD/LiveUSB.

  5. Plug and mount the external drive.

  6. If using GPTs, reproduce the partition layout using GParted.

  7. Write some (specified below) parts of the image to the new internal drive. This consists of a series of commands similar to sudo dd if=<img> of=/dev/<out> bs=<block size> skip=<input offset, in blocks> seek=<output offset, in blocks> count=<size, in blocks>, where <in> is the device name of the internal drive (example: sda).

    • bs=512 count=1 skip=0 seek=0
    • Copy all partitions (bs=1M recommended; do not forget to divide sizes, in octets, by 220)
    • Some boot loaders / boot managers use the unallocated space between the end of the partition table and the start of the first partition. You may want to use dd and xxd together to check if there is anything worth copying in that region.
  8. If the new internal drive is an SSD, force trimming on all partitions of the new drive in order to potentially increase the lifetime of the device. You can do this by first mounting each partition, then running sudo fstrim <mountpoint> for each one, where <mountpoint> is the mount point of the partition to trim.

  9. Eject (unmount) and unplug the external drive.

  10. Shutdown.

  11. If the internal drive has a Windows partition, you may need to enable some drivers in order to be able to boot Windows with certain SATA modes.

    The usual symptom is the Windows’ boot screen that appears for a brief moment, then you get an automatic reboot or a Blue Screen of Death. Furthermore, if you disable the automatic reboot after a Blue Screen of Death (possible using the advanced (hidden) boot options), the error code you will see for this problem is “STOP: 0x0000007B”.

    For the instructions on how to enable the aforementionned drivers, see:

    In summary, depending on the mode, you always need to enable:

    • For ATA: atapi
    • For AHCI: msahci and atapi

    Then, depending on the controller, you need to enable:

    • For AMD chipsets: amdsata
    • For Intel chipsets in RAID or AHCI mode: iaStor and iaStorV
    • For Nvidia chipsets: nvstor
    • For Nvidia chipsets in RAID mode: nvraid and nvstor
  12. If the internal drive has a Windows partition, you may need to fix the boot loader or the BCD using a installation/repair/recovery disk.

    Tip: If you followed this procedure correctly, only the *device attributes should be broken in our case. So, you usually do not need to recreate entire entries of the BCD and the MBR should be already fine.

    Tip: The “list me everything you can” command of bcdedit is bcdedit /enum all /v. Note that this will not list attributes of some entries that refers to an invalid partition.

    Here are some related articles you may find helpful:

    Note: For more information on bcdedit, invoke bcdedit /? from your installation/repair/recovery system.

Bibliography

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment