简体   繁体   中英

Azure - Managed Disks, how to create snapshot

I have application which deploy VM with storage account and disks, I want to convert it to use managed disks - as this is the future of Azure storage. I am looking on the REST API - and I am missing two things: 1. how can i create a snapshot form existing managed disk, there is an API to create a snapshot but it is empty or from old unmanaged 2. can i choose the lun on which the disk is created?

  1. how can i create a snapshot form existing managed disk, there is an API to create a snapshot but it is empty or from old unmanaged

According to your description, I have created a test demo to create a snapshot of an existing managed disk(OS disk), it works well. I create a Windows VM and use managed disk as the OS disk, then I create another managed disk and add it to the VM.

The result is like below: 在此处输入图片说明 If you want to create a snapshot of an existing managed disk(It has the data), I suggest you could send request to below url.

Url: https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Compute/snapshots/{snapshotName}?api-version={api-version}

Method: PUT

Parameter:
subscriptionId  The identifier of your subscription where the snapshot is being created.
resourceGroup   The name of the resource group that will contain the snapshot.
snapshotName    The name of the snapshot that is being created. The name can’t be changed after the snapshot is created. Supported characters for the name are a-z, A-Z, 0-9 and _. The max name length is 80 characters.
api-version The version of the API to use. The current version is 2016-04-30-preview.

Request content:
{
  "properties": {
    "creationData": {
      "createOption": "Copy",
      "sourceUri": "/subscriptions/{subscriptionId}/resourceGroups/{YourResourceGroup}/providers/Microsoft.Compute/disks/{YourManagedDiskName}"
    }
  },
  "location": "eastasia"
}

More details, you could refer to follow C# codes:

json.txt:

{
  "properties": {
    "creationData": {
      "createOption": "Copy",
      "sourceUri": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/BrandoSecondTest/providers/Microsoft.Compute/disks/BrandoTestVM"
    }
  },
  "location": "eastasia"
}

Code:

static void Main(string[] args)
        {
            string body = File.ReadAllText(@"D:\json.txt");
            // Display the file contents to the console. Variable text is a string.
            string tenantId = "xxxxxxxxxxxxxxxxxxxxxxxx";
            string clientId = "xxxxxxxxxxxxxxxxxxxxxxxx";
            string clientSecret = "xxxxxxxxxxxxxxxxxxxx";
            string authContextURL = "https://login.windows.net/" + tenantId;
            var authenticationContext = new AuthenticationContext(authContextURL);
            var credential = new ClientCredential(clientId, clientSecret);
            var result = authenticationContext.AcquireTokenAsync(resource: "https://management.azure.com/", clientCredential: credential).Result;
            if (result == null)
            {
               throw new InvalidOperationException("Failed to obtain the JWT token");
            }
            string token = result.AccessToken;
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://management.azure.com/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxxxxxxxxxxxxx/providers/Microsoft.Compute/snapshots/BrandoTestVM_snapshot2?api-version=2016-04-30-preview");
            request.Method = "PUT";
            request.Headers["Authorization"] = "Bearer " + token;
            request.ContentType = "application/json";

            try
            {
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(body);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            //Get the response
           var httpResponse = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                Console.WriteLine(streamReader.ReadToEnd());
            }

            Console.ReadLine();
        }

Result: 在此处输入图片说明

  1. can i choose the lun on which the disk is created?

Do you mean you want to use azuredeploy to select the LUN of the disk?

If this is your opinion, I suggest you could refer to follow json example to know how to build the deploy content of the VM and select its LUN.

More details, you could refer to below deploymentTemplate Json (Partly):

"diskArray": [
    {
    "name": "datadisk1",
    "lun": 0,
    "vhd": {
    "uri": "[concat('http://', variables('storageAccountName'),'.blob.core.windows.net/vhds/', 'datadisk1.vhd')]"
    },
    "createOption": "Empty",
    "caching": "[variables('diskCaching')]",
    "diskSizeGB": "[variables('sizeOfDataDisksInGB')]"
    },
  ]

More details, you could refer to follow link: 201-vm-dynamic-data-disks-selection/azuredeploy.json

  1. New VM's can be created in the same resource group or in different resource group using a snapshot.
  2. In case the VM is to be created is in a different RG, make sure the locations are same. Azure supports only within the same location.
  3. Take a snap shot of the OS disk disks-> click on os disk -> create snapshot Enter the name Select the resource group, where the snapshot has to be created. (In case of different RG, make sure locations are same)
  4. Once snap shot is created, Update the below commands as required and run from terminal (Azure cli - ref: [link][1]) .

Provide the subscription Id of the subscription subscriptionId=88888-8888-888888-8888-8888888

Provide the name of your resource group

resourceGroupName=RG_name

Provide the name of the snapshot that will be used to create Managed Disks(this was created in previous step)

snapshotName=OSDISK_snapshot

Provide the name of the Managed Disk which will be created

osDiskName=MY_OSDISK

Provide the size of the disks in GB. It should be greater than the VHD file size.

diskSize=30

Provide the storage type for Managed Disk. Premium_LRS or Standard_LRS. storageType=Premium_LRS

Set the context to the subscription Id where Managed Disk will be created

az account set --subscription $subscriptionId

Get the snapshot Id

snapshotId=$(az snapshot show --name $snapshotName --resource-group $resourceGroupName --query [id] -o tsv)

az disk create -n $osDiskName -g $resourceGroupName --size-gb $diskSize --sku $storageType --source $snapshotId

  • The above command will create a managed disk in the RG.
  • Select the created disk from the list under RG and click on create VM.
  • Enter the name, select RG , select the size .... and click on create.
  • Make sure NSG has all the inboud ports available Http - 80, ssh -22
  • Once the vm is created, select the vm from RG's resource list.
  • Scroll down to "run commands" and run the below commands in case SSH and HTTP are not accessible. sudo service apace2 restart sudo service ssh restart
  • This should resolve the issue with accessing from browser and terminal.
  • incase ssh is still not working, run below command rm /run/nologin now access the vm from terminal via ssh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM