Forcibly Importing Image

Last updated: 2024-06-02 10:12:31

Scenario

When a user's Linux image cannot install cloud-init for some reason, the forced image import feature can be used to complete the image import. Since the forcibly imported image does not have cloud-init installed, Tencent Cloud cannot initialize the configuration for the user's CVM. Therefore, when using a forcibly imported image, users need to set up scripts according to the configuration files provided by Tencent Cloud to configure the CVM. This document guides users on how to configure a CVM under the premise of forcibly importing an image.
Tencent Cloud provides a CD-ROM device containing configuration information for users to configure themselves. Users need to mount the CD-ROM and read the information in mount_point/qcloud_action/os.conf for configuration. If users need to use other configuration data or UserData, they can directly read the files under mount_point/.

os.conf Configuration File

The content of os.conf is as follows.
hostname=VM_10_20_xxxx
password=GRSgae1fw9frsG.rfrF
eth0_ip_addr=10.104.62.201
eth0_mac_addr=52:54:00:E1:96:EB
eth0_netmask=255.255.192.0
eth0_gateway=10.104.0.1
dns_nameserver="10.138.224.65 10.182.20.26 10.182.24.12"

Note
The parameter names above are for reference only, and the parameter values are for illustration purposes.
The description of each parameter in the os.conf configuration file is as follows:
Parameter name
Description
hostname
Host name
password
Encrypted password
eth0_ip_addr
Local area network IP of eth0 network interface
eth0_mac_addr
MAC address of the eth0 network interface
eth0_netmask
Subnet mask of eth0 network interface
eth0_gateway
Gateway for eth0 network interface
dns_nameserver
DNS Resolution Server

Limits

The image still needs to meet the requirements for Linux image import in Importing Images (excluding cloud-init).
The system partition for importing the image is not full.
The imported image contains no vulnerability that can be exploited remotely.
We recommend you change the password immediately after the instance is created successfully with the forcibly imported image.

Supports and Limits

Note the following when configuring script parsing:
The script is executed automatically at startup. Please implement this requirement based on your operating system.
The script must mount /dev/cdrom and read the qcloud_action/os.conf file under the mount point to obtain configuration information.
The password placed in the CD-ROM by Tencent Cloud is encrypted. Users can set it using chpasswd -e. Since the encrypted password may contain special characters, it is recommended to place it in a file first and then set it using chpasswd -e < passwd_file.
When creating an image from an instance made using a forcibly imported image, ensure that the script is still executed to guarantee the correct configuration of the instance. Alternatively, you can install cloud-init within the instance.

Instructions

Note
Tencent Cloud provides a sample script based on CentOS. Users can create a configuration script for their own image according to the sample script. During the creation process, please pay attention to the following points:
Place the script correctly in the system before importing the image.
This script may not be suitable for all operating systems. Users need to make appropriate modifications according to their own operating system to meet the requirements.
1. Create the os_config script based on the following example. Users can modify the os_config script according to their actual situation.
#!/bin/bash
### BEGIN INIT INFO
# Provides: os-config
# 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: config of os-init job
# Description: run the config phase without cloud-init
### END INIT INFO
###################user settings#####################
cdrom_path=blkid -L config-2
load_os_config() {
mount_path=$(mktemp -d /mnt/tmp.XXXX)
mount /dev/cdrom $mount_path
if [[ -f $mount_path/qcloud_action/os.conf ]]; then
. $mount_path/qcloud_action/os.conf
if [[ -n $password ]]; then
passwd_file=$(mktemp /mnt/pass.XXXX)
passwd_line=$(grep password $mount_path/qcloud_action/os.conf)
echo root:${passwd_line#*=} > $passwd_file
fi
return 0
else
return 1
fi
}
cleanup() {
umount /dev/cdrom
if [[ -f $passwd_file ]]; then
echo $passwd_file
rm -f $passwd_file
fi
if [[ -d $mount_path ]]; then
echo $mount_path
rm -rf $mount_path
fi
}
config_password() {
if [[ -f $passwd_file ]]; then
chpasswd -e < $passwd_file
fi
}
config_hostname(){
if [[ -n $hostname ]]; then
sed -i "/^HOSTNAME=.*/d" /etc/sysconfig/network
echo "HOSTNAME=$hostname" >> /etc/sysconfig/network
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
}
config_network() {
/etc/init.d/network stop
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
IPADDR=$eth0_ip_addr
NETMASK=$eth0_netmask
HWADDR=$eth0_mac_addr
ONBOOT=yes
GATEWAY=$eth0_gateway
BOOTPROTO=static
EOF
if [[ -n $hostname ]]; then
sed -i "/^${eth0_ip_addr}.*/d" /etc/hosts
echo "${eth0_ip_addr} $hostname" >> /etc/hosts
fi
/etc/init.d/network start
}
config_gateway() {
sed -i "s/^GATEWAY=.*/GATEWAY=$eth0_gateway" /etc/sysconfig/network
}
###################init#####################
start() {
if load_os_config ; then
config_password
config_hostname
config_dns
config_network
cleanup
exit 0
else
echo "mount ${cdrom_path} failed"
exit 1
fi
}
RETVAL=0
case "$1" in
start)
start
RETVAL=$?
;;
*)
echo "Usage: $0 {start}"
RETVAL=3
;;
esac
exit $RETVAL
2. Place the os_config script in the /etc/init.d/ directory and run the following command.
chmod +x /etc/init.d/os_config
chkconfig --add os_config
3. Run the following command to check whether os_config has been added to the startup services.
chkconfig --list
Note
Users need to ensure the correct execution of the script. If issues such as inability to connect to the instance via SSH or lack of network connection are encountered after importing the image, try connecting to the instance through the console and re-executing the script to troubleshoot the problem. If the issue persists, please contact customer support.