Skip to content

Instantly share code, notes, and snippets.

@mca-gif
Created June 12, 2020 20:28
Show Gist options
  • Save mca-gif/9c694159fe05c530970c94c0bee1219d to your computer and use it in GitHub Desktop.
Save mca-gif/9c694159fe05c530970c94c0bee1219d to your computer and use it in GitHub Desktop.
Transfer Whole Disk Windows 10 VM to new drive with Linux

Transfer Whole Disk Windows 10 VM to new drive with Linux

Moving all of my Windows 10 VMs from a 2TB hybrid drive (sda) to a 512GB NVME (nvme1n1). This only worked so easily because the Windows system partition was only 160GB to begin with, so it was going to grow instead of shrink.

The original partition map on sda was as follows:

label: gpt
label-id: DF9036C3-BB74-4013-B1DE-B4E3B60B9BC3
device: /dev/sda
unit: sectors
first-lba: 34
last-lba: 3907029134

/dev/sda1 : start=        2048, size=      921600, type=DE94BBA4-06D1-4D40-A16A-BFD50179D6AC, uuid=C72ED38F-1AED-46D2-9E50-E604C5295DDD, name="Basic data partition", attrs="RequiredPartition"
/dev/sda2 : start=      923648, size=      202752, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=4589BBB5-7C1F-4727-A7C4-966D08D9FB02, name="EFI system partition"
/dev/sda3 : start=     1126400, size=       32768, type=E3C9E316-0B5C-4DB8-817D-F92DF00215AE, uuid=0768F02E-14CE-4A68-A9DC-37CE1B1EF005, name="Microsoft reserved partition"
/dev/sda4 : start=     1159168, size=   312264269, type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7, uuid=95853D5C-C89E-428A-848A-4D07CC6EFD85, name="Basic data partition"
/dev/sda5 : start=   313423872, size=     1144832, type=DE94BBA4-06D1-4D40-A16A-BFD50179D6AC, uuid=1BE9AE0E-8004-437B-AF3A-332BE5F7645A, attrs="RequiredPartition"
/dev/sda6 : start=   314568704, size=     2048000, type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7, uuid=B9112F13-57A4-4F55-ADEA-48960AC5FF1A
/dev/sda7 : start=   316616704, size=  3590412288, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=5EE51D66-8BF1-4667-A4BB-52719C421633

sda6 and sda7 were both data paritions that did not need to come over to the new drive.

Create Parition Scheme

Dump the partition map with sfdisk:

sudo sfdisk -d /dev/sda > sda-partmap
cp sda-partmap nvme1n1-partmap

Edit nvme1n1-partmap to remove the "last-lba" and the unnecessary partitions:

label: gpt
label-id: DF9036C3-BB74-4013-B1DE-B4E3B60B9BC3
device: /dev/nvme1n1
unit: sectors
first-lba: 34

/dev/sda1 : start=        2048, size=      921600, type=DE94BBA4-06D1-4D40-A16A-BFD50179D6AC, uuid=C72ED38F-1AED-46D2-9E50-E604C5295DDD, name="Basic data partition", attrs="RequiredPartition"
/dev/sda2 : start=      923648, size=      202752, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=4589BBB5-7C1F-4727-A7C4-966D08D9FB02, name="EFI system partition"
/dev/sda3 : start=     1126400, size=       32768, type=E3C9E316-0B5C-4DB8-817D-F92DF00215AE, uuid=0768F02E-14CE-4A68-A9DC-37CE1B1EF005, name="Microsoft reserved partition"
/dev/sda4 : start=     1159168, size=   312264269, type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7, uuid=95853D5C-C89E-428A-848A-4D07CC6EFD85, name="Basic data partition"
/dev/sda5 : start=   313423872, size=     1144832, type=DE94BBA4-06D1-4D40-A16A-BFD50179D6AC, uuid=1BE9AE0E-8004-437B-AF3A-332BE5F7645A, attrs="RequiredPartition"
/dev/sda6 : start=   314568704, size=     2048000, type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7, uuid=B9112F13-57A4-4F55-ADEA-48960AC5FF1A

Build the partition on nvme1n1 with the script:

sudo sfdisk /dev/nvme1n1 < nvme1n1-partmap

Clone Data

Each partition is can now be cloned over, one by one, using dd.

dd if=/dev/sda1 of=/dev/nvme1n1p1 bs=10M
dd if=/dev/sda2 of=/dev/nvme1n1p2 bs=10M
dd if=/dev/sda3 of=/dev/nvme1n1p3 bs=10M
dd if=/dev/sda4 of=/dev/nvme1n1p4 bs=10M
dd if=/dev/sda5 of=/dev/nvme1n1p5 bs=10M
dd if=/dev/sda6 of=/dev/nvme1n1p6 bs=10M

Bonus: Setup VM

Since I was originally using the the hybrid disk with a VM and I now wanted to use the NVME drive I was able to change the definition of the VM in virsh. When setting up VM I configured the disk with the following XML snippet:

<disk type='block' device='disk'>
  <driver name='qemu' type='raw' cache='none'/>
  <source dev='/dev/disk/by-id/ata-ST2000LX001-1RG174_ZDZ9A6W4'/>
  <target dev='vda' bus='virtio'/>
  <address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0'/>
</disk>

This gives the entire disk over to the VM and allows near real speed. Beacuse the underlying driver used to access the disk isn't being changed (from Windows' perspective) all that needed to change wa the disk ID.

<disk type='block' device='disk'>
  <driver name='qemu' type='raw' cache='none'/>
  <source dev='/dev/disk/by-id/nvme-INTEL_SSDPEKNW512G8_BTNH94610LW1512A'/>
  <target dev='vda' bus='virtio'/>
  <address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0'/>
</disk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment