为lvm逻辑分区扩容

背景

如果你的设备空间有额外的硬盘空间,而原本的硬盘空间又不够时,可以进行扩容,前提是该分区是LVM分区。
# df -h

Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.9G 28K 3.9G 1% /dev/shm
tmpfs 3.9G 17M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 6.7G 5.8G 580M 92% /
/dev/vda1 240M 136M 88M 61% /boot
tmpfs 783M 0 783M 0% /run/user/0

步骤:
1.使用fdisk /dev/vda将剩余的空间分区,分别执行命令n,p,enter,enter,w即可,分别是创建一个分区,类型为primary,使用默认的分区号,使用剩余的所有硬盘空间,保存。

# fdisk -l /dev/vda

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000dd99c
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 526335 262144 83 Linux
/dev/vda2 526336 14680063 7076864 83 Linux

# 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): 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 (14680064-83886079, default 14680064):
Using default value 14680064
Last sector, +sectors or +size{K,M,G} (14680064-83886079, default 83886079):
Using default value 83886079
Partition 3 of type Linux and of size 33 GiB is set
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.

2.使用fdisk -l查看是否有新的分区生成:

# fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000dd99c
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 526335 262144 83 Linux
/dev/vda2 526336 14680063 7076864 83 Linux
/dev/vda3 14680064 83886079 34603008 83 Linux
Disk /dev/mapper/centos-root: 7243 MB, 7243563008 bytes, 14147584 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`

3.使用lsblk命令查看是否有新的分区信息,如果没有则重启设备。
4.使用pvcreate /dev/vda3命令将新的分区设置为PV物理卷。

# pvcreate /dev/vda3

File descriptor 3 (/var/log/ddicli/ddicli.log) leaked on pvcreate invocation. Parent PID 1944: bash
File descriptor 4 (pipe:[19155]) leaked on pvcreate invocation. Parent PID 1944: bash
File descriptor 5 (pipe:[19155]) leaked on pvcreate invocation. Parent PID 1944: bash
File descriptor 10 (/dev/urandom) leaked on pvcreate invocation. Parent PID 1944: bash
** Physical volume "/dev/vda3" successfully created.**

5.将PV物理卷加入卷组VG中:vgextend centos /dev/vda3

# vgextend centos /dev/vda3

File descriptor 3 (/var/log/ddicli/ddicli.log) leaked on vgextend invocation. Parent PID 1944: bash
File descriptor 4 (pipe:[19155]) leaked on vgextend invocation. Parent PID 1944: bash
File descriptor 5 (pipe:[19155]) leaked on vgextend invocation. Parent PID 1944: bash
File descriptor 10 (/dev/urandom) leaked on vgextend invocation. Parent PID 1944: bash
** Volume group "centos" successfully extended**`

6.扩容LV:lvextend -l +100%FREE /dev/mapper/centos-root

# lvextend -l +100%FREE /dev/mapper/centos-root

File descriptor 3 (/var/log/ddicli/ddicli.log) leaked on lvextend invocation. Parent PID 1944: bash
File descriptor 4 (pipe:[19155]) leaked on lvextend invocation. Parent PID 1944: bash
File descriptor 5 (pipe:[19155]) leaked on lvextend invocation. Parent PID 1944: bash
File descriptor 10 (/dev/urandom) leaked on lvextend invocation. Parent PID 1944: bash
Size of logical volume centos/root changed from <6.75 GiB (1727 extents) to 39.74 GiB (10174 extents).
** Logical volume centos/root successfully resized**.

# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 40G 0 disk
├─vda1 252:1 0 256M 0 part /boot
├─vda2 252:2 0 6.8G 0 part
│ └─centos-root 253:0 0 39.8G 0 lvm /
└─vda3 252:3 0 33G 0 part
** └─centos-root 253:0 0 39.8G 0 lvm /**

7.执行调整

# xfs_growfs /dev/mapper/centos-root

xfs_growfs: /dev/mapper/centos-root is not a mounted XFS filesystem

# resize2fs /dev/mapper/centos-root

resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/centos-root is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 5
The filesystem on /dev/mapper/centos-root is now 10418176 blocks long.

注:
xfs_growfs适用xfs文件系统,只能增加。
resizefs适用ext2/ext3/ext4文件系统,减少和增加都支持。

8.最后检查一下是不是扩容成功了。

# df -h

Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.9G 28K 3.9G 1% /dev/shm
tmpfs 3.9G 17M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 40G 5.8G 32G 16% /
/dev/vda1 240M 136M 88M 61% /boot
tmpfs 783M 0 783M 0% /run/user/0

命令列表

# df -h
# fdisk -l
# fdisk /dev/vda
# fdisk -l
# lsblk 未查看到新的分区则执行 # reboot
# pvcreate /dev/vda3
# vgextend centos /dev/vda3
# lvextend -l +100%FREE /dev/mapper/centos-root
# resize2fs /dev/mapper/centos-root
# df -h
# fdisk -l

学无止境,你说呢?
原文地址:https://www.cnblogs.com/moumouLiu/p/15020854.html