Azure Disks And Images

In this post I’m going to explore a few more scenarios around VHDs and images in Azure. If you look at this post I showed how to copy a VM between subscriptions in a semi scripted way. I’ll be extending that script to create an image or hard disk from the copied VHD, but first some more info around blobs.

Blobs

VHDs are stored in Azure Blob Storage and there are 3 types of blobs.

  • Page Blobs
    Used to store VHDs and is optimised for random reading and writing.
  • Block Blobs
    Used to store files that are suitable for streaming and written to once.
  • Append Blobs
    Used for appending data for example log files.

The different types of blobs come into play when you copy VHDs into Azure Blob Storage and depends on the tool you use. If you for instance use Azure PowerShell and the Add-AzureVHD cmdlet the VHD is copied as a page blob. If you use CloudBerry explorer you have to explicitly choose Copy As Page Blob, the default copy will create a block blob. For other tools it could be different so confirm how blobs are copied before you copy 120Gb to Azure just to find out it was the wrong format. Later in the post we will register the VHD as a disk.

Images
Images can be specialised or generalised (SysPrepped) and can contain multiple disks. If you want to create an image that can be used to create more instances of a VM, use a generalised image for example to create instances for a scale out scenario. If you want an exact copy of the VM, use a specialised image, for example to copy it to another subscription or to restore it from a snapshot. When you create an image using PowerShell remember to indicate the OSState. This equates to the check box in the portal, asking if you ran Sysprep.

Azure Disks And Images

If you want to capture an image from a VM you already have in the same subscription, shutdown the VM and save the image.

Save-AzureVMImage -ServiceName VmService -Name VMName -ImageName NewImage -OSState Specialised/Generalised

In the next script I create an image from a VHD I copied into Azure Blob Storage.

$DiskConf = New-AzureVMImageDiskConfigSet

Set-AzureVMImageOSDiskConfig -DiskConfig $DiskConf -HostCaching ReadWrite -OSState Specialised/Generalised -OS "Windows" -MediaLink $vdhurl

$DiskConf.DataDiskConfigurations = new-object Microsoft.WindowsAzure.Commands.ServiceManagement.Model.DataDiskConfigurationList     #work around for a bug

Add-AzureVMImage -ImageName "NewImage" -Label "Easier To Find" -OS Windows -DiskConfig $DiskConf -ShowInGui $true

You can use the old portal to create an image from a VHD, under the virtual machines menu, click in the images tab and choose create new, you can browse to the VHD from there.

From my investigation I could not find a way to change the OSState after the image was created, you might be able to do it by altering the meta data on the blob but I didn’t try it yet. From experience using a specialised image where generalised images are expected doesn’t work, for example creating a new VM from a specialised image that is tagged as generalised just hangs on start up and the boot sequence never completes. Could have been something specific about the VMs I used but it happened twice.

In the old portal you can create a new VM using your existing images by choosing My Images.

Azure Disks And Images

Or pass in the image name in PowerShell.

$VM = New-AzureVMConfig -Name $VmName -InstanceSize $InstanceSize     -ImageName $SourceName

When you create a VM from an image, Azure will create new disks based on the disks referenced by the image, like templates in VMWare.

OS Disks
OS disks contains the disk used for booting, the disk is assumed to be specialised. I did not try to create a VM from one that is generalised yet. If you copied a VHD into blob storage you can create an OS disk using PowerShell.

Add-AzureDisk -DiskName "NewVMBootDisk" -MediaLocation $vdhurl -Label "BootDisk" -OS "Windows"

Or you can use the old portal, under the virtual machines menu, click on the disks tab and choose create new, you can browse to the VHD from there.

Now you can reference this disk to create a new VM.

$VM = New-AzureVMConfig -Name $VmName -InstanceSize $InstanceSize -DiskName "NewVMBootDisk"

You can create a new VM from your OS disk in the old portal by choosing My Disks.

Azure Disks And Images

Once you create a new VM from this disk it is not available to other VMs since it is now attached to the VM you created. This is similar to creating a new VM but attaching an existing disk to it in VMWare.

Data Disks
Data Disks can be attached to a VM but you cannot boot from it. If you copied a VHD into blob storage you can register it as a data disk when attaching it to a VM.

Add-AzureDataDisk -VM $VM -ImportFrom -MediaLocation "VHDUrl" -LUN 1

Francois Delport

Published by

Francois Delport

I am a cloud and devops consultant, technology fan and previously a professional C# developer with a keen interest in system design and architecture. Currently I am involved in projects using Azure, the Microsoft stack and DevOps. I am based in Melbourne, Australia. Email: [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *