命令记不住

登陆

KVM 环境下虚拟机硬盘格式

KVM环境下虚拟机硬盘格式

1. 理解KVM环境下虚拟机硬盘格式

在物理服务器中,会使用硬盘来安装操作系统以及存放数据。虚拟机也是一样,同样需要硬盘安装操作系统以及存放数据,但在虚拟化环境中,没有物理存在的硬盘,使用的是虚拟出来硬盘,其实质就是硬盘镜像文件。既然使用硬盘镜像文件就存在格式,一般来说,KVM环境中会使用到两种镜像格式,一种是raw,另外一种就是qcow2(qcow是上一代,已经不使用)。

kvm虚拟机虚拟磁盘格式转换和快照管理

  • raw:裸格式,占用空间比较大,不支持快照功能,性能较好,不方便传输 总50G 占用2G
  • qcow 虚拟磁盘格式、过渡
  • qcow2:cow (copy on write)占用空间小,支持快照,性能比raw差一点(不影响),方便传输 总50G 占用2G
  • qemu 写时复制

磁盘相关命令

//qemu-img	info	查看虚拟机磁盘详细信息
[root@kvm01 opt]# qemu-img info centos2.raw

//qemu-img	create	创建虚拟机磁盘
[root@kvm01 opt]# qemu-img  create -f qcow2 /opt/create.qcow2  10G

//qemu-img	resize	调整虚拟机磁盘容量大小
[root@kvm01 opt]# qemu-img resize create.qcow2 +2T
[root@kvm01 opt]# qemu-img resize create.qcow2 5T

//qemu-img	convert	转换虚拟磁盘格式
raw转qcow2:qemu-img convert -f raw     -O qcow2           x86cloud.raw   x86cloud.qcow2

                     convert [-f fmt]   [-O output_fmt]    filename     output_filename
// 并修改xml文件的磁盘路径和磁盘类型(type='qcow2')

1
在10G的盘创建1000G的磁盘(精简置备)

[root@kvm01 opt]# qemu-img  create -f qcow2 /opt/create.qcow2  1000G

2.raw格式介绍

raw硬盘格式是KVM虚拟机原始的硬盘镜像,属于二进制镜像文件,性能好但功能简单,使用raw格式会占用大量的存储空间,一次性会把创建的空间全部使用,比如创建了10GB空间直接使用10GB,不管究竟使用多少,同时raw格式需要ext3/ext4文件系统支持。

使用命令qemu-img create可以创建raw镜像文件,如果不指定参数默认创建raw格式,具体的创建过程如下:

root@kvm-server:/vm-win# qemu-img create win10 20G  #创建名为win10镜像文件
Formatting 'win10', fmt=raw size=21474836480
root@kvm-server:/vm-win# ls  #查看新创建的win10镜像文件
win10  win2012.raw  win7-02.qcow2  win7.raw
root@kvm-server:/vm-win# qemu-img info win10  #查看新创建的win10镜像文件信息
image: win10
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 0

raw格式支持稀疏文件特性,所谓的稀疏文件特性就是文件系统会把分配的空字节文件记录在元数据中,而不会实际占用硬盘空间,使用ls命令或者是du命令查看raw镜像文件会得出大小不一样的结果。

root@kvm-server:/vm-win# ls -lh win2012.raw  #使用ls命令查看镜像文件大小20GB
-rw------- 1 libvirt-qemu kvm 20G Nov  4 12:45 win2012.raw
root@kvm-server:/vm-win# du -h win2012.raw  #使用du命令查镜像文件实际使用7.5GB
7.5G    win2012.raw

整体来说,raw格式可以满足生产环境虚拟机的使用,但需要注意的的,raw格式不支持虚拟机快照,如果生产环境需要使用快照,可以通过命令将raw格式转换为qcow2格式,后续章节会进行介绍。

3.qcow2格式介绍

qcow2格式是qcow格式的升级版,qcow格式基本被qcow2格式所取代,qcow2格式新型的KVM虚拟机硬盘镜像,性能一般但功能多,在使用镜像部署虚拟机时使用到,使用qcow2格式减少空间,类似于精简置备,比如创建了10GB,操作系统只使用了5GB,那么实际使用空间就是5G,硬盘随使用动态增长。

使用命令qemu-img create加参数可以创建qcow2镜像文件,具体的创建过程如下:

root@kvm-server:/vm-win# qemu-img create -f qcow2 win10.qcow2 20G
Formatting 'win10.qcow2', fmt=qcow2 size=21474836480 cluster_size=65536 lazy_refcounts=off refcount_bits=16
root@kvm-server:/vm-win# ls
win10  win10.qcow2  win2012.raw  win7-02.qcow2  win7.raw
root@kvm-server:/vm-win# qemu-img info win10.qcow2
image: win10.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
root@kvm-server:/vm-win#

与raw格式一样,qcow2格式支持支持稀疏文件特性,使用ls命令或者是du命令查看qcow2镜像文得出大小是一样的。

root@kvm-server:/vm-win# ls -lh win10.qcow2     
-rw-r--r-- 1 root root 193K Nov  4 13:08 win10.qcow2
root@kvm-server:/vm-win# du -h win10.qcow2    
196K    win10.qcow2

