1、场景
因项目需要,前期磁盘评估不准确,系统在用了一段时间后发现磁盘空间不够用了,通过查看磁盘做的有lvm,因此联系了云平台的进行了扩容,扩容后如第二部分所看到的vdb是1000G,但是通过查看磁盘/home/data的空间是500G,需要将剩下的500G扩容给/home/data目录。
注意: 整个扩容过程中不影响业务使用,无需重启服务器,但缩容是会影响业务的,一定要注意。
2、磁盘已添加
# 可以看到vdb是1000G
[root@cti-server-0002 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 100G 0 disk
└─vda1 253:1 0 100G 0 part /
vdb 253:16 0 1000G 0 disk
└─vdb1 253:17 0 500G 0 part └─vg01-lv01 252:0 0 500G 0 lvm /home/data# 但是/home/data可用是500G
[root@cti-server-0002 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 32G 0 32G 0% /dev
tmpfs tmpfs 32G 0 32G 0% /dev/shm
tmpfs tmpfs 32G 2.3G 29G 8% /run
tmpfs tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/vda1 ext4 99G 13G 82G 14% /
/dev/mapper/vg01-lv01 ext4 493G 270G 198G 58% /home/data
tmpfs tmpfs 6.3G 0 6.3G 0% /run/user/0
3、分区
3.1、用fdisk进行分区
需要对/dev/vdb进行分区
# 当执行完命令的时候最后出现了报错
[root@cti-server-0002 ~]# fdisk /dev/vdb# 报错如下:
The backup GPT table is not at the end of the disk, as it should be. This might mean that another operating system believes the disk is smaller. Fix, by moving the backup to the end (and removing the old backup)?Not all of the space available to /dev/vdb appears to be used, you can fix the GPT to use all of the space (an extra 1048576000 blocks) or continue with the current setting?
3.2、排错
需对系统扩容盘进行修复
3.2.1、使用parted进行修复
[root@cti-server-0002 ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.# 可以输出help看下帮助
(parted) help align-check TYPE N check partition N for TYPE(min|opt) alignmenthelp [COMMAND] print general help, or help on COMMANDmklabel,mktable LABEL-TYPE create a new disklabel (partition table)mkpart PART-TYPE [FS-TYPE] START END make a partitionname NUMBER NAME name partition NUMBER as NAMEprint [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partitionquit exit programrescue START END rescue a lost partition near START and ENDresizepart NUMBER END resize partition NUMBERrm NUMBER delete partition NUMBERselect DEVICE choose the device to editdisk_set FLAG STATE change the FLAG on selected devicedisk_toggle [FLAG] toggle the state of FLAG on selected deviceset NUMBER FLAG STATE change the FLAG on partition NUMBERtoggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBERunit UNIT set the default unit to UNITversion display the version number and copyright information of GNU Parted# 打印全部看下信息(parted) print all
Error: The backup GPT table is not at the end of the disk, as it should be. This might mean that another operating system believes the disk is smaller.
Fix, by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel? Fix
Warning: Not all of the space available to /dev/vdb appears to be used, you can fix the GPT to use all of the space (an extra 1048576000 blocks) or
continue with the current setting?
Fix/Ignore? Fix
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 1074GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:Number Start End Size File system Name Flags1 17.4kB 537GB 537GB primary lvmModel: Linux device-mapper (linear) (dm)
Disk /dev/mapper/vg01-lv01: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: Number Start End Size File system Flags1 0.00B 537GB 537GB ext4Model: Virtio Block Device (virtblk)
Disk /dev/vda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags1 1049kB 107GB 107GB primary ext4 boot(parted) print all
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 1074GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name Flags1 17.4kB 537GB 537GB primary lvmModel: Linux device-mapper (linear) (dm)
Disk /dev/mapper/vg01-lv01: 537GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: Number Start End Size File system Flags1 0.00B 537GB 537GB ext4Model: Virtio Block Device (virtblk)
Disk /dev/vda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: Number Start End Size Type File system Flags1 1049kB 107GB 107GB primary ext4 boot
(parted) exit# 开始修复
# parted /dev/vda
GNU Parted 3.1
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Error: The backup GPT table is not at the end of the disk, as it should be.
This might mean that another operating system believes the disk is smaller.
Fix, by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel? f # 选择fix进行修复
Warning: Not all of the space available to /dev/vda appears to be used, you can
fix the GPT to use all of the space (an extra 167772160 blocks) or continue
with the current setting?
Fix/Ignore? f # 选择fix进行修复
q # 退出
3.3、再次分区
3.3.1、fdisk
[root@cti-server-0002 ~]# fdisk /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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): pDisk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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: gpt
Disk identifier: 63A6C2F3-2560-4AC0-959D-6B7FB2ABCA81# Start End Size Type Name1 34 1048575966 500G Linux LVM primaryCommand (m for help): n
Partition number (2-128, default 2):
First sector (1048575967-2097151966, default 1048576000):
Last sector, +sectors or +size{K,M,G,T,P} (1048576000-2097151966, default 2097151966):
Created partition 2
Command (m for help): t
Partition number (1,2, default 2):
# 查看列表,发现31是linux的lvm
Partition type (type L to list all types): L1 EFI System C12A7328-F81F-11D2-BA4B-00A0C93EC93B2 MBR partition scheme 024DEE41-33E7-11D3-9D69-0008C781F39F3 Intel Fast Flash D3BFE2DE-3DAF-11DF-BA40-E3A556D895934 BIOS boot 21686148-6449-6E6F-744E-6565644546495 Sony boot partition F4019732-066E-4E12-8273-346C5641494F6 Lenovo boot partition BFBFAFE7-A34F-448A-9A5B-6213EB736C227 PowerPC PReP boot 9E1A2D38-C612-4316-AA26-8B49521E5A8B8 ONIE boot 7412F7D5-A156-4B13-81DC-8671749293259 ONIE config D4E6E2CD-4469-46F3-B5CB-1BFF57AFC14910 Microsoft reserved E3C9E316-0B5C-4DB8-817D-F92DF00215AE11 Microsoft basic data EBD0A0A2-B9E5-4433-87C0-68B6B72699C712 Microsoft LDM metadata 5808C8AA-7E8F-42E0-85D2-E1E90434CFB313 Microsoft LDM data AF9B60A0-1431-4F62-BC68-3311714A69AD14 Windows recovery environment DE94BBA4-06D1-4D40-A16A-BFD50179D6AC15 IBM General Parallel Fs 37AFFC90-EF7D-4E96-91C3-2D7AE055B17416 Microsoft Storage Spaces E75CAF8F-F680-4CEE-AFA3-B001E56EFC2D17 HP-UX data 75894C1E-3AEB-11D3-B7C1-7B03A000000018 HP-UX service E2A1E728-32E3-11D6-A682-7B03A000000019 Linux swap 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F20 Linux filesystem 0FC63DAF-8483-4772-8E79-3D69D8477DE421 Linux server data 3B8F8425-20E0-4F3B-907F-1A25A76F98E822 Linux root (x86) 44479540-F297-41B2-9AF7-D131D5F0458A23 Linux root (ARM) 69DAD710-2CE4-4E3C-B16C-21A1D49ABED324 Linux root (x86-64) 4F68BCE3-E8CD-4DB1-96E7-FBCAF984B70925 Linux root (ARM-64) B921B045-1DF0-41C3-AF44-4C6F280D3FAE26 Linux root (IA-64) 993D8D3D-F80E-4225-855A-9DAF8ED7EA9727 Linux reserved 8DA63339-0007-60C0-C436-083AC823090828 Linux home 933AC7E1-2EB4-4F13-B844-0E14E2AEF91529 Linux RAID A19D880F-05FC-4D3B-A006-743F0F84911E30 Linux extended boot BC13C2FF-59E6-4262-A352-B275FD6F717231 Linux LVM E6D6D379-F507-44C2-A23C-238F2A3DF92832 FreeBSD data 516E7CB4-6ECF-11D6-8FF8-00022D09712B
......
# 因为31是linux的 LVM,所以此处填写31
Partition type (type L to list all types): 31
Changed type of partition 'Linux filesystem' to 'Linux LVM'Command (m for help): pDisk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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: gpt
Disk identifier: 63A6C2F3-2560-4AC0-959D-6B7FB2ABCA81# 发现第二个分区已经有了
# Start End Size Type Name1 34 1048575966 500G Linux LVM primary2 1048576000 2097151966 500G Linux LVM # 保存退出
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.# 根据上边的提示需要执行下partprobe命令
[root@cti-server-0002 ~]# partprobe
3.3.2、vgdisplay
# 列出指定卷组或所有卷组的相关属性和状态数据。
[root@cti-server-0002 ~]# vgdisplay--- Volume group ---VG Name vg01System ID Format lvm2Metadata Areas 1Metadata Sequence No 2VG Access read/writeVG Status resizableMAX LV 0Cur LV 1Open LV 1Max PV 0Cur PV 1Act PV 1VG Size <500.00 GiBPE Size 4.00 MiBTotal PE 127999Alloc PE / Size 127999 / <500.00 GiBFree PE / Size 0 / 0 VG UUID nxDvAj-YZIS-90gw-W9BM-VAlZ-fqWD-xzo4Zb[root@cti-server-0002 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 100G 0 disk
└─vda1 253:1 0 100G 0 part /
vdb 253:16 0 1000G 0 disk
├─vdb1 253:17 0 500G 0 part
│ └─vg01-lv01 252:0 0 500G 0 lvm /home/data
└─vdb2 253:18 0 500G 0 part
3.3.3、pvcreate
# 将新建的分区磁盘 /dev/vdb2 设置为一个LVM物理卷(初始化)
[root@cti-server-0002 ~]# pvcreate /dev/vdb2Physical volume "/dev/vdb2" successfully created.
[root@cti-server-0002 ~]# vgex
vgexport vgextend
[root@cti-server-0002 ~]# vgex
vgexport vgextend
[root@cti-server-0002 ~]# vgextend vg01 /dev/vdb2Volume group "vg01" successfully extended
[root@cti-server-0002 ~]# vgdisplay --- Volume group ---VG Name vg01System ID Format lvm2Metadata Areas 2Metadata Sequence No 3VG Access read/writeVG Status resizableMAX LV 0Cur LV 1Open LV 1Max PV 0Cur PV 2Act PV 2VG Size 999.99 GiBPE Size 4.00 MiBTotal PE 255998Alloc PE / Size 127999 / <500.00 GiBFree PE / Size 127999 / <500.00 GiBVG UUID nxDvAj-YZIS-90gw-W9BM-VAlZ-fqWD-xzo4Zb
3.3.4、lvdisplay
用于在Linux系统中查询并显示所有逻辑卷的详细信息。由于没有指定特定逻辑卷,该命令将列出系统中所有已知逻辑卷的属性,为管理员或用户提供关于LVM存储资源状态的重要视图。
# 显示逻辑卷(Logical Volume, LV)的相关信息。
[root@cti-server-0002 ~]# lvdisplay --- Logical volume ---LV Path /dev/vg01/lv01LV Name lv01VG Name vg01LV UUID uFPYn7-9x8h-93gE-aeOA-xbli-e3jW-1Z0rgrLV Write Access read/writeLV Creation host, time cti-server-0002, 2024-02-18 09:18:08 +0800LV Status available# open 1LV Size <500.00 GiBCurrent LE 127999Segments 1Allocation inheritRead ahead sectors auto- currently set to 8192Block device 252:0
3.3.5、lvextend
# 动态扩展逻辑卷lv01的大小
[root@cti-server-0002 ~]# lvextend -l +100%FREE /dev/vg01/lv01Size of logical volume vg01/lv01 changed from <500.00 GiB (127999 extents) to 999.99 GiB (255998 extents).Logical volume vg01/lv01 successfully resized.# 查看磁盘还未扩容
[root@cti-server-0002 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 32G 0 32G 0% /dev
tmpfs tmpfs 32G 0 32G 0% /dev/shm
tmpfs tmpfs 32G 2.3G 29G 8% /run
tmpfs tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/vda1 ext4 99G 13G 82G 14% /
/dev/mapper/vg01-lv01 ext4 493G 270G 198G 58% /home/data
tmpfs tmpfs 6.3G 0 6.3G 0% /run/user/0
3.3.6、resize2fs
在线调整挂载在逻辑卷/dev/vg01/lv01
上的ext2、ext3或ext4文件系统的大小,使之与逻辑卷当前的实际大小保持一致。这一操作通常发生在逻辑卷容量扩大之后,以确保文件系统能够利用新增的存储空间。执行此命令时,文件系统无需被卸载,从而保证了服务的连续性。
# 先确定自己的文件类型,通过df -Th发现文件类型是ext4,所以需要使用resize2fs命令,如果是xfs类型,需要用xfs_growfs,如:xfs_growfs /dev/vg01/lv01
[root@cti-server-0002 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 32G 0 32G 0% /dev
tmpfs tmpfs 32G 0 32G 0% /dev/shm
tmpfs tmpfs 32G 2.3G 29G 8% /run
tmpfs tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/vda1 ext4 99G 13G 82G 14% /
**/dev/mapper/vg01-lv01 ext4 985G 270G 670G 29% /home/data**
tmpfs tmpfs 6.3G 0 6.3G 0% /run/user/0#
[root@cti-server-0002 ~]# resize2fs /dev/vg01/lv01
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg01/lv01 is mounted on /home/data; on-line resizing required
old_desc_blocks = 63, new_desc_blocks = 125
The filesystem on /dev/vg01/lv01 is now 262141952 blocks long.# 再次查看,发现扩容成功
[root@cti-server-0002 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 32G 0 32G 0% /dev
tmpfs tmpfs 32G 0 32G 0% /dev/shm
tmpfs tmpfs 32G 2.3G 29G 8% /run
tmpfs tmpfs 32G 0 32G 0% /sys/fs/cgroup
/dev/vda1 ext4 99G 13G 82G 14% /
/dev/mapper/vg01-lv01 ext4 985G 270G 670G 29% /home/data
tmpfs tmpfs 6.3G 0 6.3G 0% /run/user/0