Allow re-image host without deleting attached disk

When you re-image or delete a host it deletes any managed disks attached to the VM, it would be nice to have the option to change that behaviour to detach instead of delete or to be able run a script before the re-image process to detach any disks without having to do it manually via the azure portal.

 

I'm attaching the disk via Nerdio scripted actions and I have tried to circumvent this limitation by changing the default behaviour for data disks by using the following command but Nerdio just ignores the setting and tries to delete the disk.

# Ensure the disk is detached (not deleted) when VM is deleted
$vm.StorageProfile.DataDisks | ForEach-Object { $_.DeleteOption = 'Detach' }
 

A lock on the disk prevents the disk from getting deleted but means the re-image process fails.

 

2

Comments (2 comments)

0
Avatar
Dave Stephenson

Welcome to the community, David Pereira 🙂!

Having a checkbox in the “Resize/Reimage” task that would detach existing Data Disks and/or Public IPs (see Public IPs for Hosts) would be ideal for this. (see rough mock-up below)

I'm guessing what you're doing isn't working right now because the VM is running and the data disk can't detach because it's in-use. 
Do you know if you have a command in your (Azure Runbook) Scripted Action to stop/deallocate the VM first?

# Shutdown the VM (deallocate to release compute resources)
Write-Output "Deallocating VM: $AzureVMName..."
Stop-AzVM -Name $AzureVMName -ResourceGroupName $AzureResourceGroupName -Force -NoWait

# Wait for the VM to fully deallocate before modifying disks
Start-Sleep -Seconds 60
 



 

0
Avatar
David Pereira
(Edited )

Hi Dave

That looks perfect, at the moment I only run the scripted action after the host deployment to attach the disk, it's a single host as well so that's all I need, as you can imagine the problem is when re-imaging for patches or any other changes it will require manual intervention to detach the disk otherwise it will fail and delete the host.

The other option would be to be able run a scripted action before the re-image, this way I could detach the disk before the re-image and then reattach after the host is redeployed with the new image.

 

Please sign in to leave a comment.