使用 KVM 命令集管理虚拟机
查看虚拟机状态
[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
还有一种强行关机的方式, 类似于直接断电关机的.
[root@localhost ~]# virsh destroy c01
域 c01 被删除
[root@localhost ~]# virsh list --all
Id 名称 状态
----------------------------------------------------
- c01 关闭
启动还有一种通过配置文件启动虚拟机
[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 暂停
挂起后, 可以使用下面的命令恢复虚拟机.
[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 关闭
KVM 文件管理
通过文件管理可以直接查看, 修改, 复制虚拟机的内部文件. 虚拟机磁盘文件有 raw 和 qcow2 两种格式. KVM 虚拟机默认使用 raw 格式, raw 格式性能最好, 速度最快, 但是缺点就是不支持一些新功能, 如镜像, Zlib 磁盘压缩, AES 加密. 针对两种格式的文件有不同工具, 通过 yum 安装 libguestfs-tools 后产生的命令行工具, 可以直接读取 qcow2 格式的磁盘文件, 所以需要将 raw 磁盘文件转换成 qcow2 的格式.
转换 raw 格式磁盘文件至 qcow2 格式.
查看当前磁盘格式:
- [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
将虚拟机关闭后, 转换磁盘文件格式.
[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 配置文件.
- [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
来源: http://www.bubuko.com/infodetail-2720284.html