Add existing vhd to Azure VM using Azure Resource Manager PowerShell cmdlets
NOTE. This is an Azure ARM issue, not an Azure Service management issue.
I have 2 vhds in storage account - machine-os.vhd and machine-data.vhd. I can use the ARM PowerShell cmdlets to create a virtual machine with an existing OS vhd, but cannot figure out how to connect the EXISTING vhd data. All the samples I found create EMPTY data disks, but I want to attach the existing vhd as a data disk.
I tried to use a switch createOption attach
with the Add-AzureVMDataDisk
following:
$vm = New-AzureVMConfig ...
$vm = Add-AzureVMDataDisk -VM $vm -VhdUri $existingDiskUri -Name "machine-data.vhd" -Caching ReadOnly -CreateOption attach -DiskSizeInGB 200
However, the operation fails:
Blob https://x.blob.core.windows.net/vhds/machine-data.vhd already exists. Please provide a different blob URI to create a new blank data disk 'machine-data.vhd.'
I need to specify DiskSizeInGB
for a command Add-AzureVMDataDisk
to work (which seems strange). I tried to provide a SourceImageUri
different name for VhdUri
which, according to the documentation , is to copy the vhd from sourceImageUri for vhdUri and attach it. The attempt createOption fromImage
fails because "you cannot specify the size from the uri of the original image". However, the size parameter for the cmdlet is required, so I don't know how you could have specified the sourceUri and not the size.
This SO question presents a similar problem (although I don't get the same error), but the response link specifies a pattern with EMPTY data disks, which doesn't help.
Interestingly, I tried to add a disk to the VM with Azure Portal - there you need to provide the name and URI, but the operation always fails with some strange json parsing error. I can't seem to get the uri for the data drive correctly.
source to share
After playing a little more, I found a hack:
- Give a new URI for the existing drive and set it like
vhdUri
- Use the URI of an existing drive as
sourceImageUri
- Call
Add-AzureVMDataDisk
withCreateOption fromImage
- Set the size of the data disk to null (this is a hack)
- When called, the
New-AzureVM
existing disk is copied to the new Uri - After creating the virtual machine, I delete the original vhd file
Unfortunately, you need to provide a parameter to the DiskSizeInGB
command Add-AzureVMDataDisk
as it is required. However, I set it to zero, otherwise the initialization will fail (the error message states that you cannot specify the size and sourceImageUri).
Here's the last code:
$vm = New-AzureVMConfig ...
$vm = Add-AzureVMDataDisk -VM $vm `
-SourceImageUri $existingDataDiskUrl -VhdUri $newDataDiskUri `
-Name $newDataDiskName -CreateOption fromImage -Caching ReadOnly `
-DiskSizeInGB 200
# hack
$vm.StorageProfile.DataDisks[0].DiskSizeGB = $null
After that you can call: New-AzureVM -ResourceGroupName $ rgName -Location $ location -VM $ vm
After creating the virtual machine, I call Remove-AzureStorageBlob
to remove the original disk.
There might be a cleaner way out there, but I can't seem to find it. At least this way works in the end.
source to share