码迷,mamicode.com
首页 > 其他好文 > 详细

#openstack centos6 centos7 kvm镜像制作

时间:2017-11-29 21:01:26      阅读:753      评论:0      收藏:0      [点我收藏+]

标签:开机   cpu   password   集群   containe   pid   base   asi   star   

 

#openstack centos6 centos7 kvm 镜像制作

openstack pike 集群高可用  安装部署 汇总 http://www.cnblogs.com/elvi/p/7613861.html

#openstack centos6 centos7 镜像制作

####################################
#KVM环境 yum install -y kvm virt-*  libvirt  bridge-utils qemu-img qemu-kvm-tools

#创建KVm虚拟机
########centos7 mini, ks
kvname=centos6.9
ksname=c6op.ks
ISO=CentOS-6.9-x86_64-bin-DVD1.iso

# kvname=centos7.4
# ksname=c7op.ks
# ISO=CentOS-7-x86_64-DVD-1708.iso

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 #登录

# #KS文件实例
# http://elven.vip/ks/openstack/c6op.ks
# http://elven.vip/ks/openstack/c7op.ks

####################################
#kvm 虚拟机配置

#更改root密码
echo 123321|passwd root --stdin

# #时区配置#
# #centos7
# rm -f /etc/localtime
# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# ll /etc/localtime
# #centos6
# echo ‘ZONE="Asia/Shanghai"‘>/etc/sysconfig/clock

#使用阿里源#
[[ `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
 }

#根据需求,安装配置其它服务#

#清理#
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-.qcow2
ls -hl /kvm/img/$kvname*

####################################
# #拷贝镜像

#上传glance镜像
#centos7
openstack image create "centos7.4"   --file centos7.4-.qcow2   --disk-format qcow2 --container-format bare   --property hw_qemu_guest_agent=yes   --public
#
#centos6
openstack image create "centos6.9"   --file centos6.9-.qcow2   --disk-format qcow2 --container-format bare   --property hw_qemu_guest_agent=yes   --public

####################################
#kvm还原

virsh destroy $kvname 
rm -f $kvname.qcow2 $kvname-.qcow2
cp $kvname.qcow2.bak $kvname.qcow2
ls -hl $kvname*

virsh start $kvname
virsh console $kvname

####################################

#提示
#openstack创建虚拟机后,重启VM根目录才自动扩容
lsblk #查看分区

#制作的镜像分享
#链接:https://pan.baidu.com/s/1nuPm75v 密码:3iu0
####################################

  

#openstack centos6 centos7 kvm镜像制作

标签:开机   cpu   password   集群   containe   pid   base   asi   star   

原文地址:http://www.cnblogs.com/elvi/p/7922421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!