Custom Data
Custom Data (UserData) refers to the configuration script that is automatically run by the system when the host is first started or each time it is started. This script can be passed 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 is necessary to confirm that cloud-init has been installed inside the image (for the official images provided by SurferCloud, or custom images made based on SurferCloud images, it can be judged whether the program is installed in the system 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.). Its main purpose is to help users initialize their created cloud servers on cloud computing platforms.
User-defined data (Userdata) is a mechanism provided by default by Cloud-Init, which is universally applicable across multiple clouds.
Pass in custom data when creating a host
Through the console/API, you can pass in custom data when creating a host. Supported script types include: User-Data, Cloud Config, Include, Gzip compressed script, Upstart Job, etc.
Note: The script content cannot exceed 16 KB.
User-data script
The first line is always set to #!, such as #!/bin/bash, or #!/bin/python, etc.
It is only executed once when the instance is started for the first time.
Example 1: Output Hello World after the host creation is completed
#!/bin/sh
echo “Hello World!”
After creation is complete, you will be able to see the phrase "Hello World!" at the end of the /var/log/cloud-init-output.log log file.
**Example 2: **Start Httpd service when the host boots up
#!/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 set of yaml format configuration data natively defined by Cloud-Init, which almost includes all abstract descriptions related to operating system configuration.
For more details, please refer to the official example (opens in a new tab).
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: Automatically execute yum update or apt-get upgrade once after the host is created
#cloud-config
package_upgrade: true
Example 4: Configuring a key when creating a 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 more details, please refer to the Cloud-init official documentation (opens in a new tab).
Obtaining Custom Data Within the Host
You can obtain user-defined data within the host in the following ways
curl http://100.80.80.80/user-data