前言:
虚拟化的理论知识我就不啰嗦了,可以参见http://bbs.linuxtone.org/thread-24347-1-1.html,kvm全称Kernel-based Virtual Machine, 其实kvm只是一个内核模块,提供虚拟cpu和内存管理的模块,至于其它的设备是由qemu模拟的,如网卡,显卡,磁盘等。后来redhat联合IBM以及Linux社区创造了libvirt,模拟的设备性能要比qemu的好很多,并提供了一系列的管理工具和api,整个集成了kvm虚拟化的解决方案。Linux(redhat系)装载kvm模块后,妖神一变成为了VM Monitor,也称为Hypervisor,部署使用简单,需要硬件支持虚拟化。
#
egrep
'(vmx|svm)'
--color=always
/proc/cpuinfo
# modprobe kvm
# modprobe kvm_intel || modprobe kvm_amd
# modprobe kvm
# modprobe kvm_intel || modprobe kvm_amd
#
lsmod
|
grep
kvm
kvm_intel 52570 30
kvm 314739 1 kvm_intel
kvm_intel 52570 30
kvm 314739 1 kvm_intel
#
cd
/etc/sysconfig/network-scripts/
# cp ifcfg-eth0 ifcfg-br0
ifcfg-eth0 :
DEVICE = eth0
TYPE = Ethernet
ONBOOT = yes
NM_CONTROLLED = yes
BRIDGE = " br0 "
BOOTPROTO = static
IPADDR = " 192 . 168 . 2 . 149 "
NETMASK = " 255 . 255 . 255 . 0 "
GATEWAY = " 192 . 168 . 2 . 2 "
ifcfg-br0 :
DEVICE = br0
TYPE = Bridge
ONBOOT = yes
NM_CONTROLLED = yes
BOOTPROTO = static
IPADDR = " 192 . 168 . 2 . 149 "
NETMASK = " 255 . 255 . 255 . 0 "
GATEWAY = " 192 . 168 . 2 . 2 "
# service network restart
# ifconfig
br0 . . .
eth0 . . .
lo . . .
virbr0 . . .
# cp ifcfg-eth0 ifcfg-br0
ifcfg-eth0 :
DEVICE = eth0
TYPE = Ethernet
ONBOOT = yes
NM_CONTROLLED = yes
BRIDGE = " br0 "
BOOTPROTO = static
IPADDR = " 192 . 168 . 2 . 149 "
NETMASK = " 255 . 255 . 255 . 0 "
GATEWAY = " 192 . 168 . 2 . 2 "
ifcfg-br0 :
DEVICE = br0
TYPE = Bridge
ONBOOT = yes
NM_CONTROLLED = yes
BOOTPROTO = static
IPADDR = " 192 . 168 . 2 . 149 "
NETMASK = " 255 . 255 . 255 . 0 "
GATEWAY = " 192 . 168 . 2 . 2 "
# service network restart
# ifconfig
br0 . . .
eth0 . . .
lo . . .
virbr0 . . .
#
mkdir
/opt/vms
# qemu-img create -f qcow2 /opt/vms/centos63-webtest.img 40G
# qemu-img create -f qcow2 /opt/vms/centos63-webtest.img 40G
#
yum
-y
install
libX11
xorg-x11-server-utils
xorg-x11-proto-devel
dbus-x11
xorg-x11-xauth
xorg-x11-drv-ati-firmware
xorg-x11-xinit
# virt-manager
# virt-manager
1
.
virsh进入交互模式,在该交互模式下有命令不全补全,详细帮助
virsh # lis[tab补全]
virsh # help list 详细帮助
2 . virsh list --all 查看虚拟机状态
3 . virsh start instanceName 虚拟机开机
4 . virsh shutdown instanceName 虚拟机关机
需要Linux母体机电源管理
service acpid start
5 . virsh destroy instanceName 强制关机
6 . virsh create /etc/libvirt/qemu/wintest01 . xml 通过以前的配置文件创建虚拟机
7 . virsh autostart instanceName 配置自启动
8 . virsh dumpxml wintest01 > /etc/libvirt/qemu/wintest02 . xml 导出配置文件
9 . virsh undefine wintest01 删除虚拟机配置文件,不会真的删除虚拟机
10 . mv /etc/libvirt/qemu/wintest02 . xml /etc/libvirt/qemu/wintest01 . xml
virsh define /etc/libvirt/qemu/wintest01 . xml
重新定义虚拟机
11 . virsh edit wintest01 编辑虚拟机配置文件
12 . virsh suspend wintest01 挂起虚拟机
13 . virsh resume wintest01 恢复挂起虚拟机
virsh # lis[tab补全]
virsh # help list 详细帮助
2 . virsh list --all 查看虚拟机状态
3 . virsh start instanceName 虚拟机开机
4 . virsh shutdown instanceName 虚拟机关机
需要Linux母体机电源管理
service acpid start
5 . virsh destroy instanceName 强制关机
6 . virsh create /etc/libvirt/qemu/wintest01 . xml 通过以前的配置文件创建虚拟机
7 . virsh autostart instanceName 配置自启动
8 . virsh dumpxml wintest01 > /etc/libvirt/qemu/wintest02 . xml 导出配置文件
9 . virsh undefine wintest01 删除虚拟机配置文件,不会真的删除虚拟机
10 . mv /etc/libvirt/qemu/wintest02 . xml /etc/libvirt/qemu/wintest01 . xml
virsh define /etc/libvirt/qemu/wintest01 . xml
重新定义虚拟机
11 . virsh edit wintest01 编辑虚拟机配置文件
12 . virsh suspend wintest01 挂起虚拟机
13 . virsh resume wintest01 恢复挂起虚拟机
#
virt-clone
-o
centos63_webtest
-n
centos63_webtest2
-f
/opt/vms/centos_webtest2.img
#
qemu-img
info
/opt/vms/centos63-119.22.img
image : /opt/vms/centos63- 119 . 22 . img
file format : qcow2
virtual size : 40G ( 42949672960 bytes )
disk size : 136K
cluster_size : 65536
image : /opt/vms/centos63- 119 . 22 . img
file format : qcow2
virtual size : 40G ( 42949672960 bytes )
disk size : 136K
cluster_size : 65536
#
cp
centos63-119.22.img
centos63-119.22.raw
# qemu-img convert -f raw -O qcow2 centos63-119.22.raw centos63-119.22.img
# qemu-img convert -f raw -O qcow2 centos63-119.22.raw centos63-119.22.img
#
virsh
start
centos63-119.22
# virsh snapshot-create centos63-119.22
# virsh snapshot-create centos63-119.22
#
ls
/var/lib/libvirt/qemu/snapshot/centos63-119.22
1410341560 . xml
# virsh snapshot-revert centos63-119.22 1410341560
1410341560 . xml
# virsh snapshot-revert centos63-119.22 1410341560
#
qemu-img
info
centos63-119.22
1 1410341560 228M 2014 - 04 - 08 10 : 26 : 40 00 : 21 : 38 . 053
# virsh snapshot-delete centos63-119.2 1410341560
1 1410341560 228M 2014 - 04 - 08 10 : 26 : 40 00 : 21 : 38 . 053
# virsh snapshot-delete centos63-119.2 1410341560
Connected
to
domain
centos63-
119
.
22
Escape character is ^ ]
Escape character is ^ ]
#
vim
/etc/grub.conf
kernel /vmlinuz- 2 . 6 . 32 - 279 . el6 . x86_64 ro root = UUID = 25756cd7- 3597 -4e45-8dd
db-79d091c6d17f rd_NO_LUKS KEYBOARDTYPE = pc KEYTABLE = us LANG = en_US . UTF- 8 rd_NO_MM
D SYSFONT = latarcyrheb-sun16 console=ttyS0 rd_NO_LVM crashkernel = auto rhgb quiet
rd_NO_DM
kernel /vmlinuz- 2 . 6 . 32 - 279 . el6 . x86_64 ro root = UUID = 25756cd7- 3597 -4e45-8dd
db-79d091c6d17f rd_NO_LUKS KEYBOARDTYPE = pc KEYTABLE = us LANG = en_US . UTF- 8 rd_NO_MM
D SYSFONT = latarcyrheb-sun16 console=ttyS0 rd_NO_LVM crashkernel = auto rhgb quiet
rd_NO_DM
#
echo
"S0:12345:respawn:/sbin/agetty
ttyS0
115200"
>>
/etc/inittab
#
echo
"ttyS0"
>>
/etc/securetty
#
qemu-img
create
-f
qcow2
60G.img
60G
说明:
/dev/sda2 扩容guestos的/dev/sda2
centos63- 119 . 27 . img 原来的磁盘文件
60G 第一步建立的更大的磁盘文件
/dev/sda2 扩容guestos的/dev/sda2
centos63- 119 . 27 . img 原来的磁盘文件
60G 第一步建立的更大的磁盘文件
#
mv
60G.img
centos63-119.27.img
# virsh start centos63-119.27
# virsh start centos63-119.27
#
virsh
migrate
centos63-119.27
--live
qemu+ssh://192.168.119.11:9741/system
–
unsafe
说明 :
centos63- 119 . 27 迁移的实例名称
说明 :
centos63- 119 . 27 迁移的实例名称
--live 动态迁移
qemu+ssh ssh的方式