Search code examples
azureazure-sdk-for-java

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


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();


Solution

  • 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.