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
5.安装Cloud-init
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 |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if [ ! -d /root/.ssh ]; then |
| mkdir -p /root/.ssh |
| chmod 700 /root/.ssh |
| fi |
| |
| 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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?