Configure a custom Linux image

更新时间:
复制 MD 格式

If your Linux image uses an OS version unsupported by Elastic Compute Service (ECS) and cannot be initialized with cloud-init, Alibaba Cloud treats it as an unrecognized operating system. Therefore, before importing the image, you must add a parsing script to it for automatic configuration when the resulting instance starts for the first time.

Note

If you import a Linux image with an OS version that ECS does not support, it is considered a non-standard platform image. Although based on standard operating systems, these images have critical system files, environments, or applications that do not meet platform requirements. When you import a non-standard platform image, you can select one of the following options for OS Version:

  • If you import an Others Linux image, ECS does not process the resulting instances. After an instance is created, you must log on to it to manually configure its IP address, routes, password, and other settings.

  • Customized Linux: This is a custom image type that requires configuration as described in this topic before you import it.

Prerequisites

Ensure that your image meets the following requirements:

  • The first partition, typically /dev/sda1 or /dev/vda1, must have write permissions.

  • The file system of the first partition must be one of the following: FAT32, EXT2, EXT3, EXT4, or UFS.

    Note

    You can run the blkid /dev/sdXn command to check the file system type, where /dev/sdXn represents the path to the first partition, such as /dev/sda1.

  • The virtual size of the image file must be greater than 5 GiB.

    Note

    You can run the df -h /dev/sdXn command to check the space of a specific partition, where /dev/sdXn represents the path to the first partition, such as /dev/sda1.

Procedure

  1. Log on to the virtual machine that you use to create the image as the root user.

  2. In the root directory of the first partition of the image, create a directory named aliyun_custom_image.

    mkdir /aliyun_custom_image

    When an ECS instance created from a Customized Linux image starts for the first time, Alibaba Cloud writes instance information, such as the hostname, root user password, and DNS server addresses, to the os.conf file in the aliyun_custom_image directory. If the os.conf file does not exist, the system automatically creates it.

    The following sample shows the content of the os.conf file:

    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 sample. Configure them based on your actual environment.

    Parameter

    Description

    hostname

    The hostname of the instance.

    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 subnet mask of the eth0 NIC.

    eth0_gateway

    The IP address of the default gateway for the eth0 NIC.

    eth0_route

    A list of internal routes for eth0. By default, routes are separated by spaces.

    dns_nameserver

    A list of DNS server addresses. By default, addresses are separated by spaces.

  3. Create a parsing script in the image, for example, a service file named customized-config.service, to read and parse the system configurations from the os.conf file.

    This parsing script runs when the ECS instance starts for the first time to automate configurations, such as setting the hostname, root user password, DNS server addresses, and network settings.

    The following sample shows the content of the 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: The initial os-conf job, config the system.
    ### END INIT INFO
    
    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 [[ -f $os_conf_file ]]; then
            . $os_conf_file
            return 0
        else
            return 1
        fi
    }
    
    cleanup() {
        # ensure $os_conf_file is deleted, to avoid repeating config system
        rm $os_conf_file >& /dev/null
        # ensure $os_conf_dir exists
        mkdir -p $os_conf_dir
    }
    
    config_password() {
        if [[ -n $password ]]; then
            password=$(echo $password | base64 -d)
            if [[ $? == 0 && -n $password ]]; then
                echo "root:$password" | chpasswd
            fi
        fi
    }
    
    config_hostname() {
        if [[ -n $hostname ]]; then
            sed -i "s/^HOSTNAME=.*/HOSTNAME=$hostname/" /etc/sysconfig/network
            hostname $hostname
        fi
    }
    
    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
    }
    
    is_classic_network() {
        # vpc: eth0
        # classic: eth0 eth1
        grep -q 'eth1' $os_conf_file
    }
    
    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
    }
    
    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
    }
    
    config_default_gateway() {
        local gateway=$1
        sed -i "s/^GATEWAY=.*/GATEWAY=$gateway/" /etc/sysconfig/network
    }
    
    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() {
        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
    
    case "$1" in
        start)
            start
            RETVAL=$?
        ;;
        *)
            echo "Usage: $0 {start}"
            RETVAL=3
        ;;
    esac
    
    exit $RETVAL
  4. Set the script to run on system startup.

    The following commands are for Ubuntu. Use the appropriate commands for your operating system.

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

    If you create a new custom image from an instance launched from a Customized Linux image, the new image also contains this startup script. Alibaba Cloud executes the script to apply the os.conf settings only during the instance's first boot.

Related documents

After you configure the image, you can select Customized Linux as the OS Version when you import the custom image. For more information, see Import a custom image.