The great switcharoo

A long time I have worked with Windows 8.1 as the host and Linux Mint 18 as the guest operating system on my laptop. Lately, the Windows host just served as shell for running Office 365, OneDrive and Outlook. Considering that it runs full disk encryption (the worst of its kind) and a bloaty anti-virus solution, it was time to switch roles and move the guest to the host and vice-versa.

Step 1: Extract
The first step was moving my existing Windows installation to a virtual disk. I used the excellent Disk2Vhd tool from Microsoft for this. Alternatively, you can also create a VHD file using the “Windows 7” backup tools which are still available in Windows 8-10, but are completely broken on my installation.
As posted earlier, I had to run a couple of checkdisks to get it finished (I even ran `chkdsk c: /scan /V` so that I could manually delete some corrupted files).

Step 2: Transform
Now that my existing Windows installation is packed in a VHD file, I need to convert it to VDI as I’ll be running it inside VirtualBox on my Linux host and VHD support is limited (no –compact).

vboxmanage clonemedium INPUT.VHD OUTPUT.VDI --format VDI --variant standard

My Linux guest was already in the VDI format, however, the Linux virtualbox-fuse package which allows you to mount and read these directly are deprecated and the only way to get them on Linux Mint is by compiling the source code yourself. So I converted the images to a RAW format. My guest OS had two disk drives, one with the OS and another with 2 partitions for ‘/home’ and ‘/home/pw999/opt/’, I had to run the conversion twice.

vboxmanage clonemedium ROOT.VDI ROOT.RAW --format RAW
vboxmanage clonemedium HOME.VDI HOME.RAW --format RAW

With all these gigabytes converted I can now boot into a live Linux (Mint) to do all the magic.

Step 3: Load

With a new, empty disk installed in my laptop I’m now ready to migrate all data. This command will copy the ‘guest’ Linux root disk to the laptop’s internal disk drive, including the partition table and boot loader:

dd if=/media/mint/usbdrive/ROOT.RAW of=/dev/sda bs=1M

Once the copy was finished, I launched GParted. It will automatically ‘fix’ your drive so that you can partition the complete disk instead of just the original size of the VDI file. Once fixed I enlarged the root partition and created a second partition where I could mount /home.


With the root partition copied I still need to copy all my data from HOME.RAW to my newly created /home partition (which I temporarily mounted on /media/mint/hme/). I can not just dd this file to a new partition as this RAW file also has a partition table with two partitions. I thus have to mount each partition separately, for which I need both partition’s offsets.

Fist, open the file with fdisk and hit p to print the partition table.

sudo fdisk /media/mint/usbdrive/HOME.RAW
fdisk output (image kindly borrowed as I didn’t take screenshots during the process)

The offset is easily calculated by multiplying the Units with the Start position

sudo mount -o offset=1048576,ro /media/mint/usbdrive/HOME.RAW /media/mint/p1
sudo mount -o offset=105906176,ro /media/mint/usbdrive/HOME.RAW /media/mint/p2

With my two partitions mounted I can now copy the data using rsync (most people suggest to use ‘-av’ but I don’t like it because errors go missing in the tons of information it spits out)

sudo rsync -a /media/mint/p1 /media/mint/hme
sudo rsync -a /media/mint/p2 /media/mint/hme


Last but not least, I verified and updated /etc/fstab with the output of `sudo blkid`. Since the UUIDs of the root partition hadn’t changed I did not have to make any changes to GRUB.


Step 4: Reboot

Everything is now set to reboot. Once Linux was up & running it was time to remove the VirtualBox guest additions from the Linux system.

sudo aptitude remove virtualbox-guest-utils
sudo aptitude purge virtualbox-guest-utils
sudo find /etc -name "*vboxadd*" -exec rm {} \;
sudo find /etc -name "*vboxvfs*" -exec rm {} \;

Step 5: Encrypt
The last step was the most cumbersome. Having little to no experience with (post-install) encryption of the home folder I had to go through some trial & error.
Luckily for me, failure was always an option because I left some free space on the disk where I could rsync the content of my home folder as a quick and easy back-up.

I first tried ecryptfs (tutorial) but it felt so slow that I only kept it for encrypting the swap file.

sudo ecryptfs-setup-swap

For some reason, running this command did not succeed but gave me the following error message
swapon: stat of /dev/mapper/cryptswap1 failed: No such file or directory

After some more tries and cleaning up /etc/fstab and /etc/crypttab I rebooted my machine and it just worked.

I finally encrypted the home partition with LUKS using the following guide.

Step 6: Start Windows
The last step was getting Windows running in VirtualBox. It actually worked out of the box without any issues.
All I needed to do was re-configure my OneDrive shared, uninstall the manufacturer’s drivers and install the Guest additions and then it was up & running like it used to.
As last step I removed the old Linux guest images from the new Windows guest, ran `sdelete -z` to zero out the empty space and used Virtualbox to compact the Windows guest VDI image.

All this took me a couple of hours to figure out but once again it was a very educative trip for me.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s