四、利用virsh对虚拟机管理 1、开关机
[root@target ~]# virsh start node4 #开机
[root@target ~]# virsh create /etc/libvirt/qemu/node4.xml #直接通过主机配置文档启动主机
[root@target ~]# virsh shutdown node4 #关机 [root@target ~]# virsh destroy node4 #强制关闭电源 [root@target ~]# virsh list --all #查看虚拟机状态 Id 名称 状态 ---------------------------------- 18 node4 running - node5 关闭 - win8 关闭 2、添加删除虚拟机
[root@target ~]# virsh define /etc/libvirt/qemu/node5.xml [root@target ~]# virsh list --all #node5已经添加 Id 名称 状态 ---------------------------------- 18 node4 running - node5 关闭 - win8 关闭
[root@target ~]# virsh undefine node5 #移除虚拟机 [root@target ~]# ls /etc/libvirt/qemu networks node4.xml win8.xml
[root@target ~]# virsh list --all #node5已经被移除 Id 名称 状态 ---------------------------------- 18 node4 running - win8 关闭
3、远程管理虚拟机(qemu+ssh连接) [root@target ~]# yum install virt-viewer
[root@target ~]# export DISPLAY=192.168.40.18:0.0
[root@target ~]# virt-viewer -c qemu///system node4 system:获取system权限,注意qemu后是三个/
#根据主机配置文档添加虚拟机 #本地管理虚拟机,[root@manager ~]# virt-viewer -c qemu+ssh://root@192.168.32.40/system node4 #远程linux通过virt-viewer+ssh管理虚拟机
Xlib: extension \root@192.168.32.40's password: root@192.168.32.40's password: #会弹出virt-viwer的gtk管理界面
4、使用已存在的虚拟机配置文档安裝新的虚拟机
[root@target ~]# qemu-img create -f qcow2 /virhost/kvm_node/node6.img 20G #为新建虚拟机生产磁盘镜像文件 [root@target ~]# virsh list Id 名称 状态 ---------------------------------- 18 node4 running
[root@target ~]# virsh dumpxml node4 >/etc/libvirt/qemu/node6.xml #导出虚拟机node6的硬件配置信息为/etc/libvirt/qemu/node6.xml [root@target ~]# vim /etc/libvirt/qemu/node6.xml
[root@target ~]# virsh define /etc/libvirt/qemu/node6.xml
#使用虚拟描述文档建立虚拟机,可用virsh edit node6修改node6的配置文件
[root@target ~]# virsh start node6 #启动虚拟机 5 为虚拟机开启vnc
[root@target ~]# virsh edit node4 #编辑node4的配置文件;不建议直接通过vim node4.xml修改。
#固定vnc管理端口5904,不自动分配,vnc密码xiaobai,监听所有网络 2. 远程vnc访问地址:192.168.32.40:5904 五、存储池和存储卷的管理 1.创建 KVM主机存储池
1).创建基于文件夹(目录)的存储池
[root@target virhost]# virsh pool-define-as vmware_pool --type dir --target /virhost/vmware
#定义存储池vmware_pool 或
[root@target virhost]# virsh pool-create-as --name vmware_pool --type dir --target /virhost/vmware
#创建存储池vmware_pool,类型为文件目录,/virhost/vmware,与pool-define-as结果一样 2).创建基于文件系统的存储池
[root@target virhost]#virsh pool-define-as --name vmware_pool --type fs --source-dev /dev/vg_target/LogVol02 --source-format ext4 --target /virhost/vmware 或
[root@target virhost]#virsh pool-create-as --name vmware_pool --type fs --source-dev /dev/vg_target/LogVol02 --source-format ext4 --target /virhost/vmware 3).查看存储池信息
[root@target virhost]# virsh pool-info vmware_pool #查看存储域(池)信息 名称: vmware_pool
UUID: 2e9ff708-241f-fd7b-3b57-25df273a55db 状态: running Persistent: no
自动启动: no 容量: 98.40 GB 分配: 18.39 GB 可用: 80.01 GB 4).启动存储池
[root@target virhost]# virsh pool-start vmware_pool #启动存储池 [root@target virhost]# virsh pool-list 名称 状态 自动开始 ----------------------------------------- default 活动 yes virhost 活动 yes vmware_pool 活动 no 5)销毁存储域,取消存储池
[root@target virhost]# virsh pool-destroy vmware_pool #销毁存储池 [root@target virhost]# virsh pool-list --all 名称 状态 自动开始 ----------------------------------------- default 活动 yes virhost 活动 yes vmware_pool 不活跃 no
[root@target virhost]# virsh pool-undefine vmware_pool #取消存储池的定义 [root@target virhost]# virsh pool-list --all 名称 状态 自动开始 ----------------------------------------- default 活动 yes virhost 活动 yes
2.创建了存储池后,就可以创建一个卷,这个卷是用来做虚拟机的硬盘
[root@target virhost]# virsh vol-create-as --pool vmware_pool --name node6.img --capacity 10G --allocation 1G --format qcow2
#创建卷 node6.img,所在存储池为vmware_pool,容量10G,初始分配1G,文件格式类型qcow2
[root@target virhost]# virsh vol-info /virhost/vmware/node6.img #查看卷信息 名称: node6.img 类型: 文件