KVM (Kernel Virtualization Module) is an open source, popular and efficient virtualization technology provided by Linux kernel. Virtualization creates virtual ram, devices, disks, CPU’s, networks etc. Qemu is the user space tool used to provide these to the kernel level virtualization tool KVM. Disk is one of the most important device in the virtual systems. Qemu-img is the tool used to create, manage, convert shrink etc. the disk images of virtual machines.
KVM(内核虚拟化模块)是Linux内核提供的一种开源,流行且高效的虚拟化技术。 虚拟化创建虚拟ram,设备,磁盘,CPU,网络等。Qemu是用于将其提供给内核级虚拟化工具KVM的用户空间工具。 磁盘是虚拟系统中最重要的设备之一。 Qemu-img是用于创建,管理,转换收缩等虚拟机磁盘映像的工具。
We can get help about the qemu-img
command with the -h
option.
我们可以通过-h
选项获得有关qemu-img
命令的帮助。
$ qemu-img -h
Help 帮帮我
Syntax of the qemu-img
command is like below.
qemu-img
命令的语法如下。
qemu-img create -f fmt -o options fname size
Virtual Machines or simply VM’s runs operating system and user level tools like normal PC. Operating system and user level tools are stored in disk image files like physical disks. Generally all VM’s uses one disk image file to read and write data. We will use create
command to create a disk image. In the example the disk image name will be ubuntu.img
. We will provide disk size after disk name which is 10G
in gigabyte.
虚拟机或仅VM的运行操作系统和用户级工具(如普通PC)。 操作系统和用户级工具存储在磁盘映像文件(例如物理磁盘)中。 通常,所有VM都使用一个磁盘映像文件来读取和写入数据。 我们将使用create
命令创建磁盘映像。 在示例中,磁盘映像名称将为ubuntu.img
。 我们将在磁盘名称后提供10G
磁盘大小。
$ qemu-img create ubuntu.img 10G
Create Disk Image 创建磁盘映像
After disk image create some information is provided. The disk image format is raw
and size is expressed as byte. We will look image formats below. Disk image actual size is 0 because there is no data in it but the vm will see disk image as 10G disk and will be able to use up to 10G.
创建磁盘映像后,将提供一些信息。 磁盘映像格式为raw
映像,大小表示为字节。 我们将在下面查看图像格式。 磁盘映像的实际大小为0,因为其中没有数据,但是vm会将磁盘映像视为10G磁盘,并且最多可以使用10G。
As stated before qemu supports different type of disk image formats. Disk image format is different than file systems. Disk image format is related with the host system.
如前所述,qemu支持不同类型的磁盘映像格式。 磁盘映像格式与文件系统不同。 磁盘映像格式与主机系统有关。
Raw is default format if no specific format is specified while creating disk images. Raw disk images do not have special features like compression, snapshot etc. Best thing about raw disk image is performance. Raw disk images are faster than other disk image types.
如果在创建磁盘映像时未指定特定格式,则Raw是默认格式。 原始磁盘映像不具有压缩,快照等特殊功能。原始磁盘映像的最好之处在于性能。 原始磁盘映像比其他磁盘映像类型更快。
Qcow2 is opensource format developed against Vmdk and Vdi. Qcow2 provides features like compression, snapshot, backing file etc. It is popular in the Kvm, Qemu community.
Qcow2是针对Vmdk和Vdi开发的开源格式。 Qcow2提供压缩,快照,备份文件等功能。它在Kvm,Qemu社区中很流行。
Qed is a disk format provided by Qemu. It provides support for overlay and sparse images. Performance of Qed is better than Qcow2 .
Qed是Qemu提供的磁盘格式。 它提供对重叠和稀疏图像的支持。 Qed的性能优于Qcow2。
Qcow is predecessor of the Qcow2.
Qcow是Qcow2的前身。
Vmdk is default and popular disk image format developed and user by VMware. Vmware products like Player, Workstation, ESXi, uses vmdk disk image type. It is rival of Qcow2 format
Vmdk是VMware开发和使用的默认和流行的磁盘映像格式。 诸如Player,Workstation,ESXi之类的Vmware产品使用vmdk磁盘映像类型。 是Qcow2格式的竞争对手
Vdi is popular format developed Virtual Box. It has similar features to the Vmdk and Qcow2
Vdi是流行格式开发的Virtual Box。 它具有与Vmdk和Qcow2类似的功能
Vps is format used by first generation Microsoft Virtualization tool named Virtual PC. It is not actively developed right now.
Vps是第一代Microsoft虚拟化工具Virtual PC使用的格式。 目前尚未积极开发。
We can create qcow image with the -f
option. We will also provide the disk size as an option. As you remember in the first example we have specified the disk size at the and of the command. Now we will set disk size with -o size
option. In the example we create a qcow2 formatted disk image named ubuntu.img
with the size of 10GB
.
我们可以使用-f
选项创建qcow图像。 我们还将提供磁盘大小作为选项。 您还记得在第一个示例中,我们在和的命令处指定了磁盘大小。 现在,我们将使用-o size
选项设置磁盘大小。 在示例中,我们创建了一个名为ubuntu.img
的qcow2格式化磁盘映像,大小为10GB
。
$ qemu-img create -f qcow2 -o size=10G ubuntu.img
Create Qcow Disk Image 创建Qcow磁盘映像
In this example we will create an vmdk disk image. We will set disk file name as ubuntu.vmdk
. But the extension vmdk
do not have an effect on the disk image type. We will also set disk image size 20GB
with -o size
option.
在此示例中,我们将创建一个vmdk磁盘映像。 我们将磁盘文件名设置为ubuntu.vmdk
。 但是扩展名vmdk
对磁盘映像类型没有影响。 我们还将使用-o size
选项将磁盘映像大小设置为20GB
。
$ qemu-img create -f vmdk -o size=20G ubuntu.img
Create Vmdk Disk Image 创建Vmdk磁盘映像
Information about a disk file can be needed. As we learned previously the extension is not a sole disk image type shower. To get detailed information we will use info
command by providing disk image name. In the example we will list information about ubuntu.vmdk
可能需要有关磁盘文件的信息。 如前所述,该扩展名不是唯一的磁盘映像类型的淋浴器。 要获取详细信息,我们将通过提供磁盘映像名称来使用info
命令。 在示例中,我们将列出有关ubuntu.vmdk
信息
$ qemu-img info ubuntu.img
Get Information About Disk Image 获取有关磁盘映像的信息
image
shows the disk image file name
image
显示了磁盘映像文件名
virtual size
shows the disk size of the image which will be read by VM
virtual size
显示虚拟机将读取的映像的磁盘大小
disk size
show the real size which hold in the host file system. It is 16K because there is no data on the disk image file
disk size
显示保存在主机文件系统中的实际大小。 因为磁盘映像文件上没有数据,所以它是16K
After some usage disk image file sizes grows. Even we delete files in the vm the size in the host system remains the same. Because the host system do not know how much and which part of the disk image file is unused. Shrink is done with convert
command and copying existing disk image in to new one. In the example we will shrink disk image file named ubuntu.qcow2
into ubuntu_s.qcow2
.
某些使用后,磁盘映像文件大小会增加。 即使我们删除了vm中的文件,主机系统中的大小也保持不变。 因为主机系统不知道磁盘映像文件有多少以及未使用的部分。 收缩是通过convert
命令完成的, convert
现有的磁盘映像复制到新映像中。 在示例中,我们将名为ubuntu.qcow2
磁盘映像文件收缩为ubuntu_s.qcow2
。
$ qemu-img convert -O qcow2 ubuntu.qcow2 ubuntu_s.qcow2
Another useful feature of the qemu-img is disk compression. Compressing disk images will make more space for other VMs. But keep in mind compression will little performance loss. We will use convert
command to compress. In the example we will compress disk image file ubuntu.qcow2
into new file named ubuntu_c.qcow2
with -c
option. This may take some time according to disk image file size and disk performance of the host system.
qemu-img的另一个有用功能是磁盘压缩。 压缩磁盘映像将为其他VM提供更多空间。 但是请记住,压缩几乎不会降低性能。 我们将使用convert
命令进行压缩。 在示例中,我们将使用-c
选项将磁盘映像文件ubuntu.qcow2
压缩为名为ubuntu_c.qcow2
新文件。 根据磁盘映像文件的大小和主机系统的磁盘性能,这可能需要一些时间。
$ qemu-img convert -O qcow2 -c ubuntu.qcow2 ubuntu_c.qcow2
Sometime while working with VM’s and disk images there can be errors. With qemu-img command these errors can be fixed. We will use check
command for this operation. In this example we will provide disk image file name with the check
command. Disk image format can be omitted because in most situations it is detected automatically.
有时在使用虚拟机和磁盘映像时可能会出错。 使用qemu-img命令可以修复这些错误。 我们将使用check
命令进行此操作。 在此示例中,我们将使用check
命令提供磁盘映像文件名。 可以忽略磁盘映像格式,因为在大多数情况下会自动检测到它。
$ qemu-img check ubuntu.qcow2
Check Disk Image For Errors 检查磁盘映像是否有错误
While creating disk image files we generally give small amount of disk. But this may become problem in the feature as unexpectedly. We may need to increase the disk image file size. We will use resize
command in order to increase disk size.In the example we will add extra 5GB
to the disk image file named ubuntu.qcow2
.
在创建磁盘映像文件时,我们通常会提供少量磁盘。 但这可能会意外地成为功能中的问题。 我们可能需要增加磁盘映像文件的大小。 我们将使用resize
命令来增加磁盘resize
。在示例中,我们将向名为ubuntu.qcow2
的磁盘映像文件添加额外的5GB
。
$ qemu-img resize ubuntu.qcow2 +5GB
Increase Disk Image Size 增加磁盘映像大小
We can see from screenshot that new size is 25GB
从屏幕截图中我们可以看到,新大小为25GB
翻译自: https://www.poftut.com/linux-qemu-img-command-tutorial-examples-create-change-shrink-disk-images/