虚拟化技术之kvm磁盘管理工具qemu-img

  在前边的博客中,我们大致了解了virsh这个工具对kvm虚拟机的一些操作,回顾请参考https://www.cnblogs.com/qiuhom-1874/tag/virsh/;今天我们来了解下kvm的磁盘管理工具qemu-img常用命令的用法;

  1、qemu-img的帮助信息

[root@node1 ~]# qemu-img -h
qemu-img version 1.5.3, Copyright (c) 2004-2008 Fabrice Bellard
usage: qemu-img command [command options]
QEMU disk image utility

Command syntax:
  check [-q] [-f fmt] [--output=ofmt] [-r [leaks | all]] [-T src_cache] filename
  create [-q] [-f fmt] [-o options] filename [size]
  commit [-q] [-f fmt] [-t cache] filename
  compare [-f fmt] [-F fmt] [-T src_cache] [-p] [-q] [-s] filename1 filename2
  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
  info [-f fmt] [--output=ofmt] [--backing-chain] filename
  map [-f fmt] [--output=ofmt] filename
  snapshot [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename
  rebase [-q] [-f fmt] [-t cache] [-T src_cache] [-p] [-u] -b backing_file [-F backing_fmt] filename
  resize [-q] filename [+ | -]size
  amend [-q] [-f fmt] [-t cache] -o options filename

Command parameters:
  'filename' is a disk image filename
  'fmt' is the disk image format. It is guessed automatically in most cases
  'cache' is the cache mode used to write the output disk image, the valid
    options are: 'none', 'writeback' (default, except for convert), 'writethrough',
    'directsync' and 'unsafe' (default for convert)
  'src_cache' is the cache mode used to read input disk images, the valid
    options are the same as for the 'cache' option
  'size' is the disk image size in bytes. Optional suffixes
    'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),
    'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P)  are
    supported. 'b' is ignored.
  'output_filename' is the destination disk image filename
  'output_fmt' is the destination format
  'options' is a comma separated list of format specific options in a
    name=value format. Use -o ? for an overview of the options supported by the
    used format
  '-c' indicates that target image must be compressed (qcow format only)
  '-u' enables unsafe rebasing. It is assumed that old and new backing file
       match exactly. The image doesn't need a working backing file before
       rebasing in this case (useful for renaming the backing file)
  '-h' with or without a command shows this help and lists the supported formats
  '-p' show progress of command (only certain commands)
  '-q' use Quiet mode - do not print any output (except errors)
  '-S' indicates the consecutive number of bytes (defaults to 4k) that must
       contain only zeros for qemu-img to create a sparse image during
       conversion. If the number of bytes is 0, the source will not be scanned for
       unallocated or zero sectors, and the destination image will always be
       fully allocated
  '--output' takes the format in which the output must be done (human or json)
  '-n' skips the target volume creation (useful if the volume is created
       prior to running qemu-img)

Parameters to check subcommand:
  '-r' tries to repair any inconsistencies that are found during the check.
       '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all
       kinds of errors, with a higher risk of choosing the wrong fix or
       hiding corruption that has already occurred.

Parameters to snapshot subcommand:
  'snapshot' is the name of the snapshot to create, apply or delete
  '-a' applies a snapshot (revert disk to saved state)
  '-c' creates a snapshot
  '-d' deletes a snapshot
  '-l' lists all snapshots in the given image

Parameters to compare subcommand:
  '-f' first image format
  '-F' second image format
  '-s' run in Strict mode - fail on different image size or sector allocation

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@node1 ~]#

  提示:从上面的帮助信息可以看到qemu-img这个命令分了check、create、commit、compare、convert、info、map、snapshot、rebase、resize、amend这些子命令,每个子命令都有特有的功能和语法以及选项;

  qemu-img create:用于创建磁盘文件使用的命令,语法格式:qemu-img create [-q] [-f fmt] [-o options] filename [size];其中-f用于指定磁盘的格式,常用的格式有,raw,qcow,qcow2;如果要查看create子命令还有那些选项可用,可以是使用-o ?来指定,如下示例;

  示例:查看create子命令的选项说明,可以使用qemu-img create -f qcow2 -o ? /kvm/images/test.img

