Copying the VHD of a Generation 2 Linux VM–and not booting afterwards…

If you have installed Linux in a Generation 2 virtual machine – everything is probably working great for you!  You can even export the virtual machine and import it somewhere else.  However, things will not go well if you just shutdown the virtual machine and copy the virtual hard drive to a new location.

If you do this – and create a new virtual machine with the copied virtual hard drive – you will find that it does not boot.

Why does this happen?

Well, when Linux is installed in a Generation 2 virtual machine it stores key boot information in the UEFI firmware of the virtual machine.  This is, in turn, stored in the virtual machine configuration file.  If you separate the virtual hard drive and virtual machine configuration file – you can no longer boot.

Luckily – you can avoid this.  Here are the steps that I had to take with an Ubuntu virtual machine to enable VHD copying:

  1. Log in to the virtual machine.
  2. Change directory to the boot EFI directory : cd /boot/efi/EFI
  3. Copy the ubuntu directory in to a new directory named boot : sudo cp –r ubuntu/ boot
  4. Change directory to the newly created boot directory : cd boot
  5. Rename the shimx64.efi file : sudo mv shimx64.efi bootx64.efi

What this does is move the boot information out of the UEFI firmware and onto the disk. With this done you can copy and boot the virtual hard drive with ease.

Cheers, Ben