• 4 min read

.NET: Manage Azure Managed Disks

You can use Azure Management Libraries for .NET to manage Managed Disks.

We are announcing beta 5 of the Azure Management Libraries for .NET. Beta 5 adds support for Azure Managed Disks.

Today, Microsoft announced the general availability of Azure Managed Disks – it simplifies the management and scaling of Virtual Machines. Specify the size and disk you want to use for Virtual Machines. You do not have to worry about creating and managing Storage Accounts.

You can use the Azure Management Libraries for .NET to manage Managed Disks.

https://github.com/Azure/azure-sdk-for-net/tree/Fluent

You can download beta 5 from:

Create a Virtual Machine with Managed Disks

You can create a Virtual Machine with an implicit Managed Disk for the operating system and explicit Managed Disks for data using a define() … create() method chain. Creation is simplified with implicit creation of managed disks without specifying all the disk details. You do not have to worry about creating and managing Storage Accounts.

var linuxVM1 = azure.VirtualMachines
  .Define(linuxVM1Name)
  .WithRegion(Region.USEast)
  .WithNewResourceGroup(rgName)
  .WithNewPrimaryNetwork("10.0.0.0/28")
  .WithPrimaryPrivateIpAddressDynamic()
  .WithNewPrimaryPublicIpAddress(linuxVM1Pip)
  .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
  .WithRootUsername(“tirekicker”)
  .WithSsh(sshkey)
  .WithNewDataDisk(100)
  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
  .Create();

You can download the full, ready-to-run sample code.

Create a Virtual Machine Scale Set with Managed Disks

You can create a Virtual Machine Scale Set with implicit Managed Disks for operating systems and explicit Managed Disks for data using a define() … create() method chain.

