OpenStack Linux 镜像制作-CentOS7.x
1.配置YUM epel源
curl -o /etc/yum.repo.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
2.修改网络信息 /etc/sysconfig/network-scripts/ifcfg-eth0 (删除MAC地址信息),如下:
TYPE=Ethernet
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
NM_CONTROLLED=no
3.删除已生成的网络设备规则,否则制作出来的镜像可能会出现不能上网
rm -rf /etc/udev/rules.d/70-persistent-net.rules
4.增加一行到/etc/sysconfig/network
NOZEROCONF=yes #禁用ZEROCONF路由
5.安装Cloud-init
# yum install -y cloud-utils cloud-init parted
6.修改配置文件 /etc/cloud/cloud.cfg
开头的disable_root和ssh_pwauth 都改成1
users:
- default
disable_root: 1
ssh_pwauth: 1
在cloud_init_modules下新增:- resolv-conf
cloud_init_modeles:
- resolv-conf
- migrator
- bootcmd
- write-files
- growpart
7.设置系统能自动获取openstack指定的hostname和ssh-key
vi /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In constrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
if [ ! -d /root/.ssh ]; then
mkdir -p /root/.ssh
chmod 700 /root/.ssh
fi
# Fetch public key using HTTP
ATTEMPTS=30
FAILED=0
while [ ! -f /root/.ssh/authorized_keys ]; do
curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /tmp/metadata-key 2>/dev/null
if [ $? -eq 0 ]; then
cat /tmp/metadata-key >> /root/.ssh/authorized_keys
chmod 0600 /root/.ssh/authorized_keys
restorecon /root/.ssh/authorized_keys
rm -f /tmp/metadata-key
echo "Successfully retrieved public key from instance metadata"
echo "*****************"
echo "AUTHORIZED KEYS"
echo "*****************"
cat /root/.ssh/authorized_keys
echo "*****************"
curl -f http://169.254.169.254/latest/meta-data/hostname > /tmp/metadata-hostname 2>/dev/null
if [ $? -eq 0 ]; then
TEMP_HOST=`cat /tmp/metadata-hostname`
sed -i "s/^HOSTNAME=.*$/HOSTNAME=$TEMP_HOST/g" /etc/sysconfig/network
/bin/hostname $TEMP_HOST
echo "Successfully retrieved hostname from instance metadata"
echo "*****************"
echo "HOSTNAME CONFIG"
echo "*****************"
cat /etc/sysconfig/network
echo "*****************"
else
echo "Failed to retrieve hostname from instance metadata. This is a soft error so we’ll continue"
fi
rm -f /tmp/metadata-hostname
else
FAILED=$(($FAILED + 1))
if [ $FAILED -ge $ATTEMPTS ]; then
echo "Failed to retrieve public key from instance metadata after $FAILED attempts, quitting"
break
fi
echo "Could not retrieve public key from instance metadata (attempt #$FAILED/$ATTEMPTS), retrying in 5 seconds…"
sleep 5
fi
done
touch /var/lock/subsys/local
给rc.local 读写权限
chmod +x /etc/rc.d/rc.local
8.设置cloud-init相关服务的开机启动
systemctl enable cloud-config.service cloud-final.service cloud-init-local.service cloud-init.service
重启reboot
9.重启后查看cloud-init 相关服务是否启动,如果前面是绿色图标说明正常。
systemctl list-dependencies
10.压缩镜像
压缩镜像是为了让镜像模板小一些,压缩比较大,执行时间耗时很长
执行命令时不要在/var/lib/libvirt/images
# virt-sparsify --compress /var/lib/libvirt/images/CentOS7.5 CentOS7.5.qcow2
作者:一毛
本博客所有文章仅用于学习、研究和交流目的,欢迎非商业性质转载。
不管遇到了什么烦心事,都不要自己为难自己;无论今天发生多么糟糕的事,都不应该感到悲伤。记住一句话:越努力,越幸运。