2017-02-18 75 views
0

我有應用程序與存儲帳戶和磁盤部署虛擬機,我想將其轉換爲使用託管磁盤 - 因爲這是Azure存儲的未來。我期待的REST API - 和我丟失了兩兩件事:1。 如何創建現有管理磁盤快照的形式,有創建快照的API,但它是空的或舊的非託管 2.我可以選擇創建磁盤的LUN?天青 - 管理的磁盤,如何創建快照

回答

2
  1. 如何創建現有管理磁盤快照的形式,有創建快照的API,但它是空的或舊的非託管

根據您的描述中,我創建了一個測試演示來創建現有託管磁盤(操作系統磁盤)的快照,它運行良好。 我創建一個Windows VM並使用託管磁盤作爲操作系統磁盤,然後創建另一個託管磁盤並將其添加到虛擬機。

結果如下圖所示: enter image description here 如果要創建現有管理磁盤(它的數據)的快照,我建議你可以發送請求到以下網址。

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" 
} 

更多細節,你可以參考遵循C#代碼:

json.txt:

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

代碼:

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(); 
     } 

結果: enter image description here

  • 可以我選擇在其上創建該磁盤綸?
  • 你的意思是你想用azuredeploy選擇磁盤的LUN?

    如果這是你的意見,我建議你可以參考遵循JSON的例子來了解如何建立虛擬機的部署內容,並選擇它的LUN。

    更多細節,你可以參考下面deploymentTemplate JSON(晴):

    "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')]" 
        }, 
        ] 
    

    更多細節,你可以參考以下網站: 201-vm-dynamic-data-disks-selection/azuredeploy.json