[root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- 2 c01 running
[root@localhost ~]# virsh shutdown c01 域 c01 被关闭 [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- - c01 关闭
[root@localhost ~]# virsh start c01 域 c01 已开始 [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- 3 c01 running
还有一种强行关机的方式,相似于直接断电关机的。vim
[root@localhost ~]# virsh destroy c01 域 c01 被删除 [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- - c01 关闭
启动还有一种经过配置文件启动虚拟机dom
[root@localhost ~]# virsh create /etc/libvirt/qemu/c01.xml 域 c01 被建立(从 /etc/libvirt/qemu/c01.xml) [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- 4 c01 running
[root@localhost ~]# virsh suspend c01 域 c01 被挂起 [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- 4 c01 暂停
挂起后,可使用下面的命令恢复虚拟机。ide
[root@localhost ~]# virsh resume c01 域 c01 被从新恢复 [root@localhost ~]# virsh list --all Id 名称 状态 ---------------------------------------------------- 4 c01 running
[root@localhost ~]# virsh autostart c01 域 c01标记为自动开始
这里为了下一步删除虚拟机,先导出一份xml配置文件工具
[root@localhost ~]# virsh dumpxml c01 > /etc/libvirt/qemu/c02.xml
[root@localhost ~]# virsh shutdown c01 域 c01 被关闭 [root@localhost ~]# virsh undefine c01 错误:取消定义域 c01 失败 错误:所需操做无效:没法删除使用 1 快照的不活跃域
这个错误的缘由能够明显看出来,由于以前我对c01作过快照,删除虚拟机必需要把快照删除后,在进行undefine性能
[root@localhost ~]# virsh shutdown c01 [root@localhost ~]# virsh snapshot-delete c01 1533630255 已删除域快照 1533630255 [root@localhost ~]# virsh undefine c01 域 c01 已经被取消定义
这里undefine后,c01.xml文件会消失,可是c01.img磁盘文件还在,因此要删除虚拟机,须要再把c01.img删除。加密
[root@localhost ~]# ls /etc/libvirt/qemu/ autostart c02.xml networks [root@localhost ~]# ls /vdir/ c01.img c01.qcow2 [root@localhost ~]# virsh list --all Id 名称 状态 ----------------------------------------------------
[root@localhost ~]# mv /etc/libvirt/qemu/c02.xml /etc/libvirt/qemu/c01.xml [root@localhost qemu]# virsh define c01.xml 定义域 c01(从 c01.xml) [root@localhost qemu]# virsh list --all Id 名称 状态 ---------------------------------------------------- - c01 关闭
经过文件管理能够直接查看、修改、复制虚拟机的内部文件。虚拟机磁盘文件有raw和qcow2两种格式。KVM虚拟机默认使用raw格式,raw格式性能最好,速度最快,可是缺点就是不支持一些新功能,如镜像、Zlib磁盘压缩,AES加密。针对两种格式的文件有不一样工具,经过yum安装libguestfs-tools后产生的命令行工具,能够直接读取qcow2格式的磁盘文件,因此须要将raw磁盘文件转换成qcow2的格式。命令行
查看当前磁盘格式:code
[root@localhost vdir]# qemu-img info /vdir/c01.img image: /vdir/c01.img file format: raw virtual size: 20G (21474836480 bytes) disk size: 1.1G
将虚拟机关闭后,转换磁盘文件格式。orm
[root@localhost vdir]# virsh list --all Id 名称 状态 ---------------------------------------------------- - c01 关闭 [root@localhost vdir]# qemu-img convert -f raw -O qcow2 /vdir/c01.img /vdir/c01.qcow2 [root@localhost vdir]# ls /vdir/ c01.img c01.qcow2
修改c01.xml配置文件。xml
[root@localhost vdir]# vim /etc/libvirt/qemu/c01.xml <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> #这里的type从raw改为qcow2 <source file='/vdir/c01.qcow2'/> #将源文件类型也要修改 <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </disk>
经过yum安装libguestfs-tools后,可使用一些命令行工具查看qcow2文件。
[root@localhost vdir]# yum install libguestfd-tools -y [root@localhost vdir]# virt-cat -a /vdir/c01.qcow2 /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE="Ethernet" PROXY_METHOD="none" BROWSER_ONLY="no" BOOTPROTO="dhcp" DEFROUTE="yes" IPV4_FAILURE_FATAL="no" IPV6INIT="yes" IPV6_AUTOCONF="yes" IPV6_DEFROUTE="yes" IPV6_FAILURE_FATAL="no" IPV6_ADDR_GEN_MODE="stable-privacy" NAME="eth0" DEVICE="eth0" ONBOOT="yes" MAC="52:54:00:AE:FC:8A"
[root@localhost vdir]# virsh list --all Id 名称 状态 ---------------------------------------------------- - c01 关闭 #保证虚拟机已经关闭 [root@localhost vdir]# virt-clone -o c01 -n c02 -f /vdir/c02.qcow2 WARNING 设置图形设备端口为自动端口,以免相互冲突。 WARNING 当卷已经被所有分配后,须要的卷容量将超过可用池空间。(20480 M 须要的容量 > 12822 M 可用容量) 正在分配 'c02.qcow2' | 20 GB 00:00:32 成功克隆 'c02'。 [root@localhost vdir]# virsh list --all Id 名称 状态 ---------------------------------------------------- - c01 关闭 - c02 关闭
KVM虚拟机要使用镜像功能,磁盘格式必须为qcow2,下面对c01建立快照。
[root@localhost vdir]# virsh snapshot-create c01 已生成域快照 1533777836
查看虚拟机快照版本信息。
[root@localhost vdir]# virsh snapshot-current c01 <domainsnapshot> <name>1533777836</name> <state>shutoff</state> <creationTime>1533777836</creationTime> <memory snapshot='no'/> <disks> .....省略
查看快照信息
[root@localhost vdir]# virsh snapshot-list c01 名称 生成时间 状态 ------------------------------------------------------------ 1533777836 2018-08-09 09:23:56 +0800 shutoff
恢复虚拟机至指定的快照状态。
[root@localhost vdir]# virsh snapshot-revert c01 1533777836