Bog Roll ::

It's Not Magic, It's Work!

31 Aug 2020

Physical to Virtual

After upgrading my Dell laptop from HDD to SDD I had a perfect image of the laptop on an unused HDD. I thought I'd have a go at converting it from a physical system to a virtual one.

Using the SATA-USB cable I hooked it up to my desktop system, it appeared as /dev/sdc on my desktop system. If you have a USB3 or SATA connector that will be a lot faster than the USB2 I had to use.

First you need to know how big your disk is in bytes, so for that I used:
sudo fdisk -l /dev/sdc

Next you read the image from the disk to a file. You obviously need at least as much free disk space as the image, I used:
sudo cat /dev/sdc | VBoxManage convertfromraw stdin image.vdi 160041885696
where sdc is where the dive was added, image.vdi was the image I wanted to create and 160041885696 was the disk size that fdisk returned. Once that started go for a cup of tea or two...

Next up is the bit that requires luck. In Virtualbox I created a new system, and tried to match it as close as I could to the underlying physical system it came from. When you boot the system Windows could blue screen or the Linux initramfs may be missing essential drivers, and you could be stuck. In my case, both booted fine with the settings I'd given it.

Next I wanted to reallocate the disk space. So on the Linux image I de-installed all the GUI components, leaving only a small CLI system, then using an image of GParted Live I shrank the Linux system from ~110 GB down to ~15 GB and expanded Windows from ~40 GB to ~135 GB. At this point the virtual disk image was quite fragmented and not very optimal.

I booted Windows up, deleted things I didn't need, de-fragged it and then downloaded and ran sdelete.exe from Sysinternals. That doesn't actually shrink the virtual image, but it does clean it up so that it can be compacted. I shut Windows down and then used Virtual Box to compress the image:
vboxmanage modifymedium --compact <image GUID>
You are supposed to be able to use the filename of the image file, but it didn't do anything so I used the image GUID which worked fine.

The result is now I have a fully functioning virtual Windows 10 system and a residual Linux (Debian 10) system inside a Virtual box. If I could be bothered I'd remove Linux from the image and return it to booting from the Windows boot loader instead of GRUB. The total disk size of the image on my desktop is now closer to 50 GB, which isn't too bad.

I don't run Window at home, but it's useful to have a copy lying around when some idiot company doesn't provide a way to do something with Linux and you are forced to use Windows to talk to a piece of hardware.