1.libvirt相关知识
libvirt是一个虚拟化统一接口管理工具,openstack及很多虚拟化平台都采用libvirt来管理虚拟机
libvirt宕了,虚拟机仍然能运行,只是管理不了它了
虚拟机配置是靠libvirt的xml文件来定义的,xml文件是自动生成的,不能手动编辑,只能通过virsh edit命令来编辑
虚拟机配置文件解析
[root@linux-node4 qemu]# virsh edit CentOS-7.2-x86_64 #编辑虚拟机配置
<domain type='kvm'> #类型KVM
<name>CentOS-7.2-x86_64</name> #虚拟机名称
<uuid>f7c3d6d4-c54e-4069-b76a-0a2c4253544a</uuid> #默认生成的uuid
<memory unit='KiB'>1048576</memory> #虚拟机设置的最大内存
<currentMemory unit='KiB'>1048576</currentMemory> #虚拟机当前内存
<vcpu placement='static'>1</vcpu> #默认CPU为1
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type> #hvm代表这是硬件虚拟机
<boot dev='hd'/> #启动设备为硬盘
</os>
<disk type='file' device='disk'>
<driver name='qemu' type='raw'/> #磁盘类型
<source file='/opt/CentOS-7.2-x86_64.raw'/> #磁盘位置
<target dev='vda' bus='virtio'/> #磁盘名称vda,使用的virtio半虚拟化驱动
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
<interface type='network'> #默认的是net网络
<mac address='52:54:00:50:9e:0e'/>
<source network='default'/>
<model type='virtio'/> #模型
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<memballoon model='virtio'> #内存扩展
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</memballoon>
2.virsh常用命令
[root@linux-node4 ~]# virsh list --all #列出虚拟机列表状态
Id Name State
----------------------------------------------------
4 CentOS-7.2-x86_64 running
[root@linux-node4 ~]# virsh edit CentOS-7.2-x86_64 #编辑虚拟机配置文件
[root@linux-node4 ~]# virsh dumpxml CentOS-7.2-x86_64 > CentOS-7.2-x86_64.bak_$(date +%F).xml
#dumpxml : domain information in XML 导出虚拟机的配置
[root@linux-node4 ~]# ls
anaconda-ks.cfg CentOS-7.2-x86_64.bak_2016-10-04.xml ks-pre.log
[root@linux-node4 ~]# virsh define CentOS-7.2-x86_64.bak_2016-10-04.xml #恢复虚拟机,但是如果连虚拟机硬盘也删了,那就回天乏术啦
[root@linux-node4 ~]# virsh start CentOS-7.2-x86_64 #启动虚拟机
[root@linux-node4 ~]# virsh shutdown CentOS-7.2-x86_64 #关闭虚拟机
[root@linux-node4 ~]# virsh reboot CentOS-7.2-x86_64 #重启虚拟机
[root@linux-node4 ~]# virsh undefine CentOS-7.2-x86_64 #删除虚拟机(慎用啊!赶快忘记这条命令吧,destroy(强行关机威力略差一点))
[root@linux-node4 ~]# virsh suspend CentOS-7.2-x86_64 #挂起虚拟机
Domain CentOS-7.2-x86_64 suspended
[root@linux-node4 ~]# virsh resume CentOS-7.2-x86_64 #恢复虚拟机
Domain CentOS-7.2-x86_64 resumed
3.virsh快照管理
[root@linux-node4 ~]# virsh shutdown CentOS-7.2-x86_64 #要开启快照功能先关闭虚机,把镜像转为qcow2格式
Domain CentOS-7.2-x86_64 is being shutdown
[root@linux-node4 opt]# qemu-img convert -f raw CentOS-7.2-x86_64.raw -O qcow2 CentOS-7.2-x86_64.qcow2 #raw转为qcow2
[root@linux-node4 opt]# virsh edit CentOS-7.2-x86_64
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/> #将磁盘格式从raw改为qcow2
<source file='/opt/CentOS-7.2-x86_64.qcow2'/> #改成qcow2镜像的路径
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
[root@linux-node4 ~]# virsh help snapshot #快照帮助命令
Snapshot (help keyword 'snapshot'):
snapshot-create Create a snapshot from XML
snapshot-create-as Create a snapshot from a set of args
snapshot-current Get or set the current snapshot
snapshot-delete Delete a domain snapshot
snapshot-dumpxml Dump XML for a domain snapshot
snapshot-edit edit XML for a snapshot
snapshot-info snapshot information
snapshot-list List snapshots for a domain
snapshot-parent Get the name of the parent of a snapshot
snapshot-revert Revert a domain to a snapshot
[root@linux-node4 opt]# virsh snapshot-create-as CentOS-7.2-x86_64 snap1 #创建3个快照snap1,snap2,snap3
[root@linux-node4 snapshot]# virsh snapshot-create-as --domain CentOS-7.2-x86_64 --name snap2 --description "test kvm snap"
[root@linux-node4 snapshot]# virsh snapshot-create-as CentOS-7.2-x86_64 snap3
[root@linux-node4 snapshot]# virsh snapshot-list CentOS-7.2-x86_64 #查看快照列表
Name Creation Time State
------------------------------------------------------------
snap1 2016-10-05 09:26:15 +0800 running
snap2 2016-10-05 09:39:05 +0800 running
snap3 2016-10-05 09:57:00 +0800 running
[root@linux-node4 snapshot]# virsh snapshot-current CentOS-7.2-x86_64 #查看当前快照配置
[root@linux-node4 snapshot]# virsh snapshot-info CentOS-7.2-x86_64 snap1 #查看指定快照信息
Name: snap1
Domain: CentOS-7.2-x86_64
Current: yes
State: running
Location: internal
Parent: -
Children: 1
Descendants: 1
Metadata: yes
#快照目录:/var/lib/libvirt/qemu/snapshot/
[root@linux-node4 qemu]# tree /var/lib/libvirt/qemu/snapshot/
/var/lib/libvirt/qemu/snapshot/
└── CentOS-7.2-x86_64
├── snap1.xml
├── snap2.xml
└── snap3.xml
[root@linux-node4 ~]# virsh snapshot-revert CentOS-7.2-x86_64 snap1 #还原到快照1
[root@linux-node4 ~]# virsh snapshot-info CentOS-7.2-x86_64 snap1
Name: snap1
Domain: CentOS-7.2-x86_64
Current: yes #状态为yes
State: running
Location: internal
Parent: -
Children: 2
Descendants: 2
Metadata: yes
[root@linux-node4 ~]# virsh snapshot-delete CentOS-7.2-x86_64 snap2 #删除快照2
Domain snapshot snap2 deleted
[root@linux-node4 ~]# virsh snapshot-list CentOS-7.2-x86_64
Name Creation Time State
------------------------------------------------------------
snap1 2016-10-05 09:26:15 +0800 running
snap3 2016-10-05 09:57:00 +0800 running
[root@linux-node4 ~]# virsh snapshot-delete CentOS-7.2-x86_64 snap1 #删除快照1
Domain snapshot snap1 deleted
[root@linux-node4 ~]# virsh snapshot-info CentOS-7.2-x86_64 snap1 #快照已经被删除
error: Domain snapshot not found: no domain snapshot with matching name 'snap1'
● 注意事项
使用 qemu-img 创建快照、还原快照时,一定要在关机状态下,否则会搞崩虚拟机