# set the root password using user data
STATUS_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code} `
if [ "$STATUS_CODE" -eq "200" ]; then
PASS=`curl -m 10 -s | awk -F '"' '{for(i=1;i<=NF;i++){if($i ~ /password/) print $(i+2)}}'`
if [ "$PASS" != " " ]; then
/usr/bin/echo "root:${PASS}" > tmp.txt
/usr/sbin/chpasswd < tmp.txt
rm -f tmp.txt
fi
fi
# set the hostname using the meta-data service
STATUS_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code} `
if [ "$STATUS_CODE" -eq "200" ]; then
curl -f > /tmp/metadata-hostname 2>/dev/null
if [ $? -eq 0 ]; then
TEMP_HOST=`cat /tmp/metadata-hostname | awk -F '.novalocal' '{print $1}'`
/usr/bin/hostnamectl set-hostname ${TEMP_HOST}
/usr/bin/hostname $TEMP_HOST
rm -f /tmp/metadata-hostname
fi
fi
# get the user ssh key using the meta-data service
STATUS_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code} `
if [ "$STATUS_CODE" -eq "200" ]; then
mkdir -p /root/.ssh
/usr/bin/echo >> /root/.ssh/authorized_keys
curl -m 10 -s | grep 'ssh-rsa' >> /root/.ssh/authorized_keys
chmod 0700 /root/.ssh
chmod 0600 /root/.ssh/authorized_keys
fi
8.设置开机启动
coreOS使用systemd管理启动项,关于systemd的介绍:
新建一个开机启动配置文件cloudinit.service
cd /etc/systemd/system
#cat cloudinit.service
[Unit]
Description=OpenStack nova
Requires=coreos-setup-environment.service
After=coreos-setup-environment.service
Before=user-config.target
[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=-/etc/environment
ExecStart=/usr/bin/bash /etc/cloud-init.sh #执行的脚本文件cloud-init.sh
[Install]
WantedBy=multi-user.target
#加入systemd管理(设置开机启动)
systemctl enable cloudinit.service
#执行enable之后,cloudinit.service这个服务就会开机启动,从而我们的脚本cloud-init.sh就可以执行
#验证cloudinit.service服务是否设为开机启动
systemctl is-enabled cloudinit