KVM 磁盘创建 qemu-img create

目的

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

Man手册

NAME
       qemu-img - QEMU disk image utility

SYNOPSIS
       usage: qemu-img command [command options]

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

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

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

       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:

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

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

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

       Other
           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

创建nbd设备

modprobe nbd max_part=8

挂载qcow2设备

#挂载
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

单纯的xfs分区后期拓展会有些麻烦,还是建立lvm会合适些

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

使用一天后的补充结论

创建磁盘一定要预先分配空间,否则性能太差,性能太差,性能太差

当前还没有任何评论

写下你最简单的想法