我有使用存储帐户和磁盘部署VM的应用程序,我想将其转换为使用托管磁盘 - 因为这是Azure存储的未来。我正在寻找REST API - 我错过了两件事: 1.如何从现有托管磁盘创建快照,有一个API可以创建快照,但它是空的或者是旧的非托管的 2.我可以选择创建磁盘的lun吗?
答案 0 :(得分:3)
- 如何从现有托管磁盘创建快照,有一个用于创建快照的API,但它是空的,或者来自旧的非托管
醇>
根据您的描述,我创建了一个测试演示来创建现有托管磁盘(操作系统磁盘)的快照,它运行良好。 我创建一个Windows VM并使用托管磁盘作为操作系统磁盘,然后创建另一个托管磁盘并将其添加到VM。
结果如下: 如果您想创建现有托管磁盘的快照(它有数据),我建议您可以将请求发送到以下网址。
SELECT COUNT(*) FROM Contact c
GROUP BY c.ContactId, c.FirstName, c.LastName
HAVING COUNT(*) >1
更多细节,您可以参考以下C#代码:
json.txt:
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"
}
代码:
{
"properties": {
"creationData": {
"createOption": "Copy",
"sourceUri": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/BrandoSecondTest/providers/Microsoft.Compute/disks/BrandoTestVM"
}
},
"location": "eastasia"
}
- 我可以选择创建磁盘的lun吗?
醇>
您是否想要使用azuredeploy选择磁盘的LUN?
如果您的意见如此,我建议您参考json示例,了解如何构建VM的部署内容并选择其LUN。
更多细节,您可以参考下面的deploymentTemplate Json(部分):
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();
}
更多详细信息,您可以参考以下链接: 201-vm-dynamic-data-disks-selection/azuredeploy.json
答案 1 :(得分:0)
resourceGroupName = RG_name
snapshotName = OSDISK_snapshot
osDiskName = MY_OSDISK
diskSize = 30
提供托管磁盘的存储类型。 Premium_LRS或Standard_LRS。 storageType = Premium_LRS
az帐户集--subscription $ subscriptionId
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