基本KVM虚机和QEMU-IMG命令

一台虚拟机的核心就是一个磁盘镜像,这个镜像可以理解成虚拟机的磁盘,里面有虚拟机的操作系统和驱动等重要文件。本文主要介绍创建虚拟机的一般过程。

创建虚拟机镜像

要在一台host上跑起一个虚拟机一般需要两个步骤:

第一步:创建虚拟机镜像

qemu-img create -f raw /images/vm1.raw 8G

qmeu-img创建的镜像是一个稀疏文件,也就是说刚创建出来的文件并没有8G,它会随着数据的增多慢慢增加,直到8G

第二步:启动虚拟机

kvm /imges/vm1.raw

运行结果: 因为镜像里面没有任何内容,所以提示找不到可引导设备。

使用qemu-img管理镜像

qemu-img基本命令

上节介绍了使用qemu-img创建镜像,这一节将会介绍qemu-img在镜像管理上的强大功能。

qemu-img有很多命令,包括下面常用的,当然qemu-img -h你懂得。

info
查看镜像的信息

create
创建镜像

check
检查镜像

convert
转化镜像的格式,(raw,qcow ……)

snapshot
管理镜像的快照

rebase
在已有的镜像的基础上创建新的镜像

resize
增加或减小镜像大小

创建镜像

qemu-img create -f <fmt> -o <options> <fname> <size>

举例:

qemu-img create -f raw -o size=4G /images/vm2.raw


hzgatt@hzgatt:~/images$ ll
total 0-rw-r--r-- 1 hzgatt hzgatt 4.0G  629 14:11 vm2.raw
hzgatt@hzgatt:~/images$ ll -s
total 00 -rw-r--r-- 1 hzgatt hzgatt 4.0G  629 14:11 vm2.raw


hzgatt@hzgatt:~/images$ qemu-img info vm2.raw 
image: vm2.raw
file format: raw
virtual size: 4.0G (4294967296 bytes)
disk size: 0

虽然ls中看到文件的大小是4G,但是实际上磁盘大小是0。这就是稀疏文件

转化
将一个镜像文件转化为另外一种格式,qemu-img支持的格式可以看qemu-img -h最后一行。

Supported formats: vvfat vpc vmdk vdi sheepdog rbd raw host_cdrom host_floppy host_device file qed qcow2 qcow parallels nbd dmg tftp ftps ftp https http cow cloop bochs blkverify blkdebug

转化命令:

qemu-img convert -c -f fmt -O out_fmt -o options fname out_fname


-c:采用压缩,只有qcow和qcow2才支持

-f:源镜像的格式,它会自动检测,所以省略之

-O 目标镜像的格式

-o 其他选先

fname:源文件

out_fname:转化后的文件

看例子:

hzgatt@hzgatt:~/images$ qemu-img convert -c -O qcow2 vm2.raw vm2.qcow2


hzgatt@hzgatt:~/images$ ll -s
total 136K
   0 -rw-r--r-- 1 hzgatt hzgatt 5.0G  629 13:55 vm1.raw
136K -rw-r--r-- 1 hzgatt hzgatt 193K  629 14:22 vm2.qcow2
   0 -rw-r--r-- 1 hzgatt hzgatt 4.0G  629 14:11 vm2.raw
hzgatt@hzgatt:~/images$ qemu-img info vm2.qcow2 
image: vm2.qcow2
file format: qcow2
virtual size: 4.0G (4294967296 bytes)
disk size: 136K
cluster_size: 65536

如果想看要转化的格式支持的-o选项有哪些,可以在命令末尾加上 -o ?

Supported options:
size             Virtual disk size
backing_file     File name of a base image
backing_fmt      Image format of the base image
encryption       Encrypt the image
cluster_size     qcow2 cluster size
preallocation    Preallocation mode (allowed values: off, metadata)

增加减少镜像大小
注意:只有raw格式的镜像才可以改变大小

hzgatt@hzgatt:~/images$ qemu-img resize vm2.raw +2GB

hzgatt@hzgatt:~/images$ ll -s
total 136K
   0 -rw-r--r-- 1 hzgatt hzgatt 5.0G  629 13:55 vm1.raw
136K -rw-r--r-- 1 hzgatt hzgatt 193K  629 14:22 vm2.qcow2
   0 -rw-r--r-- 1 hzgatt hzgatt 6.0G  629 14:28 vm2.raw
hzgatt@hzgatt:~/images$ qemu-img info vm2.raw 
image: vm2.raw
file format: raw
virtual size: 6.0G (6442450944 bytes)
disk size: 0

快照
查看快照

qemu-img snapshot -l /images/vm2.qcow2
注意:只有qcow2才支持快照

打快照

qemu-img snapshot -c booting vm2.qcow2

举例:

hzgatt@hzgatt:~/images$ qemu-img snapshot -c booting vm2.qcow2 
hzgatt@hzgatt:~/images$ qemu-img snapshot -l vm2.qcow2 
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         booting                   0 2012-06-29 14:35:04   00:00:00.000

从快照恢复:

qemu-img snapshot -a 1 /images/vm2.qcow2
然后从kvm启动这个虚拟机,会发现虚拟机又在打快照时的状态了

删除快照:

qemu-img snapshot -d 2 /images/vm2.qcow

