KVM虚拟化基于它在的开源性受到了不少人的欢迎。KVM的装机、克隆功能是如何实现的呢?下面介绍的是在CentOS6.4上部署PXE环境来实现自动安装Rhel6.4,利用qcow2增量镜像实现克隆虚拟机功能。
我们可以通过以下命令来查看CPU是否支持全虚拟化!找到flags部分,如果其中输出有VMX或SVM,即表明支持虚拟化技术。
# egrep '(vmx|svm)' /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tscarch_perfmon pebs bts rep_good pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16xtpr pdcm lahf_lm tpr_shadow
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tscarch_perfmon pebs bts rep_good pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16xtpr pdcm lahf_lm tpr_shadow
1、安装KVM虚拟机软件
# yum groupinstall "Virtualization" "Virtualization Client""Virtualization Platform" -y
# yum install qemu-kvm virt-manager libvirt -y
# lsmod |grep kvm //kvm模块一定要加载
kvm_intel 53484 0
kvm 316506 1 kvm_intel
# service libvirtd start //启动libvirt服务
2、安装配置PXE所需的软件并配置dhcp
# yum install -y dhcp tftp-server syslinuxsystem-config-kickstart vsftpd
# vim /etc/dhcp/dhcpd.conf
subnet 192.168.10.0 netmask 255.255.255.0 {
option routers 192.168.10.254;
option domain-name-servers 192.168.10.254;
range 192.168.10.10 192.168.10.200;
next-server 192.168.10.1;
filename "pxelinux.0";
}
# service dhcpd start
# chkconfig dhcpd on
3、启动tftp服务
# vim /etc/xinetd.d/tftp
# mkdir /tftpboot
server_args = -s /tftpboot
disable = no
# service xinetd restart
4、拷贝相关文件到tftp下载目录
# mkdir /tftpboot
# cd /media/isolinux/
# cp -r * /tftpboot/
# cp /usr/share/syslinux/pxelinux.0/tftpboot/
# mkdir pxelinux.cfg
# mv isolinux.cfg pxelinux.cfg/default
# vim default
label linux
menu label ^Install or upgrade an existing system
menu default
kernel vmlinuz
append initrd=initrd.img ks=ftp://192.168.10.1/ks.cfg
5、拷贝光盘文件到ftp下载目录启动ftp服务并配置基于rhel6.4的yum源
# cd /media
# mkdir rhel6
# cp -r * /var/ftp/rhel6
# service vsftpd start
# chkconfig vsftpd on
# vim /etc/yum.repo.d/redhat.repo
[red]
name=red hat
baseual=file:///var/ftp/rhel6/
enabled=1
gpgcheck=0
6、生成ks文件
# system-config-kickstart
7、关闭NetworkManager服务
# service NetworkManager stop
# chkconfig NetworkManager off
8、网卡改名
# vim /etc/udev/rules.d/70-persistent-net.rules
查看mac地址,把指定mac地址的网卡改成你需要的名字
# modprobe -rv e1000 //停止网卡驱动
# modprobe -v e1000 //重新加载网卡驱动
9、创建桥接网卡br0并配置br0和eth0
# cp ifcfg-eth0 ifcfg-br0
# cat ifcfg-br0
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.10.1
PREFIX=24
# cat ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
BRIDGE=br0
service network restart
10、编写系统安装脚本
# vim install.sh
#!/bin/bash
cd /var/lib/libvirt/p_w_picpaths/
qemu-img create -f qcow2 rhel6base.img 80G
/usr/sbin/virt-install \
--vnc \
--noautoconsole \
--name=rhel6base \
--ram=1024 \
--arch=x86_64 \
--vcpus=1 \
--os-type=linux \
--os-variant=rhel6 \
--hvm \
--accelerate \
--disk=/var/lib/libvirt/p_w_picpaths/rhel6base.img \
-m 52:54:00:00:01:00 \
-w bridge=br0 \
--location=ftp://192.168.10.1/rhel6 \
--extra-args="ks=ftp://192.168.10.1/ks.cfg"
11、编写克隆虚拟机的脚本
# vim clone.sh
#!/bin/bash
IMG_DIR=/var/lib/libvirt/p_w_picpaths
BASE_VM=rhel6base
E_NOINPUT=65
E_NOTNUM=66
E_OUT_OF_RANGE=67
E_VM_EXISTS=68
read -p "please input a vm number:" NEW_VMNU
if [ -z $NEW_VMNU ]; then
echo "You must input a number."
exit $E_NOINPUT
fi
if [ $NEW_VMNU != $(echo"$NEW_VMNU*1" | bc) ]; then
echo "You must input a number."
exit $E_NOTNUM
fi
if [ $NEW_VMNU -lt 1 -o $NEW_VMNU -gt 254]; then
echo "You must input a number between 1 and 254"
exit $E_OUT_OF_RANGE
fi
NEW_VM=${BASE_VM}node${NEW_VMNU}
if [ -e $IMG_DIR/$NEW_VM.img ]; then
echo "$NEW_VM already exists"
exit $E_VM_EXISTS
fi
echo -en "creating diskp_w_picpath......\t\t"
qemu-img create -b $IMG_DIR/$BASE_VM.img -fqcow2 $IMG_DIR/$NEW_VM.img &> /dev/null
echo -e "\e[32m[OK]\e[0m"
echo
virsh dumpxml $BASE_VM >/tmp/$BASE_VM.xml
MAC_ADDR=$(echo"obase=16;$NEW_VMNU" | bc)
sed -i"/<name>$BASE_VM/s/$BASE_VM/$NEW_VM/" /tmp/$BASE_VM.xml
sed -i"/<uuid>/s/<uuid>.\{36\}/<uuid>$(uuidgen)/"/tmp/$BASE_VM.xml
sed -i "/$BASE_VM\.img/s/$BASE_VM\.img/$NEW_VM\.img/"/tmp/$BASE_VM.xml
sed -i "/macaddress/s/00'/$MAC_ADDR'/" /tmp/$BASE_VM.xml
virsh define /tmp/$BASE_VM.xml &>/dev/null
echo "$NEW_VM created."
12、编写删除克隆机的脚本
# vim undefine.sh
#!/bin/bash
IMG_DIR=/var/lib/libvirt/p_w_picpaths
BASE_VM=rhel6basenode
read -p "please input a vm number:" NEW_VMNU
virsh undefine ${BASE_VM}${NEW_VMNU}
rm -rf ${IMG_DIR}/${BASE_VM}${NEW_VMNU}.img
附:
virsh命令
1、查看虚拟机列表
# virsh list
# virsh list --all
2、查看virsh版本
# virsh version
3、启动名称为rhel6base的GUEST主机
# virsh start rhel6base
4、强制关闭GUEST主机
# virsh destroy rhel6base
5、导出GUEST主机的XML配置文件
# virsh dumpxml rhel6base > ~/rhel6base.xml
6、取消GUEST主机的定义
# virsh undefine ~/rhel6base
7、使用已经存在的xml文件和磁盘文件定义GUEST主机
# virsh define ~/rhel6base.xml
8、设置GUEST主机在HOST主机启动时,也会自动启动
# virsh autostart rhel6base