KVM 磁盘创建 qemu-img create


通过qumu-img 创建一个磁盘,并在centos7上进行挂载


       qemu-img - QEMU disk image utility

       usage: qemu-img command [command options]

       qemu-img allows you to create, convert and modify images offline. It can handle all image formats supported by QEMU.

       Warning: Never use qemu-img to modify images in use by a running virtual machine or any other process; this may destroy the image. Also, be aware that querying an image that is being modified by another process may encounter inconsistent state.

       The following commands are supported:

       create [-q] [-f fmt] [-o options] filename [size]

       create [-f fmt] [-o options] filename [size]
           Create the new disk image filename of size size and format fmt. Depending on the file format, you can add one or more options that enable additional features of this format.

           If the option backing_file is specified, then the image will record only the differences from backing_file. No size needs to be specified in this case. backing_file will never be modified unless you use the "commit" monitor command (or
           qemu-img commit).

           The size can also be specified using the size option with "-o", it doesn't need to be specified separately in this case.

       Supported image file formats:

       raw Raw disk image format (default). This format has the advantage of being simple and easily exportable to all other emulators. If your file system supports holes (for example in ext2 or ext3 on Linux or NTFS on Windows), then only the written
           sectors will reserve space. Use "qemu-img info" to know the real size used by the image or "ls -ls" on Unix/Linux.

           Supported options:

               Preallocation mode (allowed values: "off", "falloc", "full").  "falloc" mode preallocates space for image by calling posix_fallocate().  "full" mode preallocates space for image by writing zeros to underlying storage.

           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:

               Determines the qcow2 version to use. "compat=0.10" uses the traditional image format that can be read by any QEMU since 0.10.  "compat=1.1" enables image format extensions that only QEMU 1.1 and newer understand (this is the default).
               Amongst others, this includes zero clusters, which allow efficient copy-on-read for sparse images.

               File name of a base image (see create subcommand)

               Image format of the base image

               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.

               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 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.

               If this option is set to "on", reference count updates are postponed with the goal of avoiding metadata I/O and improving performance. This is particularly interesting with cache=writethrough which doesn't batch metadata updates. The
               tradeoff is that after a host crash, the reference count tables must be rebuilt, i.e. on the next open an (automatic) "qemu-img check -r all" is required, which may take some time.

               This option can only be enabled if "compat=1.1" is specified.

           QEMU also supports various other image file formats for compatibility with older QEMU versions or other hypervisors, including VMDK, VDI, VHD (vpc), VHDX, qcow1 and QED. For a full list of supported formats see "qemu-img --help".  For a
           more detailed description of these formats, see the QEMU Emulation User Documentation.

           The main purpose of the block drivers for these formats is image conversion.  For running VMs, it is recommended to convert the disk images to either raw or qcow2 in order to achieve good performance.


标准的qcow2格式,不加密,磁盘动态拓展, 500G大小,发现如果太大会有性能问题

qemu-img create -f qcow2 img-data.qcow2 500G


modprobe nbd max_part=8


qemu-nbd -c /dev/nbd0 img-data.qcow2

qemu-nbd -d /dev/nbd0


# 也可以使用fdisk
gdisk /dev/nbd0


mkfs.xfs /dev/nbd0p1


nbd0      43:0    0   500G  0 disk
└─nbd0p1  43:1    0   500G  0 part


# 创建LVM分区
pvcreate /dev/nbd0p1
lvcreate -L 480G VolGroupImgData -n img
mkfs.xfs /dev/mapper/VolGroupImgData-img