lvm动态扩容

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) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  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     Flags
 1      17.4kB  537GB  537GB               primary  lvm

Model: 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  Flags
 1      0.00B  537GB  537GB  ext4

Model: 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  Flags
 1      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     Flags
 1      17.4kB  537GB  537GB               primary  lvm

Model: 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  Flags
 1      0.00B  537GB  537GB  ext4

Model: 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  Flags
 1      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): p

Disk /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            Name
 1           34   1048575966    500G  Linux LVM       primary

Command (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): L
 1 EFI System                     C12A7328-F81F-11D2-BA4B-00A0C93EC93B
 2 MBR partition scheme           024DEE41-33E7-11D3-9D69-0008C781F39F
 3 Intel Fast Flash               D3BFE2DE-3DAF-11DF-BA40-E3A556D89593
 4 BIOS boot                      21686148-6449-6E6F-744E-656564454649
 5 Sony boot partition            F4019732-066E-4E12-8273-346C5641494F
 6 Lenovo boot partition          BFBFAFE7-A34F-448A-9A5B-6213EB736C22
 7 PowerPC PReP boot              9E1A2D38-C612-4316-AA26-8B49521E5A8B
 8 ONIE boot                      7412F7D5-A156-4B13-81DC-867174929325
 9 ONIE config                    D4E6E2CD-4469-46F3-B5CB-1BFF57AFC149
 10 Microsoft reserved             E3C9E316-0B5C-4DB8-817D-F92DF00215AE
 11 Microsoft basic data           EBD0A0A2-B9E5-4433-87C0-68B6B72699C7
 12 Microsoft LDM metadata         5808C8AA-7E8F-42E0-85D2-E1E90434CFB3
 13 Microsoft LDM data             AF9B60A0-1431-4F62-BC68-3311714A69AD
 14 Windows recovery environment   DE94BBA4-06D1-4D40-A16A-BFD50179D6AC
 15 IBM General Parallel Fs        37AFFC90-EF7D-4E96-91C3-2D7AE055B174
 16 Microsoft Storage Spaces       E75CAF8F-F680-4CEE-AFA3-B001E56EFC2D
 17 HP-UX data                     75894C1E-3AEB-11D3-B7C1-7B03A0000000
 18 HP-UX service                  E2A1E728-32E3-11D6-A682-7B03A0000000
 19 Linux swap                     0657FD6D-A4AB-43C4-84E5-0933C84B4F4F
 20 Linux filesystem               0FC63DAF-8483-4772-8E79-3D69D8477DE4
 21 Linux server data              3B8F8425-20E0-4F3B-907F-1A25A76F98E8
 22 Linux root (x86)               44479540-F297-41B2-9AF7-D131D5F0458A
 23 Linux root (ARM)               69DAD710-2CE4-4E3C-B16C-21A1D49ABED3
 24 Linux root (x86-64)            4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709
 25 Linux root (ARM-64)            B921B045-1DF0-41C3-AF44-4C6F280D3FAE
 26 Linux root  (IA-64)             993D8D3D-F80E-4225-855A-9DAF8ED7EA97
 27 Linux reserved                 8DA63339-0007-60C0-C436-083AC8230908
 28 Linux home                     933AC7E1-2EB4-4F13-B844-0E14E2AEF915
 29 Linux RAID                     A19D880F-05FC-4D3B-A006-743F0F84911E
 30 Linux extended boot            BC13C2FF-59E6-4262-A352-B275FD6F7172
 31 Linux LVM                      E6D6D379-F507-44C2-A23C-238F2A3DF928
 32 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): p

Disk /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            Name
 1           34   1048575966    500G  Linux LVM       primary
 2   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               vg01
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <500.00 GiB
  PE Size               4.00 MiB
  Total PE              127999
  Alloc PE / Size       127999 / <500.00 GiB
  Free  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/vdb2
  Physical 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/vdb2
  Volume group "vg01" successfully extended
[root@cti-server-0002 ~]# vgdisplay 
  --- Volume group ---
  VG Name               vg01
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               999.99 GiB
  PE Size               4.00 MiB
  Total PE              255998
  Alloc PE / Size       127999 / <500.00 GiB
  Free  PE / Size       127999 / <500.00 GiB
  VG 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/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                uFPYn7-9x8h-93gE-aeOA-xbli-e3jW-1Z0rgr
  LV Write Access        read/write
  LV Creation host, time cti-server-0002, 2024-02-18 09:18:08 +0800
  LV Status              available
  # open                 1
  LV Size                <500.00 GiB
  Current LE             127999
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           252:0

3.3.5、lvextend

# 动态扩展逻辑卷lv01的大小
[root@cti-server-0002 ~]# lvextend -l +100%FREE /dev/vg01/lv01
  Size 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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值