Jika versi sistem operasi pada image Linux tidak didukung oleh Elastic Compute Service (ECS) dan cloud-init tidak tersedia, ECS mengidentifikasi image tersebut sebagai sistem operasi yang tidak dikenali. Untuk menggunakan image semacam ini dalam inisialisasi instans, tambahkan skrip inisialisasi ke dalam image sebelum mengimpornya. Saat instans pertama kali boot, ECS menulis konfigurasi instans ke file bernama os.conf. Skrip inisialisasi tersebut membaca file ini dan menerapkan setiap pengaturan secara otomatis.
Image Linux berbasis platform non-standar terbagi menjadi dua kategori, masing-masing dengan alur kerja yang berbeda:
Others Linux: ECS mengidentifikasi image sebagai image Linux generik dan tidak melakukan konfigurasi setelah pembuatan instans. Setelah instans menyala, Anda harus mengonfigurasi alamat IP, entri rute, dan password secara manual.
Customized Linux: Sebelum mengimpor image, ikuti panduan ini untuk menambahkan skrip inisialisasi. ECS menggunakan skrip tersebut untuk mengonfigurasi instans secara otomatis saat boot pertama. Panduan ini hanya berlaku untuk image Customized Linux.
Jika skrip inisialisasi tidak ada atau salah konfigurasi, instans tetap boot, tetapi antarmuka jaringan, hostname, dan password tidak dikonfigurasi. Anda mungkin memerlukan akses konsol untuk memulihkan instans.
Prasyarat
Sebelum memulai, pastikan bahwa:
Partisi pertama (biasanya
/dev/sda1atau/dev/vda1) dapat ditulis.Jenis sistem file pada partisi pertama adalah FAT32, ext2, ext3, ext4, atau UFS — verifikasi dengan perintah
blkid /dev/sdXn(gantisdXndengan nama partisi aktual, misalnyasda1).Ukuran image disk virtual lebih dari 5 GiB — verifikasi dengan perintah
df -h /dev/sdXn.
Mempersiapkan gambar
Langkah 1: Buat direktori konfigurasi
Login ke mesin virtual sebagai pengguna root. Buat direktori aliyun_custom_image di root partisi pertama:
mkdir /aliyun_custom_imageSaat instans pertama kali boot, ECS menulis konfigurasi ke file os.conf di direktori ini. Jika file tersebut belum ada, ECS akan membuatnya. File ini memiliki format berikut:
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"Tabel berikut menjelaskan setiap parameter.
| Parameter | Deskripsi |
|---|---|
hostname | Hostname. |
password | Password pengguna root. |
eth0_ip_addr | Alamat IP NIC eth0. |
eth0_mac_addr | Alamat MAC NIC eth0. |
eth0_netmask | Mask jaringan NIC eth0. |
eth0_gateway | Gerbang default NIC eth0. |
eth0_route | Rute internal eth0. Beberapa rute dipisahkan dengan spasi. |
dns_nameserver | Daftar alamat DNS. Beberapa alamat dipisahkan dengan spasi. |
Langkah 2: Tambahkan skrip inisialisasi
Buat skrip inisialisasi dalam image untuk membaca file os.conf dan menerapkan konfigurasi. Beri nama file skrip tersebut, misalnya, customized-config.service.
Skrip contoh berikut mengatur hostname, password root, server DNS, dan konfigurasi jaringan saat boot pertama. Skrip ini menggunakan framework layanan sysvinit.
#!/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 $RETVALLangkah 3: Aktifkan skrip saat startup
Konfigurasikan skrip inisialisasi agar berjalan otomatis saat sistem startup. Perintah berikut berlaku untuk sistem berbasis Ubuntu dan systemd — sesuaikan dengan sistem init distribusi Anda jika diperlukan.
systemctl daemon-reload
systemctl enable customized-config.serviceSaat Anda membuat custom image berdasarkan image Customized Linux, skrip startup disertakan dalam image baru tersebut. ECS hanya menjalankan konfigurasi os.conf pada boot pertama setiap instans.Langkah berikutnya
Setelah Anda mengonfigurasi image, pilih Customized Linux sebagai OS Version saat mengimpornya ke ECS. Untuk informasi selengkapnya, lihat Impor custom image.