在好多时侯linux安装时lvm分区,随着业务的降低redhat linux 9.0下载,原规划的c盘分区大小可能难以满足,这时侯就须要进行扩容操作,下边介绍怎样通过LVM实现扩容
环境信息
root@lvm:~# lsblk
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设备的全部c盘容量扩充到lvm--vg-root
扩充盘配置分区
使用下边的命令步入分区操作
fdisk /dev/nvme0n1
分区帮助
步入fdisk程序以后,通过输入m获取使用帮助
root@lvm:~# fdisk /dev/nvme0n1
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:~# lsblk
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分区了
低格
低格之前linux视频教程,须要先查看须要扩容的分区当前属于哪种文件系统
root@lvm:~# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
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:~#
可以看见linux安装时lvm分区,当前/使用的文件系统类型是ext4,所以我们在低格的时侯也是须要使用这个格式
执行命令
mkfs.ext4 /dev/nvme0n1p1
执行疗效
root@lvm:~# mkfs.ext4 /dev/nvme0n1p1
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:~# vgdisplay
--- 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:~# pvcreate /dev/nvme0n1p1
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:~# pvdisplay
--- 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 <卷组> <物理卷>
所以须要执行的命令就是
vgextend lvm-vg /dev/nvme0n1p1
执行疗效
root@lvm:~# vgextend lvm-vg /dev/nvme0n1p1
Volume group "lvm-vg" successfully extended
提示:卷组“lvmvg”已成功扩充
验证
root@lvm:~# vgdisplay
--- 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
此时可以看见,当前的VGSize弄成了24G了,说明逻辑卷扩充完成了,之后我们开始分配空间
句型
lvextend -L <调整大小> <卷组>