Mount a Page Blob VHD in any Windows Azure VM outside any Web, Worker or VM Role

Following is the C# Source code to mount a Page Blob VHD in any Windows Azure VM outside any Web, Worker or VM Role:

 

 Console.Write("Role Environment Verification: ");
if (!RoleEnvironment.IsAvailable)
{
    Console.WriteLine(" FAILED........!!");

         return;
}
Console.WriteLine(" SUCCESS........!!");
Console.WriteLine("Starting Drive Mount!!");
var cloudDriveBlobPath = https://<Storage_Account>.blob.core.windows.net/<Container>/<VHD_NAME(myvhd64.vhd);
Console.WriteLine("Role Name: " + RoleEnvironment.CurrentRoleInstance.Role.Name);
StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey("<Storage_ACCOUNT>", "<Storage_KEY>");
Console.WriteLine("Deployment ID:" + RoleEnvironment.DeploymentId.ToString());
Console.WriteLine("Role count:" + RoleEnvironment.Roles.Count);
try
{
    localCache = RoleEnvironment.GetLocalResource("<Correct_Local_Storage_Name>");

         Char[] backSlash = { '\\' };

         String localCachePath = localCache.RootPath.TrimEnd(backSlash);

         CloudDrive.InitializeCache(localCachePath, localCache.MaximumSizeInMegabytes);

         Console.WriteLine(localCache.Name + " | " + localCache.RootPath + " | " + localCachePath + " ! " + localCache.MaximumSizeInMegabytes);

}
catch (Exception eXp)
{
    Console.WriteLine("Problem with Local Storage: " + eXp.Message);

         return;
}
drive = new CloudDrive(new Uri(cloudDriveBlobPath), credentials);
try
{
    Console.WriteLine("Calling Drive Mount API!!");

         string driveLetter = drive.Mount(localCache.MaximumSizeInMegabytes, DriveMountOptions.None);

         Console.WriteLine("Drive :" + driveLetter);

         Console.WriteLine("Finished Mounting!!");

         Console.WriteLine("************Lets Unmount now********************");

         Console.WriteLine("Press any key......");

         Console.ReadKey();

         Console.WriteLine("Starting Unmount......");

         drive.Unmount();

         Console.WriteLine("Finished Unmounting!!");

         Console.WriteLine("Press any key to exit......");

         Console.ReadKey();
}
catch (Exception exP)
{
    Console.WriteLine(exP.Message + "//" + exP.Source);

         Console.WriteLine("Failed Mounting!!");

         Console.ReadKey();
}

Applicaiton Output:  

Role Environment Verification: SUCCESS........!!

Starting Drive Mount!!

Role Name: VMRole1

Deployment ID:207c1761a3e74d6a8cad9bf3ba1b23dc

Role count:1

LocalStorage | C:\Resources\LocalStorage | C:\Resources\LocalStorage ! 870394

Calling Drive Mount API!!

Drive :B:\

Finished Mounting!!

************Lets Unmount now********************

Press any key......

Starting Unmount......

Finished Unmounting!!

Press any key to exit......

Possible Errors:

  • 1. Be sure to have correct Local Storage Folder
    • no such local resource
  • 2. Be sure to have your VHD otherwise you will get error:
    • ERROR_BLOB_DOES_NOT_EXIST
  • 3. If your VHD in not a page blob VHD, it will not mount
    • ERROR_BLOB_NOT_PAGE_BLOB
  • 4. If you receive the following Error it means your application build is set to x86
    • Could not load file or assembly 'mswacdmi, Version=1.1.0.0
    • Please set the build to 64bit to solve this problem.

 

To Create VHD drive:

When using Microsoft Disk Management to create VHD be sure:

  • To create FIXED SIZE VHD
  • Use MBR Partition type (GPT based disk will not be able to mount)

 

I done testing with Web Role, Worker Role and VM Role and I was able to mount valid Page Blob VHD with this code with all 3 kinds of roles with Windows Azure SDK 1.4.