How to capture Linux VM in Azure


Images are used in Azure to provide a new virtual machine with an operating system. An image might also have one or more data disks. Images are available from several sources:

  • Azure offers images in the Marketplace. There are recent versions of Windows Server and distributions of the Linux operating system. Some images also contain applications, such as SQL Server. MSDN Benefit and MSDN Pay-as-You-Go subscribers have access to additional images.
  • The open source community offers images through VM Depot.
  • You also can store and use your own VM or OS images in Azure, by either capturing an existing Azure virtual machine for use as an image or uploading an image

There is a little difference between VM image (newer type) and OS image. VM image can include disk with generalized OS (sysprep in the Windows Server’s world) and data disks attached to the VM. OS image includes only OS disk.

I’ll show you how to make a new VM image from Linux VM created in Azure Resource Manager. You can use this image to create VMs across any resource group within your subscription (thanks to azure managed disks).

Before we start download and install the latest Python

Launch CMD , verify Python’s version and install Azure CLI 2.0

python --version
pip install --user azure-cli

azurecli_01

Open SSH to your VM (use azure public ip, root creds) and start VM’s deprovision (read WARNINGS!)

sudo waagent -deprovision+user -force

azurecli_02

Now VM is ready for generalizing

Switch back to CMD and change directory to C:\Users\yourusername\AppData\Roaming\Python\Python35\Scripts
Login to the Azure Account using Azure CLI (use received code to authenticate)

az login

azurecli_03

Select subscription in which source VM is running

#To list all subscriptions and get IDs
az account list

#To select target subcription
az account set --subscription subid

azurecli_04

Stop and deallocate the source VM

az vm deallocate --resource-group "groupname" --name "vmname"

azurecli_05

Time to generalize VM and create VM image

az vm generalize --resource-group "groupname" --name "vmname"
az image create --resource-group "groupname" --name "ImageName" --source "SourceVMName"

Get image list from CLI (copy Image ID):

azurecli_07

Azure side (Images):

azurecli_06

Now we are ready to create VM or bunch of VMs from this image

az vm create --resource-group "groupname" --name "VMname" --image "imageid" --admin-username username --authentication-type password --admin-password "cleartexthere"

azurecli_08

Note: VM Size , Storage type will be selected automatically by Azure. You need to manually define them if it’s required (see examples below)

Simple script that creates bunch of VMs with naming test-VM-0x , predefined VM size and storage type

for /L %%n in (1,1,9) do (
az vm create --resource-group "groupname" --name test-VM-%%n --storage-sku "StorageTypeHere (example: Standard_LRS)" --size "VMsize (example: Basic_A4)" --image "image id here" --admin-username adminname --authentication-type password --admin-password "password here"
)

Result:vms

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s