[root@node1 ~]# qemu-img create -f qcow2 -o ? /kvm/images/test.img
Supported options:
size             Virtual disk size
compat           Compatibility level (0.10 or 1.1)
backing_file     File name of a base image
backing_fmt      Image format of the base image
encryption       Encrypt the image
cluster_size     qcow2 cluster size
preallocation    Preallocation mode (allowed values: off, metadata, falloc, full)
lazy_refcounts   Postpone refcount updates
[root@node1 ~]# 

  提示:以上示例表示要创建/kvm/images/test.img磁盘文件格式为qcow2它有哪些选项;这里需要说明一点在linux系统上文件的后缀只是起给人看的作用,方便人区分它,系统它不以后缀来确定它的格式;从上面的帮助信息可以看到create子命令的选项有size,该选项用于指定创建磁盘文件的大小;compat选项用于指定兼容性;backing_file用于指定备份文件名称;backing_fmt用于指定备份文件的格式;encryption用于指定是否加密,true表示加密false表示不加密,默认不指定是false;cluster_size指定磁盘的簇大小;preallocation用于指定磁盘预分配策略,其中off表示不预分配,metadata表示只预分配元数据大小,falloc表示随文件的增加而分配,full表示立即分配所有磁盘空间;默认是指不预分配磁盘空间大小;

  示例:创建一个2G的磁盘,分别用不同的预分配策略机制

[root@node1 ~]# qemu-img create -f qcow2 -o preallocation=off,size=2G /kvm/images/a1.img
Formatting '/kvm/images/a1.img', fmt=qcow2 size=2147483648 encryption=off cluster_size=65536 preallocation='off' lazy_refcounts=off 
[root@node1 ~]# ll -h /kvm/images/a1.img
-rw-r--r-- 1 root root 193K 8月  18 22:17 /kvm/images/a1.img
[root@node1 ~]# qemu-img info /kvm/images/a1.img
image: /kvm/images/a1.img
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 ~]# qemu-img  create -f qcow2 -o preallocation=metadata,size=2G /kvm/images/a2.img
Formatting '/kvm/images/a2.img', fmt=qcow2 size=2147483648 encryption=off cluster_size=65536 preallocation='metadata' lazy_refcounts=off 
[root@node1 ~]# ll -h /kvm/images/a2.img
-rw-r--r-- 1 root root 2.1G 8月  18 22:18 /kvm/images/a2.img
[root@node1 ~]# qemu-img info /kvm/images/a2.img
image: /kvm/images/a2.img
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 708K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 ~]# qemu-img  create -f qcow2 -o preallocation=falloc,size=2G /kvm/images/a3.img               
Formatting '/kvm/images/a3.img', fmt=qcow2 size=2147483648 encryption=off cluster_size=65536 preallocation='falloc' lazy_refcounts=off 
[root@node1 ~]# ll -h /kvm/images/a3.img
-rw-r--r-- 1 root root 2.1G 8月  18 22:19 /kvm/images/a3.img
[root@node1 ~]# qemu-img info /kvm/images/a3.img
image: /kvm/images/a3.img
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 2.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 ~]# qemu-img  create -f qcow2 -o preallocation=full,size=2G /kvm/images/a4.img              
Formatting '/kvm/images/a4.img', fmt=qcow2 size=2147483648 encryption=off cluster_size=65536 preallocation='full' lazy_refcounts=off 
[root@node1 ~]# ll -h /kvm/images/a4.img                                                  
-rw-r--r-- 1 root root 2.1G 8月  18 22:21 /kvm/images/a4.img
[root@node1 ~]# qemu-img info /kvm/images/a4.img                                          
image: /kvm/images/a4.img
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 2.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 ~]#

  提示:从上面的示例可以看到除了off指定创建的磁盘,我们看到的大小是一个很小的大小,其他模式在文件系统上表现形式都是我们指定大小的空间;从qemu-img info 命令来看,off和metadata disk size是很小的空间,虚拟空间是我们指定的大小;后者falloc和full disk大小和virtual size大小都是我们指定的大小;在文件系统上看到的磁盘文件之所以要大于我们指定的空间,是因为在文件系统上它作为一个文件形式存在,它也有元素数据信息的;

  qemu-img info :用于查看指定磁盘文件的详细信息;用法格式:qemu-img info [-f fmt] [--output=ofmt] [--backing-chain] filename

