Create a Linux VM from a custom disk with the Azure CLI
This article shows you how to upload a customized virtual hard disk (VHD), and how to copy an existing VHD in Azure. The newly created VHD is then used to create new Linux virtual machines (VMs). You can install and configure a Linux distro to your requirements and then use that VHD to create a new Azure virtual machine.
To create multiple VMs from your customized disk, first create an image from your VM or VHD. For more information, see Create a custom image of an Azure VM by using the CLI.
You have two options to create a custom disk:
- Upload a VHD
- Copy an existing Azure VM
To complete the following steps, you'll need:
- A Linux virtual machine that has been prepared for use in Azure. The Prepare the VM section of this article covers how to find distro-specific information on installing the Azure Linux Agent (waagent), which is needed for you to connect to a VM with SSH.
- The VHD file from an existing Azure-endorsed Linux distribution (or see information for non-endorsed distributions) to a virtual disk in the VHD format. Multiple tools exist to create a VM and VHD:
The newer VHDX format is not supported in Azure. When you create a VM, specify VHD as the format. If needed, you can convert VHDX disks to VHD with qemu-img convert or the Convert-VHD PowerShell cmdlet. Azure does not support uploading dynamic VHDs, so you'll need to convert such disks to static VHDs before uploading. You can use tools such as Azure VHD Utilities for GO to convert dynamic disks during the process of uploading them to Azure.
- Make sure that you have the latest Azure CLI installed and you are signed in to an Azure account with az login.
In the following examples, replace example parameter names with your own values, such as
Prepare the VM
Azure supports various Linux distributions (see Endorsed Distributions). The following articles describe how to prepare the various Linux distributions that are supported on Azure:
- CentOS-based Distributions
- Debian Linux
- Oracle Linux
- Red Hat Enterprise Linux
- SLES & openSUSE
- Others: Non-Endorsed Distributions
Also see the Linux Installation Notes for more general tips on preparing Linux images for Azure.
The Azure platform SLA applies to VMs running Linux only when one of the endorsed distributions is used with the configuration details as specified under "Supported Versions" in Linux on Azure-Endorsed Distributions.
Option 1: Upload a VHD
You can now upload VHD straight into a managed disk. For instructions, see Upload a VHD to Azure using Azure CLI.
Option 2: Copy an existing VM
You can also create a customized VM in Azure and then copy the OS disk and attach it to a new VM to create another copy. This is fine for testing, but if you want to use an existing Azure VM as the model for multiple new VMs, create an image instead. For more information about creating an image from an existing Azure VM, see Create a custom image of an Azure VM by using the CLI.
If you want to copy an existing VM to another region, you might want to use azcopy to creat a copy of a disk in another region.
Otherwise, you should take a snapshot of the VM and then create a new OS VHD from the snapshot.
Create a snapshot
This example creates a snapshot of a VM named myVM in resource group myResourceGroup and creates a snapshot named osDiskSnapshot.
osDiskId=$(az vm show -g myResourceGroup -n myVM --query "storageProfile.osDisk.managedDisk.id" -o tsv) az snapshot create \ -g myResourceGroup \ --source "$osDiskId" \ --name osDiskSnapshot
Create the managed disk
Create a new managed disk from the snapshot.
Get the ID of the snapshot. In this example, the snapshot is named osDiskSnapshot and it is in the myResourceGroup resource group.
snapshotId=$(az snapshot show --name osDiskSnapshot --resource-group myResourceGroup --query [id] -o tsv)
Create the managed disk. In this example, we will create a managed disk named myManagedDisk from our snapshot, where the disk is in standard storage and sized at 128 GB.
az disk create \ --resource-group myResourceGroup \ --name myManagedDisk \ --sku Standard_LRS \ --size-gb 128 \ --source $snapshotId
Create the VM
Create your VM with az vm create and attach (--attach-os-disk) the managed disk as the OS disk. The following example creates a VM named myNewVM using the managed disk you created from your uploaded VHD:
az vm create \ --resource-group myResourceGroup \ --location eastus \ --name myNewVM \ --os-type linux \ --attach-os-disk myManagedDisk
You should be able to SSH into the VM with the credentials from the source VM.
After you have prepared and uploaded your custom virtual disk, you can read more about using Resource Manager and templates. You may also want to add a data disk to your new VMs. If you have applications running on your VMs that you need to access, be sure to open ports and endpoints.