KVM虚拟机无损扩容磁盘(非LVM)

一、非系统盘
整体流程:
1、KVM虚拟机关机
2、宿主机使用qemu-img改变虚拟机镜像文件的大小
3、虚拟机开机,磁盘大小增加,但文件系统大小没有变化
4、使用growpart扩容分区大小
5、使用resize2fs扩大文件系统

以vdb为例,原始大小20G

[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 50G 0 disk
├─vda1 252:1 0 200M 0 part /boot
└─vda2 252:2 0 49.8G 0 part
└─VolGroup-LogVol00 (dm-0) 253:0 0 49.8G 0 lvm /
vdb 252:16 0 20G 0 disk

分区############################################################################
[root@localhost ~]# fdisk /dev/vdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x96c89827.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won’t be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).

Command (m for help): p

Disk /dev/vdb: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x96c89827

Device Boot Start End Blocks Id System

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-41610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-41610, default 41610):
Using default value 41610

Command (m for help): p

Disk /dev/vdb: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x96c89827

Device Boot Start End Blocks Id System
/dev/vdb1 1 41610 20971408+ 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.ext4 /dev/vdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242852 blocks
262142 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

挂载目录############################################################################
[root@localhost ~]# mkdir /data
[root@localhost ~]# mount /dev/vdb1 /data/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol00
50G 5.4G 42G 12% /
tmpfs 1.9G 16K 1.9G 1% /dev/shm
/dev/vda1 194M 35M 150M 19% /boot
/dev/vdb1 20G 172M 19G 1% /data

关机,宿主机调整vdb镜像文件的大小,增加30G##################################################
[root@uat-server11 data-01]# qemu-img resize dearda-test-vdb.img +30G
Image resized.
[root@uat-server11 data-01]# qemu-img info dearda-test-vdb.img
image: dearda-test-vdb.img
file format: raw
virtual size: 50G (53687091200 bytes)
disk size: 453M

虚拟机开机,vdb大小增加,vdb1没有变化#######################################################
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 50G 0 disk
├─vda1 252:1 0 200M 0 part /boot
└─vda2 252:2 0 49.8G 0 part
└─VolGroup-LogVol00 (dm-0) 253:0 0 49.8G 0 lvm /
vdb 252:16 0 50G 0 disk
└─vdb1 252:17 0 20G 0 part /data

安装growpart(需要安装epel源)###############################################################
yum install cloud-utils-growpart

扩容vdb1############################################################################
[root@localhost ~]# growpart /dev/vdb 1
CHANGED: partition=1 start=63 old: size=41942817 end=41942880 new: size=104857137,end=104857200

查看lsblk和fdisk,大小已增加#################################################################
[root@localhost ~]# fdisk /dev/vdb

WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).

Command (m for help): p

Disk /dev/vdb: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x96c89827

Device Boot Start End Blocks Id System
/dev/vdb1 1 104025 52428568+ 83 Linux

[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 50G 0 disk
├─vda1 252:1 0 200M 0 part /boot
└─vda2 252:2 0 49.8G 0 part
└─VolGroup-LogVol00 (dm-0) 253:0 0 49.8G 0 lvm /
vdb 252:16 0 50G 0 disk
└─vdb1 252:17 0 50G 0 part /data

此时df -h看到文件系统还未增加,执行resize2fs扩容###############################################
[root@localhost ~]# e2fsck -f /dev/vdb1
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vdb1: 12/1310720 files (0.0% non-contiguous), 126290/5242852 blocks

[root@localhost ~]# resize2fs /dev/vdb1
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vdb1 to 13107142 (4k) blocks.
The filesystem on /dev/vdb1 is now 13107142 blocks long.

/data目录容量已增加############################################################################
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol00
50G 5.4G 42G 12% /
tmpfs 1.9G 12K 1.9G 1% /dev/shm
/dev/vda1 194M 35M 150M 19% /boot
/dev/vdb1 50G 180M 47G 1% /data

二、系统盘,根分区无LVM,流程与非系统盘一样
[root@dearda-test2 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 15G 0 disk
├─vda1 252:1 0 500M 0 part /boot
├─vda2 252:2 0 1G 0 part [SWAP]
└─vda3 252:3 0 13.5G 0 part /
[root@dearda-test2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda3 14G 2.2G 11G 17% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/vda1 485M 38M 422M 9% /boot

宿主机操作扩容虚拟机镜像,增加10G,重启后大小更新
[root@dearda-test2 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 25G 0 disk
├─vda1 252:1 0 500M 0 part /boot
├─vda2 252:2 0 1G 0 part [SWAP]
└─vda3 252:3 0 13.5G 0 part /
sr0 11:0 1 1024M 0 rom

growpart更新分区大小,并重启虚拟机
[root@dearda-test2 yum.repos.d]# growpart /dev/vda 3
CHANGED: partition=3 start=3123200 old: size=28334080 end=31457280 new: size=49304896,end=52428096
[root@dearda-test2 yum.repos.d]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 25G 0 disk
├─vda1 252:1 0 500M 0 part /boot
├─vda2 252:2 0 1G 0 part [SWAP]
└─vda3 252:3 0 13.5G 0 part /
sr0 11:0 1 1024M 0 rom
[root@dearda-test2 yum.repos.d]# reboot

重启后,vda3大小更新
[root@dearda-test2 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 25G 0 disk
├─vda1 252:1 0 500M 0 part /boot
├─vda2 252:2 0 1G 0 part [SWAP]
└─vda3 252:3 0 23.5G 0 part /

此时根目录大小未变化
[root@dearda-test2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda3 14G 2.4G 11G 19% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/vda1 485M 38M 422M 9% /boot

使用resize2fs扩大文件系统
[root@dearda-test2 ~]# resize2fs /dev/vda3
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vda3 is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vda3 to 6163112 (4k) blocks.
The filesystem on /dev/vda3 is now 6163112 blocks long.

根目录已扩容
[root@dearda-test2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda3 24G 2.4G 20G 11% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/vda1 485M 38M 422M 9% /boot

三、系统盘,根分区有LVM
1、加硬盘,加入VG
2、调整硬盘大小,pvresize

growpart对于没有分区的硬盘,即/dev/vdb(而不是/dev/vdb1)直接挂载文件目录这种情况不支持

对于整块盘不分区直接挂在目录的做法,国外有个解释可以看下:
Creating a filesystem on a whole disk rather than a partition is possible, but unusual. The documentation only explicitly mentions the partition because that’s the most usual case (it does say usually). You can create a filesystem on anything that acts sufficiently like a fixed-size file, i.e. something where if you write data at a certain location and read back from the same location then you get back the same data. This includes whole disks, disk partitions, and other kinds of block devices, as well as regular files (disk images).

After doing mkfs.fat -n A /dev/sdb, you no longer have a partition on that disk. Beware that the kernel still thinks that the disk has a partition, because it keeps the partition table cached in memory. But you shouldn’t try to use /dev/sdb1 anymore, since it no longer exists; writing to it would corrupt the filesystem you created on /dev/sdb since /dev/sdb1 is a part of /dev/sdb (everything except a few hundred bytes at the beginning). Run the command partprobe as root to tell the kernel to re-read the partition table.

While creating a filesystem on a whole disk is possible, I don’t recommend it. Some operating systems may have problems with it (I think Windows would cope but some devices such as cameras might not), and you lose the possibility of creating other partitions. See also The merits of a partitionless filesystem