Linux VirtualBox Conversion
This procedure converts and existing linux install on an EFI system into a virtual machine within VirtualBox.
-
Boot with the debian live CD. You can use the one labeled “system” which is the smallest, or any you have.
-
Become root with
sudo -i
-
Install “zerofree”
-
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. -
Mount the partitions associated with the linux install; at minimum the EFI boot partition and the root partition, to say /mnt/linux.
-
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 withdpkg -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. -
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.
-
Convert the raw partitions to VirtualBox vdi files by running
vboxmanage convertfromraw /dev/sdXX /mnt/flash/sdXX_descr.vdi
where sdXX is the source partition and sdXX_descr describes the partition as a filename, e.g. sda2_root. -
Type
sync
then typereboot
to reboot the machine. -
Allow the machine to boot into Windows
-
Install VirtualBox
-
Add the vdi files to the list under Tools, Media, Hard disks.
-
Create a new virtual machine
-
Attach these disks to the machine, under Storage, Controller:SATA, adds hard disk
-
Under System, Extended Features, check the box for Enable EFI
-
Set other settings as needed.
-
Attempt to boot.
-
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), sdb, sdc, etc. -
Boot the live cd image virtual machine
-
Become root with
sudo -i
-
Move to /mnt, and create /mnt/linux; mount the partitions under /mnt/linux; mounting the EFI partition under /mnt/linux/boot/efi
-
Also mount
proc
,sys
, anddev
by repeating this as needed for each:mount -o bind /proc /mnt/linux/proc
-
Change root to the /mnt/linux:
chroot /mnt/linux /bin/bash
-
Run
grub-install --target=x86_64-efi /dev/sdX
where sdX corresponds to the partition that holds the EFI partition. -
Run
update-grub
-
exit
, then unmount all the partitions you manually mounted in reverse order, including /mnt/linux as the last one. -
shutdown -h now
to shutdown the live cd virtual machine. -
Attempt to boot the virtual machine again.
-
It will get part way and you will have to enter the system’s root password for system prompt, then edit the /etc/fstab file with the new partition identifiers,
/dev/sda
, etc, and comment out any that you will set up later, including swap, thenexit
to continue.
You should arrive at the same login as you had on the real machine.
You’ll want to setup the guest additions in the linux machine to gain performance.