如何将数据磁盘附加到 Windows Server Azure VM 并直接在模板中对其进行格式化?
How can I attach a data disk to a Windows Server Azure VM and format it directly in the template?
我需要将数据磁盘附加到 VM(在 VMSS 中),并希望立即格式化和使用磁盘 w/o 进一步手动干预。
如何直接在 ARM 模板中实现此目的?
您可以在模板中使用指向脚本 lo
的 customscript 对象
{
"type": "Microsoft.Compute/virtualMachineScaleSets/extensions",
"name": "[concat(variables('VmssName'),'/', variables('extensionName'))]",
"apiVersion": "2015-05-01-preview",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Compute/virtualMachineScaleSets/', variables('VmssName'))]"
],
"properties": {
"publisher": "Microsoft.Azure.Extensions",
"type": "CustomScript",
"typeHandlerVersion": "2.0",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"[parameters('BootScriptUri')]"
]
},
"protectedSettings": {
"commandToExecute": "[parameters('commandToExecute')]"
}
}
然后像这样的脚本
Get-Disk |
Where partitionstyle -eq 'raw' |
Initialize-Disk -PartitionStyle MBR -PassThru |
New-Partition -DriveLetter "F" -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel "DataDisk" -Confirm:$false
有一个 linux 版本的脚本 - 具有更多功能!在 https://github.com/Azure/azure-quickstart-templates/blob/master/shared_scripts/ubuntu/vm-disk-utils-0.1.sh
我在 ARM 模板中添加了 3 个参数:
...
"scriptLocation": {
"type": "string",
"metadata": {
"description": "Location of custom extension scripts on storage account container"
}
},
"scriptStorageAccount": {
"type": "string",
"metadata": {
"description": "Name of custom extension scripts storage account"
}
},
"scriptStorageAccountKey": {
"type": "string",
"metadata": {
"description": "Key to custom extension scripts storage account"
}
},
...
这些参数在上传自定义扩展脚本文件和调用 New-AzureRmResourceGroupDeployment
的 PowerShell 脚本中填充。
...
$StorageAccountName = "mydeploymentstorage"
$StorageContainerName = "ext"
$ArtifactStagingDirectory = ".\ExtensionScripts"
...
# transfer Extension script to Storage $StorageAccount = (Get-AzureRmStorageAccount | Where-Object{$_.StorageAccountName -eq $StorageAccountName})
$StorageAccountContext = $StorageAccount.Context
New-AzureStorageContainer -Name $StorageContainerName -Context $StorageAccountContext -Permission Container -ErrorAction SilentlyContinue *>&1
$ArtifactFilePaths = Get-ChildItem $ArtifactStagingDirectory -Recurse -File | ForEach-Object -Process {$_.FullName}
foreach ($SourcePath in $ArtifactFilePaths) {
Write-Host "transfering" $SourcePath
$BlobName = $SourcePath.Substring($SourcePath.LastIndexOf("\")+1)
Set-AzureStorageBlobContent -File $SourcePath -Blob $BlobName -Container $StorageContainerName -Context $StorageAccountContext -Force -ErrorAction Stop
}
# prepare and pass script parameters
$DynamicParameters = New-Object -TypeName Hashtable
$DynamicParameters["scriptLocation"] = $StorageAccountContext.BlobEndPoint + $StorageContainerName
$DynamicParameters["scriptStorageAccount"] = $StorageAccountName
$DynamicParameters["scriptStorageAccountKey"] = ($StorageAccount | Get-AzureRmStorageAccountKey).Value[0]
...
# start deployment
New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) ` `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFile `
-TemplateParameterFile $TemplateParametersFile `
@DynamicParameters `
-Verbose
在 VMSS extensionProfile
中,我添加了自定义脚本扩展(将其与其他扩展放在一个地方):
...
"storageProfile": {
"imageReference": {
"publisher": "[parameters('vmImagePublisher')]",
"offer": "[parameters('vmImageOffer')]",
"sku": "[parameters('vmImageSku')]",
"version": "[parameters('vmImageVersion')]"
},
"osDisk": {
"caching": "ReadWrite",
"createOption": "FromImage",
"managedDisk": {
"storageAccountType": "[parameters('storageAccountType')]"
}
},
"dataDisks": [
{
"diskSizeGB": 128,
"lun": 0,
"createOption": "Empty",
"managedDisk": {
"storageAccountType": "[parameters('storageAccountType')]"
}
}
]
}
...
"virtualMachineProfile": {
"extensionProfile": {
"extensions": [
...
{
"name": "[concat(parameters('vmNodeType0Name'),'_CreateDisk')]",
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.9",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"[concat(parameters('scriptLocation'),'/CreateDisk.ps1')]"
]
},
"protectedSettings": {
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File CreateDisk.ps1",
"storageAccountName": "[parameters('scriptStorageAccount')]",
"storageAccountKey": "[parameters('scriptStorageAccountKey')]"
}
}
}
]
然后终于创建了脚本。我最初的问题是,我在 C: 上没有足够的 space 用于...-smalldisk VM SKU 来保存所有 docker 图像,所以我将 docker
移动到新的开车。
# create and format disk
Get-Disk |
Where PartitionStyle -eq 'Raw' |
Select-Object -First 1 |
Initialize-Disk -PartitionStyle MBR -PassThru |
New-Partition -DriveLetter F -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel "Containers" -Confirm:$false
# move docker to F:\docker
docker images -a -q | %{docker rmi $_ --force}
Stop-Service Docker
$service = (Get-Service Docker)
$service.WaitForStatus("Stopped","00:00:30")
@{"data-root"="F:\docker"} | ConvertTo-Json | Set-Content C:\programdata\docker\config\daemon.json
Get-Process docker* | % {Stop-Process -Id $_.Id -Force}
docker system info
Copy-Item C:\programdata\docker F:\docker -Recurse
Start-Service Docker
我需要将数据磁盘附加到 VM(在 VMSS 中),并希望立即格式化和使用磁盘 w/o 进一步手动干预。 如何直接在 ARM 模板中实现此目的?
您可以在模板中使用指向脚本 lo
的 customscript 对象{
"type": "Microsoft.Compute/virtualMachineScaleSets/extensions",
"name": "[concat(variables('VmssName'),'/', variables('extensionName'))]",
"apiVersion": "2015-05-01-preview",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Compute/virtualMachineScaleSets/', variables('VmssName'))]"
],
"properties": {
"publisher": "Microsoft.Azure.Extensions",
"type": "CustomScript",
"typeHandlerVersion": "2.0",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"[parameters('BootScriptUri')]"
]
},
"protectedSettings": {
"commandToExecute": "[parameters('commandToExecute')]"
}
}
然后像这样的脚本
Get-Disk |
Where partitionstyle -eq 'raw' |
Initialize-Disk -PartitionStyle MBR -PassThru |
New-Partition -DriveLetter "F" -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel "DataDisk" -Confirm:$false
有一个 linux 版本的脚本 - 具有更多功能!在 https://github.com/Azure/azure-quickstart-templates/blob/master/shared_scripts/ubuntu/vm-disk-utils-0.1.sh
我在 ARM 模板中添加了 3 个参数:
...
"scriptLocation": {
"type": "string",
"metadata": {
"description": "Location of custom extension scripts on storage account container"
}
},
"scriptStorageAccount": {
"type": "string",
"metadata": {
"description": "Name of custom extension scripts storage account"
}
},
"scriptStorageAccountKey": {
"type": "string",
"metadata": {
"description": "Key to custom extension scripts storage account"
}
},
...
这些参数在上传自定义扩展脚本文件和调用 New-AzureRmResourceGroupDeployment
的 PowerShell 脚本中填充。
...
$StorageAccountName = "mydeploymentstorage"
$StorageContainerName = "ext"
$ArtifactStagingDirectory = ".\ExtensionScripts"
...
# transfer Extension script to Storage $StorageAccount = (Get-AzureRmStorageAccount | Where-Object{$_.StorageAccountName -eq $StorageAccountName})
$StorageAccountContext = $StorageAccount.Context
New-AzureStorageContainer -Name $StorageContainerName -Context $StorageAccountContext -Permission Container -ErrorAction SilentlyContinue *>&1
$ArtifactFilePaths = Get-ChildItem $ArtifactStagingDirectory -Recurse -File | ForEach-Object -Process {$_.FullName}
foreach ($SourcePath in $ArtifactFilePaths) {
Write-Host "transfering" $SourcePath
$BlobName = $SourcePath.Substring($SourcePath.LastIndexOf("\")+1)
Set-AzureStorageBlobContent -File $SourcePath -Blob $BlobName -Container $StorageContainerName -Context $StorageAccountContext -Force -ErrorAction Stop
}
# prepare and pass script parameters
$DynamicParameters = New-Object -TypeName Hashtable
$DynamicParameters["scriptLocation"] = $StorageAccountContext.BlobEndPoint + $StorageContainerName
$DynamicParameters["scriptStorageAccount"] = $StorageAccountName
$DynamicParameters["scriptStorageAccountKey"] = ($StorageAccount | Get-AzureRmStorageAccountKey).Value[0]
...
# start deployment
New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $TemplateFile).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) ` `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFile `
-TemplateParameterFile $TemplateParametersFile `
@DynamicParameters `
-Verbose
在 VMSS extensionProfile
中,我添加了自定义脚本扩展(将其与其他扩展放在一个地方):
...
"storageProfile": {
"imageReference": {
"publisher": "[parameters('vmImagePublisher')]",
"offer": "[parameters('vmImageOffer')]",
"sku": "[parameters('vmImageSku')]",
"version": "[parameters('vmImageVersion')]"
},
"osDisk": {
"caching": "ReadWrite",
"createOption": "FromImage",
"managedDisk": {
"storageAccountType": "[parameters('storageAccountType')]"
}
},
"dataDisks": [
{
"diskSizeGB": 128,
"lun": 0,
"createOption": "Empty",
"managedDisk": {
"storageAccountType": "[parameters('storageAccountType')]"
}
}
]
}
...
"virtualMachineProfile": {
"extensionProfile": {
"extensions": [
...
{
"name": "[concat(parameters('vmNodeType0Name'),'_CreateDisk')]",
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.9",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"[concat(parameters('scriptLocation'),'/CreateDisk.ps1')]"
]
},
"protectedSettings": {
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -File CreateDisk.ps1",
"storageAccountName": "[parameters('scriptStorageAccount')]",
"storageAccountKey": "[parameters('scriptStorageAccountKey')]"
}
}
}
]
然后终于创建了脚本。我最初的问题是,我在 C: 上没有足够的 space 用于...-smalldisk VM SKU 来保存所有 docker 图像,所以我将 docker
移动到新的开车。
# create and format disk
Get-Disk |
Where PartitionStyle -eq 'Raw' |
Select-Object -First 1 |
Initialize-Disk -PartitionStyle MBR -PassThru |
New-Partition -DriveLetter F -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel "Containers" -Confirm:$false
# move docker to F:\docker
docker images -a -q | %{docker rmi $_ --force}
Stop-Service Docker
$service = (Get-Service Docker)
$service.WaitForStatus("Stopped","00:00:30")
@{"data-root"="F:\docker"} | ConvertTo-Json | Set-Content C:\programdata\docker\config\daemon.json
Get-Process docker* | % {Stop-Process -Id $_.Id -Force}
docker system info
Copy-Item C:\programdata\docker F:\docker -Recurse
Start-Service Docker