All Products
Search
Document Center

Elastic Compute Service:Configure custom images for instance initialization

Last Updated:Sep 27, 2024

If the operating system version of your Linux image is not supported by Elastic Compute Service (ECS) and the image cannot be used to initialize an instance through cloud-init, then the image is identified as being created from an unrecognized operating system. Before you import the image to an ECS instance, you must add a parsing script to the image, which is used to automatically configure the operating system during the first start of the instance.

Note

Linux images not supported by ECS are considered non-standard platform images. Such images are developed on a standard operating system platform. However, their critical system configuration files, basic system environments, or applications of the images do not comply with the standard operating system requirements of ECS. The following are the supported OS Version and guidelines for importing non-standard platform images:

  • Others Linux: ECS identifies all images of this type as other Linux images. If you import an Others Linux image and then use it to create instances, ECS does not process the created instances. After you create an instance, you must configure the IP addresses, routes, and password of the instance.

  • Customized Linux: Before you import a Customized Linux image, follow the instructions in this topic to configure your image.

Prerequisites

Your image must satisfy the following requirements:

  • The first partition, typically /dev/sda1 or /dev/vda1, must be writable.

  • The file system type of the first partition must be FAT32, ext2, ext3, ext4, or UFS.

    Note

    You can use the blkid /dev/sdXn command to verify the file system type, where sdXn represents the first partition name, such as /sda1.

  • The size of the virtual file of the customized Linux image must be larger than 5 GiB.

    Note

    You can check the size of the partition using the df -h /dev/sdXn command, where sdXn is the first partition name, such as /sda1.