使用派生镜像(qcow2)
当创建的虚拟机越来越多,并且你发现好多虚拟机都是同一个操作系统,它们的区别就是安装的软件不大一样,那么你肯定会希望把他们公共的部分提取出来,只保存那些与公共部分不同的东西,这样镜像大小下去了,空间变多了,管理也方便了。派生镜像就是用来干这事的!

首先看一个原始镜像

hzgatt@hzgatt:~/images$ qemu-img info vm3_base.raw 
image: vm3_base.raw
file format: raw
virtual size: 2.0G (2147483648 bytes)
disk size: 2.0G

现在我们新建一个镜像,但是派生自它

hzgatt@hzgatt:~/images$ qemu-img create -f qcow2 vm3_5.qcow2 -o backing_file=vm3_base.raw 5G
Formatting 'vm3_5.qcow2', fmt=qcow2 size=5368709120 backing_file='vm3_base.raw' encryption=off cluster_size=65536


hzgatt@hzgatt:~/images$ ll-rw-r--r-- 1 hzgatt hzgatt 193K  629 15:00 vm3_5.qcow2
-rw-r--r-- 1 hzgatt hzgatt 2.0G  629 14:51 vm3_base.raw
hzgatt@hzgatt:~/images$ qemu-img info vm3_5.qcow2 
image: vm3_5.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 136K
cluster_size: 65536
backing file: vm3_base.raw (actual path: vm3_base.raw)

^_^,这个镜像才136K,够省了吧。DRY永远的真理啊!

现在我们在vm3_5.qcow2上打了很多安全补丁,然后发现我又想在vm3_5.qcow2上派生新的虚拟机,o(∩∩)o…哈哈,这下怎么办呢?

hzgatt@hzgatt:~/images$ qemu-img convert -O raw vm3_5.qcow2 vm3_base2.raw

hzgatt@hzgatt:~/images$ qemu-img info vm3_base2.raw
image: vm3_base2.raw
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 592M

这个转化将会将vm3_5和base合并,生成新的vm3_base2.raw,然后你就可以继续无穷无尽的派生之旅了!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
img2kvm是一个在PVE下将OW固件添加到VM的工具,可以方便地将固件文件(包括gz方式的压缩格式)快速添加到OW虚拟机中,从而简化让人工操作的麻烦。 若是你的PVE主机能访问Internet,可以直接下载到目录“/usr/bin”或“/usr/local/bin”下,下面是将img2kvm下载到目录“/usr/local/bin”以及增加其可执行权限的命令: wget -P /usr/local/bin http://dl.everun.top/softwares/utilities/img2kvm/img2kvm chmod +x /usr/local/bin/img2kvm 若是PVE主机不能直接访问Internet,可以先下载到电脑中,然后上传到指定的目录“/usr/bin”或“/usr/local/bin”下,然后再通过chmod命令增加可执行权限。 这样完成后,就可以直接使用img2kvm命令了。 img2kvm详细的帮助信息可以通过命令img2kvm -h”直接获取,旧版本img2kvm帮助信息的主要内容如下: A utility that convert OpenWrt firmware to disk image for KVM guest in Proxmox VE. Copyright (C) 2017-2018 everun.top usage: img2kvm [storage] -h or --help display this help. -V or --version output img2kvm version informaton. Command parameters: img_name the name of OpenWrt image file, e.g. 'openwrt-x86-kvm64-combined-ext4.img'. vm_id the ID of VM for OpenWrt guest, e.g. '200'. vmdisk_name the name of disk for OpenWrt guest, e.g. 'vm-200-disk-1'. storage Storage pool of Proxmox VE, default is 'local-lvm'. 其中,-h可以获得所有的帮助信息,-V可以获得版本信息。 所用的命令格式为: img2kvm [storage] 其具体参数说明如下: img_name:是OW固件的文件名称。一般为“xxx.img”或“xxx.img.gz”的格式。img2kvm可以直接识别并转换“img.gz”压缩格式的固件文件,对于“xxx.img.gz”来说,只需要输入“xxx.img”作为名称即可,不需要再另外加“.gz”。 vm_id:是创建好的OW虚拟机的ID。一般为一组非零开头的数字,如200。 vmdisk_name:是OW虚拟机要使用的磁盘名称。建议采用vm--disk-的命名方式,如vm-200-disk-1。 storage:是指导入使用的存储池的ID,默认为“local-lvm”,这是安装PVE时自动创建的。此项为可选项,若不指定则使用默认值。 v0.1.8版之后的img2kvm对所需的选项参数进行了简化,取消了参数“”,简化后的命令格式如下: img2kvm [storage] 即img2kvm命令后面仅需要加加两个参数:固件的文件名称和需要添加磁盘的虚拟机ID。 其中,固件的文件可以是img的映像格式,也可以是.gz的压缩格式。 例如,将文件名为“openwrt-x86-kvm64-combined-squashfs.img.gz”的固件导入到ID为“123”的VM中的命令为: img2kvm openwrt-x86-kvm64-combined-squashfs.img.gz 123 这里使用了默认的存储池local,故可以省略。 * 增补信息: 当前img2kvm最新的版本为v0.1.10。 最后更新于2019年3月20日。 特注:本下载已获得原作者授权。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值