Use kvmqemu img command

There are few commands used to create a virtual machine through KVM, and you can see a very detailed explanation through QEMU img - help. The common commands are as follows:

1. QEMU img info #info [- f FMT] [-- output = OFMT] [-- backing chain] filename

1 [root@KVM03-10 opt]# qemu-img info  centos2.1.raw
2 image: centos2.1.raw
3 file format: raw
4 virtual size: 10G (10737418240 bytes)
5 disk size: 1.2G

Disk file type:

RAW: RAW format, continuous, does not support advanced functions such as snapshot, but has excellent performance;

qcow2: support snapshot, discontinuous and good performance. qcow2, q=qemu, cow=copy on write, copy on write, how much content to write and how much disk space to use

 

2. QEMU img create create disk #create [-q] [-f fmt] [-o options] filename [size]

 1 [root@KVM03-10 opt]# qemu-img --help
 2 qemu-img version 1.5.3, Copyright (c) 2004-2008 Fabrice Bellard
 3 usage: qemu-img command [command options]
 4 QEMU disk image utility
 5 
 6 Command syntax:
 7   check [-q] [-f fmt] [--output=ofmt] [-r [leaks | all]] [-T src_cache] filename
 8   create [-q] [-f fmt] [-o options] filename [size]
 9   commit [-q] [-f fmt] [-t cache] filename
10   compare [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] filename1 filename2
11   convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename
12   info [-f fmt] [--output=ofmt] [--backing-chain] filename
13   map [-f fmt] [--output=ofmt] filename
14   snapshot [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename
15   rebase [-q] [-f fmt] [-t cache] [-T src_cache] [-p] [-u] -b backing_file [-F backing_fmt] filename
16   resize [-q] filename [+ | -]size
17   amend [-q] [-f fmt] [-t cache] -o options filename
18 
19 Command parameters:
20   'filename' is a disk image filename
21   'fmt' is the disk image format. It is guessed automatically in most cases
22   'cache' is the cache mode used to write the output disk image, the valid
23     options are: 'none', 'writeback' (default, except for convert), 'writethrough',
24     'directsync' and 'unsafe' (default for convert)
25   'src_cache' is the cache mode used to read input disk images, the valid
26     options are the same as for the 'cache' option
27   'size' is the disk image size in bytes. Optional suffixes
28     'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),
29     'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P)  are
30     supported. 'b' is ignored.
31   'output_filename' is the destination disk image filename
32   'output_fmt' is the destination format
33   'options' is a comma separated list of format specific options in a
34     name=value format. Use -o ? for an overview of the options supported by the
35     used format
36   '-c' indicates that target image must be compressed (qcow format only)
37   '-u' enables unsafe rebasing. It is assumed that old and new backing file
38        match exactly. The image doesn't need a working backing file before
39        rebasing in this case (useful for renaming the backing file)
40   '-h' with or without a command shows this help and lists the supported formats
41   '-p' show progress of command (only certain commands)
42   '-q' use Quiet mode - do not print any output (except errors)
43   '-S' indicates the consecutive number of bytes (defaults to 4k) that must
44        contain only zeros for qemu-img to create a sparse image during
45        conversion. If the number of bytes is 0, the source will not be scanned for
46        unallocated or zero sectors, and the destination image will always be
47        fully allocated
48   '--output' takes the format in which the output must be done (human or json)
49   '-n' skips the target volume creation (useful if the volume is created
50        prior to running qemu-img)
51 
52 Parameters to check subcommand:
53   '-r' tries to repair any inconsistencies that are found during the check.
54        '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all
55        kinds of errors, with a higher risk of choosing the wrong fix or
56        hiding corruption that has already occurred.
57 
58 Parameters to snapshot subcommand:
59   'snapshot' is the name of the snapshot to create, apply or delete
60   '-a' applies a snapshot (revert disk to saved state)
61   '-c' creates a snapshot
62   '-d' deletes a snapshot
63   '-l' lists all snapshots in the given image
64 
65 Parameters to compare subcommand:
66   '-f' first image format
67   '-F' second image format
68   '-s' run in Strict mode - fail on different image size or sector allocation
69 
70 Supported formats: vvfat vpc vmdk vhdx vdi ssh sheepdog rbd raw host_cdrom host_floppy host_device file qed qcow2 qcow parallels nbd iscsi gluster dmg tftp ftps ftp https http cloop bochs blkverify blkdebug

 [root@KVM03-10 ~]# qemu-img create /opt/test.raw 5G

#/opt/test.raw virtual disk storage location and file name;

#5G virtual disk size;

[root@KVM03-10 ~]# qemu-img create /opt/test.raw
qemu-img: /opt/test.raw: Image creation needs a size parameter
[root@KVM03-10 ~]# qemu-img create /opt/test.raw 5G
Formatting '/opt/test.raw', fmt=raw size=5368709120
[root@KVM03-10 opt]#
[root@KVM03-10 opt]# ll -h
total 5.3G
-rw-------. 1 qemu qemu  10G Oct 11 10:32 centos2.1.raw
-rw-r--r--. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 5.0G Oct 11 10:37 test.raw
[root@KVM03-10 opt]#

[root@KVM03-10 opt]# qemu-img info test.raw
image: test.raw
file format: raw
virtual size: 5.0G (5368709120 bytes)
disk size: 0
[root@KVM03-10 opt]

In fact, in the past, the dd command of Linux can also be used to create and be responsible for disks and realize conversion. For details, you can read man dd or refer to the blog of predecessors: https://www.cnblogs.com/ginvip/p/6370836.html

[root@KVM03-10 dev]# dd if=/dev/zero of=/opt/test-dd.raw bs=100M count=4
4+0 records in
4+0 records out
419430400 bytes (419 MB) copied, 1.93646 s, 217 MB/s
[root@KVM03-10 dev]# cd /opt/
[root@KVM03-10 opt]# ll -h
total 5.7G
-rw-------. 1 qemu qemu  10G Oct 11 10:46 centos2.1.raw
-rw-r--r--. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 400M Oct 11 10:58 test-dd.raw
-rw-r--r--. 1 root root 5.0G Oct 11 10:50 test.raw
[root@KVM03-10 opt]# qemu-img info test-dd.raw
image: test-dd.raw
file format: raw
virtual size: 400M (419430400 bytes)
disk size: 400M
[root@KVM03-10 opt]#

It is found that when the disk type parameter is not added, the default disk is the raw type disk. The raw and qcow2 formats have their own advantages and disadvantages. You can specify the disk format during the creation process or replace the disk format after the creation. Now let's learn the command of disk conversion.

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

'fmt' is the disk image format. It is guessed automatically in most cases

[root@KVM03-10 opt]# qemu-img  create -f qcow2 /opt/test.qcow2 2G
Formatting '/opt/test.qcow2', fmt=qcow2 size=2147483648 encryption=off cluster_size=65536 lazy_refcounts=off
[root@KVM03-10 opt]# ll
total 5520240
-rw-------. 1 qemu qemu 10737418240 Oct 11 11:21 centos2.1.raw
-rw-r--r--. 1 qemu qemu  4379901952 Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root  5368709120 Oct 11 11:19 test-1.raw
-rw-r--r--. 1 root root  5368709120 Oct 11 11:15 test-2.raw
-rw-r--r--. 1 root root      197120 Oct 11 11:24 test.qcow2
-rw-r--r--. 1 root root  5368709120 Oct 11 10:50 test.raw
[root@KVM03-10 opt]# qemu-img info test.qcow2
image: test.qcow2
file format: qcow2                          #File type: qcow2
virtual size: 2.0G (2147483648 bytes)      #Virtual disk size: 2G
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

3. Disk format conversion

  convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename

## - f original disk format

#- O destination disk format

[root@KVM03-10 opt]# qemu-img convert -f raw -O qcow2 test.raw raw-to-qcow2.qcow2
[root@KVM03-10 opt]# ll
total 5509896
-rw-------. 1 qemu qemu 10737418240 Oct 11 11:51 centos2.1.raw
-rw-r--r--. 1 qemu qemu  4379901952 Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root      197120 Oct 11 11:55 raw-to-qcow2.qcow2
-rw-r--r--. 1 root root  1073741824 Oct 11 11:35 resize.raw
-rw-r--r--. 1 root root      262656 Oct 11 11:36 test.qcow2
-rw-r--r--. 1 root root  5368709120 Oct 11 10:50 test.raw
[root@KVM03-10 opt]#
[root@KVM03-10 opt]# qemu-img info raw-to-qcow2.qcow2
image: raw-to-qcow2.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@KVM03-10 opt]#

 

