Udostępnij za pośrednictwem


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

Comments

  • Anonymous
    February 13, 2015
    A better way of doing it right to begin with is to run this command: grub-install --target=x86_64-efi --efi-directory=/boot/efi --no-nvram --removable What command above that does is it properly re-installs grub to /boot/efi/EFI in removal mode. (i.e. using the well known /EFI/BOOT/BOOTX64.EFI bootfile). I remember seeing this issue being reported to Ubuntu's launchpad as a bug and I've privately contacted a few individuals inside MSFT to let them know about this initial workaround I came up with almost a year ago.

    • Anonymous
      March 21, 2016
      The grub-install command mentioned is not functional on all Linux distros, and therefore the 'manual' steps recommend are more universal to work on all distros and versions.
  • Anonymous
    March 25, 2015
    That's a great tip. Is there any way to shrink the VHD? For example, I installed Ubuntu 14.04.2 with no extra packages (other than the openssh server) and ended up with a 6GB VHD ... most of which I assume is unused.

    • Anonymous
      October 04, 2016
      Set BlockSize on VHD to 1MB, as described in this post:https://technet.microsoft.com/en-us/windows-server-docs/compute/hyper-v/best-practices-for-running-linux-on-hyper-v
  • Anonymous
    July 29, 2015
    Benjamin, Thank you for the post.  We just built a UnbuntuLamp server for a production blog.  We thought we had good backups and good copies of vhdx files, until we tried to restore......  Your post saved us a lot of time and effort. You rock!

  • Anonymous
    September 21, 2015
    There is the final resoution? Today no have a hotfix for it ? Thanks

  • Anonymous
    June 19, 2016
    Is there a way I can copy the bootorder config from the original to the new VM using the command Get-VMFirmware and the bootorder sub comand ?

    • Anonymous
      June 23, 2016
      Unfortunately, no. These commands just modify the boot order - they do not transfer the content of the firmware itself.
  • Anonymous
    June 24, 2016
    Hi,I have a problem where the original configuration file is lost. Now to i get to mount the ubuntu vhdx to do the necessary changes? Thank you.

  • Anonymous
    February 25, 2017
    Anyway if you already copy the VHDX without the config file, you could just fix the booting problem reintalling GRUB. Just mount an ISO of the distribution and boot it into rescue mode. From there reintall the GRUB and thats it. Worked for me with a ubuntu server VM gen2 in Server 2016.

  • Anonymous
    May 24, 2017
    I am seeing some sort of restart loop when I apply either the fix in the post or the alternative posted by TriJetScud. My VM keeps rebooting many times a second and the only message visible is "System BootOrder not found. Initializing defaults" (for a fraction of a second).Has something changed since this was posted?

  • Anonymous
    June 06, 2017
    It seems like the real problem here is MS took the aggressively customer-hostile action of making the config file binary-only so there's no way to modify it and fix it directly. With VMware, I would have been able to directly modify the VMX file with no problems.

  • Anonymous
    June 06, 2017
    The comment has been removed

  • Anonymous
    November 23, 2017
    The comment has been removed