I need to attach data disks to VMs (in a VMSS) and like to instantly format and use the disk w/o further manual intervention. How can I achieve this directly in an ARM template?
How can I attach a data disk to a Windows Server Azure VM and format it directly in the template?
880 Views Asked by Kai Walter At
2
There are 2 best solutions below
1

You can use a customscript object in your template that points to a script 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')]"
}
}
then a script like this
Get-Disk |
Where partitionstyle -eq 'raw' |
Initialize-Disk -PartitionStyle MBR -PassThru |
New-Partition -DriveLetter "F" -UseMaximumSize |
Format-Volume -FileSystem NTFS -NewFileSystemLabel "DataDisk" -Confirm:$false
There is a linux version of the script - with a lot more facilities! at https://github.com/Azure/azure-quickstart-templates/blob/master/shared_scripts/ubuntu/vm-disk-utils-0.1.sh
I added 3 parameters to the ARM template:
These parameters are populated in the PowerShell script uploading the custom extension script file and calling the
New-AzureRmResourceGroupDeployment
.In the VMSS
extensionProfile
I added the custom script extension (to have it in one place with the other extensions):And then finally created the script. My initial problem had been, that I did not have enough space on C: for a ...-smalldisk VM SKU to hold all docker images, so I moved
docker
to the new drive.