1
2
3
4
|
系统版本:CentOS
release 6.4 (Final) libvirtd版本: 1.1 . 3 virsh版本: 1.1 . 3 virt-manager版本: 0.9 . 0 |
1
|
yum
install libvirt qemu-kvm virt-install libssh2 virt-viewer bridge-utils -y |
1
2
3
4
5
6
7
|
桌面环境安装(最小化安装系统) 1 .
yum -y groupinstall Desktop 2 .yum
-y groupinstall "X
Window System" 3 .yum
-y groupinstall chinese-support 4 .编辑/etc/sysconfig/i18n文件,将en_US改成zh_CN kvm管理组件安装 yum
install libvirt virt-manager virt-viewer -y |
1
2
3
|
/etc/init.d/ksm
stop;/etc/init.d/ksmtuned stop;chkconfig ksm off;chkconfig ksmtuned off ##停掉没必要服务 echo 'vnc_listen
= "0.0.0.0"' >>
/etc/libvirt/qemu.conf ##设定vnc监听所有IP /etc/init.d/libvirtd
restart;chkconfig libvirtd on ##启动libvirtd服务 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
vi
ifcfg-eth0 DEVICE=eth0 ONBOOT=yes BOOTPROTO= static IPADDR= 10.10 . 17.30 ##
桥接网卡启动,此处的ip就不生效 NETMASK= 255.255 . 255.0 USERCTL=no TYPE=Ethernet BRIDGE=br0
##添加这一条配置即可 vi
ifcfg-br0 ##撰写桥接网卡的配置信息 DEVICE=br0 NM_CONTROLLED=no ONBOOT=yes IPADDR= 10.10 . 17.30 NETMASK= 255.255 . 255.0 BOOTPROTO=none IPV6INIT=no USERCTL=no TYPE=Bridge /etc/init.d/network
restart |
1
2
3
4
5
6
|
mdkdir
/data/kvm/ 10.10 . 17.100 -p
创建目录 cd
/data/kvm/ 10.10 . 17.100 进入项目目录 qemu-img
create -f qcow2 system.qcow2 300G 创建磁盘 virt-install
--name= 10.10 . 17.100 --ram 1024 --vcpus= 2 --arch=x86_64
--os-type=linux --os- var iant= 'rhel6' --disk
path=/data/kvm/ 10.10 . 17.100 /system.qcow2,device=disk,bus=virtio,format=qcow2
--accelerate --cdrom /data/CentOS- 6.4 -x86_64-CD.iso
--graphics vnc,password= 123456 --network
bridge=br0 --force --autostart PS:如果想网络安装
把--cdrom修改成--pxe virsh
vncdisplay id or vm-name 查看虚拟机的vnc端口,之后就可以用vnc客户端连上虚拟机操作了(宿主ip:vnc端口) |
1
2
3
4
5
6
7
8
|
安装linux virt-install
--name=vm-name --ram 1024 --vcpus= 2 --arch=x86_64
--os-type=linux --os- var iant= 'rhel6' --disk
path=/data/kvm/test/system.qcow2,device=disk,bus=virtio,format=qcow2 --accelerate --pxe --graphics vnc,password= 123456 --network
bridge=br0 --force --autostart 安装windows 2008 virt-install
--name=server2008 --ram 1024 --vcpus= 2 --arch=x86_64
--os-type=windows --os- var iant= 'win2k' --disk
path=/data/kvm/ 2008 /system.qcow2,device=disk,bus=ide,format=qcow2,cache=writeback
--accelerate --graphics vnc,password= 123456 --network
bridge=br0 --force --cdrom /data/2008R2.iso --autostart 安装windows 7 virt-install
--name=vm-name --ram 1024 --vcpus= 2 --arch=x86_64
--os-type=windows --os- var iant= 'win7' --disk
path=/data/win7.qcow2,device=disk,bus=ide,format=qcow2,cache=writeback --accelerate --graphics vnc --network bridge=br0 --force --cdrom /data/cn_windows_7_ultimate_with_sp1_x64.iso --autostart windows
XP virt-install
--name xp-lovae --ram 1024 --disk
/data/xp.qcow2,format=qcow2 --cdrom /data/HP-WinxpSp3.iso --os-type=windows --os- var iant= 'winXP' --accelerate
--graphics vnc --network bridge=br0 |
1
2
3
4
5
|
cp
/data/kvm/example/system.qcow2 /data/kvm/ 10.10 . 17.102 /
## example必须是关机状态下 virsh
dumpxml example(id or vm-name) >/data/kvm/ 10.10 . 17.102 /my.xml
获取xml文件 PS:my.xml文件需要删除uuid、网卡的mac地址字段,修改虚拟机的名字、和磁盘镜像存放路径,至于内存、cpu这些信息看自己的需求 virsh
define /data/kvm/ 10.10 . 17.102 /my.xml virsh
start 10.10 . 17.102 (id
or vm-name) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
列出正在运行的vm virsh
list 列出所有vm(包括关机的) virsh
list --all vm的生命周期管理 virsh
start/reset/destroy/undefine/suspend/resume/reboot/shutdown vm-name 编辑vm配置 virsh
edit vm-name 查看vnc端口 virsh
vncdisplay vm-name 查看虚拟机信息 virsh
dominfo vm-name 查看vm的xml信息 virsh
dumpxml 2 or
vm-name 在线增加网卡 virsh
attach- interface --domain
vm-name --type bridge --source br0 虚拟机开机自启动 /etc/init.d/libvirt-guests
start 监控已开启的虚拟机,当宿主重启了,会把之前开着的虚拟机启动 virsh
autostart vm-name 让虚拟机随着宿主开机启动 删除虚拟机: 1 、virsh
undefine vm-name 2 、rm
-rf /data/kvm/vm-name 关机: virsh
destroy vm-name 硬关机 virsh
shutdown vm-name 软关机 注意:需要在虚拟机内安装acpid(yum -y install acpid;service acpid restart;chkconfig acpid on) 关于xml里的启动顺序 <boot
dev= 'hd' />
表示优先硬盘启动,可选设备有: hd,cdrom,network 使用xml创建虚拟机 持久 virsh
define my-vm.xml 临时(虚拟机关机后配置消失) virsh
create my-vm.xml |
1
2
3
4
5
6
7
8
9
10
11
12
|
A、创建快照 virsh
snapshot-create- as --domain
rhel6-navy --name 2014 - 02 - 28 --description "new
vm" B、查看快照 virsh
snapshot-list rhel6-navy C、查看快照配置 virsh
snapshot-current rhel6-navy D、回复快照位置 virsh
snapshot-revert rhel6-navy 2014 - 02 - 28 E、删除快照 birsh
snapshot- delete rhel6-navy 2014 - 02 - 28 F、获取帮助 virsh
help snapshot |
1
2
3
4
5
6
7
8
9
|
/etc/libvirt/libvirtd.conf添加如下配置: listen_tcp
= 1 tcp_port
= "16509" listen_addr
= "0.0.0.0" auth_tcp
= "none" listen_tls= 0 echo 'LIBVIRTD_ARGS="--listen"' >>/etc/sysconfig/libvirtd /etc/init.d/libvirtd
restart 重启服务 上面这些配置只是为了让virt-manager可以通过tcp方式连接都kvm主机 |