0

I am creating a linux vm using a custom image from an shared image gallery using java sdk.

virtualMachine = azure.virtualMachines()
                        .define(linuxVMName)
                        .withRegion(location)
                        .withExistingResourceGroup(resourceGroup)
                        .withExistingPrimaryNetworkInterface(networkInterface)
                        .withLinuxCustomImage(customImageUrl)
                        .withRootUsername(username)
                        .withRootPassword(password)
                        .withCustomData(custDatastring)
                        .withComputerName(linuxVMName)
                        .withExistingStorageAccount(storageAccount)
                        .withSize(vmSize())
                        .create();

How to pass a linux shell script here in the withCustomData part. I know the input for the method is Base64 encoded string. I am trying to pass an example script

#!/bin/bash
echo "This is some text" > randomtext.txt

p.s: I can't create a file for the script here, i have to pass the above command directly to the api. That is my requirement.

How should i handle the customdata part here. Any help is appreciated.

vm create request osprofile json

"osProfile":{"computerName":"xxxx","adminUsername":"xxx","adminPassword":"xxx","customData":"IyEvYmluL2Jhc2gKIGVjaG8gIlRoaXMgaXMgc29tZSB0ZXh0IiA+IHJhbmRvbXRleHQudHh0","linuxConfiguration":{"disablePasswordAuthentication":false}}

response os profile

"osProfile": {
      "computerName": "xxxx",
      "adminUsername": "xxxx",
      "linuxConfiguration": {
        "disablePasswordAuthentication": false,
        "provisionVMAgent": true
      },
      "secrets": [],
      "allowExtensionOperations": true,
      "requireGuestProvisionSignal": true
    }

The image i use here is a custom linux image built on centos 8

smootherbug
  • 129
  • 12

1 Answers1

1

Refer to Java 8 Basic Base64, you could use java.util.Base64. First, import it as you normally do:

import java.util.Base64;

then

String originalInput = "#!/bin/bash\n echo \"This is some text\" > randomtext.txt";
String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());

Then use it like this,

virtualMachine = azure.virtualMachines()
                        .define(linuxVMName)
                        .withRegion(location)
                        .withExistingResourceGroup(resourceGroup)
                        .withExistingPrimaryNetworkInterface(networkInterface)
                        .withLinuxCustomImage(customImageUrl)
                        .withRootUsername(username)
                        .withRootPassword(password)
                        .withCustomData(encodedString)
                        .withComputerName(linuxVMName)
                        .withExistingStorageAccount(storageAccount)
                        .withSize(vmSize())
                        .create();

I tried it.

enter image description here

On Linux, this data is passed to the VM via the ovf-env.xml file, which is copied to the /var/lib/waagent directory during provisioning. Newer versions of the Microsoft Azure Linux Agent will also copy the base64-encoded data to /var/lib/waagent/CustomData as well for convenience.

I can find the custom data in the path /var/lib/waagent/ovf-env.xml on My Ubuntu16.04LTS.

enter image description here

For more examples, you could refer to this thread and Custom Data and Cloud-Init on Microsoft Azure.

Nancy
  • 26,865
  • 3
  • 18
  • 34
  • I tried as you said, in my request os profile i can see the custom data encoded string, but in my response for vm creation there is no customdata part. i have updated the json req and resp in the question part – smootherbug Aug 26 '20 at 09:07
  • Why do you think it will exist on the response, I check the [get API](https://learn.microsoft.com/en-us/rest/api/compute/virtualmachines/get#linuxconfiguration). It did not display the custom data even I use the standard Azure Ubuntu image and I also create another VM with `az vm create..` Both are the same. The custom data did not display in the response. – Nancy Aug 26 '20 at 09:40
  • In fact, a provisioning agent processes that, such as the Linux Agent and cloud-init. The provisioning agents installed on the VMs handle interfacing with the platform and placing it on the file system. Read [this](https://learn.microsoft.com/en-us/azure/virtual-machines/custom-data#linux) for more details. – Nancy Aug 26 '20 at 09:55
  • I am storing the script data in a blob field in database, guess I have made a mistake in storing the script properly. Thanks for the help. much appreciated – smootherbug Aug 26 '20 at 10:44