Moving Virtual Machine Disk to Physical Partition

The following steps are tested on Ubuntu 14.04 host OS and Ubuntu 16.04 VirtualBox image. This process should work on any other systems with appropriate changes.

1. Convert VirtualBox drive to raw image. Find out the virtual disk filename from the settings of the virtual machine.

$ VBoxManage internalcommands converttoraw vmdisk.vdi disk.img

2. In most cases, the OS partition will have an offset due to MBR or EFI. Determine this offset and note it down. We will need that offset in next steps.

$ parted disk.img
WARNING: You are not superuser. Watch out for permissions.
GNU Parted 2.3
Using /home/ninja/Documents/vvv/disk.img
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit
Unit? [compact]? B
(parted) print

The output should look something like:

Model: (file)
Disk /home/ninja/Documents/vvv/disk.img: 32212254720B
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1048576B 30064771071B 30063722496B primary ext4 boot
2 30064771072B 32212254719B 2147483648B extended
5 30065819648B 32212254719B 2146435072B logical linux-swap(v1)

(parted) q

3. In the above case, offset of the first partition (in which Ubuntu 16.04 is installed) is 1048576. I prefer renaming the file by suffixing the offset to the filename.

$ mv disk.img disk_1048576.img

4. Using GParted, create a new partition to store this image.
5. Mount the disk image

$ sudo mkdir /media/vmimage
$ sudo mount -o loop,offset=1048576 -t ext4 disk_1048576.img /media/vmimage

6. Mount the newly created partition. In the following command, /dev/sda4 is the new partition

$ sudo mkdir /media/iCub
$ sudo mount /dev/sda4 /media/iCub

7. Copy files from image to physical partition using either rsync of direct copy. Warnings related to special files while copying can be ignored.

$ sudo rsync -rtvpl /media/vmimage/ /media/iCub/

8. Mount /dev, /proc, and /sys

$ sudo mount --bind /dev /media/iCub/dev
$ sudo mount --bind /proc /media/iCub/proc
$ sudo mount --bind /sys /media/iCub/sys

9. Run bklid to find out the UUID of new partition (/dev/sda4 in this case). This is required in step 11.

$ sudo blkid
/dev/sda1: UUID="E1E2-36E0" TYPE="vfat"
/dev/sda2: UUID="ba551082-be43-426b-ac14-d32ef82f4648" TYPE="ext4"
/dev/sda3: UUID="e6a0fc82-0090-42e2-8494-82231d563f5b" TYPE="swap"
/dev/sda4: LABEL="iCub" UUID="b42fd8eb-dd33-455e-b400-8ef27932a046" TYPE="ext4"

10. chroot into the partition to fix fstab and grub

$ sudo chroot /media/iCub

11. As the disk was mounted on the virtual machine, its UUID was different. Now that we are mounting this disk on a physical partition, we should replace the UUID in /etc/fstab so that the system knows where to boot from

root:/# vi /etc/fstab
# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).

UUID=ba551082-be43-426b-ac14-d32ef82f4648 / ext4 errors=remount-ro 0 1

UUID=e6a0fc82-0090-42e2-8494-82231d563f5b none swap sw 0 0

UUID=E1E2-36E0 /boot/efi vfat defaults 0 1

12. generate grub config and update grub.

root:/# grub-mkconfig
root:/# update-grub
root:/# update initramfs -u

13. Exit from chroot and unmount the partitions

root:/# exit
$ sudo umount /media/iCub/dev
$ sudo umount /media/iCub/proc
$ sudo umount /media/iCub/sys
$ sudo umount /media/iCub

14. run boot-repair and select recommended repair from the menu

$ sudo add-apt-repository ppa:yannubuntu/boot-repair
$ sudo apt-get update
$ sudo apt-get install -y boot-repair && boot-repair

15. Reboot your system and select the new OS from grub menu
16. Graphics might fail to load, drop to shell and run the following. In this case, icub is a user configured in the virtual machine, replace it with your user id from the virtual machine. If you have multiple users in VM, fix folder permissions for all the users.

root:/# chown -R icub:icub /home/icub
root:/# rm /home/icub/.Xauthority

root:/# rm -rf /var/cache
root:/# apt-get update
root:/# apt-get dist-upgrade
root:/# apt-get install --reinstall lightdm xserver-xorg-lts-xenial ubuntu-desktop apt apt-utils aptdaemon aptdaemon-data update-manager update-manager-core dbus
root:/# updatedb

17. Install Nvidia drivers, if applicable and reboot

root:/# apt-get install nvidia-384
root:/# reboot

That’s all folks!!!