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:
When creating a new VM with az vm create from a customized or specialized disk, you attach the disk (--attach-os-disk) rather than specifying a custom or marketplace image (--image). The following example creates a VM named myVM using the managed disk named myManagedDisk created from your customized VHD:
az vm create --resource-group myResourceGroup --location eastus --name myVM \ --os-type linux --attach-os-disk myManagedDisk
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 myResourceGroup, mystorageaccount, and mydisks.
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 upload a customized VHD that you have running on a local machine or that you exported from another cloud. To use a VHD to create a new Azure VM, you'll need to upload the VHD to a storage account and create a managed disk from the VHD. For more information, see Azure Managed Disks overview.
Create a resource group
Before uploading your custom disk and creating VMs, you'll need to create a resource group with az group create.
The following example creates a resource group named myResourceGroup in the eastus location:
az group create \ --name myResourceGroup \ --location eastus
Create a storage account
Create a storage account for your custom disk and VMs with az storage account create. The following example creates a storage account named mystorageaccount in the resource group previously created:
az storage account create \ --resource-group myResourceGroup \ --location eastus \ --name mystorageaccount \ --kind Storage \ --sku Standard_LRS
List storage account keys
Azure generates two 512-bit access keys for each storage account. These access keys are used when authenticating to the storage account, such as when carrying out write operations. For more information, see managing access to storage.
You view the access keys with az storage account keys list. For example, to view the access keys for the storage account you created:
az storage account keys list \ --resource-group myResourceGroup \ --account-name mystorageaccount
The output is similar to:
info: Executing command storage account keys list + Getting storage account keys data: Name Key Permissions data: ---- ---------------------------------------------------------------------------------------- ----------- data: key1 d4XAvZzlGAgWdvhlWfkZ9q4k9bYZkXkuPCJ15NTsQOeDeowCDAdB80r9zA/tUINApdSGQ94H9zkszYyxpe8erw== Full data: key2 Ww0T7g4UyYLaBnLYcxIOTVziGAAHvU+wpwuPvK4ZG0CDFwu/mAxS/YYvAQGHocq1w7/3HcalbnfxtFdqoXOw8g== Full info: storage account keys list command OK
Make a note of key1 as you will use it to interact with your storage account in the next steps.
Create a storage container
In the same way that you create different directories to logically organize your local file system, you'll create containers within a storage account to organize your disks. A storage account can contain many containers. Create a container with az storage container create.
The following example creates a container named mydisks:
az storage container create \ --account-name mystorageaccount \ --name mydisks
Upload the VHD
Upload your custom disk with az storage blob upload. You'll upload and store your custom disk as a page blob.
Specify your access key, the container you created in the previous step, and then the path to the custom disk on your local computer:
az storage blob upload --account-name mystorageaccount \ --account-key key1 \ --container-name mydisks \ --type page \ --file /path/to/disk/mydisk.vhd \ --name myDisk.vhd
Uploading the VHD may take a while.
Create a managed disk
Create a managed disk from the VHD with az disk create. The following example creates a managed disk named myManagedDisk from the VHD you uploaded to your named storage account and container:
az disk create \ --resource-group myResourceGroup \ --name myManagedDisk \ --source https://mystorageaccount.blob.core.windows.net/mydisks/myDisk.vhd
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.
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.