Procedure

  1. Log on to the virtual machine where the image is created as the root user.

  2. Create the aliyun_custom_image directory in the root directory of the first partition of the server from which the image is created.

    mkdir /aliyun_custom_image

    When the ECS instance created from the customized Linux image is started for the first time, Alibaba Cloud writes instance configurations such as the hostname, password, and DNS server to the os.conf file in the aliyun_custom_image directory. If the os.conf file does not exist, Alibaba Cloud will create one.

    The following example shows the os.conf file content:

    hostname=<yourHostName>
    password=<yourPassword>
    eth0_ip_addr=10.0.0.2
    eth0_mac_addr=00:xx:xx:xx:xx:23
    eth0_netmask=255.255.255.0
    eth0_gateway=10.0.0.1
    eth0_route="0.0.0.0/0 10.0.0.1"
    dns_nameserver="7.7.X.X 8.8.8.8"

    The following table describes the parameters in the preceding example. You can configure the parameters based on your needs.

    Parameter

    Description

    hostname

    The hostname.

    password

    The password of the root user.

    eth0_ip_addr

    The IP address of the eth0 NIC.

    eth0_mac_addr

    The MAC address of the eth0 NIC.

    eth0_netmask

    The network mask of the eth0 NIC.

    eth0_gateway

    The default gateway of the eth0 NIC.

    eth0_route

    The eth0 internal routes. By default, routes are separated with spaces.

    dns_nameserver

    The DNS address list. By default, addresses are separated with spaces.

  3. Create a parsing script within the image, such as customized-config.service, to interpret and apply the system configuration from the os.conf file.

    This script automates initial configuration tasks such as setting the hostname, root password, DNS server, and network configurations when the ECS instance is started for the first time.

    Refer to the following sample script:

    #!/bin/bash
    
    ### BEGIN INIT INFO
    # Provides:          os-conf
    # Required-Start:    $local_fs $network $named $remote_fs
    # Required-Stop:
    # Should-Stop:
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Initialize system configuration
    ### END INIT INFO
    
    # Define the directory for the first partition and os.conf file
    first_partition_dir='/boot/'
    os_conf_dir=${first_partition_dir}/aliyun_custom_image
    os_conf_file=${os_conf_dir}/os.conf
    
    # Load os.conf if it exists
    load_os_conf() {
        if [[ -f $os_conf_file ]]; then
            . $os_conf_file
            return 0
        else
            return 1
        fi
    }
    
    # Cleanup function to remove os.conf and ensure directory exists
    cleanup() {
        rm $os_conf_file >& /dev/null
        mkdir -p $os_conf_dir
    }
    
    # Configure the root password
    config_password() {
        if [[ -n $password ]]; then
            password=$(echo $password | base64 -d)
            if [[ $? == 0 && -n $password ]]; then
                echo "root:$password" | chpasswd
            fi
        fi
    }
    
    # Set the hostname
    config_hostname() {
        if [[ -n $hostname ]]; then
            sed -i "s/^HOSTNAME=.*/HOSTNAME=$hostname/" /etc/sysconfig/network
            hostname $hostname
        fi
    }
    
    # Configure DNS settings
    config_dns() {
        if [[ -n $dns_nameserver ]]; then
            dns_conf=/etc/resolv.conf
            sed -i '/^nameserver.*/d' $dns_conf
            for i in $dns_nameserver; do
                echo "nameserver $i" >> $dns_conf
            done
        fi
    }
    
    # Determine if the network is classic or VPC
    is_classic_network() {
        grep -q 'eth1' $os_conf_file
    }
    
    # Configure network settings
    config_network() {
        /etc/init.d/network stop
        config_interface eth0 ${eth0_ip_addr} ${eth0_netmask} ${eth0_mac_addr}
        config_route eth0 "${eth0_route}"
        if is_classic_network ; then
            config_interface eth1 ${eth1_ip_addr} ${eth1_netmask} ${eth1_mac_addr}
            config_route eth1 "${eth1_route}"
        fi
        /etc/init.d/network start
    }
    
    # Configure network interface
    config_interface() {
        local interface=$1
        local ip=$2
        local netmask=$3
        local mac=$4
        interface_cfg="/etc/sysconfig/network-scripts/ifcfg-${interface}"
        cat << EOF > $interface_cfg
    DEVICE=$interface
    IPADDR=$ip
    NETMASK=$netmask
    HWADDR=$mac
    ONBOOT=yes
    BOOTPROTO=static
    EOF
    }
    
    # Set the default gateway
    config_default_gateway() {
        local gateway=$1
        sed -i "s/^GATEWAY=.*/GATEWAY=$gateway/" /etc/sysconfig/network
    }
    
    # Configure routing
    config_route() {
        local interface=$1
        local route="$2"
        route_conf=/etc/sysconfig/network-scripts/route-${interface}
        > $route_conf
        echo $route | sed 's/;/\n/' | \
            while read line; do
                dst=$(echo $line | awk '{print $1}')
                gw=$(echo $line | awk '{print $2}')
                if ! grep -q "$dst" $route_conf 2> /dev/null; then
                    echo "$dst via $gw dev $interface" >> $route_conf
                fi
                if [[ "$dst" == "0.0.0.0/0" ]]; then
                    config_default_gateway $gw
                fi
            done
    }
    
    ################## sysvinit service portal ####################
    
    # Start the configuration process
    start() {
        if load_os_conf ; then
            config_password
            config_network
            config_hostname
            config_dns
            cleanup
            return 0
        else
            echo "not load $os_conf_file"
            return 0
        fi
    }
    
    RETVAL=0
    
    # Service action handling
    case "$1" in
        start)
            start
            RETVAL=$?
        ;;
        *)
            echo "Usage: $0 {start}"
            RETVAL=3
        ;;
    esac
    
    exit $RETVAL
  4. Set the parsing script to run automatically on system startup.

    The following sample commands apply to Ubuntu systems. Modify the startup commands based on your operating system.

    systemctl daemon-reload
    systemctl enable customized-config.service
    Note

    When a custom image is created based on a Customized Linux image, the automatic startup script is also included. Alibaba Cloud will execute the configurations of the os.conf file when the instance is started for the first time.

References

After you configure the image, you can select Customized Linux as the operating system version when importing the custom image. For more information, see Import custom images.