openstack pike + ceph +高可用集成 -- 镜像制作 (十四)
#openstack centos7 镜像制作
#环境配置
#Centos7.4系统
#cpu 4 core
#ram 4 G
####################################
#配置可以连外网的网络
#关闭防火墙
echo '关闭selinux、防火墙' systemctl stop firewalld.service systemctl disable firewalld.service firewall-cmd --state sed -i '/^SELINUX=.*/c SELINUX=disabled' /etc/selinux/config sed -i 's/^SELINUXTYPE=.*/SELINUXTYPE=disabled/g' /etc/selinux/config grep --color=auto '^SELINUX' /etc/selinux/config setenforce 0
#配置yum源
mkdir remark && mv *.repo remark/
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo sed -i '/aliyuncs.com/d' /etc/yum.repos.d/*.repo #删除阿里内网地址#
##生成 阿里云源文件
echo " #阿里源 # [Aliyun-pike] name=Aliyun-pike baseurl=https://mirrors.aliyun.com/centos/$releasever/cloud/$basearch/openstack-pike/ gpgcheck=0 enabled=1 cost=88 [Aliyun-qemu-ev] name=Aliyun-qemu-ev baseurl=https://mirrors.aliyun.com/centos/$releasever/virt/$basearch/kvm-common/ https://mirrors.aliyun.com/centos gpgcheck=0 enabled=1 ">/etc/yum.repos.d/ali.repo
yum clean all && yum makecache #清理缓存 生成缓存
#升级系统 重启
yum update -y && reboot
#安装KVM环境 yum install -y libvirt bridge-utils kvm virt-* qemu-img qemu-kvm-tools
#安装完成后 需要重启一下,有时候 libvirt服务无法启动
reboot
##查看 libvirt服务是否启动
systemctl status libvirtd
#没起来启动一下
#systemctl start libvirtd
#systemctl enable libvirtd
##创建环境目录
mkdir -p /kvm/img /kvm/iso
#使用上传工具 xftp scp工具上传系统镜像到 /kvm/iso目录下
#创建KVm虚拟机
########centos7 mini, ks kvname=centos7.4 ksname=centos7.ks ISO=CentOS-7-x86_64-Minimal-1708.iso
#####生成 自动化 ks文件
echo " install cdrom text keyboard us lang en_US.UTF-8 authconfig --enableshadow --passalgo=sha512 rootpw 123456 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 %end %post # add user #useradd -g wheel admin -d/home/admin -m #echo 123456|passwd admin --stdin %end ">/kvm/iso/$ksname
qemu-img create -f qcow2 /kvm/img/$kvname.qcow2 2G #创建一个镜像文件 virt-install \ --name $kvname \ --ram 1024 --vcpus 1 \ --os-type linux --os-variant rhel6 --arch=x86_64 \ --network network=default,model=virtio \ --disk path=/kvm/img/$kvname.qcow2,format=qcow2 \ --location /kvm/iso/$ISO \ --console pty,target_type=serial \ --initrd-inject=/kvm/iso/$ksname \ --extra-args="console=ttyS0,115200n8 serial ks=file:/$ksname" \ --graphics none #安装完成后,按键Ctrl + ] 退出kvm虚拟机 virsh destroy $kvname #强制关闭电源 cp /kvm/img/$kvname.qcow2{,.bak} #备份 ls -hl /kvm/img/$kvname* virsh start $kvname #开启 virsh console $kvname #登录 #################################### #kvm 虚拟机配置 #更改root密码 ##echo 123456|passwd root --stdin #使用阿里源# [[ `uname -r` == *el6* ]] && { Ve=6; } || { Ve=7; } curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-$Ve.repo curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-$Ve.repo sed -i '/aliyuncs.com/d' /etc/yum.repos.d/*.repo #删除阿里内网地址# yum clean all #安装 yum install -y acpid cloud-init cloud-utils jq parted qemu-guest-agent [[ `uname -r` == *el6* ]] && { yum install -y dracut-modules-growroot; } #cloud-int,开启root密码登录# sed -i 's/disable_root: 1/disable_root: 0/g' /etc/cloud/cloud.cfg sed -i 's/ssh_pwauth: 0/ssh_pwauth: 1/g' /etc/cloud/cloud.cfg #sed -i 's/name: centos/name: root/g' /etc/cloud/cloud.cfg #grub启动项添加console=ttyS0,115200n8,安装kvm时已配置# echo 'NETWORKING=yes NOZEROCONF=yes'>/etc/sysconfig/network #ssh配置# sed -i 's/^PasswordAu.*$/PasswordAuthentication yes/' /etc/ssh/sshd_config sed -i 's/^GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config sed -i 's/^#UseDNS.*$/UseDNS no/' /etc/ssh/sshd_config [[ $Ve = 6 ]] && { service sshd restart; } || { systemctl restart sshd; } #开机启动# [[ $Ve = 6 ]] && { chkconfig acpid on chkconfig cloud-init on chkconfig qemu-ga on sed -i 's/timeout=5/timeout=2/g' /boot/grub/grub.conf } || { systemctl enable acpid cloud-init qemu-guest-agent sed -i 's/timeout=5/timeout=2/g' /boot/grub2/grub.cfg }
##关闭 Centos系统自带NetworkManager 服务
systemctl stop NetworkManager #临时关闭
systemctl disable NetworkManager #永久关闭网络管理命令
#根据需求,安装配置其它服务#
#安装 自己的组件环境吧
####################### #清理# yum clean all rm -rf /tmp/* rm -rf /root/* rm -f /var/log/wtmp /var/log/btmp >/var/log/cloud-init.log #删除网卡信息# sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth* sed -i '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth* #>/etc/sysconfig/network-scripts/ifcfg-eth* >/etc/udev/rules.d/7*.rules #清空历史记录 history -c poweroff #关机 #################################### #yum install libvirt libguestfs-tools #virsh destroy kvm #显示kvm所有虚拟机 virsh list --all #执行清理任务 virt-sysprep -d $kvname #压缩 #cd /kvm/img/ virt-sparsify --compress $kvname.qcow2 $kvname-cloud.qcow2 ls -hl /kvm/img/$kvname* #################################### # #拷贝镜像 #前面集成了 ceph 所有上传镜像需要用 raw格式上传 #上传glance镜像 #centos7 openstack image create "centos7.4" \ --file centos7.4-cloud.qcow2 \ --disk-format raw --container-format bare \ --property hw_qemu_guest_agent=yes \ --public # #################################### #kvm还原 virsh destroy $kvname rm -f $kvname.qcow2 $kvname-cloud.qcow2 cp $kvname.qcow2.bak $kvname.qcow2 ls -hl $kvname* virsh start $kvname virsh console $kvname ####################################