qcow2格式支持其他一些高级特性,例如虚拟机快照、压缩、加密等,因此,qcow2格式也是目推荐使用的虚拟机硬盘镜像格式。

4.raw/qcow2格式对比

了解raw以及qcow2格式后,将其特性进行对比以便于选择。

功能 raw格式 qcow2格式
空间占用 全部使用 实际使用
虚拟机快照 不支持 支持
镜像文件加密 不支持 支持
文件压缩 不支持 支持
格式转换 支持 支持
文件删除后空间占用 空间变小 空间不变

通过基本的对比,对于生产环境来说,多数的选择是使用qcow2格式,当然也可以使用raw格式,并且在后续也可以进行格式之间的转换。

5.raw/qcow2格式常见操作

生产环境中会经常根据需要对两种格式进行操作,常见的操作有格式转换、增加硬盘容量等。

1.将raw格式转换为qcow2格式

第1步,进行格式转换注意需要关闭虚拟机电源,使用命令“virsh list --all”查看虚拟机运行状态。

root@kvm-server:/vm-win# virsh list --all
 Id    Name                           State
 8     win2012                        running
 19    centos7-04                     running
 33    win7-02                        running
 -     centos7                        shut off
 -     centos7-02                     shut off
 -     centos7-03                     shut off
 -     win7                           shut off
 -     win7-kvm                       shut off

第2步,使用命令“ls”查看虚拟机硬盘文件。

root@kvm-server:/vm-win# ls
win10  win10.qcow2  win2012.raw  win7-02.qcow2  win7-kvm.raw  win7.raw

第3步,使用命令“qemu-img convert -f raw -O qcow2 win7-kvm.raw win7-kvm.qcow2”进行格式转换。

root@kvm-server:/vm-win# qemu-img convert -f raw -O qcow2 win7-kvm.raw win7-kvm.qcow2

第4步,使用命令“ls”查看通过转换生成的新文件,注意原虚拟机硬盘文件保留。

root@kvm-server:/vm-win# ls
win10  win10.qcow2  win2012.raw  win7-02.qcow2  win7-kvm.qcow2  win7-kvm.raw  win7.raw

第5步,使用命令“qemu-img info win7-kvm.qcow2”查看新文件相关系统。

root@kvm-server:/vm-win# qemu-img info win7-kvm.qcow2
image: win7-kvm.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 19G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
root@kvm-server:/vm-win# qemu-img info win7-kvm.raw
image: win7-kvm.raw
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 19G

第6步,使用命令“virsh edit win7-kvm”编辑虚拟机配置文件,修改硬盘文件格式以及路径。

root@kvm-server:/vm-win# virsh edit win7-kvm
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/vm-win/win7-kvm.qcow2'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
Domain win7-kvm XML configuration edited.

第7步,使用命令“virsh start win7-kvm”启动虚拟机验证。

root@kvm-server:/vm-win# virsh start win7-kvm
Domain win7-kvm started

第8步,虚拟机启动正常,说明硬盘格式转换正常,如果无法启动或启动报错说明硬盘格式转换出现问题。

2、 增加硬盘容量

对于虚拟机硬盘扩容可以分为Windows系统以及Linux系统,其操作方式基本相同,分别进行介绍。

(1)Windows虚拟机硬盘扩容

第1步,增加硬盘容量需要关闭虚拟机电源,使用命令“virsh shutdown win7-kvm”关闭虚拟机电源。

root@kvm-server:/vm-win# virsh shutdown win7-kvm    
Domain win7-kvm is being shutdown
root@kvm-server:/vm-win# virsh list --all
 Id    Name                           State
 8     win2012                        running
 19    centos7-04                     running
 33    win7-02                        running
 -     centos7                        shut off
 -     centos7-02                     shut off
 -     centos7-03                     shut off
 -     win7                           shut off
 -     win7-kvm                       shut off

第2步,使用命令“qemu-img resize win7-kvm.qcow2 +10G”增加硬盘容量10GB。

root@kvm-server:/vm-win# qemu-img resize win7-kvm.qcow2 +10G
Image resized.

第3步,使用命令“qemu-img info win7-kvm.qcow2”验证容量是否增加。

root@kvm-server:/vm-win# qemu-img info win7-kvm.qcow2
image: win7-kvm.qcow2
file format: qcow2
virtual size: 30G (32212254720 bytes)
disk size: 19G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

第4步,使用命令“virsh start win7-kvm”启动虚拟机,同时使用vnc viewer连接到虚拟机进行查看,Windows操作系统可以通过扩展卷的方式将新容量添加到C盘,扩容后C盘容量为29.9GB。

(2)Linux虚拟机硬盘扩容

第1步,使用命令“fdisk -l”查看虚拟机硬盘信息,可以看到/dev/vda容量为16.1GB。

[root@kvm-centos ~]# fdisk -l
Disk /dev/vda: 16.1 GB, 16106127360 bytes, 31457280 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: 0x000043d2
   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200     4196351     1048576   82  Linux swap / Solaris