After conversion, the original file will not be modified, but a file in the target disk format will be added. This is similar to computer video conversion software. After conversion, the original file is still there.

After the file is converted, modify the disk file used in the xml file of the virtual machine to start the disk file using qcow2 type.

  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/opt/centos2.1.raw.qcow2'/>           #Modify the disk file of the virtual machine
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>

[root@KVM03-10 ~]# virsh list --all
Id Name State
----------------------------------------------------
1 test-kvm running

It doesn't seem quite right. Although you can see that it is running through virsh list --all, you can find the startup exception through vnc connection. It is found that the parameters of disk type need to be modified!

  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/opt/centos2.1.raw.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>

 

It is normal to re enable after modification.

4. resize [-q] filename [+ | -]size

After creating the disk, you may need to expand the disk. At this time, you can use the QEMU img resize command

The test found that both qcow2 and raw format disk files support capacity expansion, but only raw format files support capacity reduction, qcow2 does not. This is related to the two disk file types, and raw is continuous. In addition, it should be noted that in the production environment, shrinking the disk is a dangerous operation and is not recommended.

[root@KVM03-10 opt]# ll -h
total 5.3G
-rw-------. 1 qemu qemu  10G Oct 11 11:27 centos2.1.raw
-rw-r--r--. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 1.0G Oct 11 11:30 resize.raw
-rw-r--r--. 1 root root 193K Oct 11 11:24 test.qcow2
-rw-r--r--. 1 root root 5.0G Oct 11 10:50 test.raw
[root@KVM03-10 opt]# qemu-img resize resize.raw +1G
Image resized.
[root@KVM03-10 opt]# ll -h
total 5.3G
-rw-------. 1 qemu qemu  10G Oct 11 11:27 centos2.1.raw
-rw-r--r--. 1 qemu qemu 4.1G Apr 27 20:56 CentOS-7.3-x86_64-DVD-1611.iso
-rw-r--r--. 1 root root 2.0G Oct 11 11:31 resize.raw
-rw-r--r--. 1 root root 193K Oct 11 11:24 test.qcow2
-rw-r--r--. 1 root root 5.0G Oct 11 10:50 test.raw
[root@KVM03-10 opt]#

Tags: kvm

Posted by l053r on Thu, 12 May 2022 03:59:09 +0300