[root@node1 ~]# qemu-img info /kvm/images/centos7.qcow2 
image: /kvm/images/centos7.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 ~]# qemu-img info /kvm/images/win7.qcow2 
image: /kvm/images/win7.qcow2
file format: qcow2
virtual size: 50G (53687091200 bytes)
disk size: 8.5G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 ~]# 

  qemu-img check:对指定磁盘文件做检查;用法格式:qemu-img check [-q] [-f fmt] [--output=ofmt] [-r [leaks | all]] [-T src_cache] filename

[root@node1 ~]# qemu-img check /kvm/images/win7.qcow2
No errors were found on the image.
138808/819200 = 16.94% allocated, 27.22% fragmented, 0.00% compressed clusters
Image end offset: 9098887168
[root@node1 ~]# qemu-img check /kvm/images/centos7.qcow2 
No errors were found on the image.
Image end offset: 262144
[root@node1 ~]# 

  qemu-img snapshot:对指定磁盘文件做快照相关操作;语法格式:qemu-img snapshot [-q] [-l | -a snapshot | -c snapshot | -d snapshot] filename

  -c:表示创建快照

[root@node1 ~]# qemu-img snapshot -c snapshot_centos7_1 /kvm/images/centos7.qcow2 

  -l:查看指定磁盘文件的快照列表

[root@node1 ~]# qemu-img snapshot -l /kvm/images/centos7.qcow2                     
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         snapshot_centos7_1        0 2017-03-29 01:16:08   00:00:00.000
[root@node1 ~]#

  -a:应用快照,将磁盘恢复到做快照那一刻;

[root@node1 ~]# qemu-img snapshot -a snapshot_centos7_1 /kvm/images/centos7.qcow2 
[root@node1 ~]# 

  -d:删除快照

[root@node1 ~]# qemu-img snapshot -l /kvm/images/centos7.qcow2   
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         snapshot_centos7_1        0 2017-03-29 01:16:08   00:00:00.000
[root@node1 ~]# qemu-img snapshot -d snapshot_centos7_1 /kvm/images/centos7.qcow2
[root@node1 ~]# qemu-img snapshot -l /kvm/images/centos7.qcow2                   
[root@node1 ~]# 

  qemu-img convert:镜像格式转换,语法格式:qemu-img 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;-c表示压缩输出文件,但只有qcow2和qcow格式的镜像文件才支持压缩,而且这种压缩是只读的,如果压缩的扇区被重写,则会被重写为未压缩的数据。-p用于显示转换进度;-o用于指定输出文件的选项,比如是否加密呀,大小,等等。。

  示例1:不加输出格式直接转换qcow2格式的磁盘文件

[root@node1 ~]# qemu-img create -f qcow2 /kvm/images/test1.img 1G
Formatting '/kvm/images/test1.img', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@node1 ~]# qemu-img info /kvm/images/test1.img 
image: /kvm/images/test1.img
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 ~]# qemu-img convert /kvm/images/test1.img test1-1.img
[root@node1 ~]# qemu-img info test1-1.img
image: test1-1.img
file format: raw
virtual size: 1.0G (1073741824 bytes)
disk size: 0
[root@node1 ~]# 

  提示:qemu-img convert输入的文件格式它会自动识别,输出格式如果不指定它默认转换为raw格式;

  示例2:就vmdk格式的文件转换成qcow2格式的磁盘文件

[root@node1 ~]# cd /kvm/images/
[root@node1 images]# ls
centos7.qcow2  test1.img
[root@node1 images]# rz
rz waiting to receive.
 zmodem trl+C ȡ

  100%     512 KB  512 KB/s 00:00:01       0 Errors01-s001.vmdk...

[root@node1 images]# ls
CentOS-6.9-x86_64-000001-s001.vmdk  centos7.qcow2  test1.img
[root@node1 images]# qemu-img info CentOS-6.9-x86_64-000001-s001.vmdk 
image: CentOS-6.9-x86_64-000001-s001.vmdk
file format: vmdk
virtual size: 4.0G (4261412864 bytes)
disk size: 512K
cluster_size: 65536
Format specific information:
    cid: 4294967295
    parent cid: 4294967295
    create type: monolithicSparse
    extents:
        [0]:
            virtual size: 4261412864
            filename: CentOS-6.9-x86_64-000001-s001.vmdk
            cluster size: 65536
            format: 
