使用VHDUpload上传VHD文件到云存储,并在Azure VM中加挂Azure Drive


1.上传VHD文件到Azure Blob Storage

VHDUpload程序的源代码在Windows Azure Training Kit目录Labs\ExploringWindowsAzureStorageVS2010\Source\Assets\VHDUpload,先编译成VHDUpload.exe。然后上传

Uploads a virtual hard disk (VHD) file to Windows Azure page blob service.

usage: VHDUPLOAD vhdFilePath blobUri accountName accountKey

  vhdFilePath  - path to virtual hard disk (VHD) file
  blobUri      - destination page blob relative URI (i.e. container/blobname)
  accountName  - storage account name (use devstorage for storage emulator)
  accountKey   - storage account primary key (omit for storage smulator)

VHDUpload上传过程如下所示:


2. 自写命令行程序Mount Drive

主要代码如下(请修改成相应的存储帐户和存储密钥,并编译成EXE):

 class Program
    {
        const string sMainStorageName = "StorageAccountName";
        const string sMainStorageKey = "StorageKey";

        static void Main(string[] args)
        {
            string sVHDPathName = string.Empty;
            string sOperation = string.Empty;
            bool IfUnmount = false;
            if (args.Count() == 0)
            {
                Console.WriteLine("Syntax: MountAzureDrive ContainerName/Disk.VHD");
                return;
            }

            try
            {
                sVHDPathName = args[0];
                if (args.Length > 1)
                {
                    sOperation = args[1];
                    if (args[1] == "/u")
                        IfUnmount = true;
                }

                var cloudDriveBlobPath = string.Format("http://{0}.blob.core.windows.net/{1}", sMainStorageName, sVHDPathName);
                StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey(sMainStorageName, sMainStorageKey);
                // LocalResource localCache = RoleEnvironment.GetLocalResource(sMainLocalStorageName);
                //  Char[] backSlash = { '\\' };
                //String localCachePath = localCache.RootPath.TrimEnd(backSlash);
                //CloudDrive.InitializeCache(localCachePath, localCache.MaximumSizeInMegabytes);


                //string cachePath = @"C:\Resources\LocalStorage";   //for VM role

                //Console.WriteLine("Local Cache initialized.....{0}", cachePath);
                //try
                //{
                //    CloudDrive.InitializeCache(cachePath, 10);
                //}
                //catch (Exception e)
                //{
                //    Console.WriteLine(e.Message);
                //}

                if (IfUnmount)
                {
                    Console.WriteLine("UnMount Drive " + cloudDriveBlobPath);
                    CloudDrive mountDrive = new CloudDrive(new Uri(cloudDriveBlobPath), credentials);

                    Console.WriteLine("Calling Cloud Drive UnMount API....", 0);
                    mountDrive.Unmount();
                    Console.WriteLine("Finished Cloud Drive UnMount .");
                }
                else
                {
                    Console.WriteLine("Mount Drive " + cloudDriveBlobPath);
                    CloudDrive mountDrive = new CloudDrive(new Uri(cloudDriveBlobPath), credentials);

                    Console.WriteLine("Calling Cloud Drive Mount API....", 0);
                    string driveLetter = mountDrive.Mount(0, DriveMountOptions.FixFileSystemErrors | DriveMountOptions.Force);
                    Console.WriteLine("Finished Cloud Drive Mounting at Drive :" + driveLetter, 0);

                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }


3. 在虚拟机里执行Mount动作

使用上述命令行:


下图的E盘是Mount之后的效果。(VM Role里实验通过)





阅读更多
个人分类: 云计算Azure
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