When dealing with Windows virtual machines from the marketplace on Azure, sometimes a CD-ROM will temporarily be present in the first moments in the lifetime of a virtual machine or virtual machine scale set instances. Microsoft needs this drive to perform various actions like preparing and licensing the VM. If present, this drive will eventually go away, leaving you with a drive letter reassignment that might cause you trouble... at last, it did for me.

The problem

In my scenario, I was creating a virtual machine scale sets and was starting to assign my first data disk drive letter as F. I used an initialization script I took from the Azure documentation. It worked very well until I needed to re-image instances or perform an OS upgrade in the scale sets. When I did this, the instances were back running but with my data disk with letter E instead of F. It goes without saying that this made my application crash because it was referring to a data location of F:... in its configuration.

Why is this happening

After some troubleshooting and gathering information here and there, I realized it was a CD-ROM drive used by Microsoft that was causing this behavior. When dealing with some Azure marketplace images (Generalized), Microsoft will use this drive as a bootstrap point to properly license Windows & configure the OS. When the virtual machine agent enter a ready state and everything is fine, this drive is no longer needed and will be purged eventually, therefore vanishing at some point in the lifecycle of the virtual machine.

The Solution

I started to think about how to be consistent across the lifecycle of my instances and got an idea. I'd start from the initializing script I was previously using and chage it a bit. What if I change the CD-ROM drive letter to Z and prepare my data disks right after the D drive (temporary VM drive on Azure), starting from E instead of F. This way it would always be the same letter sequence whatever happen. It worked like a charm and after several tests, I now have my virtual machine scale sets instances very stable across reboot, de-allocate, re-image & host relocation on Azure.

Here is what my data disk preparation script now does: It will move (if present) the CD-ROM drive to letter Z and perform initialization, formatting and assignation of drive letters for all un-initialized disks from letter E to Y.

I am using it with a custom script extension that runs in an ARM template but you could use it directly on the VM yourself in a PowerShell console if you'd like.

# snippet of what's inside 
 
Get-WmiObject -Class Win32_volume -Filter 'DriveType=5'... 
 
... 
$disks = Get-Disk | Where-Object partitionstyle -eq 'raw' | Sort-Object number 
... 
foreach ($disk in $disks) { 
    ... 
    Initialize-Disk -PartitionStyle MBR -PassThru | 
    New-Partition -UseMaximumSize -DriveLetter $driveLetter | 
    Format-Volume -FileSystem NTFS -NewFileSystemLabel "$label.$count" -Confirm:$false -Force 
    ... 
}

Conclusion

You can download the full version (for free) on Microsoft Script Center

References