Project Description
MountVHDAzureVM application will help Windows Azure users to mount a VHD in any Windows Azure VM outside Role. Windows Azure users can host a group of tools and contents in a VHD and store this VHD in Azure Storage. Using this application, Azure VM users, can mount this VHD.

 

Main Application Image


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

private static CloudDrive drive;
private static LocalResource localCache;



Console.Write("Role Environment Verification: ");
if (!RoleEnvironment.IsAvailable)
{
Console.WriteLine(" FAILED........!!");
return;
}
Console.WriteLine(" SUCCESS........!!");
Console.WriteLine("Starting Drive Mount!!");
var cloudDriveBlobPath = "http://<StorageName>.blob.core.windows.net/<ContainerName>/<VHD_NAME (myvhd.vhd)>"; 
Console.WriteLine("Role Name: " + RoleEnvironment.CurrentRoleInstance.Role.Name);
StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey("<StorageNAME>", "<STORAGEKEY>");
Console.WriteLine("Deployment ID:" + RoleEnvironment.DeploymentId.ToString());
Console.WriteLine("Role count:" + RoleEnvironment.Roles.Count);
try
{
localCache = RoleEnvironment.GetLocalResource("LocalStorage");
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("Caling 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();
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Console Application 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:
- ERRORBLOBDOESNOTEXIST
3. If your VHD will not be page blob it will not mount 
- ERRORBLOBNOTPAGEBLOB
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 application 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 tested 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.

 

 

 

 

 

Last edited Apr 17, 2011 at 12:25 AM by Avkash, version 6