How to: Clone a Linux Azure VM

In this post we are going to cover how to clone a Linux VM using the Microsoft Azure ARM portal and Azure CLI. In the next post we will use the JSON template and VHD we capture from today’s post to deploy a second VM, and then in a future post, we will cover tying everything together and creating an availability set.

First, you will want to ensure you have Azure CLI installed. You can download it for free here. To start, launch Windows PowerShell, or whichever shell you prefer, and set your configuration mode to ARM. To do so, type:

azure config mode arm

Next, to login to your Azure account via CLI type:

azure login

You can then verify subscriptions available by typing:

azure account list

Now, you will want to ssh over to the VM you wish to capture. Note: Capturing the VM will require deprovisioning it, generalizing it, and then retiring it. You will not be able to reuse the VM once it has been deprovisioned and generalized. Once you are tunneled into the vm, type the following command to deprovision:

sudo waagent –deprovision+user

Next, you can use the following script to quickly capture the image and save a JSON template file to a local folder of your choice. Simply fill in the variables with the desired information and run the script after. #-----fill in these variables$rgName = "LVMTest"$template = "Template1.json" # local file path specified$vmName = ”LVMTest”$vhdName = "CoolVHDTest"

#-----stop the vm azure vm deallocate -g $rgName -n $vmName#-----generalize the imageazure vm generalize –g $rgName -n $vmName#-----capture the imageazure vm capture $rgName $vmName $vhdName -t $template