[root@node1 images]# qemu-img convert ./CentOS-6.9-x86_64-000001-s001.vmdk -O qcow2 ./centos6.qcow2
[root@node1 images]# ll
总用量 1244560
-rw-r--r-- 1 root root     524288 4月  19 2020 CentOS-6.9-x86_64-000001-s001.vmdk
-rw-r--r-- 1 root root     197120 3月  29 01:33 centos6.qcow2
-rw-r--r-- 1 qemu qemu 1273561600 3月  29 01:32 centos7.qcow2
-rw-r--r-- 1 root root     197120 3月  29 01:24 test1.img
[root@node1 images]# qemu-img info ./centos6.qcow2
image: ./centos6.qcow2
file format: qcow2
virtual size: 4.0G (4261412864 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 images]#

  提示:我们可以根据把不同格式的磁盘文件相互转换,从而实现把虚拟机从一个平台迁移到另一个平台;

  qemu-img resize:动态增删磁盘的大小;语法格式:qemu-img resize [-q] filename [+ | -]size;

[root@node1 images]# ll -h
总用量 1.2G
-rw-r--r-- 1 root root 512K 4月  19 2020 CentOS-6.9-x86_64-000001-s001.vmdk
-rw-r--r-- 1 root root 193K 3月  29 01:33 centos6.qcow2
-rw-r--r-- 1 qemu qemu 1.2G 3月  29 02:03 centos7.qcow2
-rw-r--r-- 1 root root 193K 3月  29 01:24 test1.img
[root@node1 images]# qemu-img info test1.img 
image: test1.img
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 images]# qemu-img resize test1.img +1G
Image resized.
[root@node1 images]# qemu-img info test1.img      
image: test1.img
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 260K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 images]# ll -h 
总用量 1.2G
-rw-r--r-- 1 root root 512K 4月  19 2020 CentOS-6.9-x86_64-000001-s001.vmdk
-rw-r--r-- 1 root root 193K 3月  29 01:33 centos6.qcow2
-rw-r--r-- 1 qemu qemu 1.2G 3月  29 02:04 centos7.qcow2
-rw-r--r-- 1 root root 257K 3月  29 02:04 test1.img
[root@node1 images]# 

  提示:动态缩减空间必须保证磁盘空间大于里面存储的数据空间,在做删减操作有必要先备份一免磁盘损坏导致数据丢失;

[root@node1 images]# qemu-img info test1.img 
image: test1.img
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 200K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 images]# qemu-img resize test1.img -1G
qemu-img: qcow2 doesn't support shrinking images yet
qemu-img: This image does not support resize
[root@node1 images]# 

  提示:这里还需要注意一点,qcow2的格式磁盘不支持删减操作;

  2、示例:动态将创建好的磁盘插入现运行的虚拟机上;

  新建磁盘

[root@node1 ~]# qemu-img create -f qcow2 -o preallocation=metadata,size=2G /kvm/images/c1.img   
Formatting '/kvm/images/c1.img', fmt=qcow2 size=2147483648 encryption=off cluster_size=65536 preallocation='metadata' lazy_refcounts=off 
[root@node1 ~]# qemu-img info /kvm/images/c1.img
image: /kvm/images/c1.img
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 708K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 ~]# ll -h /kvm/images/c1.img
-rw-r--r-- 1 root root 2.1G 3月  29 02:18 /kvm/images/c1.img
[root@node1 ~]# 

  把磁盘附加到现运行的虚拟机上