/dev/vda3         4196352    20971519     8387584   83  Linux

第2步,使用命令“df -Th”查看各分区使用情况,可以看到根分区为8GB。

[root@kvm-centos ~]# df -Th  
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda3      xfs       8.0G  1.1G  7.0G  13% /
devtmpfs       devtmpfs  911M     0  911M   0% /dev
tmpfs          tmpfs     920M     0  920M   0% /dev/shm
tmpfs          tmpfs     920M  8.5M  912M   1% /run
tmpfs          tmpfs     920M     0  920M   0% /sys/fs/cgroup
/dev/vda1      xfs      1014M  135M  880M  14% /boot
tmpfs          tmpfs     184M     0  184M   0% /run/user/0

第3步,关闭Linux虚拟机电源,在KVM主机上使用命令“qemu-img info centos7-04.qcow2”查看虚拟机硬盘信息,容量为15GB,对容量进行调整。

root@kvm-server:/vm-linux# qemu-img info centos7-04.qcow2   
image: centos7-04.qcow2
file format: qcow2
virtual size: 15G (16106127360 bytes)
disk size: 275M
cluster_size: 65536
backing file: centos7.raw
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
corrupt: false

第4步,使用命令“qemu-img resize centos7-04.qcow2 +5G”增加5GB硬盘容量。

root@kvm-server:/vm-linux# qemu-img resize centos7-04.qcow2 +5G       
Image resized.
root@kvm-server:/vm-linux# qemu-img info centos7-04.qcow2    
image: centos7-04.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 275M
cluster_size: 65536
backing file: centos7.raw
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
corrupt: false

第5步,在KVM主机上使用命令“virsh start centos7-04”打开虚拟机电源。

root@kvm-server:/vm-linux# virsh start centos7-04
Domain centos7-04 started

第6步,使用命令“virsh console centos7-04”连接到虚拟机控制台。

root@kvm-server:/vm-linux# virsh console centos7-04
Connected to domain centos7-04

第7步,使用命令“fdisk -l”查看虚拟机硬盘信息,可以看到/dev/vda容量已经变为21.5GB,说明扩容成功。

[root@kvm-centos ~]# fdisk -l
Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000043d2
   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200     4196351     1048576   82  Linux swap / Solaris
/dev/vda3         4196352    20971519     8387584   83  Linux

第8步,使用命令“df -Th”查看各分区使用情况,可以看到根分区依旧为8GB,说明扩容后的容量还未应用到分区。

[root@kvm-centos ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda3      xfs       8.0G  1.1G  7.0G  13% /
devtmpfs       devtmpfs  911M     0  911M   0% /dev
tmpfs          tmpfs     920M     0  920M   0% /dev/shm
tmpfs          tmpfs     920M  8.5M  912M   1% /run
tmpfs          tmpfs     920M     0  920M   0% /sys/fs/cgroup
/dev/vda1      xfs      1014M  135M  880M  14% /boot
tmpfs          tmpfs     184M     0  184M   0% /run/user/0

第9步,使用命令“fdisk /dev/vda”命令删除根分区进行重建。

[root@kvm-centos ~]# fdisk /dev/vda
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.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

第10步,在“Command”后输入d,删除根分区。

Command (m for help): d
Partition number (1-3, default 3): 
Partition 3 is deleted

第11步,在“Command”后输入p,查看删除后的分区信息。

Command (m for help): p
Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000043d2
   Device Boot      Start         End      Blocks   Id  System
/dev/vda1            2048     2099199     1048576   83  Linux
/dev/vda2         2099200     4196351     1048576   82  Linux swap / Solaris

第12步,在“Command”后输入n,创建新的根分区,使用默认值即可。

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

第13步,在“Command”后输入p,查看新建根分区信息。

Command (m for help): p
Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000043d2
   Device Boot      Start         End      Blocks   Id  System
/dev/vda1            2048     2099199     1048576   83  Linux
/dev/vda2         2099200     4196351     1048576   82  Linux swap / Solaris
/dev/vda3         4196352    41943039    18873344   83  Linux

第14步,在“Command”后输入w,保存配置退出。

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

第15步,使用命令“partprobe /dev/vda”激活容量。

[root@kvm-centos ~]# partprobe /dev/vda

第16步,使用命令“xfs_growfs /dev/vda3”刷新容量。

[root@kvm-centos ~]#xfs_growfs /dev/vda3

第17步,使用命令“df -Th” 查看各分区使用情况,可以看到根分区依旧为18GB,说明扩容后的容量成功应用到分区。

[root@kvm-centos ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda3      xfs        18G  889M   18G   5% /
devtmpfs       devtmpfs  911M     0  911M   0% /dev
tmpfs          tmpfs     920M     0  920M   0% /dev/shm
tmpfs          tmpfs     920M  8.4M  912M   1% /run
tmpfs          tmpfs     920M     0  920M   0% /sys/fs/cgroup
/dev/vda1      xfs      1014M  135M  880M  14% /boot
tmpfs          tmpfs     184M     0  184M   0% /run/user/0
推荐阅读