Custom Data
Custom Data (UserData) refers to the configuration script executed automatically by the system when the host starts for the first time or each time it restarts. This script can be input into the metadata server by the console/API and obtained by the cloud-init program inside the host.
To determine whether the host supports user custom data, it must be confirmed that cloud-init has been installed internally in the image (for official images provided by SurferCloud or custom images based on SurferCloud images, whether the system has installed the program can be judged by whether the Feature array of the image contains the CloudInit item). When the conditions are met, the host creation page will display the "Custom Data" option.
Cloud-Init
Cloud-init is an open source software launched by Canonical, the parent company of the Linux distribution Ubuntu. This software can be installed on mainstream Linux distributions (Ubuntu, CentOS, Debian, etc.) and its main use is to help users initialize the cloud hosting they created on the cloud computing platform.
User-defined data (Userdata) is a mechanism provided by Cloud-Init by default, which is universal among clouds.
Pass Custom Data When Creating a Host
Through the console/API, custom data can be passed in when creating a host. Supported script types include: User-Data, Cloud Config, Include, Gzip compressed scripts, Upstart Job, etc..
Note: The script content should not exceed 16 KB.
User-data Script
The first line is fixed as #!, such as #!/bin/bash, or #!/bin/python, etc.
It only executes 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 the word "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 Script
The first line is fixed as #cloud-config
This indicates that you are providing a piece of yaml format data specifically configured by Cloud-Init natively. It almost covers all abstract descriptions related to operating system configuration.
Please refer to the official example (opens in a new tab) for details.
Example 1: Change Hostname
#cloud-config
hostname: uhost1
Example 2: Change the data disk mount point to /opt/data
#cloud-config
mounts:
- [ /dev/vdb, /opt/data ]
Example 3: After the host is created, execute yum update or apt-get upgrade once automatically
#cloud-config
package_upgrade: true
Example 4: Configure keys when creating the host
#cloud-config
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAGEA3FSyQwBI6Z+nCSjUUk8EEAnnkhXlukKoUPND/RRClWz2s5TCzIkd3Ou5+Cyz71X0XmazM3l5WgeErvtIwQMyT1KjNoMhoJMrJnWqQPOt5Q8zWd9qG7PBl9+eiH5qV7NZ mykey@host
Other Script Types
Cloud hosts also support the input of Include scripts, Gzip compressed scripts, Upstart Job and other script types.
For details, please refer to Cloud-init Official Documentation (opens in a new tab)
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