如何将数据磁盘附加到Windows Server Azure VM并直接在模板中格式化?

时间:2018-07-06 07:56:26

标签: azure azure-resource-manager azure-vm-templates

我需要将数据磁盘连接到VM(在VMSS中),并且想要立即格式化和使用磁盘,而无需进一步的手动干预。 如何直接在ARM模板中实现这一目标?

2 个答案:

答案 0 :(得分:1)

我在ARM模板中添加了3个参数:

abs_tol

这些参数填充在PowerShell脚本中,以上传自定义扩展脚本文件并调用... "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

在VMSS ... $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 中,我添加了自定义脚本扩展名(将其与其他扩展名放在一起):

extensionProfile

然后最终创建了脚本。我最初的问题是,我在C:上没有足够的空间来放置...- smalldisk VM SKU以容纳所有docker映像,因此我将... "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')]" } } } ] 移到了新驱动器上。

docker

答案 1 :(得分:0)

您可以在模板中使用customscript对象,该对象指向脚本lo

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