LVM 分区扩容
LVM 名词解释:
LVM (logical volume manager) 逻辑卷管理器
其中主要分为这几个概念:
-
物理卷 - Physical volume 简称 PV
物理卷在逻辑卷管理器中属于最底层的,任何的逻辑卷和卷组都必需依靠物理卷来建立,物理卷可以是一个完整的硬盘,也可以是硬盘中的莫一个分区。
-
卷组 - Volume group 简称 VG
卷组是建立在物理卷之上,一个卷组中可以包含一个或者多个物理卷。
-
逻辑卷 - Logical volume 简称 LV
逻辑卷类似于非 LVM 系统中的硬盘分区,在逻辑卷之上可以建立文件系统 (比如
/home
或者/usr
等)。
一个建立逻辑卷的流程如下:PV -> VG -> LV,物理卷包含卷组,卷组包含逻辑卷。
本例为 CentOS 7,一块磁盘,独立 /boot
分区,两个 LVM 分区,如下:
# root @ C7-SYSIN in ~ [12:41:56]
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 160G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 159G 0 part
├─centos-root 253:0 0 155G 0 lvm /
└─centos-swap 253:1 0 4G 0 lvm [SWAP]
sr0 11:0 1 1024M 0 rom
# root @ C7-SYSIN in ~ [12:41:56]
$ df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 155G 1.5G 154G 1% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 12M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 142M 873M 14% /boot
tmpfs tmpfs 378M 0 378M 0% /run/user/0
将该虚拟磁盘扩展到 260G。
说明:也可以添加一块磁盘,可以模拟新增了物理磁盘,只是下面盘符对应修改 “/dev/sda=/dev/sdb”,“/dev/sda3=/dev/sdb1”。
(1)创建分区
$ 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 #p 查看当前分区
Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 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: 0x000af364
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 335544319 166722560 8e Linux LVM
Command (m for help): n #n 新建分区
Partition type:
p primary (2 primary, 0 ex,tended, 2 free)
e ex,tended
Se,lect (default p): p #p 主分区
Partition number (3,4, default 3): #默认按顺序,直接回车
First sector (335544320-545259519, default 335544320): #默认直接回车
Using default value 335544320
Last sector, +sectors or +size{K,M,G} (335544320-545259519, default 545259519): #默认直接回车使用全部剩余空间
Using default value 545259519
Partition 3 of type Linux and of size 100 GiB is set
Command (m for help): p #p 再次查看分区
Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 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: 0x000af364
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 335544319 166722560 8e Linux LVM
/dev/sda3 335544320 545259519 104857600 83 Linux
Com,mand (m for help): w #w 保存
The partition table has been altered!
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)刷新分区
# root @ C7-SYSIN in ~ [13:31:00]
$ partprobe /dev/sda
# root @ C7-SYSIN in ~ [13:31:54]
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 260G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 159G 0 part
│ ├─centos-root 253:0 0 155G 0 lvm /
│ └─centos-swap 253:1 0 4G 0 lvm [SWAP]
└─sda3 8:3 0 100G 0 part
sr0 11:0 1 1024M 0 rom
(3)创建 PV
$ pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.
(4)查看 VG
$ vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <159.00 GiB
PE Size 4.00 MiB
Total PE 40703
Alloc PE / Size 40703 / <159.00 GiB
Free PE / Size 0 / 0
VG UUID Aul9M5-OJu8-3RB5-DU9Y-yi5m-ngyd-QyIKLw
VG 名称为 centos
(5)扩展 VG
使用 /dev/sda3 PV 扩展到 centos VG 中。
$ vgextend centos /dev/sda3
Volume group "centos" successfully extended
(6)查看 LV
$ lvdisplay
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID ntq8LZ-qivt-B6ij-AZWi-97a9-H2Q5-YxznfS
LV Write Access read/write
LV Creation host, time localhost, 2021-08-22 14:12:50 +0800
LV Status available
# open 1
LV Size <155.00 GiB
Current LE 39679
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID jvNgUR-KUsk-1hD4-h383-w3pc-OhBT-ZFMpyi
LV Write Access read/write
LV Creation host, time localhost, 2021-08-22 14:12:51 +0800
LV Status available
# open 2
LV Size 4.00 GiB
Current LE 1024
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
需要扩展的根分区 LV 为 /dev/centos/root
(7)将 VG 中的空闲空间扩展到根分区 LV
$ lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from <155.00 GiB (39679 extents) to 254.99 GiB (65278 extents).
Logical volume centos/root successfully resized.
(8)刷新根分区
如果是 ext4 文件系统(包括 2、3),使用 resize2fs 命令。
xfs_growfs /dev/centos/root
(9)验证结果
# root @ C7-SYSIN in ~ [13:39:53]
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 260G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 159G 0 part
│ ├─centos-root 253:0 0 255G 0 lvm /
│ └─centos-swap 253:1 0 4G 0 lvm [SWAP]
└─sda3 8:3 0 100G 0 part
└─centos-root 253:0 0 255G 0 lvm /
sr0 11:0 1 1024M 0 rom
# root @ C7-SYSIN in ~ [13:39:53]
$ df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 255G 1.5G 254G 1% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 12M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 142M 873M 14% /boot
tmpfs tmpfs 378M 0 378M 0% /run/user/0
可以看到根目录容量扩展成功。