1

I want to mount vhd from my private container. After google I get that it is only possible through .net. I am more of a JAVA person. I need a batch script or code in c# (So that I can get an exe file) which can automatically run at startup and mount vhd. So I decided to create a console app in order to get exe file.(I have very less knowledge c#/Visual studio) I am using following C# console application to do this.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;

using Microsoft.WindowsAzure.Internal;

namespace WorkerRole1
{
public class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {
        // This is a sample worker implementation. Replace with your logic.
        Trace.WriteLine("WorkerRole1 entry point called", "Starting");
        MountDrive();

        //while (true)
        //{
        //    Thread.Sleep(10000);
        //    Trace.WriteLine("Working", "Information");
        //}
    }

    public override bool OnStart()
    {
        // Set the maximum number of concurrent connections 
        ServicePointManager.DefaultConnectionLimit = 12;

        // For information on handling configuration changes
        // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

        return base.OnStart();
    }

    public void MountDrive()
    {
        string connectionStringSettingName = "DefaultEndpointsProtocol=http;AccountName=abc;AccountKey=xyz";
        string azureContainerName = "vhds";
        string vhdName = "myvhd.vhd";
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionStringSettingName);
        //CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;

        LocalResource localCache = RoleEnvironment.GetLocalResource("MyAzureDriveCache");
        CloudDrive.InitializeCache(localCache.RootPath, localCache.MaximumSizeInMegabytes);
        Trace.WriteLine("RootPath =====" + localCache.RootPath);
        Trace.WriteLine("MaximumSizeInMegabytes =====" + localCache.MaximumSizeInMegabytes);
        // Just checking: make sure the container exists
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        blobClient.GetContainerReference(azureContainerName).CreateIfNotExist();

        // Create cloud drive
        CloudDrive myCloudDrive = storageAccount.CreateCloudDrive(
            blobClient
            .GetContainerReference(azureContainerName)
            .GetPageBlobReference(vhdName)
            .Uri.ToString()
        );
        Trace.WriteLine("Uri =====" + blobClient
            .GetContainerReference(azureContainerName)
            .GetPageBlobReference(vhdName)
            .Uri.ToString());

        try
        {
            myCloudDrive.Create(1024);
        }
        catch (CloudDriveException ex)
        {
            // handle exception here
            // exception is also thrown if all is well but the drive already exists
        }

        string driveLetter = myCloudDrive.Mount(50, DriveMountOptions.Force);//Here It throws a Exception
        Trace.WriteLine("Drive =====" + driveLetter);

        for (int i = 0; i < 10; i++)
        {
            System.IO.File.WriteAllText(driveLetter + "\\" + i.ToString() + ".txt", "Test");
        }


    }

}
}

But I keep getting exception ERROR_DEVFABRIC_LOCAL_MOUNT_ONLY at

string driveLetter = myCloudDrive.Mount(50, DriveMountOptions.Force);

Please tell me where am I going wrong?

Anurag Tripathi
  • 1,208
  • 1
  • 12
  • 31

2 Answers2

1

When RoleEnvironment.IsAvailable is set to false, this means you are not running in a Windows Azure Web/Worker/VM Role. The Windows Azure Drive only works when mounted in these roles (since it depends on RoleEnvironment).

More information can be found in the whitepaper.

Sandrino Di Mattia
  • 24,739
  • 2
  • 60
  • 65
0

ERROR_DEVFABRIC_LOCAL_MOUNT_ONLY means that when you are running locally you must mount the drive form development storage.

change the following line:

string connectionStringSettingName = "UseDevelopmentStorage=true";

or even better use RoleEnvironment.GetConfigurationSettingValue, like:

string connectionStringSettingName = RoleEnvironment.GetConfigurationSettingValue("DriveConnectionString");

and set the Setting in the service configuration (files)

borrel
  • 911
  • 9
  • 17