Prepare an Ubuntu virtual machine for Azure

Note

Azure has two different deployment models for creating and working with resources: Resource Manager and classic. This article covers using both models, but Microsoft recommends that most new deployments use the Resource Manager model.

Official Ubuntu cloud images

Ubuntu now publishes official Azure VHDs for download at http://cloud-images.ubuntu.com/. If you need to build your own specialized Ubuntu image for Azure, rather than use the manual procedure below it is recommended to start with these known working VHDs and customize as needed. The latest image releases can always be found at the following locations:

Prerequisites

This article assumes that you have already installed an Ubuntu Linux operating system to a virtual hard disk. Multiple tools exist to create .vhd files, for example a virtualization solution such as Hyper-V. For instructions, see Install the Hyper-V Role and Configure a Virtual Machine.

Ubuntu installation notes

  • Please see also General Linux Installation Notes for more tips on preparing Linux for Azure.
  • The VHDX format is not supported in Azure, only fixed VHD. You can convert the disk to VHD format using Hyper-V Manager or the convert-vhd cmdlet.
  • When installing the Linux system it is recommended that you use standard partitions rather than LVM (often the default for many installations). This will avoid LVM name conflicts with cloned VMs, particularly if an OS disk ever needs to be attached to another VM for troubleshooting. LVM or RAID may be used on data disks if preferred.
  • Do not configure a swap partition on the OS disk. The Linux agent can be configured to create a swap file on the temporary resource disk. More information about this can be found in the steps below.
  • All VHDs on Azure must have a virtual size aligned to 1MB. When converting from a raw disk to VHD you must ensure that the raw disk size is a multiple of 1MB before conversion. See Linux Installation Notes for more information.

Manual steps

Note

Before attempting to create your own custom Ubuntu image for Azure, please consider using the pre-built and tested images from http://cloud-images.ubuntu.com/ instead.

  1. In the center pane of Hyper-V Manager, select the virtual machine.

  2. Click Connect to open the window for the virtual machine.

  3. Replace the current repositories in the image to use Ubuntu's Azure repos. The steps vary slightly depending on the Ubuntu version.

    Before editing /etc/apt/sources.list, it is recommended to make a backup:

     # sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
    

    Ubuntu 12.04:

     # sudo sed -i 's/[a-z][a-z].archive.ubuntu.com/azure.archive.ubuntu.com/g' /etc/apt/sources.list
     # sudo apt-get update
    

    Ubuntu 14.04:

     # sudo sed -i 's/[a-z][a-z].archive.ubuntu.com/azure.archive.ubuntu.com/g' /etc/apt/sources.list
     # sudo apt-get update
    

    Ubuntu 16.04:

     # sudo sed -i 's/[a-z][a-z].archive.ubuntu.com/azure.archive.ubuntu.com/g' /etc/apt/sources.list
     # sudo apt-get update
    
  4. The Ubuntu Azure images are now following the hardware enablement (HWE) kernel. Update the operating system to the latest kernel by running the following commands:

    Ubuntu 12.04:

     # sudo apt-get update
     # sudo apt-get install linux-image-generic-lts-trusty linux-cloud-tools-generic-lts-trusty
     # sudo apt-get install hv-kvp-daemon-init
     (recommended) sudo apt-get dist-upgrade
    
     # sudo reboot
    

    Ubuntu 14.04:

     # sudo apt-get update
     # sudo apt-get install linux-image-virtual-lts-vivid linux-lts-vivid-tools-common
     # sudo apt-get install hv-kvp-daemon-init
     (recommended) sudo apt-get dist-upgrade
    
     # sudo reboot
    

    Ubuntu 16.04:

     # sudo apt-get update
     # sudo apt-get install linux-generic-hwe-16.04 linux-cloud-tools-generic-hwe-16.04
     (recommended) sudo apt-get dist-upgrade
    
     # sudo reboot
    

    See also:

  5. Modify the kernel boot line for Grub to include additional kernel parameters for Azure. To do this open /etc/default/grub in a text editor, find the variable called GRUB_CMDLINE_LINUX_DEFAULT (or add it if needed) and edit it to include the following parameters:

     GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0,115200n8 earlyprintk=ttyS0,115200 rootdelay=300"
    

    Save and close this file, and then run sudo update-grub. This will ensure all console messages are sent to the first serial port, which can assist Azure technical support with debugging issues.

  6. Ensure that the SSH server is installed and configured to start at boot time. This is usually the default.

  7. Install the Azure Linux Agent:

     # sudo apt-get update
     # sudo apt-get install walinuxagent
    

    Note

    The walinuxagent package may remove the NetworkManager and NetworkManager-gnome packages, if they are installed.

  8. Run the following commands to deprovision the virtual machine and prepare it for provisioning on Azure:

     # sudo waagent -force -deprovision
     # export HISTSIZE=0
     # logout
    
  9. Click Action -> Shut Down in Hyper-V Manager. Your Linux VHD is now ready to be uploaded to Azure.

References

Ubuntu hardware enablement (HWE) kernel

Next steps

You're now ready to use your Ubuntu Linux virtual hard disk to create new virtual machines in Azure. If this is the first time that you're uploading the .vhd file to Azure, see Create a Linux VM from a custom disk.