[root@node1 ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 2     centos7                        running

[root@node1 ~]# virsh domblklist centos7 
目标     源
------------------------------------------------
vda        /kvm/images/centos7.qcow2
hda        -

[root@node1 ~]# virsh help attach-disk
  NAME
    attach-disk - 附加磁盘设备

  SYNOPSIS
    attach-disk    [--targetbus ] [--driver ] [--subdriver ] [--iothread ] [--cache ] [--io ] [--type ] [--mode ] [--sourcetype ] [--serial ] [--wwn ] [--rawio] [--address ] [--multifunction] [--print-xml] [--persistent] [--config] [--live] [--current]

  DESCRIPTION
    附加新磁盘设备.

  OPTIONS
    [--domain]   domain name, id or uuid
    [--source]   磁盘设备源
    [--target]   磁盘设备目标
    --targetbus   target bus of disk device
    --driver   磁盘设备驱动
    --subdriver   磁盘设备副驱动
    --iothread   IOThread to be used by supported device
    --cache   磁盘设备的缓存模式
    --io     io policy of disk device
    --type   目标设备类型
    --mode   设备读写模式
    --sourcetype   源类型  (block|file)
    --serial   磁盘设备序列号
    --wwn    磁盘设备的 wwn
    --rawio          需要 rawio 容量
    --address   磁盘设备地址
    --multifunction  在指定地址中使用多功能 pci
    --print-xml      输出 XML 文档而不是附加该磁盘
    --persistent     让实时更改持久
    --config         影响下一次引导
    --live           影响运行的域
    --current        影响当前域


[root@node1 ~]# virsh attach-disk centos7 /kvm/images/c1.img hda
错误:附加磁盘失败
错误:XML 错误:磁盘源 'hda' 和 '' 有重复的目标 '/kvm/images/c1.img'

[root@node1 ~]# virsh attach-disk centos7 /kvm/images/c1.img hdb
错误:附加磁盘失败
错误:不支持的操作:无法热插拔磁盘总线 'ide'。

[root@node1 ~]# virsh attach-disk centos7 /kvm/images/c1.img vdb
成功附加磁盘

[root@node1 ~]# virsh domblklist centos7 
目标     源
------------------------------------------------
vda        /kvm/images/centos7.qcow2
vdb        /kvm/images/c1.img
hda        -

[root@node1 ~]#

  提示:hd类型的磁盘上ide接口,ide接口类型的磁盘上不支持热插拔的,所以我们要给它识别成支持热插拔的磁盘类型,比如vd类型(virtual disk虚拟磁盘)

  验证:连接至centos7控制台,用fdisk -l命令查看 是否有新的磁盘加入到虚拟机?

[root@node1 ~]# virsh console centos7 
连接到域 centos7
换码符为 ^]


CentOS Linux 7 (Core)
Kernel 3.10.0-693.el7.x86_64 on an x86_64

localhost login: root
Password: 
Last login: Wed Aug 19 09:30:56 on tty1
[root@localhost ~]# fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a5c54

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1050623      524288   83  Linux
/dev/vda2         1050624    20971519     9960448   8e  Linux LVM

Disk /dev/mapper/centos-root: 10.2 GB, 10192158720 bytes, 19906560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 4 MB, 4194304 bytes, 8192 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/vdb: 2148 MB, 2148073472 bytes, 4195456 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@localhost ~]# 

  提示:可以看到有一个vdb的磁盘已经被虚拟机识别;

  在虚拟机内部对/dev/vdb进行分区,并格式化

[root@localhost ~]# fdisk /dev/vdb 
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xe6bebed1.

Command (m for help): p

Disk /dev/vdb: 2148 MB, 2148073472 bytes, 4195456 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe6bebed1

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-4195455, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4195455, default 4195455): 
Using default value 4195455
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/vdb: 2148 MB, 2148073472 bytes, 4195456 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe6bebed1

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     4195455     2096704   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# mkfs.xfs /dev/vdb1
meta-data=/dev/vdb1              isize=512    agcount=4, agsize=131044 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=524176, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# 

  提示:到此我们新加的硬盘就可以实现挂载使用了;

  挂载/dev/vdb1到/mnt

