在很多时候,随着业务的增加,原规划的磁盘分区大小可能无法满足,这时候就需要进行扩容操作,下面介绍如何通过LVM 实现扩容
环境信息
root@lvm:~
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 487M 0 part /boot
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 19.5G 0 part
├─lvm--vg-root 254:0 0 18.6G 0 lvm /
└─lvm--vg-swap_1 254:1 0 980M 0 lvm [SWAP]
sr0 11:0 1 3.6G 0 rom
nvme0n1 259:0 0 5G 0 disk
root@lvm:~
场景设计
由于lvm--vg-root 分区无法满足需求,现需把nvme0n1 设备的全部磁盘容量扩展到lvm--vg-root
扩展盘配置
分区
使用下面的命令进入分区操作
fdisk /dev/nvme0n1
分区帮助
进入fdisk 程序之后,通过输入m 获取使用帮助
root@lvm:~
Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x985ce025.
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Command (m for help):
分区过程
创建gpt 分区并规划大小
Command (m for help): g
Created a new GPT disklabel (GUID: 67C1E069-6405-3C42-BE61-D115D319D2EB).
Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-10485726, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-10485726, default 10485726):
Created a new partition 1 of type 'Linux filesystem' and of size 5 GiB.
Command (m for help): p
Disk /dev/nvme0n1: 5 GiB, 5368709120 bytes, 10485760 sectors
Disk model: VMware Virtual NVMe Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 67C1E069-6405-3C42-BE61-D115D319D2EB
Device Start End Sectors Size Type
/dev/nvme0n1p1 2048 10485726 10483679 5G Linux filesystem
此时成功创建了一个5Gb 的分区
设置分区类型lvm
Command (m for help): t
Selected partition 1
Partition type or alias (type L to list all): 30
Changed type of partition 'Linux LVM' to 'Linux LVM'.
Command (m for help):
保存更改
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
root@lvm:~
查看分区
root@lvm:~
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 487M 0 part /boot
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 19.5G 0 part
├─lvm--vg-root 254:0 0 18.6G 0 lvm /
└─lvm--vg-swap_1 254:1 0 980M 0 lvm [SWAP]
sr0 11:0 1 3.6G 0 rom
nvme0n1 259:0 0 5G 0 disk
└─nvme0n1p1 259:2 0 5G 0 part
此时可以看到,有一个nvme0n1p1 分区了
格式化
格式化之前,需要先查看需要扩容的分区当前属于哪种文件系统
root@lvm:~
文件系统 类型 容量 已用 可用 已用% 挂载点
udev devtmpfs 958M 0 958M 0% /dev
tmpfs tmpfs 195M 736K 195M 1% /run
/dev/mapper/lvm--vg-root ext4 19G 1.4G 16G 9% /
tmpfs tmpfs 975M 0 975M 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda1 ext2 470M 85M 361M 19% /boot
tmpfs tmpfs 195M 0 195M 0% /run/user/0
tmpfs tmpfs 195M 0 195M 0% /run/user/1000
root@lvm:~
可以看到,当前/ 使用的文件系统类型是ext4 ,所以我们在格式化的时候也是需要使用这个格式
执行命令
mkfs.ext4 /dev/nvme0n1p1
执行效果
root@lvm:~
mke2fs 1.46.2 (28-Feb-2021)
Creating filesystem with 1310459 4k blocks and 327680 inodes
Filesystem UUID: caadad9d-34a2-4058-99e5-de133935a959
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
root@lvm:~
至此,物理卷创建完成,然后开始配置lvm
LVM 配置
获取当前VG 名称
执行下面的命令
vgdisplay
效果
root@lvm:~
--- Volume group ---
VG Name lvm-vg
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 <19.52 GiB
PE Size 4.00 MiB
Total PE 4997
Alloc PE / Size 4997 / <19.52 GiB
Free PE / Size 0 / 0
VG UUID XVVOI3-kdhu-xYUd-cCwS-s92f-MOOe-Vg31th
结果中可以得到,当前/ 分区使用的vg 名称是lvm-vg ,记住这个名称,继续
创建PV
语法
pvcreate <分区>
所以需要执行的是
pvcreate /dev/nvme0n1p1
执行之后会提示删除这个分区的数据
WARNING: ext4 signature detected on /dev/nvme0n1p1 at offset 1080. Wipe it?
警告:在偏移量为1080的/dev/nvme0n1p1上检测到ext4签名。擦干净?
这里选择y ,注意,执行这个操作会删除该分区的数据,请谨慎操作
创建过程
root@lvm:~
WARNING: ext4 signature detected on /dev/nvme0n1p1 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/nvme0n1p1.
Physical volume "/dev/nvme0n1p1" successfully created.
此时,PV 创建成功了,然后查询当前pv列表
root@lvm:~
--- Physical volume ---
PV Name /dev/sda5
VG Name lvm-vg
PV Size 19.52 GiB / not usable 2.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4997
Free PE 0
Allocated PE 4997
PV UUID af50QU-qih4-hV68-ZJ32-vA3E-H64Z-M7mNRv
"/dev/nvme0n1p1" is a new physical volume of "<5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/nvme0n1p1
VG Name
PV Size <5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID YrejbH-1UNn-h0HG-c7YJ-89QT-RJ1F-5yoerz
向卷组增加物理卷
语法
vgextend <卷组> <物理卷>
卷组 : 指的是需要扩容的卷VG Name ,在这里则是lvm-vg 物理卷 : 指的是新增的物理卷,在这里则是/dev/nvme0n1p1
所以需要执行的命令就是
vgextend lvm-vg /dev/nvme0n1p1
执行效果
root@lvm:~
Volume group "lvm-vg" successfully extended
提示: 卷组“lvm vg”已成功扩展
验证
root@lvm:~
--- Volume group ---
VG Name lvm-vg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size <24.52 GiB
PE Size 4.00 MiB
Total PE 6276
Alloc PE / Size 4997 / <19.52 GiB
Free PE / Size 1279 / <5.00 GiB
VG UUID XVVOI3-kdhu-xYUd-cCwS-s92f-MOOe-Vg31th
此时可以看到,当前的VG Size 变成了24G 了,说明逻辑卷扩展完成了,然后我们开始分配空间
语法
lvextend -L <调整大小> <卷组>
大小不得大于实际空间
实践
root@lvm:~
Size of logical volume lvm-vg/root changed from 18.56 GiB (4752 extents) to 22.56 GiB (5776 extents).
Logical volume lvm-vg/root successfully resized.
但是使用df -h 命令查看显示还是之前的大小
root@lvm:~
文件系统 容量 已用 可用 已用% 挂载点
udev 958M 0 958M 0% /dev
tmpfs 195M 736K 195M 1% /run
/dev/mapper/lvm--vg-root 19G 1.4G 16G 9% /
tmpfs 975M 0 975M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda1 470M 85M 361M 19% /boot
tmpfs 195M 0 195M 0% /run/user/0
tmpfs 195M 0 195M 0% /run/user/1000
这是因为我们没有刷新这信息,系统还没更新,下面开始刷新
语法
resize2fs 逻辑卷名
实际操作
root@lvm:~
resize2fs 1.46.2 (28-Feb-2021)
Filesystem at /dev/mapper/lvm--vg-root is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 3
The filesystem on /dev/mapper/lvm--vg-root is now 5914624 (4k) blocks long.
执行完成之后,再次查看挂载信息
root@lvm:~
文件系统 容量 已用 可用 已用% 挂载点
udev 958M 0 958M 0% /dev
tmpfs 195M 736K 195M 1% /run
/dev/mapper/lvm--vg-root 23G 1.5G 20G 7% /
tmpfs 975M 0 975M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda1 470M 85M 361M 19% /boot
tmpfs 195M 0 195M 0% /run/user/0
tmpfs 195M 0 195M 0% /run/user/1000
此时,/ 分区已经变成23GB 了,说明扩容已经完成了
|