How do i set VM generation while creating disk using Azure sdk for java

644 Views Asked by At

I don't find any option to set the VM generation, by default its 1 but i need to change it to 2.
Azure portal create disk

Disk managedDisk = azure.disks().define("myosdisk") .withRegion(Region.US_EAST2) .withExistingResourceGroup("test") .withWindowsFromVhd ("https://abcd.blob.core.windows.net/vm/‘laptop_vm’.vhd") .withSizeInGB(500).withSku(DiskSkuTypes.PREMIUM_LRS).create();

2

There are 2 best solutions below

2
On BEST ANSWER

You can use hyperVGeneration set function. The hypervisor generation of the Virtual Machine. Applicable to OS disks only. Possible values include: 'V1', 'V2'

Possible call withHyperVGeneration('V2');

Checkout azure-sdk-java implementation for details.

You need to import mgmt.compute libraries. It can be found in below maven artifact.

<dependency>
  <groupId>com.azure.resourcemanager</groupId>
  <artifactId>azure-resourcemanager</artifactId>
  <version>2.5.0</version>
</dependency>

You can call disk creation as below

List<String> diskNames = Arrays.asList("myosdisk", "myosdisk2");
List<Creatable<Disk>> creatableDisks = diskNames.stream()
    .map(diskName -> azure.disks()
        .define(diskName)
        .withRegion(Region.US_EAST2)
        .withExistingResourceGroup("test")
        .withWindowsFromVhd ("https://abcd.blob.core.windows.net/vm/‘laptop_vm’.vhd")
        .withHyperVGeneration('V2')
        .withData()
        .withSizeInGB(500)
        .withSku(DiskSkuTypes.PREMIUM_LRS)
    .collect(Collectors.toList());
Collection<Disk> disks = azure.disks().create(creatableDisks).values();
azure.disks().deleteByIds(disks.stream().map(Disk::id).collect(Collectors.toList()));

More information can be found in Github Azure/azure-sdk-for-java repository.

0
On

This is the workaround i found to resolve this,After creating the disk I am just updating the disk to HyperVGeneration.V2

Disk managedDisk = azure.disks().define("myosdisk")
.withRegion(Region.US_EAST2)
.withExistingResourceGroup("umbc")
.withWindowsFromVhd("https://abcd.blob.core.windows.net
/vmimages/‘laptop_vm’.vhd")
.withStorageAccountName("abcd")
.withSku(DiskSkuTypes.PREMIUM_LRS)
.create();
            
             
managedDisk.inner().withHyperVGeneration(HyperVGeneration.V2);
managedDisk.update().apply();