一.CentOS7鏡像制作
1.1創建CentOS7虛擬機
1.1.1創建虛擬磁盤
1 #注:盡量將虛擬機創建在控制節點,以便於將鏡像上傳至glance 2 [root@linux-node1 ~]# qemu-img create -f qcow2 /tmp/centos7.qcow2 10G
1.1.2創建虛擬機
#非橋接網卡 [root@linux-node1 ~]# virt-install \ --virt-type kvm \ --name centos \ --ram 1024 \ --disk /tmp/centos2.qcow2,format=qcow2 \ --network network=default \ --graphics vnc,listen=0.0.0.0 --noautoconsole \ --os-type=linux --os-variant=rhel7 \ --location=/tmp/CentOS-7-x86_64-DVD-1511.iso #橋接網卡 [root@linux-node1 ~]# virt-install \ --virt-type kvm \ --name centos \ --ram 1024 \ --disk /tmp/centos.qcow2,format=qcow2 \ --network bridge=brqce3d2bda-d5 \ --graphics vnc,listen=0.0.0.0 --noautoconsole \ --os-type=linux --os-variant=rhel7 \ --location=/tmp/CentOS-7.2-x86_64-DVD-1511.iso
1.1.3VNC連接虛擬機
#選擇英文語言,下一步
#選擇時區:亞洲上海
#選擇最小化安裝包組
#自定義磁盤分區
#不使用LVM,單機 "+" 添加分區
#只分配一個 “/” 分區,不創建 swap分區,本身就是虛擬機,更影響性能。
#創建完,單機左上角“done”
#開始安裝系統
#給root用戶創建密碼
1.2連接虛擬機並優化
1.2.1安裝常用基礎軟件包
1 [root@example ~]# yum install -y snmap net-tools tree screen wget git vim salt-minion zabbix-agent lsof tcpdump
1.2.2修改網卡信息
1 #刪除UUI、mac等信息,只保留以下內容 2 [root@example ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 3 TYPE=Ethernet 4 BOOTPROTO=dhcp 5 NAME=eth0 6 DEVICE=eth0 7 ONBOOT=yes
1.2.3修改yum源
1 [root@example ~]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
1.2.4關閉Networkemanager和防火牆
1 [root@example ~]# systemctl disable firewalld 2 [root@example ~]# systemctl stop firewalld 3 [root@example ~]# systemctl stop NetworkManager
1.2.5關閉selinux
1 #修改selinux配置文件 2 [root@example ~]# vim /etc/sysconfig/selinux 3 SELINUX=enforcing 改為 SELINUX=disabled 4 #重啟后,檢查結果如下 5 [root@example ~]# getsebool 6 getsebool: SELinux is disabled
1.3編寫metadata腳本
1 [root@example ~]# vim /tmp/init.sh 2 #!/bin/bash 3 4 set_key(){ 5 if [ ! -d /root/.ssh ]; then 6 mkdir -p /root/.ssh 7 chmod 700 /root/.ssh 8 fi 9 for ((i=1;i<=5;i++));do 10 if [ ! -f /root/.ssh/authorized_keys ];then 11 curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /tmp/metadata-key 2>/dev/null 12 if [ $? -eq 0 ];then 13 cat /tmp/metadata-key >> /root/.ssh/authorized_keys 14 chmod 0600 /root/.ssh/authorized_keys 15 restorecon /root/.ssh/authorized_keys 16 rm -f /tmp/metadata-key 17 echo "Successfully retrieved public key from instance metadata" 18 echo "*****************" 19 echo "AUTHORIZED KEYS" 20 echo "*****************" 21 cat /root/.ssh/authorized_keys 22 echo "*****************" 23 fi 24 fi 25 done 26 } 27 28 set_hostname(){ 29 PRE_HOSTNAME=$(curl -s http://169.254.169.254/latest/meta-data/hostname) 30 DOMAIN_NAME=$(echo $PRE_HOSTNAME | awk -F '.' '{print $1}') 31 hostnamectl set-hostname `echo ${DOMAIN_NAME}.example.com` 32 } 33 34 set_static_ip(){ 35 PRE_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4) 36 NET_FILE="/etc/sysconfig/network-scripts/ifcfg-eth0" 37 echo "TYPE=Ethernet" > $NET_FILE 38 echo "BOOTPROTO=static" >> $NET_FILE 39 echo "NAME=eth0" >> $NET_FILE 40 echo "DEVICE=eth0" >> $NET_FILE 41 echo "ONBOOT=yes" >> $NET_FILE 42 echo "IPADDR=${PRE_IP}" >> $NET_FILE 43 echo "NETMASK=255.255.255.0" >> $NET_FILE 44 echo "GATEWAY=192.168.56.2" >> $NET_FILE 45 } 46 47 main(){ 48 set_key; 49 set_hostname; 50 set_static_ip; 51 /bin/cp /tmp/rc.local /etc/rc.d/rc.local 52 reboot 53 } 54 55 main
1.3.1修改rc.local
1 #將rc.local拷貝到tmp下 2 [root@example ~]# cp /etc/rc.d/rc.local /tmp 3 #CentOS7開機執行rc.local,需將rc.local添加執行權限 4 [root@example ~]# chmod +x /etc/rc.d/rc.local 5 #編輯rc.local 6 [root@example ~]# vim /etc/rc.d/rc.local 7 /bin/sh /tmp/init.sh
1.4關機並壓縮鏡像
1 #關機 2 [root@example ~]# init 0 3 #壓縮鏡像,清理磁盤碎片 4 [root@linux-node1 ~]# qemu-img convert -c disk -O qcow2 centos7.qcow2
1.5上傳鏡像至glance
注:鏡像就是一開始創建虛擬機的qcow2的虛擬磁盤
1 [root@linux-node1 ~]# openstack image create "CentOS-7-x86_64" \ 2 --file /tmp/centos7.qcow2 \ 3 --disk-format qcow2 \ 4 --container-format bare \ 5 --public
至此,openstack的鏡像就制作完畢,請根據自己公司優化需求來制定
【開源是一種精神,分享是一種美德】
— By GoodCook
— 筆者QQ:253097001
— 歡迎大家隨時來交流
—原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。