Changing Drive Letter of an Azure Drive (aka X-drive)

Monday, February 7, 2011

Sometimes it might be necessary that you want your Azure drive to be always mounted on a fixed drive letter. Consider a scenario of an Azure VM Role where you need to mount an azure drive for data persistance and your VM demands the same letter for you azure drive, e.q. you installed SQL Server on your VM Role and for mdf files you specified azure drive as path so as to make the data persist.

But now, we know that Azure drives are mounted on random-drive letters. To always have a fixed letter what you can do is that after your drive is mounted, you can change the drive letter to a fixed value using diskpart from within the windows service you use to mount the drive in VM Role, or from other part of code if you are not working with VM Role. Check this post to know how to mount Azure Drive in VM Role.

To get a basic idea on how to change drive letter using diskpart visit this Microsoft support link :

To change the drive letter of the mounted Azure Drive using diskpart, we will create a temporary file in local resource storage. This temp file will be used to store the current and target drive letters, and using this we can construct diskpart commands. Following code can be used to achieve the same:


//create temporary diskpart file
string diskpartFile = drive.CachePath + "\\diskpart.txt";

if (File.Exists(diskpartFile))

string driveLetter = drive.DriveLetter;
//start the process
using (Process changeletter = new Process())
changeletter.StartInfo.Arguments = "/s" + " " + diskpartFile;
changeletter.StartInfo.FileName = System.Environment.GetEnvironmentVariable("WINDIR") + "\\System32\\diskpart.exe";


Mounting Azure Drive in Azure Virtual Machine (VM) Role

Mounting an Azure Drive in Azure VM Role can be beneficial in many scenarios. As we all know that Azure VM Role is not persistent, so once you deploy a VM Role and it is restarted, all the data that was not the part of the base image is gone.

As we already know from a previous post that in a VM Role data is not persistent between restarts or hardware-failures, we need to identify a way to provide data persistence. As a solution we will keep the data that needs to be persisted on a mounted vhd, and then this vhd will be uploaded to a page blob. Once VM Role is deployed to Azure this page blob will be mounted as Azure Drive. I already talked about the need of Azure Drive in VM Role for data persistence in my other post on - "Data Persistance in Azure VM Role". Therefore, in this post we will focus on how to mount azure drive in VM Role.

As azure drives un-mount themselves in 30 minutes if the process that mounted is no longer alive, we will create a windows service that will mount the azure drive containing SQL data. This windows service would be setup to start automatically, and thus it will behave as a VM Role Adaptor. See this post to learn how to create VM Role Adaptor for startup tasks.

The windows service that we use will mount azure drive in it's OnStart method and would unmount it in its OnStop method. The code to mount and unmount is same as normal. To initialize cache you can specify any local path while mounting drive. The cache path and size can be configured in app.config of the service.

string cachePath = "C:\Resources\AzureDriveCache";
int cacheSize = 500;
CloudDrive.InitializeCache(cachePath, cacheSize + 20);    

Also, you must keep the following things in mind:

1. Make the Windows service to start by itself once installed, as described in a previous post regarding startup tasks in VM Role.
2. Set the Windows Service to Automatic(Delayed) start. The delayed start would ensure that VM Role has been done starting and loading all the changes it requires.
3. In the Recovery tab of the service set the service to Restart after all the failures.

Data Persitence in Azure VM Role

Azure VM Role involves creating a base image, uploading it to Azure using csupload, and then creating a servic model in Visual Studio to point to the uploaded base image.

Once Azure VM Role is deployed, it creates a new drive with letter D: and name it Resources. A shortcut to this drive is added to your C: drive (Windows Drive). Once you restart the machine the data on drive D: would be persited, however all the data or any software installed after VM ROle is deployed would be gone. Also, in case of system/hardware failures, even the data in drive D: will not persist.

But, what if you need to have data persistance in Azure VM Role. For example, you are installing SQL Server or may be Sharepoint on your VM Role. In this case you want your SQL Server's data files to persist. The option here would be to mount azure drive and this azure drive will hold your data files. Again, mounting an Azure drive is not a plain simple scenairo and it desribed here - Mounting Azure drive in VM Role.



2009 ·Techy Freak by TNB