Copy An Azure VM Using Managed Disk Snapshots

chrome 2018 03 19 09 21 47
In this post, I will show you how you can use the snapshot feature of managed disks to create an exact copy (including identity and SID) of a virtual machine.
 

 
This technique should only be used for lab and troubleshooting purposes. The official creation/deployment methods of Azure should be used for production deployments from a generalized image.

Managed Disks

This technique is based on the snapshot feature of managed disks. If you are using un-managed disks (VHD files in storage accounts) then check out what managed disks offer and how to convert un-managed disks into managed disks.

Create Snapshots

The method I am using does the following:

  1. Create a snapshot from each disk attached to the virtual machine.
  2. Create a managed disk from each snapshot.
  3. Create a virtual machine from the new OS disk.
  4. Add any data disks to the virtual machine.
  5. Reboot the virtual machine.
  6. Delete the snapshots.

Managed disk snapshots are done on a per disk basis. To create a consistent image of the original virtual machine, you must shut it down first (via Azure, not the guest OS). Once the virtual machine is shut down (deallocated), you can create a snapshot of each disk, one at a time.
Identify each of the disks of the virtual machine. Then open each disk and click Create Snapshot.

Creating a snapshot of the virtual machine’s OS disk [Image Credit: Aidan Finn]
Creating a Snapshot of the Virtual Machine’s OS Disk [Image Credit: Aidan Finn]
 
When you are creating the snapshot, use a descriptive name for the disk and save it to a resource group that you’ll remember. For example, if a data disk was connected to LUN0, be sure to give it a name to remind you to attach it back to LUN0 in the new machine.
Configuring the snapshot of the virtual machine’s OS disk [Image Credit: Aidan Finn]
Configuring the Snapshot of the Virtual Machine’s OS Disk [Image Credit: Aidan Finn]
 
Make sure you do this process for the OS disk and every data disk of the virtual machine. You cannot and do not need to do this for the temp drive.

Create Managed Disks

You can generate newly managed disks from the snapshots that were just created. Create an OS disk from the OS disk snapshot and create a data disk from each data disk snapshot. To do this, click Create A Resource in the Azure Portal and search for and select Managed Disks.
Name the managed disk. Once again, use a name that reminds you to attach the disk back to the same slot it was connected to in the original machine. Be sure to pick the correct resource group (it’s hard to move a managed disk), and pick the disk type (Premium SSD, Standard HDD, etc). When it comes to source type, select Snapshot and pick the appropriate disk.
In my example, I’m creating a new data disk from the data disk snapshot. Note that you can pick a larger size if you wish. You will need to extend the partition later in the guest OS. Also note that you cannot create a disk smaller than the original disk using this method.

Create a new managed data disk from the data disk snapshot [Image Credit: Aidan Finn]
Create a Newly Managed Data Disk from the Data Disk Snapshot [Image Credit: Aidan Finn]
 
You should end up with one new disk for each original disk in the virtual machine. In the below example, you can see the snapshots and the new disks alongside the originally deployed disks and virtual machine.
The snapshots, the original disks, and the new disks [Image Credit: Aidan Finn]
The Snapshots, the Original Disks, and the New Disks [Image Credit: Aidan Finn]

Create the Virtual Machine

Now you have enough to build the new machine. Open the new OS disk and click + Create VM. This will open the regular Create Virtual Machine blade. Many of the options will be greyed out because they are pre-configured in the existing disk. Finish the wizard and wait for the virtual machine to be deployed.

Add the Data Disks

You can add each of the data disks to the virtual machine. Be sure that each data disk is attached to the same LUN as it was in the original virtual machine. Also, make sure that you copy the Host Caching configuration from the original virtual machine.
There is a pretty strong chance that some service in the virtual machine has failed because the virtual machine previously booted up without the data disks. To be safe, reboot the virtual machine.

Adding data disks to the new virtual machine [Image Credit: Aidan Finn]
Adding Data Disks to the New Virtual Machine [Image Credit: Aidan Finn]

Clean Up

Log into the virtual machine and ensure that:

  • All services are running
  • All volumes and data are present
  • You have extended any volumes if you expanded the size of the newly managed disks

If everything is OK, you can delete the snapshots, assuming that you don’t want to create any more test/troubleshooting copies of the virtual machine.