Contact

Technology

Nov 04, 2013

Rapidly Deploying Virtual Machines From a Custom Image in Windows Azure

Bobby Crotty

Bobby Crotty

Default image background

Windows Azure allows you to create a virtual machine (VM) from a growing list of images such as Windows Server 2012, but did you know you can create your own custom image or disk? An image is a template that can be used to create many systems, whereas a disk can only be used on a single machine. Don’t get confused between disk objects in Windows Azure and virtual hard disk (VHD) files. You can have a VHD file uploaded to Windows Azure, but you won’t be able to attach it to a VM until you also create a disk object associated with that VHD file. If you’re interested in moving a system to Windows Azure or using your own custom disk, see my blog Moving a System or Drive to Windows Azure.

Custom images allow you to rapidly deploy systems that are already configured and have applications installed. These systems can easily be changed later, but using an image can save you a lot of time if you regularly perform the same initial configuration on your systems.

Prerequisites

You’ll need a Windows Azure subscription with a management certificate installed. You can create a self-signed X.509 v3 certificate in IIS or Visual Studio. You need a hypervisor that supports the VHD format. I’ll be using Microsoft’s Hyper-V, but there are several other solutions that will work. You also need to configure your base system that you will clone to create your image. You might, for example, add software, install updates or drivers, or change Windows settings.

Preparing your VHD for uploading is much easier if your system is running as a virtual machine. Merge or delete any VM snapshots. If you want to keep using your VM locally, export the VM or backup the VHD file before you begin. Also, Windows Azure does not support the VHDX format yet. If your disk file is in that format, you can click Edit Disk… in Hyper-V, locate your disk, and choose the convert option.

If the base system is a physical machine, use the Disk2vhd utility to create a VHD copy of your hard drive, and then create a virtual machine in Hyper-V with the disk attached. See Jeff Hicks’s blog for how to do that.

Generalize the Image

Multiple systems cannot share a computer name or security identifier, which is why you get an error if you try to create a VM from a copy of another VM’s VHD. To create an image that can be duplicated you need to remove all of the machine-specific information, which is a process called generalization.

Windows systems can be generalized using the Sysprep utility, which is located on every Windows machine at %WINDIR%\System32\Sysprep\sysprep.exe. When you launch this program, under System Cleanup Action, select Enter System Out-of-Box Experience (OOBE) from the dropdown menu and mark the checkbox next to Generalize. Under Shutdown Options choose Shutdown from the dropdown menu, and click OK.

The Sysprep process will start, and when complete the VM will shut down. The VHD associated with the VM is now ready to be used as an image. Save the location of the VHD file because we’ll use it when we start the upload.

Create a Windows Azure Storage Account and Container

Now we need somewhere to put the VHD in Windows Azure. If you do not already have a Windows Azure storage account and container, log in to the Windows Azure Management Portal. Click the New button on the bottom left and click Data Services > Storage > Quick Create. Type in a globally-unique URL for the storage account, choose the Location/Affinity Group and Geo-Replication settings that best suit your needs

In the main portal, click Storage on the left, and then click the storage account you just created. Click Containers at the top of the page, and click Add at the bottom middle. Choose a name for the container and under Access choose Private unless you want the container to be publically accessible. Click the checkmark on the bottom right of the popup and wait for the container to be created. From the list of containers, save the URL of the container you just created.

Connect PowerShell to Windows Azure

Previously you had to use the Windows Azure command prompt to upload VHD files, but using PowerShell is now the preferred method. Install and start Windows Azure PowerShell. Run the command Get-AzurePublishSettingsFile, which will open a web browser and ask you to log in to Windows Azure. Copy the path to the file that downloads, and then run the command Import-AzurePublishSettingsFile <path>, replacing <path> with the path you just copied. The PowerShell window will tell you which subscription is currently active. To select a different subscription use the Get-AzureSubscription to see a list of subscriptions and the Select-AzureSubscription command to pick one.

Upload the VHD

In the same PowerShell window, run the command:

Add-AzureVhd -Destination <container URL>/<VHD name> -LocalFilePath <path to image VHD>

Replace <container URL> with the URL from the list of containers. <VHD name> can be any name you want for the VHD file in Windows Azure, but make sure you have “.vhd” at the end. Finally, replace <path to image VHD> with the local path of the VHD you created earlier. The command should look something like this:

Add‑AzureVhd ‑Destination http://teststorageacct.blob.core.windows.net/test/test.vhd ‑LocalFilePath “c:\VHDs\test.vhd”

This command will create a hash of the VHD file to ensure it uploads correctly and then begin the upload.

Create a New Windows Azure Image

Once the VHD file is in the storage container, we still need to create an associated image object that can be used to create VMs. Head back to the Windows Azure Management Portal and click Virtual Machines on the left. Click Images in the middle of the page and click the Create button at the bottom middle. Give the image a name and description, and then browse for the VHD you just uploaded. Choose whether the operating system is Windows or Linux, and confirm that you have run Sysprep. Click the checkmark, and Windows Azure will start creating the image

Creating a Virtual Machine From the Image

Your image can now be used to create a new VM with all the settings and software from your base system. Click the New button at the bottom left, and then under Compute > Virtual Machine, click From Gallery

If everything worked, when you click My Images you will see your image

If you don’t see it yet, try reloading the site first. Microsoft’s tutorial walks you through the steps of creating and connecting to the VM from here.

Creating an Image From a Windows Azure Virtual Machine

Windows Azure makes it very easy to create an image from a VM that it’s hosting. Simply generalize the VM using Sysprep as before, and from the Virtual Machines section, click Capture at the bottom middle. Type a name for the image, confirm that you have run Sysprep, and after you click the checkmark, Windows Azure will create an image from this VM.

Deleting Virtual Machines

When you create a VM based on an image, Windows Azure will automatically create a disk object and a VHD file. If you delete the VM, the disks and associated VHD files remain, but you can easily delete both from the Disks page in the Virtual Machines section by selecting the disk and clicking Delete and then clicking Delete the associated VHD.

The list of disks shows which VM a disk is attached to, but if a disk is no longer attached, you cannot see which VM it was attached to. If you have trouble determining which disk was attached to a VM, you can check the last modified date by browsing to the associated VHD file in the Storage section of the portal.

Final Thoughts

Windows Azure’s Infrastructure as a Service has been steadily improving and adding new features, and it is a great way to easily add scalable and resilient capacity to your infrastructure. Using images will allow you to quickly spin up new VMs, which you can then use with several other new features such autoscaling, endpoint monitoring, and remote PowerShell management.

For more great Windows Azure tips follow us @CrederaIT and LinkedIn.  If you have questions concerning Windows Azure, please contact us or visit our Technology Solutions overview for more information.

Conversation Icon

Contact Us

Ready to achieve your vision? We're here to help.

We'd love to start a conversation. Fill out the form and we'll connect you with the right person.

Searching for a new career?

View job openings