KVM虚拟机qcow2格式硬盘扩容

KVM虚拟机适用命令行对qcow2格式硬盘扩容

Posted by Jae on February 26, 2019

1、环境

一台Centos7虚拟机,名称为jae-dev运行在KVM之上,磁盘信息如下,现将磁盘增加100G

[xidian@localhost ~]$ df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   26G  8.1G   18G   31% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G     0  1.9G    0% /dev/shm
tmpfs                    1.9G  8.8M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  179M  836M   18% /boot
tmpfs                    380M   28K  380M    1% /run/user/1000
[xidian@localhost ~]$

2、宿主机命令行扩展qcow2磁盘

2.1 查看需扩容的虚拟机硬盘信息

master@slave:/var/lib/libvirt/images$ virsh edit Jae-Dev
<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2'/>
  <source file='/var/lib/libvirt/images/jae-dev.qcow2'/>
  <target dev='hda' bus='ide'/>
  <address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>

可以看到因硬盘的路径为/var/lib/libvirt/images/jae-dev.qcow2,格式为qcow2等信息。

关闭虚拟机准备扩容。

2.2 宿主机内扩展虚拟机硬盘

master@slave:~$ sudo qemu-img resize /var/lib/libvirt/images/jae-dev.qcow2 +100G
[sudo] password for master:
Image resized.
master@slave:~$

从返回结果可以看到硬盘容量已经修改,但是虚拟机中的硬盘容量不会显示增加,需要启动虚拟机进行扩展磁盘。

3、进入虚拟机进行磁盘扩展

3.1 启动虚拟机并查看分区大小

[xidian@localhost ~]$ df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   26G  8.2G   18G   32% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G     0  1.9G    0% /dev/shm
tmpfs                    1.9G  8.9M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  179M  836M   18% /boot
tmpfs                    380M   28K  380M    1% /run/user/1000
[xidian@localhost ~]$

我们发现虚拟机容量并没有改变 使用fdisk -l查看分区大小

[root@localhost xidian]# fdisk -l

磁盘 /dev/sda:139.6 GB, 139586445312 字节,272629776 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000e341c

设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    62914559    30407680   8e  Linux LVM

磁盘 /dev/mapper/centos-root:27.9 GB, 27913093120 字节,54517760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-swap:3221 MB, 3221225472 字节,6291456 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

[root@localhost xidian]#

磁盘 /dev/sda:139.6 GB说明我们已经成功对磁盘进行了扩容,但是需要分区

3.2 开始分区

[root@localhost xidian]# fdisk /dev/sda  这里的/dev/sda是我们刚扩容的磁盘
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): p 输入

Disk /dev/sda: 139.6 GB, 139586445312 bytes, 272629776 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: 0x000e341c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    62914559    30407680   8e  Linux LVM

Command (m for help): n 输入
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p 输入
Partition number (3,4, default 3): 3 输入 我们创建的分区号为3
First sector (62914560-272629775, default 62914560): 回车默认
Using default value 62914560
Last sector, +sectors or +size{K,M,G} (62914560-272629775, default 272629775): 回车默认
Using default value 272629775
Partition 3 of type Linux and of size 100 GiB is set

Command (m for help): t 输入
Partition number (1-3, default 3): 3 输入
Hex code (type L to list all codes): 8e 输入
Changed type of partition 'Linux' to 'Linux LVM'

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.
[root@localhost xidian]# partprobe 键入
[root@localhost xidian]#

查看磁盘空间

[root@localhost xidian]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   26G  8.1G   18G  32% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  8.9M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               1014M  179M  836M  18% /boot
tmpfs                    380M  4.0K  380M   1% /run/user/42
tmpfs                    380M   24K  380M   1% /run/user/1000
[root@localhost xidian]#

发现磁盘容量并没有增加。

3.3 创建物理卷并加入卷组,扩展逻辑卷

这里使用pvs查看逻辑卷名称

[root@localhost xidian]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  centos lvm2 a--  <29.00g    0
[root@localhost xidian]#

逻辑卷名称为 centos.

下面创建物理卷并加入卷组

[root@localhost xidian]# pvs  下面VG对应的值就是卷组名为centos
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  centos lvm2 a--  <29.00g    0
[root@localhost xidian]# pvcreate /dev/sda3  键入命令
  Physical volume "/dev/sda3" successfully created.
[root@localhost xidian]# pvs 查看物理卷/dev/sda3大小为100G
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  centos lvm2 a--  <29.00g      0
  /dev/sda3         lvm2 ---  100.00g 100.00g
[root@localhost xidian]# vgextend centos /dev/sda3 键入命令
  Volume group "centos" successfully extended
[root@localhost xidian]# lvextend -l +100%FREE /dev/centos/root 键入命令
  Size of logical volume centos/root changed from <26.00 GiB (6655 extents) to 125.99 GiB (32254 extents).
  Logical volume centos/root successfully resized.
[root@localhost xidian]# resize2fs /dev/centos/root 键入命令,这里可能报下面的错误但是可以[解决](http://www.systemadmin.me.uk/?p=434)
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/centos/root
Couldn't find valid filesystem superblock.
[root@localhost xidian]#

如果报上面Couldn't find valid filesystem superblock.错误,使用下面命令:

[root@localhost xidian]# xfs_growfs /dev/mapper/centos-root 键入命令
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1703680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=6814720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=3327, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 6814720 to 33028096
[root@localhost xidian]#

这个时候已经扩容完成

[root@localhost xidian]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  126G  8.2G  118G   7% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  8.9M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               1014M  179M  836M  18% /boot
tmpfs                    380M  4.0K  380M   1% /run/user/42
tmpfs                    380M   28K  380M   1% /run/user/1000
[root@localhost xidian]#

关于报错的解决方案

报错解决方案

参考

参考


< script src = " / js / bootstrap.min.js " >