KVM 基本功能管理
查看 KVM 的配置文件存放目录 (c01.xml 是虚拟机系统实例的配置文件)
- [root@localhost ~]# ls /etc/libvirt/qemu
- c01.xml networks
查看查看虚拟机的状态
[root@localhost ~]# virsh list --all
Id 名称 状态
- ----------------------------------------------------
- 1 c01 running
虚拟机的关机与开启
[root@localhost ~]# virsh shutdown c01
域 c01 被关闭
[root@localhost ~]# virsh start c01
域 c01 已开始
[root@localhost ~]# virsh create /etc/libvirt/qemu/c01.xml #通过配置文件启动
- 挂起, 恢复虚拟机
[root@localhost ~]# virsh suspend c01
域 c01 被挂起
[root@localhost ~]# virsh list --all
Id 名称 状态
2 c01 暂停
[root@localhost ~]# virsh resume c01
域 c01 被重新恢复
[root@localhost ~]# virsh list --all
Id 名称 状态
2 c01 running
- 导出虚拟机配置
- [root@localhost ~]# virsh dumpxml c01> /etc/libvirt/qemu/c02.xml
- [root@localhost ~]# ls /etc/libvirt/qemu
- c01.xml c02.xml networks
- 虚拟机的删除与添加
- [root@localhost ~]# virsh shutdown c01
- [root@localhost ~]# virsh undefine c01
域 c01 已经被取消定义
- [root@localhost ~]# ls /etc/libvirt/qemu
- c02.xml networks
- 通过备份的配置文件重新定义虚拟机
- [root@localhost ~]# cd /etc/libvirt/qemu/
- [root@localhost qemu]# mv c02.xml c01.xml
- [root@localhost qemu]# virsh define c01.xml
- [root@localhost qemu]# ls
- c01.xml networks
- 修改虚拟机配置信息 (系统内存大小, 磁盘文件等信息)
- [root@localhost ~]# vim /etc/libvirt/qemu/c01.xml #直接通过 vim 命令修改
- [root@localhost ~]# virsh edit c01 #通过 virsh 命令修改
- ### KVM 文件管理
通过文件管理可以直接查看, 修改, 复制虚拟机的内部文件. 虚拟机的磁盘文件有 raw 与 qcow2 两种格式, KVM 虚拟机默认使用 raw 格式, raw 格式性能最好, 速度快, 其确定是不支持一些新的功能, 如镜像, ZLib 磁盘压缩, AFS 加密等. 针对两种格式的文件有不同的工具可供选择. 这里介绍本地 YUM 安装 libguestfs-tools 后产生的命令行工具 (这个工具可以直接读取 qcow2 格式的磁盘文件, 因此需要将 raw 格式的磁盘文件转换成 qcow2 的格式).
1. 转换 raw 格式磁盘文件至 qcow2 格式
- 查看当前磁盘格式
- [root@localhost ~]# qemu-img info /vdir/c01.img
- image: /vdir/c01.img
- file format: raw
- virtual size: 30G (32212254720 bytes)
- disk size: 940M
- 关闭虚拟机
[root@localhost ~]# virsh shutdown c01
- 转换磁盘格式
- [root@localhost ~]# qemu-img convert -f raw -O qcow2 /vdir/c01.img /vdir/c01.qcow2
- [root@localhost ~]# ls /vdir/c01.qcow2
- /vdir/c01.qcow2
2. 修改 c01 的 xml 配置文件
- [root@localhost ~]# virsh edit c01
- .....// 省略
- <disk type='file' device='disk'> #39 行
- <driver name='qemu' type='qcow2'/> #40 行 这里的 type 从 raw 改成 qcow2
- <source file='/vdir/c01.qcow2'/> #41 行 将源文件类型也要修改
3. 通过 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"
4. 虚拟机克隆
[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 关闭 5. 虚拟机快照
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-2721096.html