欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自制openstack centos7.8镜像

程序员文章站 2022-03-11 22:50:55
...

默认密码:000000,可以在下方c7op.ks文件中修改默认密码为其他。
安装KVM环境:(宿主机)

[[email protected] ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[[email protected] ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[[email protected] ~]# yum install -y kvm virt-*  libvirt  bridge-utils qemu-img qemu-kvm-tools
[[email protected] ~]# systemctl start libvirtd
[[email protected] ~]# systemctl enable libvirtd
[[email protected] ~]# echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf
[[email protected] ~]# sysctl -p

定义文件变量:(宿主机)

[[email protected] ~]# mkdir /kvm
[[email protected] ~]# ksname=c7op.ks
[[email protected] ~]# kvname=centos7-8
[[email protected] ~]# ISO=CentOS-7-x86_64-Everything-2003

编写ks文件与移动镜像:(宿主机)

[[email protected] ~]# cd /kvm
[[email protected] ~]# vi c7op.ks            #ks文件在最页面最下方
[[email protected] ~]# mv CentOS-7-x86_64-Everything-2003 /kvm

创建KVM虚拟机:(宿主机)

[[email protected] ~]# qemu-img create -f qcow2 /kvm/$kvname.qcow2 5G
[[email protected] ~]# virt-install --name $kvname --ram 2048 --vcpus 2 --os-type linux --os-variant rhel7 --arch=x86_64 --network network=default,model=virtio --disk path=/kvm/$kvname.qcow2,format=qcow2 \
 --location /kvm/$ISO --console pty,target_type=serial --initrd-inject=/kvm/$ksname --extra-args="console=ttyS0,115200n8 edd=off serial ks=file:/$ksname" --graphics none

重启虚拟机并连接:(宿主机)
# 安装完成后,按键Ctrl + ] 退出kvm虚拟机
# 密码在c7op.ks文件提前设置的

[[email protected] ~]# virsh destroy $kvname #强制关闭电源
[[email protected] ~]# cp /kvm/$kvname.qcow2{,.bak} #备份
[[email protected] ~]# ls -hl /kvm/$kvname*
[[email protected] ~]# virsh start   $kvname #开启
[[email protected] ~]# virsh console $kvname #登录

时区配置:(虚拟机)

[[email protected] ~]# rm -f /etc/localtime
[[email protected] ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[[email protected] ~]# ll /etc/localtime

使用阿里源:(虚拟机)

[[email protected] ~]# [[ `uname -r` == *el6* ]] && { Ve=6; } || { Ve=7; }
[[email protected] ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-$Ve.repo
[[email protected] ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-$Ve.repo
[[email protected] ~]# sed -i '/aliyuncs.com/d' /etc/yum.repos.d/*.repo   # 删除阿里内网地址#
[[email protected] ~]# yum clean all

安装插件包:(虚拟机)

[[email protected] ~]# yum install -y acpid cloud-init cloud-utils jq parted qemu-guest-agent
[[email protected] ~]# [[ `uname -r` == *el6* ]] && { yum install -y dracut-modules-growroot; }

cloud-int开启root密码登录:(虚拟机)

[[email protected] ~]# sed -i 's/disable_root: 1/disable_root: 0/g' /etc/cloud/cloud.cfg
[[email protected] ~]# sed -i 's/ssh_pwauth:   0/ssh_pwauth:   1/g' /etc/cloud/cloud.cfg
[[email protected] ~]# echo 'NETWORKING=yes
NOZEROCONF=yes'>/etc/sysconfig/network

SSH配置:(虚拟机)

[[email protected] ~]# sed  -i 's/^PasswordAu.*$/PasswordAuthentication yes/' /etc/ssh/sshd_config
[[email protected] ~]# sed  -i 's/^GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
[[email protected] ~]# sed  -i 's/^#UseDNS.*$/UseDNS no/' /etc/ssh/sshd_config
[[email protected] ~]# systemctl restart sshd

开机启动软件:(虚拟机)

[[email protected] ~]# systemctl enable acpid cloud-init qemu-guest-agent
[[email protected] ~]# sed -i 's/timeout=5/timeout=2/g' /boot/grub2/grub.cfg

清理垃圾数据:(虚拟机)

[[email protected] ~]# yum clean all
[[email protected] ~]# rm -rf /tmp/*
[[email protected] ~]# rm -rf /root/*
[[email protected] ~]# rm -f /var/log/wtmp /var/log/btmp
[[email protected] ~]# >/var/log/cloud-init.log

删除网卡信息:(虚拟机)

[[email protected] ~]# sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth*
[[email protected] ~]# sed -i '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth*
[[email protected] ~]# >/etc/udev/rules.d/7*.rules
[[email protected] ~]# history -c
[[email protected] ~]# poweroff #关机

清理任务后压缩文件:(宿主机)

[[email protected] ~]# virt-sysprep -d $kvname
[[email protected] ~]# virt-sparsify --compress /kvm/$kvname.qcow2 /kvm/$kvname-one.qcow2

上传镜像到OpenStack:(控制节点)

[[email protected] ~]# openstack image create "centos7.8"  --file centos7-8-one.qcow2 --disk-format qcow2 --container-format bare --progress</kvm/centos7-8-one.qcow2

c7op.ks文件内容:
[[email protected] ~]# cat /kvm/c7op.ks

# centos 7 自动安装脚本
# openstack镜像制作,centos7自动安装ks脚本
# 使用实例
#by edison zhou

install
cdrom

text
keyboard us
lang en_US.UTF-8
authconfig --enableshadow --passalgo=sha512
rootpw 000000
skipx
timezone Asia/Shanghai --isUtc
firewall --enabled --port=22:tcp
selinux --disabled
firewall --disabled

#设置网络
bootloader --location=mbr  --driveorder=sda,sdb  #--append="net.ifnames=0 biosdevname=0"
network  --bootproto=dhcp   --onboot=yes --nameserver=8.8.8.8
network  --hostname=CentOS7

#分区
zerombr
clearpart --all --initlabel
#part /boot --asprimary --fstype="ext4" --size=300 --ondrive=sda
#part swap --fstype="swap" --size=2048
part / --asprimary --fstype=xfs --grow --size=1

reboot

#安装的软件包
%packages
@base
@core
@fonts
net-tools
ntp
tree
wget
vim
openssh
%end

%post
%end

提醒:在openstack中创建实例时可能会进入Probing EDD (edd=off to disable)…不用担心,耐心等待片刻就可以进去,如有大神发现解决方案,请私信或留言。