OpenStack鏡像製做-CentOS

雲平臺中鏡像仍是很重要的,提供各類定製化的鏡像使得用戶體驗更好。html

最開始玩OpenStack的時候用的是安裝文檔中提到的cirros,其密碼cubswin:) 剛開始感受很怪,如今已經能夠隨手打出。ps:打的還很熟練:-)git

而後慢慢開始想嘗試各類鏡像,因而乎在網上搜了不少。以下:github

關於CentOS鏡像製做須要注意如下幾點:centos

(1)修改網絡信息 /etc/sysconfig/network-scripts/ifcfg-eth0 (刪掉mac信息),以下:網絡

TYPE=Ethernet DEVICE=eth0 ONBOOT=yes BOOTPROTO=dhcp NM_CONTROLLED=no

(2)刪除已生成的網絡設備規則,不然製做的鏡像不能上網dom

# rm -rf /etc/udev/rules.d/70-persistent-net.rules 

(3)增長一行到/etc/sysconfig/networkssh

NOZERCONF=yes

(4)安裝cloud-init(可選),cloud-init能夠在開機時進行密鑰注入以及修改hostname等,關於cloud-init,陳沙克的一篇博文有介紹:http://www.chenshake.com/about-openstack-centos-mirror/curl

# yum install -y cloud-utils cloud-init parted
修改配置文件/etc/cloud/cloud.cfg ,在cloud_init_modules 下面增長:
- resolv-conf

(5)設置系統能自動獲取openstack指定的hostname和ssh-key(可選)
編輯/etc/rc.local文件,該文件在開機後會執行,加入如下代碼:ide

 1 if [ ! -d /root/.ssh ]; then
 2 mkdir -p /root/.ssh
 3 chmod 700 /root/.ssh
 4 fi
 5 # Fetch public key using HTTP  6 ATTEMPTS=30
 7 FAILED=0
 8 
 9  
10 
11 while [ ! -f /root/.ssh/authorized_keys ]; do
12 curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /tmp/metadata-key 2>/dev/null
13 if [ $? -eq 0 ]; then
14 cat /tmp/metadata-key >> /root/.ssh/authorized_keys 15 chmod 0600 /root/.ssh/authorized_keys 16 restorecon /root/.ssh/authorized_keys 17 rm -f /tmp/metadata-key 18 echo 「Successfully retrieved public key from instance metadata」 19 echo 「*****************20 echo 「AUTHORIZED KEYS」 21 echo 「*****************22 cat /root/.ssh/authorized_keys 23 echo 「*****************24 
25 curl -f http://169.254.169.254/latest/meta-data/hostname > /tmp/metadata-hostname 2>/dev/null
26 if [ $? -eq 0 ]; then
27 TEMP_HOST=`cat /tmp/metadata-hostname` 28 sed -i 「s/^HOSTNAME=.*$/HOSTNAME=$TEMP_HOST/g」 /etc/sysconfig/network 29 /bin/hostname $TEMP_HOST 30 echo 「Successfully retrieved hostname from instance metadata」 31 echo 「*****************32 echo 「HOSTNAME CONFIG」 33 echo 「*****************34 cat /etc/sysconfig/network 35 echo 「*****************36 
37 else
38 echo 「Failed to retrieve hostname from instance metadata. This is a soft error so we’ll continue」 39 fi
40 rm -f /tmp/metadata-hostname
41 else
42 FAILED=$(($FAILED + 1)) 43 if [ $FAILED -ge $ATTEMPTS ]; then
44 echo 「Failed to retrieve public key from instance metadata after $FAILED attempts, quitting」 45 break 46 fi
47 echo 「Could not retrieve public key from instance metadata (attempt #$FAILED/$ATTEMPTS), retrying in 5 seconds…」 48 sleep 5
49 fi
50 done 

或者ui

 1 # set a random pass on first boot  2 if [ -f /root/firstrun ]; then
 3   dd if=/dev/urandom count=50|md5sum|passwd --stdin root  4   passwd -l root  5   rm /root/firstrun  6 fi
 7 
 8 if [ ! -d /root/.ssh ]; then
 9   mkdir -m 0700 -p /root/.ssh
10   restorecon /root/.ssh
11 fi
12 # Get the root ssh key setup 13 # Get the root ssh key setup 14 ReTry=0
15 while [ ! -f /root/.ssh/authorized_keys ] && [ $ReTry -lt 10 ]; do
16   sleep 2
17   curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /root/.ssh/pubkey
18   if [ 0 -eq 0 ]; then
19     mv /root/.ssh/pubkey /root/.ssh/authorized_keys 20   fi
21   ReTry=$[Retry+1] 22 done
23 chmod 600 /root/.ssh/authorized_keys && restorecon /root/.ssh/authorized_keys

主要目的就是獲取hostname和公鑰

 (6)其餘

route命令查看一下路由表

查看/etc/ssh/sshd_conf中PermitRootLogin是否是爲yes

相關文章
相關標籤/搜索