安装KVM虚拟化条件:
首先CPU要支持虚拟化(Intel是vmx,AMD是svm)
[root@localhost ~]# egrep '(vmx|svm)' --color /proc/cpuinfo
确保BIOS里开启VT
Intel(R) Virtualization Tech [Enabled]
安装虚拟化套件
[root@localhost ~]# LANG=en_US-utf8
[root@localhost ~]# yum -y groupinstall "Virtualization" "Virtualization Client" "Virtualization Platform" "Virtualization Tools"
[root@localhost ~]# /etc/init.d/libvirtd start
[root@localhost ~]# chkconfig libvirtd on
[root@localhost ~]# virt-manager
手动安装系统...
设置HOST主机网络
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-br0
[root@localhost network-scripts]# cat ifcfg-br0
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=172.30.6.149
PREFIX=24
ARPCHECK=no
[root@localhost network-scripts]# cat ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:7E:DF:4F
TYPE=Ethernet
UUID=743094da-ab6c-406d-8238-a8e51be4a515
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
BRIDGE=br0 //将eth0桥接到br0
[root@localhost network-scripts]# /etc/init.d/NetworkManager stop //关闭此服务
[root@localhost network-scripts]# chkconfig NetworkManager off
[root@localhost network-scripts]# service network restart
[root@localhost network-scripts]# chkconfig network on
[root@localhost network-scripts]# reboot
配置DHCP
[root@localhost ~]# yum -y install dhcp
[root@localhost ~]# cat /etc/dhcp/dhcpd.conf
ddns-update-style interim;
filename "pxelinux.0";
next-server 172.30.6.149;
subnet 172.30.6.0 netmask 255.255.255.0 {
option routers 172.30.6.149;
option domain-name "tarena.com";
option domain-name-servers 172.30.1.16,172.30.6.149;
range dynamic-bootp 172.30.6.10 172.30.6.20;
}
[root@localhost ~]# service dhcpd start
[root@localhost ~]# chkconfig dhcpd on
配置TFTP
[root@localhost ~]# yum -y install tftp-server
[root@localhost ~]# grep -E "args|disable" /etc/xinetd.d/tftp
server_args = -s /tftpboot
disable = no
[root@localhost ~]# service xinetd restart
[root@localhost ~]# chkconfig xinetd on
[root@localhost ~]# netstat -ln | grep :69
udp 0 0 0.0.0.0:69 0.0.0.0:*
配置NFS
[root@localhost ~]# cat /etc/exports
/data/iso *(ro)
/var/ftp/pub/iso/CentOS/6.5 *(ro)
/var/ftp/pub/iso/RedHat/5.10 *(ro)
[root@localhost ~]# service rpcbind restart
[root@localhost ~]# service nfs restart
[root@localhost ~]# chkconfig rpcbind on
[root@localhost ~]# chkconfig nfs on
[root@localhost ~]# yum -y install syslinux
[root@localhost ~]# cd /var/ftp/pub/iso/RedHat/5.10/isolinux/
[root@localhost isolinux]# mkdir -p /tftpboot/pxelinux.cfg
[root@localhost isolinux]# cp initrd.img vmlinuz /tftpboot/
[root@localhost isolinux]# cp isolinux.cfg /tftpboot/pxelinux.cfg/default
[root@localhost ~]# cp /usr/share/syslinux/pxelinux.0 /tftpboot/
无人值守安装RHEL5.10
/var/lib/libvirt/images //安装kvm虚拟机默认存放位置
/etc/libvirt/qemu/ //kvm虚拟机配置文件存放位置
管理命令
[root@localhost ~]# virsh list //查看已打开虚拟机列表
[root@localhost ~]# virsh list --all //查看所有虚拟机列表
[root@localhost ~]# virsh version //查看版本号
[root@localhost ~]# virsh start kvm_snap1 //启动kvm_snap1虚拟机
[root@localhost ~]# virsh create /etc/libvirt/qemu/kvm_snap1.xml
[root@localhost ~]# /etc/init.d/acpid restart
[root@localhost ~]# chkconfig acpid on
[root@localhost ~]# virsh shutdown kvm_snap1 //关闭kvm_snap1虚拟机
[root@localhost ~]# virsh destroy kvm_snap1 //强制关闭kvm_snap1虚拟机
[root@localhost ~]# virsh autostart kvm_snap1 //设置kvm_snap1自启动
[root@localhost ~]# virsh dumpxml kvm_snap1 > kvm_snap1.xml //导出kvm_snap1虚拟机配置文件
[root@localhost ~]# virsh undefine kvm_snap1 //取消kvm_snap1定义
[root@localhost ~]# virsh define kvm_snap1.xml //定义kvm_snap1
[root@localhost ~]# virsh dominfo kvm_snap1 //列举kvm_snap1信息
[root@localhost ~]# virsh edit kvm_snap1 //编辑kvm_snap1配置文件
[root@localhost ~]# virt-clone -o rhel5.10 -n rhel5.10-clone -f /data/images/rhel5.10-clone.img //克隆虚拟机
-o 指定模板主机
-n 指定克隆出来虚拟机名字
-f 指定克隆出来虚拟机存放位置
使用LVM方式管理虚拟主机磁盘
1,创建LV
[root@localhost ~]# fdisk -l | grep /dev/sda6
/dev/sda6 6170 39163 265015296 8e Linux LVM
[root@localhost ~]# pvcreate /dev/sda6
[root@localhost ~]# vgextend vg_data /dev/sda6
[root@localhost ~]# vgdisplay | grep "Free PE"
Free PE / Size 12799 / 50.00 GiB
[root@localhost ~]# lvcreate -L 10G -n lv_kvm_node1 vg_data
2,使用创建的LV安装Guest
[root@localhost ~]# virt-install \
--name kvm_node1 \ //定义虚拟机名字
--noautoconsole \ //使用本选项指定不自动试图连接到客户机控制台
--ram 1024 \ //指定虚拟机内存大小,单位M
--arch=x86_64 \ //指定CPU架构
--vcpus=1 \ //指定虚拟cpu个数
--os-type=linux \ //指定安装系统类型
--os-variant=rhel5 \ //指定特定版本
--hvm \ //使用全虚拟化
--accelerate \ //使用内核加速功能
--disk path=/dev/vg_data/lv_kvm_node1 \
--network bridge=br0 \
--location nfs:192.168.194.253:/var/ftp/pub/iso/RedHat/5.9 \
--extra-args="ks=http://192.168.194.253/rhel-ks.cfg ip=192.168.194.10 netmask=255.255.255.0 gateway=192.168.194.253 dns=192.168.194.253 noipv6" //可以写成脚本
3,设置模板虚拟机,去掉一些个性信息(在kvm_node1操作)
[root@localhost ~]# touch /.unconfigured
4,生成快照
LVM快照
快照就是将当时的系统信息记录下来,就好像照相记录一样。未来若有任何数据更改,则原始数据会被放入到快照区,没有被更改的区域则由快照区与文件系统共享。
[root@localhost ~]# lvcreate -s -L 20G -n lv_node1 /dev/vg_data/lv_data
//指定快照大小名字
5,定义到virt-manager
[root@localhost ~]# virsh dumpxml kvm_node1 > /root/kvm_snap1.xml //虚拟机系统的名字
[root@localhost ~]# uuidgen
d2d8fe52-d244-46e5-97fb-7da9440d785b
修改 /root/kvm_snap1.xml文件(名字,UUID,磁盘位置,mac地址)
[root@localhost ~]# virsh define /root/kvm_snap1.xml
6,删除虚拟机
[root@localhost ~]# virsh undefine kvm_snap1
使用qcow2管理KVM虚拟机磁盘
目前主要有那些格式来作为虚拟机的镜像:
raw 默认,不支持快照
cow
qcow
qcow2 qcow2的性能上接近raw裸格式的性能
vmdk VMware格式
vdi