Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SomeBottle/6e08c213b9a06f068d47f409967f055c to your computer and use it in GitHub Desktop.
Save SomeBottle/6e08c213b9a06f068d47f409967f055c to your computer and use it in GitHub Desktop.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment