Working with user data scripts

Working with user data scripts

User data scripts of Linux Bare-metal

User data scripts of Linux BMSs are customized by using the open-source Cloud-Init architecture. This architecture uses BMS metadata as the data source for automatically configuring the BMSs. The script types are compatible with open-source Cloud-Init. For details about Cloud-Init, see http://cloudinit.readthedocs.io/en/latest/topics/format.html.

User-data scripts are directly executed as shell scripts after they are passed into Linux instances. User-data scripts have the following characteristics:

  1. The first line starts with a number sign and an exclamation mark (#!).
  2. User-data scripts are run once only the first time the instance starts.
Example:

#!/bin/sh

echo "Hello World. The time is now $(date -R)!" | tee /root/userdata_test.txt

The example user-data script can be run to write the system time to the userdata_test.txt file the first time the instance starts.

Cloud config data

Cloud-config is a convenient way to pre-configure specific services (such as YUM repository update, SSH key import, and dependency installation) for instances. Cloud-config data has the following characteristics:
  1. The first line starts with #cloud-config, and the header cannot include spaces.
  2. The script must follow the YAML syntax.
  3. The frequency at which the user data is run varies based on your configured modules. For example, if you configure the Apt Configure module, the user data is run only once for each instance. If you configure the Bootcmd module, the user data is run each time the instance starts.
Example

#cloud-config

Apt:

Primary:

-   Arches: [default]

Uri: https: //us.archive.unbutu.com/unbutu/ Bootcmd:

- Echo: “Hello World. The time is now $(date -R) !” | tee /root/userdata_test.txt
The example cloud-config data can be run to modify the default software repository and write the latest system time to the userdata_test.txt file each time the instance starts

Example 1

This case illustrates how to inject user data so as to simplify BMS configuration.

In this example, vim is configured to enable syntax highlighting, display line numbers, and set the tab stop to 4. Configuration file .vimrc is created and injected into the /root/.vimrc directory during BMS creation. After the BMS is created, vim is automatically configured based on your requirements. This helps to improve BMS configuration efficiency, especially when you are creating BMSs in a batch.

The script is as follows:

#cloud-config

write_files:
- path: /root/.vimrc
content: |
      syntax on set
      tabstop=4 set
      number

Example 2

This case illustrates how to inject user data so as to reset the password for logging in to a Linux BMS.

In this example, the password of user root will be reset to "******".

The script is as follows (retain the indentation in the following script):

#cloud-config
chpasswd:
      list: |
            root:******
      expire: False

After the BMS is created, you can use the new password to log in to it. To ensure system security, change the password of user root after logging in to the BMS for the first time.


    • Related Articles

    • Step 2: Sign is as the root user

      When you first create an GreenNode account, you begin with one sign-in identity that has complete access to all GreenNode services and resources in the account. This identity is called the GreenNode account root user and is accessed by signing in ...
    • Create Bare-metal GPU Servers

      This document describes how to create and manage bare metal servers on the GreenNode console. Access to creation page To use a Bare-Metal GPU Server, you must create a server on the GreenNode console. The following describes how to create a bare ...
    • Tutorial: Get started with GreenNode Bare-metal

      Step 1: Configure Tenant Network You need to configure the Network so that the bare metal GPU server can communicate with the Network service. This section covers configuring the Network for a single planar network for bare metal GPU server ...
    • What Are the Key Features of Bare Metal GPU Servers?

      Bare-metal GPU Servers Key Features Direct Hardware Access: Bare-metal servers provide direct access to the underlying physical hardware without the need for a hypervisor or virtualization layer. This direct access contributes to enhanced performance ...
    • Connect to Bare-metal

      Users have many ways to connect to the Bare-metal such as the Console function on GreenNode Portal or other client tools. Depending on the operating system of the bare-metal, the operating system of your personal computer or the desired connection ...