Linux VirtualBox Conversion

This procedure converts and existing linux install on an EFI system into a virtual machine within VirtualBox.

  1. Boot with the debian live CD. You can use the one labeled “system” which is the smallest, or any you have.

  2. Become root with sudo -i

  3. Install “zerofree”

  4. Run `zerofree -v /dev/sdXX, where sdXX is the partition of the root partition; repeat for the other ext partitions; exclude the EFI boot partition from this step.

  5. Mount the partitions associated with the linux install; at minimum the EFI boot partition and the root partition, to say /mnt/linux.

  6. If you have only the “system” live cd, which is text mode only, then install elinks, open elinks to the virtualbox download page elinks https://www.virtualbox.org/wiki/Linux_Downloads arrow down to the version of Debian you have (e.g. Debian 11) and press “D” for download. Then press “Q” to exit elinks. The downloaded file will be in your current directory. Install this file with dpkg -i <debian pkg name>. There will be warnings, but all we need from this step is the vboxmanage utility, which will work despite the warnings.

  7. Mount a flash drive or removable drive that is formatted with vfat or extfat so that is can be seen by Windows; e.g. onto /mnt/flash.

  8. Convert the raw partitions to VirtualBox vdi files by running vboxmanage convertfromraw /dev/sdXX sdXX_descr.vdi where sdXX is the source partition and sdXX_descr describes the partition as a filename, e.g. sda2_root.

  9. Type sync then type reboot to reboot the machine.

  10. Allow the machine to boot into Windows

  11. Install VirtualBox

  12. Add the vdi files to the list under Tools, Media, Hard disks.

  13. Create a new virtual machine

  14. Attach these disks to the machine, under Storage, Controller:SATA, adds hard disk

  15. Under System, Extended Features, check the box for Enable EFI

  16. Set other settings as needed.

  17. Attempt to boot.

  18. If the boot fails, then setup the debian live cd within VirtualBox as well, and attach these same drives to that machine; note the order of the drives as they appear on the summary page of the machine. This order will determine where the drives are found in the running system; the first will be sda (just sda, no partition number after it), etc.

  19. Boot the live cd image virtual machine

  20. Become root with sudo -i

  21. Move to /mnt, and create /mnt/linux; mount the partitions under /mnt/linux; mounting the EFI partition under /mnt/linux/boot/efi

  22. Also mount proc, sys, and dev by repeating this as needed for each: mount -o bind /proc /mnt/linux/proc

  23. Change root to the /mnt/linux: chroot /mnt/linux /bin/bash

  24. Run grub-install --target=x86_64-efi /dev/sdX where sdX corresponds to the partition that hold the EFI partition.

  25. Run update-grub

  26. exit, then unmount all the partitions you manually mounted in reverse order, including /mnt/linux as the last one.

  27. shutdown -h now to shutdown the live cd virtual machine.

  28. Attempt to boot the virtual machine again.