This article shows you how to upload a customized virtual hard disk (VHD) or copy a an existing VHD in Azure and create new Linux virtual machines (VMs) from the custom disk. You can install and configure a Linux distro to your requirements and then use that VHD to quickly create a new Azure virtual machine.
If you want to create multiple VMs from your customized disk, you should create an image from your VM or VHD. For more information, see Create a custom image of an Azure VM using the CLI.
You have two options:
When creating a new VM using az vm create from a customized or specialized disk you attach the disk (--attach-os-disk) instead of 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 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 required for the VM to work properly in Azure and for you to be able to connect to it using 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 using qemu-img convert or the Convert-VHD PowerShell cmdlet. Further, Azure does not support uploading dynamic VHDs, so you 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 to Azure.
- Make sure that you have the latest Azure CLI 2.0 installed and logged in to an Azure account using az login.
In the following examples, replace example parameter names with your own values. Example parameter names included myResourceGroup, mystorageaccount, and mydisks.
Prepare the VM
Azure supports various Linux distributions (see Endorsed Distributions). The following articles guide you through 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
- Other - 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 the VHD to create a new Azure VM, you need to upload the VHD to a storage account and create a managed disk from the VHD.
Create a resource group
Before uploading your custom disk and creating VMs, you first need to create a resource group with az group create.
The following example creates a resource group named myResourceGroup in the eastus location: Azure Managed Disks overview
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, like carrying out write operations. Read more about managing access to storage here. You view the access keys with az storage account keys list.
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 create containers within a storage account to organize your disks. A storage account can contain any number of 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
Now upload your custom disk with az storage blob upload. You 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 using 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 the 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, you really should 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 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, that is 128GB in size in standard storage.
az disk create \ --resource-group myResourceGroup \ --name myManagedDisk \ --sku Standard_LRS \ --size-gb 128 \ --source $snapshotId
Create the VM
Now, 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 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 using 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.