Freigeben über


Create your own VM images

(deutsche Version hier)

Azure provides a variety of images for new VMs (currently more than 800). But sometimes you may want to use your own image for new VMs. It could be for example that a desired image is yet not available in the Microsoft Cloud Germany, and you don’t want to wait and are looking for a way...

Now, a word of caution here: clearly you can do that, but for a productive use in the Microsoft Cloud Germany please rely on an officially published image. This here is only for warming up…

We take as an example an Ubuntu image, and since we don't have our own, we take the one provided in the public Azure Cloud and pretend that we did all the adjustments  we need...

How do we proceed? The rough sequence is:

  1. Create new Ubuntu VM in the public Azure Cloud
  2. prepare for imaging
  3. copy VHD to the Microsoft Cloud Germany
  4. create new Ubuntu VM in the Microsoft Cloud Germany

Here we go:

Create new Ubuntu VM in the public cloud

We create a classic VM with a service, the following one-liner should be clear, or? The used image can vary, I took version 12.4.5:

$image="b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-12_04_5_LTS-amd64-server-20150413-en-us-30GB"
New-AzureQuickVM -Linux -Name "ubuntu-image" -Servicename "ubuntu-service"
-Imagename $image –Linuxuser "superman" -Password "P@ssw0rd!"
-Location "West Europe" -Instancesize Small

Prepare image

We login to the VM once using SSH, finding the address is easy:

$vm=Get-AzureVM -Name "ubuntu-image" -Servicename "ubuntu-service"
$vm | Get-AzureEndpoint

Use the information for VIP and port. Of course it makes sense to wait until the VM started completely. After the login, we do the deprovisioning:

sudo waagent –deprovision
sudo init 0

and our connection is gone. Now we are using the following commands to remove the VM and get a free blob, because Azure treats the VHD as a disc, so we first delete the VM (without deleting the VHD of course), and after that we delete the disc, which will leave us with a simple blob. We need the disc name for this, and luckily we have stored all necessary information in the $vm object (see above)

$diskname = $vm.VM.OSVirtualHardDisk.DiskName
$vm | Stop-AzureVM
$vm | Remove-AzureVM
Remove-AzureDisk –DiskName $diskname

Please remember to remove the VM, but without –DeleteVHD, that would be stupid now...

Copy VHD

We have now a blob containing a VHD, and we copy it over to our new environment. You can use a tool like the CloudBerry storage Explorer that can copy directly between clouds.

A little remark: Blobs can’t be renamed, but you can do a copy and delete. To save time you should provide the new name as target already.

Prepare image

Now we will register the copied blob as an image to be able to use it for new VMs. We already did the main work. For now, we first collect all necessary information. We will assume the new StorageAccount is called "germany1", the container is called "images", and the BLOB is called "ubuntu-2016-v1.vhd". Of course you can choose different names…:

$storage="germany1"
$container="images"
$vhd="ubuntu-2016-v1.vhd"

$context=New-AzureStorageContext -StorageAccountName $storage
    -StorageAccountKey (Get-AzureStorageKey -StorageAccountName $storage).Primary

$blob=Get-AzureStorageBlob -Context $context -Container $container -Blob $vhd
$uri = ($blob.ICloudBlob).Uri.AbsoluteUri

Now here comes the magic part:

Add-AzureVMImage -Imagename $blob.Name -RecommendedVMSize Small -OS Linux -MediaLocation $uri

and we see our new image in the list of available images:

Get-AzureVMImage | select Label,ImageName

Use image

We can now use it as an image when provisioning a new VM. This is nearly the same command as in the beginning of this article, only the image name has changed (and the location of course).

$image="ubuntu-2016-v1"
New-AzureQuickVM -Linux -Name "ubuntu-image" -Servicename "ubuntu-service"
-Imagename $image –Linuxuser "superman" -Password "P@ssw0rd!"
-Location "Germany Central" -Instancesize Small

Congratulations! You just created a new VM based on a self-made image. Wasn’t so hard, was it?

Comments

  • Anonymous
    March 16, 2016
    Wie erzeuge ich eigene VM Images in Azure mit PowerShell?