[root@localhost ~]# df -TH
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        11G  940M  9.3G  10% /
devtmpfs                devtmpfs  509M     0  509M   0% /dev
tmpfs                   tmpfs     521M     0  521M   0% /dev/shm
tmpfs                   tmpfs     521M   14M  507M   3% /run
tmpfs                   tmpfs     521M     0  521M   0% /sys/fs/cgroup
/dev/vda1               xfs       534M  124M  410M  24% /boot
tmpfs                   tmpfs     105M     0  105M   0% /run/user/0
[root@localhost ~]# mount /dev/vdb1 /mnt
[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       9.5G  897M  8.7G  10% /
devtmpfs                devtmpfs  486M     0  486M   0% /dev
tmpfs                   tmpfs     497M     0  497M   0% /dev/shm
tmpfs                   tmpfs     497M   13M  484M   3% /run
tmpfs                   tmpfs     497M     0  497M   0% /sys/fs/cgroup
/dev/vda1               xfs       509M  119M  391M  24% /boot
tmpfs                   tmpfs     100M     0  100M   0% /run/user/0
/dev/vdb1               xfs       2.0G   33M  2.0G   2% /mnt
[root@localhost ~]# 

  3、示例:动态将插入到虚拟机上的磁盘拆卸下来;

[root@node1 ~]# virsh domblklist centos7    
目标     源
------------------------------------------------
vda        /kvm/images/centos7.qcow2
vdb        /kvm/images/c1.img
hda        -

[root@node1 ~]# virsh help detach-disk 
  NAME
    detach-disk - 分离磁盘设备

  SYNOPSIS
    detach-disk   [--persistent] [--config] [--live] [--current] [--print-xml]

  DESCRIPTION
    分离磁盘设备。

  OPTIONS
    [--domain]   domain name, id or uuid
    [--target]   磁盘设备目标
    --persistent     让实时更改持久
    --config         影响下一次引导
    --live           影响运行的域
    --current        影响当前域
    --print-xml      print XML document rather than detach the disk


[root@node1 ~]# virsh  detach-disk centos7 vdb
成功分离磁盘

[root@node1 ~]# virsh domblklist centos7 
目标     源
------------------------------------------------
vda        /kvm/images/centos7.qcow2
hda        -

[root@node1 ~]# 

  提示:拆除硬盘可以直接使用virsh detach-disk 直接拆卸即可;

  验证:链接到虚拟机内部查看磁盘是否被拆卸了?

[root@node1 ~]# virsh console centos7 
连接到域 centos7
换码符为 ^]


[root@localhost ~]# fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a5c54

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1050623      524288   83  Linux
/dev/vda2         1050624    20971519     9960448   8e  Linux LVM

Disk /dev/mapper/centos-root: 10.2 GB, 10192158720 bytes, 19906560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-swap: 4 MB, 4194304 bytes, 8192 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@localhost ~]#

  提示:可以看到在虚拟机内部就只有vda没有vdb了,说明我们把磁盘成功拆除了;

  查看拆除下来的磁盘信息

[root@node1 ~]# qemu-img info /kvm/images/c1.img 
image: /kvm/images/c1.img
file format: raw
virtual size: 2.0G (2148073472 bytes)
disk size: 11M
[root@node1 ~]#

  提示:这里需要说明一下,我们在附加磁盘到虚拟内部以后,如果对磁盘进行了分区格式化,它的格式默认会变成raw格式;

  4、示例:给虚拟机做快照;

[root@node1 ~]# virsh help snapshot-create-as
  NAME
    snapshot-create-as - 使用一组参数生成快照

  SYNOPSIS
    snapshot-create-as  [--name ] [--description ] [--print-xml] [--no-metadata] [--halt] [--disk-only] [--reuse-external] [--quiesce] [--atomic] [--live] [--memspec ] [[--diskspec] ]...

  DESCRIPTION
    使用一组参数生成快照(磁盘和 RAM)

  OPTIONS
    [--domain]   domain name, id or uuid
    --name   快照名称
    --description   快照描述
    --print-xml      输出 XML 文档而不是生成 XML
    --no-metadata    提取快照但不生成元数据
    --halt           生成快照后停止域
    --disk-only      捕获磁盘状态而不是 vm 状态
    --reuse-external  重新使用任意现有外部文件
    --quiesce        静默虚拟机的文件系统
    --atomic         需要自动操作
    --live           提取实时快照
    --memspec   内存属性:[file=]name[,snapshot=type]
    [--diskspec]   磁盘属性: disk[,snapshot=type][,driver=type][,file=name]


[root@node1 ~]# virsh snapshot-create-as centos7 --name centos7-snap1 --description "touch test file before" 
已生成域快照 centos7-snap1
[root@node1 ~]# virsh snapshot-list centos7 
 名称               生成时间              状态
------------------------------------------------------------
 centos7-snap1        2017-03-29 02:44:58 +0800 running

[root@node1 ~]# 

  提示:以上命令是使用命令行参数来创建虚拟机快照,它默认会把快照的配置文件存储到/var/lib/libvirt/qemu/snapshot/虚拟机名称/快照名称.xml;

  查看快照文件

[root@node1 ~]# ll /var/lib/libvirt/qemu/snapshot/
总用量 0
drwxr-xr-x 2 root root 31 3月  29 02:45 centos7
[root@node1 ~]# tree /var/lib/libvirt/qemu/snapshot/
/var/lib/libvirt/qemu/snapshot/
└── centos7
    └── centos7-snap1.xml

