In this post I will discuss how we can create custom virtual machine images and deploy them to the Microsoft Azure platform. To complete this process you will need an Azure Subscription, the Azure PowerShell module installed and a pre-prepared VHD which you would like to use (VHDX is not supported at present.)
You can sign up for a free trial of Microsoft Azure here if you don’t currently hold a subscription.
Completing this process will allow you take advantage of platforms which aren’t offered “out of the box” on Microsoft Azure eg, Server 2003 and Server 2008 for testing and development. Currently Microsoft offers Server 2008 R2 as the minimum level from the Azure Image Gallery.
What do I need to do to prepare my image?
To complete this process, I built a volume license copy of Windows Server 2008 Standard inside a generation one Hyper-V guest virtual machine. Once the installation of the operating system completed I installed Adobe Acrobat Reader. I then ran sysprep.exe to generalise the image. This is important, if you don’t generalise your images, they will fail to deploy on the Azure platform.
I will detail the steps carried out after the operating system install below.
- Log into the newly created virtual machine
- Install the Hyper-V virtual machine additions (if your guest doesn’t already have it installed)
- Install any software that is required in your image (I installed Acrobat Reader)
- From an Administrative command prompt, navigate to %windir%\system32\sysprep and then execute the command “sysprep.exe”
- Once the SysPrep window has opened, select Enter System Out of Box Experience (OOBE) and tick the Generalize check box. The shutdown action should be set to Shutdown, this will shut down the machine gracefully once the sysprep process has completed.
- Once you are ready, select OK and wait for the process to complete.
I built my machine inside a dynamically expanding VHD, the main reason for doing so was to avoid having to upload a file size which was larger than necessary. As a result of this, I chose to compact the VHD before moving on to the next step by using the disk wizard inside the Hyper-V management console. To complete this process, follow the steps below.
- From the Hyper-V Host pane, select Edit Disk
- Browse to the path of VHD we were working on, in my case it is “C:\VHDs\Server2008.vhd” and select Next
- Select Compact and Finish.
- Wait for the process to complete. Your VHD file is now ready to upload.
What’s next?
We are now ready to upload the virtual machine image, to complete this process you will need access to the Azure PowerShell cmd-lets and a storage account for the source VHD. If you do not already have a storage account created, you can follow the documentation provided by Microsoft here.
IMPORTANT: Once you have a storage account in Azure, ensure that you have a container called VHDs. If you don’t have a container you can create on by selecting Add from the bottom toolbar, name it vhds and ensure the access is set to Private (container shown below.)
We are now ready to connect to the Azure account to kick off the upload process. To do so, launch an Administrative Azure PowerShell console and follow the following steps.
- Run the cmd-let Add-AzureAccount, this will present a window which will allow you to authenticate to Azure.
- On the next screen, enter your Password. The PowerShell session is now connected.
- To verify that the session connected successfully, run the cmd Get-AzureAccount, you should see your account listed below.
We are now ready to commence the upload process. You will need your storage blob URL. You can find this on the container page we visited previously to create the vhds container.
The complete command is as follows.
Add-AzureVhd -Destination “<StorageBlobURL>/vhds/Server2008.vhd” -LocalFilePath “C:\VHDs\Server2008.vhd”
Once you have executed the command, two things happen..
- The VHD file is indexed by calculating the MD5 hash
- Once the indexing process is completed, the upload starts.
This is very neat, as the demo gods often fail us… (my upload actually failed part way through.) Thankfully I was able to re-execute the command, which resumed the upload process where the first pass left off (see below.)
- Wait for the upload process to complete.
Creating the Image in the Azure console.
Now that our upload has completed, we are ready to create an image in the Azure console. This will allow us to easily spawn virtual machines based on the image we uploaded earlier. To complete this process you will need access to the Azure console and your freshly uploaded image.
- Select Virtual Machines from the management portal.
- Select Images from the virtual machines portal.
- Select Create an Image
- A new window titled Create an image from a VHD will pop up. Enter the following details (as shown below.)
- Name
- Description
- VHD URL (from your storage blob)
- Operating System Family
Ensure you have ticked I have run Sysprep on the virtual machine or you will not be able to proceed.
- The Image will now appear under MY IMAGES in the image gallery.
Deploying the image!
All the work we have completed so far won’t be much use if the deployment phase fails. In this part of the process we will deploy the image to ensure it will work as expected.
- Select Virtual Machines from the management portal.
- Select New > Compute > Virtual Machine > From Gallery
- From the Choose an Image screen, select MY IMAGES. You should see the image that we just created in the gallery (shown below.)
- Select the Image and click Next
- Complete the Virtual Machine Configuration with your desired settings.
- Wait for the virtual machine to complete deployment provisioning.
Connecting to the virtual machine.
The hard work is done! We are now ready to connect to our newly deployed machine to ensure it is functioning as expected.
- Select Virtual Machines from the management portal.
- Select the Virtual Machine and then click Connect from the toolbar down the bottom. This will kick-off a download for the RDP file which will allow you to connect to the virtual machine.
- Launch the RDP file, you will be asked to authenticate. Enter the credentials you specified during the deployment phase and click OK
- You will now be presented with your remote desktop session, connected to your custom image deployed on Microsoft Azure.
I went ahead and activated my Virtual Machine. To prove there is no funny business involved, I have provided one final screenshot showing the machine activation status (which details the Windows version) and a snip showing the results of the ipconfig command. This lists the internal.cloudapp.net addresses showing that machine is running on Microsoft Azure.
Enjoy!