Skip to Content

Custom Data

User-Defined Data (UserData) refers to a configuration script that the system automatically runs when a host is first started or each time it boots. This script can be passed to the metadata server via the console/API and retrieved by the cloud-init program within the host.

To determine whether a host supports user-defined data, it is necessary to confirm that cloud-init is installed in the image. For official UCloud Global-provided images or custom images created based on UCloud Global images, you can check whether the image’s Feature array includes the CloudInit item to determine if the program is installed in the system. When this condition is met, a Custom Data option will be displayed on the host creation page.

Cloud-Init

Cloud-init is an open-source software developed by Canonical, the parent company of the Linux distribution Ubuntu. It can be installed on mainstream Linux distributions (such as Ubuntu, CentOS, and Debian) and is primarily used to help users initialize newly created UHost on cloud computing platforms.

Userdata (User-Defined Data) is a default mechanism provided by Cloud-Init, which is universally applicable across multi-cloud environments.

Pass Custom Data When Creating a Host

Custom data can be passed during UHost creation via the console or API. Supported script types include: User-Data, Cloud Config, Include, Gzip-compressed scripts, Upstart Job, etc.

Note: The script content must not exceed 16 KB.

User-data Scripts

The first line must start with #!, for example: #!/bin/bash or #!/bin/python.

The script executes only once when the instance starts for the first time.

Example 1: Output “Hello World” after the host creation is complete

#!/bin/sh echo “Hello World!”

After creation, you will see “Hello World!” at the end of the “/var/log/cloud-init-output.log” log file.

Example 2: Start the Httpd service when the host starts

#!/bin/bash service httpd start chkconfig httpd on

Cloud Config Scripts

The first line is fixed as #cloud-config

This indicates that you’re providing a YAML-formatted configuration data natively defined by Cloud-Init. This format covers almost all abstract descriptions related to OS configuration.

Please refer to the official example for details.

Example 1: Change the data disk mount point to /opt/data

#cloud-config mounts: - [ /dev/vdb, /opt/data ]

Example 2: After the host is created, execute yum update or apt-get upgrade once automatically

#cloud-config package_upgrade: true

Example 3: Configure keys when creating the host

#cloud-config ssh_authorized_keys: - ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAGEA3FSyQwBI6Z+nCSjUUk8EEAnnkhXlukKoUPND/RRClWz2s5TCzIkd3Ou5+Cyz71X0XmazM3l5WgeErvtIwQMyT1KjNoMhoJMrJnWqQPOt5Q8zWd9qG7PBl9+eiH5qV7NZ mykey@host

Other Script Types

UHost also supports the input of Include scripts, Gzip compressed scripts, Upstart Job and other script types.

For details, please refer to Cloud-init Official Documentation

Retrieve Custom Data in the Host

In the following way, user-defined data can be obtained inside the host.

curl http://100.80.80.80/user-data