扩容背景:
项目的centos服务器安装mongodb,考虑后期数据库增大,目前的2T盘,给数据库划分的分区为/var分区是1.5T,需要进行扩容
原磁盘和分区大小
之前修复的/dev/dm-2,这里可以看到其实映射的是/var分区
ls -l /dev/mapper/
--------------------------------------------------------------------------------------------------------------------
首先本地虚拟机测试
afdf
[root@redhat ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID ubHPkY-rrET-30q9-Rccx-Acj7-zc2Y-5mO2ub
LV Write Access read/write
LV Creation host, time localhost, 2021-09-30 11:31:09 +0800
LV Status available
# open 2
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID c0ji2l-Op9V-gIMi-Um0G-EYOF-mcPG-s2Op69
LV Write Access read/write
LV Creation host, time localhost, 2021-09-30 11:31:10 +0800
LV Status available
# open 1
LV Size <17.00 GiB
Current LE 4351
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@redhat ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
[root@redhat ~]# 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 <19.00 GiB
PE Size 4.00 MiB
Total PE 4863
Alloc PE / Size 4863 / <19.00 GiB
Free PE / Size 0 / 0
VG UUID JO10aE-eqWx-dPfI-m4Iv-aQ20-ep6P-hCfohu
[root@redhat ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
[root@redhat ~]# ls /dev/mapper/
centos-root centos-swap control
[root@redhat ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <19.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4863
Free PE 0
Allocated PE 4863
PV UUID yFnmW3-O0rT-eds0-goW5-MZtx-y9Z6-2JX1eo
[root@redhat ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
[root@redhat ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sr0 11:0 1 918M 0 rom
[root@redhat ~]# blkid
/dev/sr0: UUID="2018-11-25-21-21-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="4315f9ab-918c-4124-91d0-3d3b15fe4fcd" TYPE="xfs"
/dev/sda2: UUID="yFnmW3-O0rT-eds0-goW5-MZtx-y9Z6-2JX1eo" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="e0752633-69a1-4d4e-8536-b2a72f9e88eb" TYPE="xfs"
/dev/mapper/centos-swap: UUID="cd9e27de-af72-4c5f-bef2-a4eca3645cc0" TYPE="swap"
[root@redhat ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 17G 5.2G 12G 31% /
devtmpfs devtmpfs 898M 0 898M 0% /dev
tmpfs tmpfs 910M 0 910M 0% /dev/shm
tmpfs tmpfs 910M 9.6M 901M 2% /run
tmpfs tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 146M 869M 15% /boot
tmpfs tmpfs 182M 0 182M 0% /run/user/0
[root@redhat ~]# ll /dev/mapper/
总用量 0
lrwxrwxrwx 1 root root 7 5月 24 10:12 centos-root -> ../dm-0
lrwxrwxrwx 1 root root 7 5月 24 10:12 centos-swap -> ../dm-1
crw------- 1 root root 10, 236 5月 24 10:12 control
[root@redhat ~]# ll /dev/dm*
brw-rw---- 1 root disk 253, 0 5月 24 10:12 /dev/dm-0
brw-rw---- 1 root disk 253, 1 5月 24 10:12 /dev/dm-1
crw-rw---- 1 root audio 14, 9 5月 24 10:12 /dev/dmmidi
第一种方法:
添加磁盘:
添加一个硬盘
然后虚拟机开机
此时查看发现了新增的10G盘
[root@redhat ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
sr0 11:0 1 918M 0 rom
分区
[root@redhat ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0xdc29d686 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):m
命令操作
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-20971519,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-20971519,默认为 20971519):
将使用默认值 20971519
分区 1 已设置为 Linux 类型,大小设为 10 GiB
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xdc29d686
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 20971519 10484736 83 Linux
这里输入t是转换磁盘类型为逻辑卷
命令(输入 m 获取帮助):t
已选择分区 1
Hex 代码(输入 L 列出所有代码):L
0 空 24 NEC DOS 81 Minix / 旧 Linu bf Solaris
1 FAT12 27 隐藏的 NTFS Win 82 Linux 交换 / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 隐藏的 C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux 扩展 c7 Syrinx
5 扩展 41 PPC PReP Boot 86 NTFS 卷集 da 非文件系统数据
6 FAT16 42 SFS 87 NTFS 卷集 db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux 纯文本 de Dell 工具
8 AIX 4e QNX4.x 第2部分 8e Linux LVM df BootIt
9 AIX 可启动 4f QNX4.x 第3部分 93 Amoeba e1 DOS 访问
a OS/2 启动管理器 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad 休 eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 扩展 (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC
11 隐藏的 FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq 诊断 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 隐藏的 FAT16 <3 61 SpeedStor ab Darwin 启动 f2 DOS 次要
16 隐藏的 FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 隐藏的 HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST 智能睡眠 65 Novell Netware b8 BSDI swap fd Linux raid 自动
1b 隐藏的 W95 FAT3 70 DiskSecure 多启 bb Boot Wizard 隐 fe LANstep
1c 隐藏的 W95 FAT3 75 PC/IX be Solaris 启动 ff BBT
1e 隐藏的 W95 FAT1 80 旧 Minix
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xdc29d686
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 20971519 10484736 8e Linux LVM
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
然后手动同步一下磁盘给系统内核
[root@redhat ~]# partprobe
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开
查看刚分区的sdb1
[root@redhat ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 10G 0 part
sr0 11:0 1 918M 0 rom
格式化
[root@redhat ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
创建物理卷PV
[root@redhat ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
查看物理卷PV
[root@redhat ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 lvm2 --- <10.00g <10.00g
[root@redhat ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <19.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4863
Free PE 0
Allocated PE 4863
PV UUID yFnmW3-O0rT-eds0-goW5-MZtx-y9Z6-2JX1eo
"/dev/sdb1" is a new physical volume of "<10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size <10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID BRuhet-cgJv-eYG2-iFyO-r1aN-fEHx-EJtUwy
查看VG卷组,此时的卷组名为centos
[root@redhat ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
[root@redhat ~]# 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 <19.00 GiB
PE Size 4.00 MiB
Total PE 4863
Alloc PE / Size 4863 / <19.00 GiB
Free PE / Size 0 / 0
VG UUID JO10aE-eqWx-dPfI-m4Iv-aQ20-ep6P-hCfohu
扩容vg
向vg卷组 添加 pv物理卷
查看VG卷组,此时的卷组名为centos
使用vgextend 添加pv到vg中,vg名字centos
[root@redhat ~]# vgextend centos /dev/sdb1
Volume group "centos" successfully extended
再次查看VG,发现变大了之前是19G,然后空闲10G
[root@redhat ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz--n- 28.99g <10.00g
[root@redhat ~]# vgdisplay
--- Volume group ---
VG Name centos
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 28.99 GiB
PE Size 4.00 MiB
Total PE 7422
Alloc PE / Size 4863 / <19.00 GiB
Free PE / Size 2559 / <10.00 GiB
VG UUID JO10aE-eqWx-dPfI-m4Iv-aQ20-ep6P-hCfohu
扩容逻辑卷 lvm
通过lvdisplay 可以看到 / 分区 的lv 名称为 root
此时root的LV是17G,root路径为 /dev/centos/root
[root@redhat ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
[root@redhat ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID ubHPkY-rrET-30q9-Rccx-Acj7-zc2Y-5mO2ub
LV Write Access read/write
LV Creation host, time localhost, 2021-09-30 11:31:09 +0800
LV Status available
# open 2
LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID c0ji2l-Op9V-gIMi-Um0G-EYOF-mcPG-s2Op69
LV Write Access read/write
LV Creation host, time localhost, 2021-09-30 11:31:10 +0800
LV Status available
# open 1
LV Size <17.00 GiB
Current LE 4351
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
使用lvextend 增加逻辑卷 10G
[root@redhat ~]# lvextend -L +10G /dev/centos/root
Insufficient free space: 2560 extents needed, but only 2559 available
此时报错了,因为10G需要2560个pe,实际只有2559个
lvextend 命令用于逻辑卷在线扩容,也就是说lvextend扩容是不需要停机的,应用服务也不需要关闭,其语法格式如下:
常用参数:
-l + :指定逻辑卷的LE个数,如 -l +200 (这里的l是小写的L)
-L + :表示增加多少空间,如 -L +15G ,单位有bBsSkKmMgGtTpPeE
-l +100%FREE :表示增加vg的全部可用空间 (这里的l是小写的L)
之前看到的PE空闲是 2559,上面的报错也提示了是2559
这里先分配2500个PE
[root@redhat ~]# lvextend -l +2500 /dev/centos/root
Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to 26.76 GiB (6851 extents).
Logical volume centos/root successfully resized.
添加成功
也能用lvextend -L +10G /dev/centos/root 也可以
再次查看lv,此时LV由原来的17g变成26.76g了
[root@redhat ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 26.76g
swap centos -wi-ao---- 2.00g
查看剩余vg的pe,可以看到还有59个,236.00 MiB
[root@redhat ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz--n- 28.99g 236.00m
[root@redhat ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
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 28.99 GiB
PE Size 4.00 MiB
Total PE 7422
Alloc PE / Size 7363 / 28.76 GiB
Free PE / Size 59 / 236.00 MiB
VG UUID JO10aE-eqWx-dPfI-m4Iv-aQ20-ep6P-hCfohu
添加成功,我们看下磁盘大小,和挂载的情况
此时root的lv已经变大了
但是挂载的分区还未变大
此时需要手动xfs_growfs一下
有两个解决方法:
1、重启
2、刷新下文件系统
这里,我们采用第二种方法,刷新文件系统
注意:不同的文件系统的刷新命令不一样
resize2fs 逻辑卷名 (只适用于ext文件系统)
xfs_growfs 挂载点 (只适用于xfs文件系统)
通过blkid 查看到/ 分区的文件系统为 xfs 格式【/dev/mapper/centos-root】
[root@redhat ~]# blkid
/dev/sdb1: UUID="BRuhet-cgJv-eYG2-iFyO-r1aN-fEHx-EJtUwy" TYPE="LVM2_member"
/dev/sda1: UUID="4315f9ab-918c-4124-91d0-3d3b15fe4fcd" TYPE="xfs"
/dev/sda2: UUID="yFnmW3-O0rT-eds0-goW5-MZtx-y9Z6-2JX1eo" TYPE="LVM2_member"
/dev/sr0: UUID="2018-11-25-21-21-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/mapper/centos-root: UUID="e0752633-69a1-4d4e-8536-b2a72f9e88eb" TYPE="xfs"
/dev/mapper/centos-swap: UUID="cd9e27de-af72-4c5f-bef2-a4eca3645cc0" TYPE="swap"
[root@redhat ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1113856 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=4455424, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 4455424 to 7015424
再次查看,/分区就扩展成功
[root@redhat ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 27G 5.2G 22G 20% /
查看数据都是还在的
[root@redhat /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 26.8G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 10G 0 part
└─centos-root 253:0 0 26.8G 0 lvm /
sr0 11:0 1 918M 0 rom
-------------------------------------------------------------------------------------------------------------------------------
第二种方法,直接扩容硬盘,不加硬盘
之前的虚拟机有快照,无法直接库容,先克隆一个出来
发现用快照-链接克隆的也不行
重新克隆-完整克隆
再次编辑虚拟机,扩容磁盘
这里的扩展,就变成可以点击的状态了,之前是灰色的
原来是20
改成30
这里提示了,还需要在客户机操作系统对磁盘重新分区或者扩展文件系统。
然后开机
此时发现sda从原来的20G变为30G了
[root@redhat ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
查看/分区没有变
[root@redhat ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 17G 5.2G 12G 31% /
查看pv vg lv 都是没变的
[root@redhat ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
[root@redhat ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
[root@redhat ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
此时先分区
[root@redhat ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):p
磁盘 /dev/sda:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000ae4a1
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
命令(输入 m 获取帮助):n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
分区号 (3,4,默认 3):
起始 扇区 (41943040-62914559,默认为 41943040):
将使用默认值 41943040
Last 扇区, +扇区 or +size{K,M,G} (41943040-62914559,默认为 62914559):
将使用默认值 62914559
分区 3 已设置为 Linux 类型,大小设为 10 GiB
命令(输入 m 获取帮助):p
磁盘 /dev/sda:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000ae4a1
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
/dev/sda3 41943040 62914559 10485760 83 Linux
这里并没有输入t来转换磁盘类型为LVM类型,测试 也是可以的。
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
查看新建分区
[root@redhat ~]# fdisk -l
磁盘 /dev/sda:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000ae4a1
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
/dev/sda3 41943040 62914559 10485760 83 Linux
lsblk里面没变
[root@redhat ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sr0 11:0 1 918M 0 rom
新建物理卷pv
新建PV时候,此时报错
[root@redhat ~]# pvcreate /dev/sda3
Device /dev/sda3 not found.
手动同步一下分区
[root@redhat ~]# partprobe
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
然后再看lsblk这时候发现sda3了
[root@redhat ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 19G 0 part
│ ├─centos-root 253:0 0 17G 0 lvm /
│ └─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─sda3 8:3 0 10G 0 part
然后再次创建PV,这次成功
[root@redhat ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
查看pv变大了
[root@redhat ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sda3 lvm2 --- 10.00g 10.00g
[root@redhat ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
扩展VG卷组,centos是卷组的名字,扩展成功
[root@redhat ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended
扩展卷组后,再次查看VG就,发现VG变大,并且空闲了10G
[root@redhat ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz--n- 28.99g <10.00g
扩展LV
先查看
[root@redhat ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
再扩展
查看空闲的PE数量
扩展,此时报错,给定的2599不大于现有的4351,因为现在的root是4351个LE
这里应该用+才对,少个加号。
[root@redhat ~]# lvextend -l 2599 /dev/centos/root
New size given (2599 extents) not larger than existing size (4351 extents)
[root@redhat ~]# lvdisplay /dev/centos/root
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID c0ji2l-Op9V-gIMi-Um0G-EYOF-mcPG-s2Op69
LV Write Access read/write
LV Creation host, time localhost, 2021-09-30 11:31:10 +0800
LV Status available
# open 1
LV Size <17.00 GiB
Current LE 4351
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
重新扩展,再次报错,因为LE数量没有10G,是小于10G的,所以不能精确指定10G
[root@redhat ~]# lvextend -L +10G /dev/centos/root
Insufficient free space: 2560 extents needed, but only 2559 available
重新扩展,这里使用添加精确的LE数量,扩展成功
[root@redhat ~]# lvextend -l +2559 /dev/centos/root
Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to 26.99 GiB (6910 extents).
Logical volume centos/root successfully resized.
查看root分区的lv已经变大
[root@redhat ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 26.99g
swap centos -wi-ao---- 2.00g
此时查看发现文件系统还没变大
[root@redhat ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 17G 5.2G 12G 31% /
重新growfs一下
[root@redhat ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1113856 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=4455424, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 4455424 to 7075840
再次查看成功了
[root@redhat ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 27G 5.2G 22G 20% /
[root@redhat ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 19G 0 part
│ ├─centos-root 253:0 0 27G 0 lvm /
│ └─centos-swap 253:1 0 2G 0 lvm [SWAP]
└─sda3 8:3 0 10G 0 part
└─centos-root 253:0 0 27G 0 lvm /
查看数据也都还在,扩容成功。