Home >Operation and Maintenance >Linux Operation and Maintenance >How to select Customized Linux when importing a custom image

How to select Customized Linux when importing a custom image

坏嘻嘻
坏嘻嘻Original
2018-09-18 14:59:152610browse

The content of this article is about how to select a customized image when importing a custom image. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Customize Linux custom image

When your image operating system is not among the existing platform types supported by Alibaba Cloud and cloud-init cannot be installed, you can import the custom image Select Customized Linux (customized version image). Alibaba Cloud treats the customized Linux image as an unrecognized operating system type and lacks the standard configuration information necessary for initial startup of an ECS instance. You need to add a parsing script to the customized image as described in this article before importing the image to facilitate initial startup. Automated configuration examples.

Restrictions

The first partition of the Customized Linux image must be writable.

The first partition type of Customized Linux image supports FAT32, EXT2, EXT3, EXT4 or UFS.

The virtual file size of Customized Linux images must be greater than 5 GiB.

Customized Linux images have the following security requirements:

There must be no high-risk vulnerabilities that can be exploited remotely.

When you use the management terminal of the console to log in to an instance, if there is an initial default password, it must be changed when you log in for the first time. You are not allowed to enter the instance to perform any operations before changing the password.

The default SSH key pair is not supported, and the initial SSH key pair must be randomly generated by Alibaba Cloud.

Configuration method

Create a new directory aliyun_custom_image in the root directory of the first partition of the image.

When an instance created using this Customized Linux image is started for the first time, Alibaba Cloud will write instance-related configuration information in the os.conf file in the aliyun_custom_image directory. If the os.conf file does not exist, the system will automatically create it.

Create a parsing script in the image to parse the system configuration of the os.conf file. See Parse Script Considerations and Parse Script Examples for scripting.

os.conf file example

Classic network type example

hostname=iZ23r29djmjZ
password=cXdlcjEyMzQK
eth0_ip_addr=10.171.254.123
eth0_mac_addr=00:8c:fa:5e:14:23
eth0_netmask=255.255.255.0
eth0_gateway=10.171.254.1
eth0_route="10.0.0.0/8 10.171.254.1;172.16.0.0/12 10.171.254.1"
eth1_ip_addr=42.120.74.105
eth1_mac_addr=00:8c:fa:5e:14:24
eth1_netmask=255.255.255.0
eth1_gateway=42.120.74.1
eth1_route="0.0.0.0/0 42.120.74.1"
dns_nameserver="7.7.7.7 8.8.8.8"

Parameter description is as shown in the following table:

How to select Customized Linux when importing a custom image

Private network VPC type instance

hostname=iZ23r29djmjZ
password=cXdlcjEyMzQK
eth0_ip_addr=10.171.254.123
eth0_mac_addr=00:8c:fa:5e:14:23
eth0_netmask=255.255.255.0
eth0_gateway=10.171.254.1
eth0_route="0.0.0.0/0 10.171.254.1"
dns_nameserver="7.7.7.7 8.8.8.8"

The parameter description is as shown in the following table:

How to select Customized Linux when importing a custom image

Notes on parsing scripts

When the instance is started for the first time, under normal circumstances Alibaba Cloud automatically writes the relevant information of the configuration items to the root of the first partition. In the os.conf file of the aliyun_custom_image directory under the directory. To configure a Customized Linux image, you must create a predefined parsing script in the image to read the instance configuration information from the os.conf file and complete the instance configuration. The following are the conditions that need to be met to parse the script.

Start at boot: The parsing script needs to be set to start automatically at boot. For example, store the parsing script in the /etc/init.d/ directory.

Configuration item value rules: As described in the configuration items of the os.conf file example, the number of configuration items and the value rules of some configuration items are different between VPC and classic network instances.

Configuration file reading path: When creating an I/O optimized instance or a non-I/O optimized instance for a Customized Linux image, the device name assigned to the first partition is different by default. Therefore, it is best to use uuid or label to identify the device of the first partition in the parsing script. The user password is a Base64-encoded string, and related processing needs to be done when setting the password.

Determine VPC or classic network: When parsing the script to determine the network type, you can check whether there is eth1_route or other eth1-related configuration items. Determine the network type of the current instance and then analyze and process it in a targeted manner.

VPC type instances configure the default public network route in the eth0_route parameter of the os.conf file.

For classic network type instances, the default public network route is configured in the eth1_route parameter of the os.conf file, and the internal network route is configured in eth0_route.

Configuration optimization: The configuration in the os.conf file can be executed once during the entire life cycle of the instance. It is recommended to delete the os.conf file after the parsing script is successfully executed. At the same time, if the parsing script does not read the os.conf file configuration, it will not execute the configuration in the file.

Custom image processing: When making a custom image based on an instance created by a Customized Linux image, the image will also include this boot script. Alibaba Cloud will write the os.conf configuration when the instance is started for the first time, and the parsing script can execute the relevant configuration when it detects the configuration.

修改相关配置时的处理:当实例的配置信息通过阿里云的控制台或 API 发生变更时,阿里云将相关信息写入到 os.conf 文件中,解析脚本将被再次执行从而下发这些更改。

解析脚本示例

以下为以 CentOS 操作系统为例的解析脚本示例,仅供参考,您需要根据实际的操作系统类型调整脚本内容。在使用脚本前,务必在镜像中调试脚本,并保证调试通过。

#!/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 is exitst
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
inteface_cfg="/etc/sysconfig/network-scripts/ifcfg-${interface}"
cat << EOF > $inteface_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 &#39;s/;/\n/&#39; | \
while read line; do
dst=$(echo $line | awk &#39;{print $1}&#39;)
gw=$(echo $line | awk &#39;{print $2}&#39;)
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

The above is the detailed content of How to select Customized Linux when importing a custom image. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn