您好,欢迎访问一九零五行业门户网

如何导入自定义镜像时选择 Customized Linux(定制版镜像)

本篇文章给大家带来的内容是关于如何导入自定义镜像时选择定制版镜像,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
定制 linux 自定义镜像
当您的镜像操作系统不在阿里云所支持的已有平台类型中,亦不能安装 cloud-init 时,您可以在导入自定义镜像时选择 customized linux(定制版镜像)。阿里云将定制版 linux 镜像当作无法识别的操作系统类型,缺少初次启动ecs实例时所必要的标准配置信息,需要您导入镜像前根据本文描述在定制版镜像中添加解析脚本,便于初次启动时自动化配置实例。
限制条件
customized linux 镜像的第一个分区必须可以被写入。
customized linux 镜像的第一个分区类型支持 fat32、ext2、ext3、ext4 或 ufs。
customized linux 镜像的虚拟文件大小必须大于 5 gib。
customized linux 镜像有如下安全要求:
不能存在可被远程利用的高危漏洞。
使用控制台的 管理终端 登录实例时,如果存在初始默认密码,必须在首次登录时修改,修改密码之前不允许进入实例做任何操作。
不支持默认 ssh 密钥对,初始 ssh 密钥对必须由阿里云随机生成。
配置方法
在镜像的第一个分区的根目录下新建目录 aliyun_custom_image。
使用该 customized linux 镜像所创建的实例初次启动时,阿里云会在 aliyun_custom_image 目录的 os.conf 文件中写入实例相关配置信息。如果不存在 os.conf 文件,系统则自动创建。
在镜像中创建一份解析脚本,用以解析 os.conf 文件的系统配置。参阅 解析脚本注意事项 和 解析脚本示例编写脚本。
os.conf 文件示例
经典网络类型实例
hostname=iz23r29djmjzpassword=cxdlcjeymzqketh0_ip_addr=10.171.254.123eth0_mac_addr=00:8c:fa:5e:14:23eth0_netmask=255.255.255.0eth0_gateway=10.171.254.1eth0_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.105eth1_mac_addr=00:8c:fa:5e:14:24eth1_netmask=255.255.255.0eth1_gateway=42.120.74.1eth1_route="0.0.0.0/0 42.120.74.1"dns_nameserver="7.7.7.7 8.8.8.8"
参数说明如下表所示:
专有网络 vpc 类型实例
hostname=iz23r29djmjzpassword=cxdlcjeymzqketh0_ip_addr=10.171.254.123eth0_mac_addr=00:8c:fa:5e:14:23eth0_netmask=255.255.255.0eth0_gateway=10.171.254.1eth0_route="0.0.0.0/0 10.171.254.1"dns_nameserver="7.7.7.7 8.8.8.8"
参数说明如下表所示:
解析脚本注意事项
实例初次启动时,正常情况下阿里云自动将配置项的相关信息写入第一个分区的根目录下 aliyun_custom_image 目录的 os.conf 文件中。配置 customized linux 镜像必须要在镜像中创建预定义解析脚本,用以从 os.conf 文件中读取实例配置信息并完成实例配置。以下为解析脚本需要满足的条件。
开机启动:解析脚本需要设置成开机自启动,例如,将解析脚本存放在 /etc/init.d/ 目录下。
配置项取值规则:如 os.conf 文件示例 的配置项所述,vpc 与经典网络实例的配置项数量和部分配置项的取值规则均有所不同。
配置文件读取路径:customized linux 镜像在创建 i/o 优化实例或非 i/o 优化实例时,为第一个分区所分配的设备名默认不一样。所以在解析脚本中最好可以用 uuid 或 label 识别第一个分区的设备。用户密码为 base64 编码的字符串,设置密码时需要做相关处理。
判断 vpc 或经典网络:解析脚本判断该网络类型时,可以查看是否存在 eth1_route 或其他 eth1 相关的配置项。判断出当前实例的网络类型后再有针对性地解析和处理。
vpc 类型实例在 os.conf 文件的 eth0_route 参数中配置默认公网路由。
经典网络类型实例在 os.conf 文件的 eth1_route 参数中配置默认公网路由,内网路由配置在 eth0_route 中。
配置优化:os.conf 文件中的配置在实例的整个生命周期中执行一次即可,解析脚本执行成功后建议删除 os.conf 文件。同时解析脚本如果没有读取到 os.conf 文件配置,则不执行文件中的配置。
自定义镜像处理:根据 customized linux 镜像创建的实例再制作自定义镜像时,镜像中也会包含这个开机启动脚本。阿里云会在实例第一次启动时写入 os.conf 配置,解析脚本在检测到该配置时即可执行相关配置。
修改相关配置时的处理:当实例的配置信息通过阿里云的控制台或 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 infofirst_partition_dir='/boot/'os_conf_dir=${first_partition_dir}/aliyun_custom_imageos_conf_file=${os_conf_dir}/os.confload_os_conf() {if [[ -f $os_conf_file ]]; then. $os_conf_filereturn 0elsereturn 1fi}cleanup() {# ensure $os_conf_file is deleted, to avoid repeating config systemrm $os_conf_file >& /dev/null# ensure $os_conf_dir is exitstmkdir -p $os_conf_dir}config_password() {if [[ -n $password ]]; thenpassword=$(echo $password | base64 -d)if [[ $? == 0 && -n $password ]]; thenecho "root:$password" | chpasswdfifi}config_hostname() {if [[ -n $hostname ]]; thensed -i "s/^hostname=.*/hostname=$hostname/" /etc/sysconfig/networkhostname $hostnamefi}config_dns() {if [[ -n $dns_nameserver ]]; thendns_conf=/etc/resolv.confsed -i '/^nameserver.*/d' $dns_conffor i in $dns_nameserver; doecho "nameserver $i" >> $dns_confdonefi}is_classic_network() {# vpc: eth0# classic: eth0 eth1grep -q 'eth1' $os_conf_file}config_network() {/etc/init.d/network stopconfig_interface eth0 ${eth0_ip_addr} ${eth0_netmask} ${eth0_mac_addr}config_route eth0 ${eth0_route}if is_classic_network ; thenconfig_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=$1local ip=$2local netmask=$3local mac=$4inteface_cfg="/etc/sysconfig/network-scripts/ifcfg-${interface}"cat << eof > $inteface_cfgdevice=$interfaceipaddr=$ipnetmask=$netmaskhwaddr=$maconboot=yesbootproto=staticeof}config_default_gateway() {local gateway=$1sed -i "s/^gateway=.*/gateway=$gateway/" /etc/sysconfig/network}config_route() {local interface=$1local route=$2route_conf=/etc/sysconfig/network-scripts/route-${interface}> $route_confecho $route | sed 's/;/\n/' | \while read line; dodst=$(echo $line | awk '{print $1}')gw=$(echo $line | awk '{print $2}')if ! grep -q "$dst" $route_conf 2> /dev/null; thenecho "$dst via $gw dev $interface" >> $route_conffiif [[ "$dst" == "0.0.0.0/0" ]]; thenconfig_default_gateway $gwfidone}################## sysvinit service portal ####################start() {if load_os_conf ; thenconfig_passwordconfig_networkconfig_hostnameconfig_dnscleanupreturn 0elseecho "not load $os_conf_file"return 0fi}retval=0case "$1" in start) start retval=$? ;; *) echo "usage: $0 {start}" retval=3 ;;esacexit $retval
以上就是如何导入自定义镜像时选择 customized linux(定制版镜像)的详细内容。
其它类似信息

推荐信息