Use AzureCLI2.0 to create an Azure virtual machine from a custom disk, azurecli2.0azure

Source: Internet
Author: User
Tags myvm

Use AzureCLI2.0 to create an Azure virtual machine from a custom disk, azurecli2.0azure

This article describes how to upload a custom virtual hard disk (VHD) in Azure or copy an existing VHD, and create a Linux Virtual Machine (VM) from a custom disk ). You can install and configure the Linux distribution as required, and use the VHD to quickly create a new Azure virtual machine.

If you want to create multiple VMS from a custom disk, you should create an image from the VM or VHD.

Two options are available:

Upload VHD

Copy existing Azure VM

Quick Command

Note

Before using Azure CLI 2.0 in Azure China, run az cloud set-n AzureChinaCloud to change the cloud environment. If you want to switch back to the international version of Azure, run az cloud set-n AzureCloud again.

When using az vm create to create a new VM from a custom or dedicated disk, attach the disk (-- attach-OS-disk ), do not specify a custom image or a Marketplace image (-- image ). In the following example, a VM named myVM is created using a managed disk named myManagedDisk created from the custom VHD:

Azure CLI

Az vm create -- resource-group myResourceGroup -- location chinaeast -- name myVM \

-- OS-type linux -- attach-OS-disk myManagedDisk

Requirements

To complete the following steps, you must:

Prepare a Linux Virtual Machine for use in Azure. The VM section in this article describes how to find distribution-specific information about installing the Azure Linux agent (waagent. This proxy is required to make the VM work normally in Azure and connect to the VM through SSH.

This tool is used to install Azure-recognized Linux distributions (or refer to unapproved distributions) to VHD files on Virtual Disks in VHD format. You can use multiple tools to create VMS and VHD:

Install and configure QEMU or KVM, and use VHD as the image format. If necessary, you can use qemu-img convert to convert the image.

You can also use Hyper-V on Windows 10 or Windows Server 2012/2012 R2.

Note

Azure does not support the updated VHDX format. When creating a VM, specify VHD as the image format. If necessary, you can use the qemu-img convert or Convert-VHDPowerShell cmdlet to convert a VHDX disk to a VHD disk. In addition, Azure does not support dynamic VHD upload. Therefore, before uploading, You need to convert this type of disk to a static VHD. 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 installed the latest Azure CLI 2.0 and have logged on to the Azure account using az login.

In the following example, replace the parameter name with your own value. The example parameter names include myResourceGroup, mystorageaccount, and mydisks.

Prepare VM

Azure supports various Linux distributions (see authorized distributions ). The following articles guide you through the various Linux distributions supported on Azure:

CentOS-based distribution

Debian Linux

Oracle Linux

Red Hat Enterprise Linux

SLES and openSUSE

Ubuntu

Others-unauthorized Distribution

For more information about how to prepare a Linux image for Azure, see the Linux installation instructions.

Note

Only when an authorized distribution is used and the configuration details specified under "supported versions" in Linux in the Azure accredited distribution are used, the Azure platform SLA is applicable to VMS running Linux.

Option 1: Upload VHD

You can upload custom VHD files running on a local computer or exported from another cloud. To create a new Azure VM using VHD, upload the VHD to the storage account and create a hosted disk from the VHD.

Create Resource Group

Before uploading a custom disk and creating a VM, you must use az group create to create a Resource group.

The following example creates a resource group named myResourceGroup at the chinaeast location: Azure hosted disk Overview

Azure CLI

Az group create \

-- Name myResourceGroup \

-- Location chinaeast

Create a storage account

You can use az storage account create to create storage accounts for custom disks and VMS.

The following example shows how to create a storage account named mystorageaccount in the Resource Group created earlier:

Azure CLI

Az storage account create \

-- Resource-group myResourceGroup \

-- Location chinaeast \

-- 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 for identity authentication to the storage account for operations (such as write operations. Learn more about managing access to storage. You can use the az storage account keys list to view the access key.

View the accesskey of the created storage account:

Azure CLI

Az storage account keys list \

-- Resource-group myResourceGroup \

-- Account-name mystorageaccount

The output is similar:

Azure CLI

Info: Executing command storage account keys list

+ Getting storage account keys

Data: Name Key Permissions

Data: ---- certificate :-------------------------------------------------------------------------------------------------------

Data: key1 d4XAvZzlGAgWdvhlWfkZ9q4k9bYZkXkuPCJ15NTsQOeDeowCDAdB80r9zA/tUINApdSGQ94H9zkszYyxpe8erw = Full

Data: key2 Ww0T7g4UyYLaBnLYcxIOTVziGAAHvU + wpwupvk4z1_cdfwu/mAxS/YYvAQGHocq1w7/3HcalbnfxtFdqoXOw8g = Full

Info: storage account keys list command OK

Write down key1 because it needs to be used to interact with the storage account in subsequent steps.

Create a storage container

Create a container for organizing disks in the storage account in the same way as creating directories to organize local file systems in a organized manner. A storage account can contain any number of containers. You can use az storage container create to create a container.

The following example creates a container named mydisks:

Azure CLI

Az storage container create \

-- Account-name mystorageaccount \

-- Name mydisks

Upload VHD

Now, use az storage blob upload to upload a custom disk. You can upload and store custom disks in the form of page Blob.

Specify the access key, the container created in the previous step, and the path of the custom disk on the local computer:

Azure CLI

Az storage blob upload -- account-name mystorageaccount \

-- Account-key key1 \

-- Container-name mydisks \

-- Type page \

-- File/path/to/disk/mydisk. vhd \

-- Name myDisk. vhd

It may take some time to upload a VHD file.

Create a hosted Disk

Use az disk create to create a hosted disk from VHD. The following example creates a managed disk named myManagedDisk from the VHD that has been uploaded to the named storage account and container:

Azure CLI

Az disk create \

-- Resource-group myResourceGroup \

-- Name myManagedDisk \

-- Source https://mystorageaccount.blob.core.chinacloudapi.cn/mydisks/myDisk.vhd

Option 2: copy an existing VM

You can also create a custom VM in Azure, copy the OS disk, and attach it to the new VM to create another copy. This practice does not cause any problems during testing, but to use the existing Azure VM as a model for multiple new VMS, you must create an image instead. For more information about how to create an image from an existing Azure VM, see use CLI to create a custom image for the Azure VM.

Create Snapshot

In this example, create a snapshot of the VM named myVM in the Resource Group myResourceGroup and create a snapshot named osDiskSnapshot.

Azure-cli

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 a hosted Disk

Create a new hosted disk from the snapshot.

Obtains the snapshot ID. In this example, the snapshot name is osDiskSnapshot and is located in the myResourceGroup Resource Group.

Azure-cli

SnapshotId = $ (az snapshot show -- name osDiskSnapshot -- resource-group myResourceGroup -- query [id]-o tsv)

Create a hosted disk. In this example, a hosted disk named myManagedDisk is created from a snapshot of 128 GB in standard storage.

Azure-cli

Az disk create \

-- Resource-group myResourceGroup \

-- Name myManagedDisk \

-- Sku Standard_LRS \

-- Size-gb 128 \

-- Source $ snapshotId

Create VM

Now, use az vm create to create a VM and attach the hosted disk to an OS disk (-- attach-OS-disk ). The following example uses a hosted disk created based on the uploaded VHD to create a VM named myNewVM:

Azure CLI

Az vm create \

-- Resource-group myResourceGroup \

-- Location chinaeast \

-- Name myNewVM \

-- OS-type linux \

-- Attach-OS-disk myManagedDisk

Now, you should be able to use creden。 to connect to the VM from the source VM through SSH.

Subsequent steps

After preparing and uploading a custom virtual disk, you can read details about using Resource Manager and templates. You may also need to add data disks to the new VM. To access an application running on the VM, open the port and endpoint.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.