1 directory, 1 file
[root@node1 ~]# cat /var/lib/libvirt/qemu/snapshot/centos7/centos7-snap1.xml 



  centos7-snap1
  touch test file before
  running
  1490726698
  
  
    
    
  
  
    centos7
    a15b793e-055b-4c91-89a5-ceba0c7ec98d
    1048576
    1048576
    2
    
      /machine
    
    
      hvm
      
    
    
      
      
    
    
      Broadwell
    
    
      
      
      
    
    destroy
    restart
    destroy
    
      
      
    
    
      /usr/libexec/qemu-kvm
      
        
        
        
        

  5、示例:还原虚拟机到做快照时的状态;

  首先连接至虚拟机内不在root目录下新建一个测试文件,然后将虚拟机关机,然后在还原到创建测试文件前端快照状态,然后开机,看看虚拟机是否回到测试文件创建前的状态了?

[root@node1 ~]# virsh console centos7 
连接到域 centos7
换码符为 ^]


[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# echo "this is test file" >test.xml
[root@localhost ~]# cat test.xml
this is test file
[root@localhost ~]# 
[root@node1 ~]# virsh destroy centos7 
域 centos7 被删除

[root@node1 ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 -     centos7                        关闭

[root@node1 ~]# virsh domstate centos7
关闭

[root@node1 ~]#

  将虚拟机欢迎到刚才创建的快照状态

[root@node1 ~]# virsh snapshot-list centos7 
 名称               生成时间              状态
------------------------------------------------------------
 centos7-snap1        2017-03-29 02:44:58 +0800 running

[root@node1 ~]# virsh snapshot-revert centos7 centos7-snap1 

[root@node1 ~]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 3     centos7                        running

[root@node1 ~]# virsh snapshot-current centos7 

  centos7-snap1
  touch test file before
  running
  1490726698
  
  
    
    
  
  
    centos7
    a15b793e-055b-4c91-89a5-ceba0c7ec98d
    1048576
    1048576
    2
    
      /machine
    
    
      hvm
      
    
    
      
      
    
    
      Broadwell
    
    
      
      
      
    
    destroy
    restart
    destroy
    
      
      
    
    
      /usr/libexec/qemu-kvm
      
        
        
        
        

  提示:从上面的信息可以看到,我们还原虚拟机到我们做的快照状态后,虚拟机处于运行状态,并且当前虚拟机快照是centos7_snap1这个快照是我们在新建测试文件前创建的快照;

  验证:连接虚拟控制台,查看测试文件是否还存在?

[root@node1 ~]# virsh console centos7 
连接到域 centos7
换码符为 ^]


[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# pwd
/root
[root@localhost ~]# 

  提示:可以看到测试文件没有了;

  查看当前虚拟机磁盘文件信息

[root@node1 ~]# virsh domblklist centos7 
目标     源
------------------------------------------------
vda        /kvm/images/centos7.qcow2
hda        -

[root@node1 ~]# qemu-img info /kvm/images/centos7.qcow2 
image: /kvm/images/centos7.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.5G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         centos7-snap1          294M 2017-03-29 02:44:58   01:33:32.709
Format specific information:
    compat: 1.1
    lazy refcounts: false
[root@node1 ~]# 

  提示:可以看到磁盘信息上有我们给虚拟机做到快照信息;

  有关虚拟机快照的命令有

[root@node1 ~]# virsh help snapshot
 Snapshot (help keyword 'snapshot'):
    snapshot-create                使用 XML 生成快照
    snapshot-create-as             使用一组参数生成快照
    snapshot-current               获取或者设定当前快照
    snapshot-delete                删除域快照
    snapshot-dumpxml               为域快照转储 XML
    snapshot-edit                  编辑快照 XML
    snapshot-info                  快照信息
    snapshot-list                  为域列出快照
    snapshot-parent                获取快照的上级快照名称
    snapshot-revert                将域转换为快照

[root@node1 ~]# 

  提示:以上命令和virsh 其他命令都比较类似,我这里就不过多阐述,不清楚命令语法可以查看每个命令的帮助信息来使用;

你可能感兴趣的:(虚拟化技术之kvm磁盘管理工具qemu-img)