var vmScaleSet = azure.VirtualMachineScaleSets
  .Define(vmScaleSetName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithSku(VirtualMachineScaleSetSkuTypes.StandardD5v2)
  .WithExistingPrimaryNetworkSubnet(network, "subnet1")
  .WithExistingPrimaryInternetFacingLoadBalancer(publicLoadBalancer)
  .WithoutPrimaryInternalLoadBalancer()
  .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
  .WithRootUsername("tirekicker")
  .WithSsh(sshkey)
  .WithNewDataDisk(100)
  .WithNewDataDisk(100, 1, CachingTypes.ReadWrite)
  .WithNewDataDisk(100, 2, CachingTypes.ReadOnly)
  .WithCapacity(3)
  .Create();

You can download the full, ready-to-run sample code.

Create an Empty Managed Disk and Attach It to a Virtual Machine

You can create an empty Managed Disk using a define() … create() method chain.

var dataDisk = azure.Disks.Define(diskName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithData()
  .WithSizeInGB(50)
  .Create();

You can attach the empty Managed Disk to a Virtual Machine using another define() … create() method chain.

var linuxVM2 = azure.VirtualMachines.Define(linuxVM2Name)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithNewPrimaryNetwork("10.0.0.0/28")
  .WithPrimaryPrivateIpAddressDynamic()
  .WithNewPrimaryPublicIpAddress(linuxVM2Pip)
  .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
  .WithRootUsername("tirekicker")
  .WithSsh(sshkey)
  .WithNewDataDisk(100)
  .WithNewDataDisk(100, 1, CachingTypes.ReadWrite)
  .WithExistingDataDisk(dataDisk)
  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
  .Create();

You can download the full, ready-to-run sample code.

Update a Virtual Machine

You can detach Managed Disks and attach new Managed Disks using an update() … apply() method chain.

linuxVM2.Update()
  .WithoutDataDisk(2)
  .WithNewDataDisk(200)
  .Apply();

You can download the full, ready-to-run sample code.

Create a Virtual Machine From a Specialized VHD

You can create a Virtual Machine from a Specialized VHD using a define() … create() method chain.

var linuxVM4 = azure.VirtualMachines.Define(linuxVmName3)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithNewPrimaryNetwork("10.0.0.0/28")
  .WithPrimaryPrivateIpAddressDynamic()
  .WithoutPrimaryPublicIpAddress()
  .WithSpecializedOsUnmanagedDisk(specializedVhd, OperatingSystemTypes.Linux)
  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
  .Create();

You can download the full, ready-to-run sample code.

Create a Virtual Machine Using a Custom Image

You can create a custom image from a de-allocated and generalized Virtual Machine using a define() … create() method chain.

var virtualMachineCustomImage = azure.VirtualMachineCustomImages
  .Define(customImageName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .FromVirtualMachine(linuxVM) // from a de-allocated and generalized Virtual Machine
  .Create();

You can create a Virtual Machine from the custom image using another define() … create() method chain.

var linuxVM4 = azure.VirtualMachines.Define(linuxVM4Name)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithNewPrimaryNetwork("10.0.0.0/28")
  .WithPrimaryPrivateIpAddressDynamic()
  .WithoutPrimaryPublicIpAddress()
  .WithLinuxCustomImage(virtualMachineCustomImage.Id)
  .WithRootUsername(userName)
  .WithSsh(sshkey)
  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
  .Create();

You can download the full, ready-to-run sample code.

Create a Virtual Machine Using Specialized Disks From Snapshots

You can create a Managed Disk Snapshot for an operating system disk.

 

// Create a Snapshot for an operating system disk
var osDisk = azure.Disks.GetById(linuxVM.OsDiskId);
var osSnapshot = azure.Snapshots.Define(managedOSSnapshotName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithLinuxFromDisk(osDisk)
  .Create();
// Create a Managed Disk from the Snapshot for the operating system disk
var newOSDisk = azure.Disks.Define(managedNewOSDiskName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithLinuxFromSnapshot(osSnapshot)
  .WithSizeInGB(100)
  .Create();

You can create a Managed Disk Snapshot for a data disk.

// Create a Snapshot for a data disk
var dataSnapshot = azure.Snapshots.Define(managedDataDiskSnapshotName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithDataFromDisk(dataDisk)
  .WithSku(DiskSkuTypes.StandardLRS)
  .Create();
// Create a Managed Disk from the Snapshot for the data disk
var newDataDisk = azure.Disks.Define(managedNewDataDiskName)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithData()
  .FromSnapshot(dataSnapshot)
  .Create();

You can create a Virtual Machine from these specialized disks using a define() … create() method chain.

var linuxVM5 = azure.VirtualMachines.Define(linuxVm5Name)
  .WithRegion(Region.USEast)
  .WithExistingResourceGroup(rgName)
  .WithNewPrimaryNetwork("10.0.0.0/28")
  .WithPrimaryPrivateIpAddressDynamic()
  .WithoutPrimaryPublicIpAddress()
  .WithSpecializedOsDisk(newOSDisk, OperatingSystemTypes.Linux)
  .WithExistingDataDisk(newDataDisk)
  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
  .Create();

You can download the full, ready-to-run sample code.

Convert a Virtual Machine to Use Managed Disks With a Single Reboot

You can convert a Virtual Machine with unmanaged disks (Storage Account based) to Managed Disks with a single reboot.

var linuxVM6 = azure.VirtualMachines.Define(linuxVM6Name)
  .WithRegion(Region.USEast)
  .WithNewResourceGroup(rgName)
  .WithNewPrimaryNetwork("10.0.0.0/28")
  .WithPrimaryPrivateIpAddressDynamic()
  .WithNewPrimaryPublicIpAddress(linuxVM6Pip)
  .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
  .WithRootUsername("tirekicker")
  .WithSsh(sshkey)
  .WithUnmanagedDisks() // uses Storage Account
  .WithNewUnmanagedDataDisk(100) // uses Storage Account
  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
  .Create();

linuxVM7.Deallocate();
linuxVM7.ConvertToManaged();

You can download the full, ready-to-run sample code.

Try It

You can run the samples above or go straight to our GitHub repo. Give it a try and let us know what do you think (via e-mail or comments below). Over the next few weeks, we will be adding support for more Azure services and applying finishing touches to the API.