qemu-img create创建磁盘
qemu-img工具是qemu软件的一部分,更加详细的使用wiki可以参考qemu官网,以下在pve下操作,首先查看man qemu-img,如下:
注意size 默认单位为byte,比如size为10 表示文件大小10byte 10M表示文件大小10M
依上,解释-u参数,当指定-b backing file时,当不使用-u参数时,backing file会被打开验证文件是否合法;当使用-u参数时,表示开启了unsafe backing file mode模式,意味着甚至当backing file文件不予指定时,也可以create一个基于不存在backing file(只是指定文件名,该文件可以不存在)的img文件,但a matching backing file must be created or additional options be used to make the backing specification valid(一个匹配的backing file必须后来创建或者通过额外的选项来保证backing specification合法)。当通过backing file创建img时,可以不必指定size参数。
当qemu-img使用img后缀文件(也即raw格式文件时)来作为backing file创建disk image时,提示不支持,如下:
2)关于qemu的qcow2格式,在man qemu-img手册中最后有文件格式的相关说明,如下:
qcow2
QEMU image format, the most versatile format. Use it to have smaller images (useful if your filesystem does not supports holes, for example on Windows), optional AES encryption, zlib based compression and support of multiple VM snapshots.
Supported options:
"backing_file"
File name of a base image (see create subcommand)
"backing_fmt"
Image format of the base image
"encryption"
If this option is set to "on", the image is encrypted.Encryption uses the AES format which is very secure (128 bit keys). Use a long password (16 characters)
to get maximum protection.
"cluster_size"
Changes the qcow2 cluster size (must be between 512 and 2M). Smaller cluster sizes can improve the image file size whereas larger cluster sizes generally provide better performance.
"preallocation"
Preallocation mode (allowed values: "off", "metadata", "falloc", "full"). An image with preallocated metadata is initially larger but can improve performance when the image needs to grow. "falloc" and "full" preallocations are like the same options of "raw" format, but sets up metadata also.
以上参数都是通过-o 选项来指定,
其中的 preallocation解释如下:
off模式:缺省预分配策略,即不使用预分配策略
metadata模式:分配qcow2的元数据(metadata),预分配后的虚拟磁盘仍然属于稀疏映像类型(allocates qcow2 metadata, and it's still a sparse image.)
full模式:分配所有磁盘空间并置零,预分配后的虚拟磁盘属于非稀疏映像类型(allocates zeroes and makes a non-sparse image)
falloc模式:使用posix_fallocate()函数分配文件的块并标示它们的状态为未初始化,相对full模式来说,创建虚拟磁盘的速度要快很多(which uses posix_fallocate() to "allocate blocks and marking them as uninitialized", and is relatively faster than writing out zeroes to a file)
总结:off和metadata模式创建的是sparse稀疏模式磁盘文件。而full和falloc创建的是非稀疏磁盘文件,但full模式需要置零,而falloc不需要置零,所以falloc创建的速度要快很多。
qemu-img create -f qcow2 -o preallocation=off /root/q1.qcow2 size=10M
qemu-img create -f qcow2 -o preallocation=metadata /root/q2.qcow2 size=10M
qemu-img create -f qcow2 -o preallocation=full /root/q3.qcow2 size=10M
qemu-img create -f qcow2 -o preallocation=falloc /root/q4.qcow2 size=10M
qemu-img create -f qcow2 -o preallocation=off /root/q1.qcow2 10M
qemu-img create -f qcow2 -o preallocation=metadata /root/q2.qcow2 10M
qemu-img create -f qcow2 -o preallocation=full /root/q3.qcow2 10M
qemu-img create -f qcow2 -o preallocation=falloc /root/q4.qcow2 10M
3)尝试创建加密磁盘
出现错误提示,应该还需要创建key,留后续填坑