Linux

如何在CentOS 7上通过扩lvm来扩展文件系统

一背景说明和诉求

1 背景

用户的一套私有化虚拟机环境,部署的跑在docker容器里的Java应用比较多,磁盘空间偏小,需要扩文件系统。

[root@oa ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[root@oa ~]# uptime
14:03:36 up 5 days,  5:08,  2 users, load average: 0.62, 0.46, 0.42
[root@oa lh]# df -Th
Filesystem             Type     Size Used Avail Use% Mounted on
devtmpfs               devtmpfs   16G     0   16G   0% /dev
tmpfs                   tmpfs     16G     0   16G   0% /dev/shm
tmpfs                   tmpfs     16G   49M   16G   1% /run
tmpfs                   tmpfs     16G     0   16G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs       50G   35G   16G  70% /
/dev/mapper/centos-home xfs       42G 107M   41G   1% /home
/dev/sda1               xfs     1014M 154M 861M  16% /boot
tmpfs                   tmpfs     3.2G     0  3.2G   0% /run/user/0
[root@oa lh]# docker ps |wc -l
31
[root@oa ~]#

当前的磁盘空间如下:

[root@oa ~]# ll /dev/sd*
brw-rw---- 1 root disk 8, 0 Dec 28 11:42 /dev/sda
brw-rw---- 1 root disk 8, 1 Dec 28 11:42 /dev/sda1
brw-rw---- 1 root disk 8, 2 Dec 28 11:42 /dev/sda2
[root@oa ~]# fdisk -l /dev/sda

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: dos
Disk identifier: 0x000b68ed

  Device Boot     Start         End     Blocks   Id System
/dev/sda1   *        2048     2099199     1048576   83 Linux
/dev/sda2         2099200   209715199   103808000   8e Linux LVM
[root@oa ~]# pvdisplay
 --- Physical volume ---
PV Name               /dev/sda2
VG Name               centos
PV Size               <99.00 GiB / not usable 3.00 MiB
Allocatable           yes
PE Size               4.00 MiB
Total PE              25343
Free PE               1
Allocated PE          25342
PV UUID               7qgd8p-pelD-0nPU-Jh3Y-sQsg-CHlZ-co7NVc
 
[root@oa ~]# vgdisplay
 --- Volume group ---
VG Name               centos
System ID            
Format               lvm2
Metadata Areas        1
Metadata Sequence No  4
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                3
Open LV               2
Max PV                0
Cur PV                1
Act PV                1
VG Size               <99.00 GiB
PE Size               4.00 MiB
Total PE              25343
Alloc PE / Size       25342 / 98.99 GiB
Free PE / Size       1 / 4.00 MiB
VG UUID               VzXVhd-c5MZ-MLTV-HBis-vwxB-0m1w-KqL2R5
 
[root@oa ~]# lvdisplay
 --- Logical volume ---
LV Path               /dev/centos/swap
LV Name               swap
VG Name               centos
LV UUID               1BpmN3-QFem-uwX5-U6Ti-hQnM-lYLZ-15irfp
LV Write Access       read/write
LV Creation host, time localhost.localdomain, 2021-12-01 13:06:21 +0800
LV Status             available
 # open                 0
LV Size               <7.88 GiB
Current LE             2016
Segments               1
Allocation             inherit
Read ahead sectors     auto
 - currently set to     8192
Block device           253:1
 
 --- Logical volume ---
LV Path               /dev/centos/home
LV Name               home
VG Name               centos
LV UUID               7DUUs5-rT2r-lzEv-wdk0-SJuf-TKho-Xki03P
LV Write Access       read/write
LV Creation host, time localhost.localdomain, 2021-12-01 13:06:21 +0800
LV Status             available
 # open                 1
LV Size               <41.12 GiB
Current LE             10526
Segments               1
Allocation             inherit
Read ahead sectors     auto
 - currently set to     8192
Block device           253:2
 
 --- Logical volume ---
LV Path               /dev/centos/root
LV Name               root
VG Name               centos
LV UUID               4O4Xhq-JeFA-g1D1-0cBL-VzZu-r80j-PYJ74a
LV Write Access       read/write
LV Creation host, time localhost.localdomain, 2021-12-01 13:06:21 +0800
LV Status             available
 # open                 1
LV Size                50.00 GiB
Current LE             12800
Segments               1
Allocation             inherit
Read ahead sectors     auto
 - currently set to     8192
Block device           253:0
 
[root@oa ~]#

只有1块磁盘设备/dev/sda,空间是107GB,其中/dev/sda2分区做成了物理卷,放到名为centos的卷组vg里了,然后该vg下创建了3个逻辑卷lvm,/dev/centos/root,/dev/centos/home,/dev/centos/swap。然后把对应的lvm挂载到文件系统上使用。

2诉求

我们希望可以扩充根文件系统/,然后客户通过虚拟机管理平台,把磁盘从107G扩到200GB了。接下来,就需要我们这边在操作系统层面扩文件系统了。

二 操作步骤和流程

1 重启虚拟机

如果不重启的话,是看不到客户新加的磁盘空间的。比如:直接对/dev/sda磁盘进行添加分区的话,会提示没有可用扇区。No free sectors available

[root@oa ~]# fdisk /dev/sda 
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/sda: 107.4 GB, 107374182400 bytes, 209715200 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: dos
Disk identifier: 0x000b68ed

  Device Boot     Start         End     Blocks   Id System
/dev/sda1   *        2048     2099199     1048576   83 Linux
/dev/sda2         2099200   209715199   103808000   8e Linux LVM

Command (m for help): n
Partition type:
  p   primary (2 primary, 0 extended, 2 free)
  e   extended
Select (default p): p
Partition number (3,4, default 3): 3
No free sectors available

Command (m for help):q

reboot之后,便可以看到新加的磁盘空间:

[root@oa ~]# uptime 
14:37:38 up 0 min,  1 user, load average: 0.05, 0.01, 0.01
[root@oa ~]# fdisk -l /dev/sda

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: dos
Disk identifier: 0x000b68ed

  Device Boot     Start         End     Blocks   Id System
/dev/sda1   *        2048     2099199     1048576   83 Linux
/dev/sda2         2099200   209715199   103808000   8e Linux LVM
[root@oa ~]#
2 给磁盘创建新的分区

通过fdisk给磁盘创建新的分区,选择primary主分区类型,然后给新建的分区/dev/sda3打上了lvm类型的卷标。

[root@oa ~]# fdisk /dev/sda 
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/sda: 214.7 GB, 214748364800 bytes, 419430400 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: dos
Disk identifier: 0x000b68ed

  Device Boot     Start         End     Blocks   Id System
/dev/sda1   *        2048     2099199     1048576   83 Linux
/dev/sda2         2099200   209715199   103808000   8e Linux LVM

Command (m for help): n
Partition type:
  p   primary (2 primary, 0 extended, 2 free)
  e   extended
Select (default p): p
Partition number (3,4, default 3):
First sector (209715200-419430399, default 209715200):
Using default value 209715200
Last sector, +sectors or +size{K,M,G} (209715200-419430399, default 419430399):
Using default value 419430399
Partition 3 of type Linux and of size 100 GiB is set

Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: dos
Disk identifier: 0x000b68ed

  Device Boot     Start         End     Blocks   Id System
/dev/sda1   *        2048     2099199     1048576   83 Linux
/dev/sda2         2099200   209715199   103808000   8e Linux LVM
/dev/sda3       209715200   419430399   104857600   8e 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.
[root@oa ~]#
3 创建物理卷

把新加的磁盘分区/dev/sda3做成物理卷。如果提示找不到/dev/sda3设备时,可以通过partprobe 来重新发现设备。

[root@oa ~]# pvdisplay 
 --- Physical volume ---
PV Name               /dev/sda2
VG Name               centos
PV Size               <99.00 GiB / not usable 3.00 MiB
Allocatable           yes
PE Size               4.00 MiB
Total PE              25343
Free PE               1
Allocated PE          25342
PV UUID               7qgd8p-pelD-0nPU-Jh3Y-sQsg-CHlZ-co7NVc
 
[root@oa ~]# pvcreate /dev/sda3
Device /dev/sda3 not found.
[root@oa ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@oa ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.
[root@oa ~]# pvdisplay
 --- Physical volume ---
PV Name               /dev/sda2
VG Name               centos
PV Size               <99.00 GiB / not usable 3.00 MiB
Allocatable           yes
PE Size               4.00 MiB
Total PE              25343
Free PE               1
Allocated PE          25342
PV UUID               7qgd8p-pelD-0nPU-Jh3Y-sQsg-CHlZ-co7NVc
 
 "/dev/sda3" is a new physical volume of "100.00 GiB"
 --- NEW Physical volume ---
PV Name               /dev/sda3
VG Name              
PV Size               100.00 GiB
Allocatable           NO
PE Size               0  
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               7ATNJW-cJoM-NnVD-o7U8-P48W-ZVjG-tflVbS
 
[root@oa ~]#
4 扩卷组

通过vgextend VG_name PV_name命令来扩展卷组,如下是扩展centos这个卷组前后的对比信息。

[root@oa ~]# vgdisplay 
 --- Volume group ---
VG Name               centos
System ID            
Format               lvm2
Metadata Areas        1
Metadata Sequence No  4
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                3
Open LV               2
Max PV                0
Cur PV                1
Act PV                1
VG Size               <99.00 GiB
PE Size               4.00 MiB
Total PE              25343
Alloc PE / Size       25342 / 98.99 GiB
Free PE / Size       1 / 4.00 MiB
VG UUID               VzXVhd-c5MZ-MLTV-HBis-vwxB-0m1w-KqL2R5
 
[root@oa ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended
[root@oa ~]# 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                3
Open LV               2
Max PV                0
Cur PV                2
Act PV                2
VG Size               198.99 GiB
PE Size               4.00 MiB
Total PE              50942
Alloc PE / Size       25342 / 98.99 GiB
Free PE / Size       25600 / 100.00 GiB
VG UUID               VzXVhd-c5MZ-MLTV-HBis-vwxB-0m1w-KqL2R5
 
[root@oa ~]#
5 扩逻辑卷

我们可以通过lvextend或者lvresize命令来扩展,需要注意的是,前者不能缩小LV。命令行格式上,没有区别:

lvextend -L +60g /dev/centos/root   ##表示的是给逻辑卷/dev/centos/root额外新增加60GB空间

或者:

lvresize -L +60g /dev/centos/root   ##同样表示的是把逻辑卷/dev/centos/root额外新增加60GB空间

扩展前:

[root@oa ~]# lvdisplay 
 --- Logical volume ---
LV Path               /dev/centos/swap
LV Name               swap
VG Name               centos
LV UUID               1BpmN3-QFem-uwX5-U6Ti-hQnM-lYLZ-15irfp
LV Write Access       read/write
LV Creation host, time localhost.localdomain, 2021-12-01 13:06:21 +0800
LV Status             available
 # open                 0
LV Size               <7.88 GiB
Current LE             2016
Segments               1
Allocation             inherit
Read ahead sectors     auto
 - currently set to     8192
Block device           253:1
 
 --- Logical volume ---
LV Path               /dev/centos/home
LV Name               home
VG Name               centos
LV UUID               7DUUs5-rT2r-lzEv-wdk0-SJuf-TKho-Xki03P
LV Write Access       read/write
LV Creation host, time localhost.localdomain, 2021-12-01 13:06:21 +0800
LV Status             available
 # open                 1
LV Size               <41.12 GiB
Current LE             10526
Segments               1
Allocation             inherit
Read ahead sectors     auto
 - currently set to     8192
Block device           253:2
 
 --- Logical volume ---
LV Path               /dev/centos/root
LV Name               root
VG Name               centos
LV UUID               4O4Xhq-JeFA-g1D1-0cBL-VzZu-r80j-PYJ74a
LV Write Access       read/write
LV Creation host, time localhost.localdomain, 2021-12-01 13:06:21 +0800
LV Status             available
 # open                 1
LV Size                50.00 GiB
Current LE             12800
Segments               1
Allocation             inherit
Read ahead sectors     auto
 - currently set to     8192
Block device           253:0
 
[root@oa ~]#

通过lvextend命令来把/dev/centos/root 这个逻辑卷增加60GB,相当于扩展到110GB:

[root@oa ~]# lvextend -L +60g /dev/centos/root 
Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 110.00 GiB (28160 extents).
Logical volume centos/root successfully resized.
[root@oa ~]# lvdisplay /dev/centos/root
 --- Logical volume ---
LV Path               /dev/centos/root
LV Name               root
VG Name               centos
LV UUID               4O4Xhq-JeFA-g1D1-0cBL-VzZu-r80j-PYJ74a
LV Write Access       read/write
LV Creation host, time localhost.localdomain, 2021-12-01 13:06:21 +0800
LV Status             available
 # open                 1
LV Size                110.00 GiB
Current LE             28160
Segments               2
Allocation             inherit
Read ahead sectors     auto
 - currently set to     8192
Block device           253:0
 
[root@oa ~]#

通过lvresize来扩容/dev/centos/root 逻辑卷,相当于再新增10GB到总大小120GB。

[root@oa ~]# lvresize -L +10g /dev/centos/root 
Size of logical volume centos/root changed from 110.00 GiB (28160 extents) to 120.00 GiB (30720 extents).
Logical volume centos/root successfully resized.
[root@oa ~]# lvdisplay /dev/centos/root
 --- Logical volume ---
LV Path               /dev/centos/root
LV Name               root
VG Name               centos
LV UUID               4O4Xhq-JeFA-g1D1-0cBL-VzZu-r80j-PYJ74a
LV Write Access       read/write
LV Creation host, time localhost.localdomain, 2021-12-01 13:06:21 +0800
LV Status             available
 # open                 1
LV Size                120.00 GiB
Current LE             30720
Segments               2
Allocation             inherit
Read ahead sectors     auto
 - currently set to     8192
Block device           253:0
 
[root@oa ~]#

最后,把这个新增的100G全部加到逻辑卷/dev/centos/root上去。

[root@oa ~]# lvresize -L +20g /dev/centos/root 
Size of logical volume centos/root changed from 130.00 GiB (33280 extents) to 150.00 GiB (38400 extents).
Logical volume centos/root successfully resized.
[root@oa ~]# lvdisplay /dev/centos/root
 --- Logical volume ---
LV Path               /dev/centos/root
LV Name               root
VG Name               centos
LV UUID               4O4Xhq-JeFA-g1D1-0cBL-VzZu-r80j-PYJ74a
LV Write Access       read/write
LV Creation host, time localhost.localdomain, 2021-12-01 13:06:21 +0800
LV Status             available
 # open                 1
LV Size                150.00 GiB
Current LE             38400
Segments               2
Allocation             inherit
Read ahead sectors     auto
 - currently set to     8192
Block device           253:0
 
[root@oa ~]#
6 扩文件系统

扩充完LV之后,操作系统上还需要扩一下文件系统:xfs_growfs / 或者xfs_growfs /dev/centos/root 。因为这里的根文件系统/对应的是通过/dev/centos/root这个逻辑卷挂载上来的。

[root@oa ~]# df -Th
Filesystem             Type     Size Used Avail Use% Mounted on
devtmpfs               devtmpfs   16G     0   16G   0% /dev
tmpfs                   tmpfs     16G     0   16G   0% /dev/shm
tmpfs                   tmpfs     16G   11M   16G   1% /run
tmpfs                   tmpfs     16G     0   16G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs       50G   35G   16G  70% /
/dev/sda1               xfs     1014M 154M 861M  16% /boot
/dev/mapper/centos-home xfs       42G 107M   41G   1% /home
tmpfs                   tmpfs     3.2G     0  3.2G   0% /run/user/0
[root@oa ~]# xfs_growfs /
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=3276800 blks
        =                       sectsz=512   attr=2, projid32bit=1
        =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=13107200, imaxpct=25
        =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6400, version=2
        =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 13107200 to 39321600
[root@oa ~]# df -Th
Filesystem             Type     Size Used Avail Use% Mounted on
devtmpfs               devtmpfs   16G     0   16G   0% /dev
tmpfs                   tmpfs     16G     0   16G   0% /dev/shm
tmpfs                   tmpfs     16G   11M   16G   1% /run
tmpfs                   tmpfs     16G     0   16G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs       150G   35G 116G  24% /
/dev/sda1               xfs     1014M 154M 861M  16% /boot
/dev/mapper/centos-home xfs       42G 107M   41G   1% /home
tmpfs                   tmpfs     3.2G     0  3.2G   0% /run/user/0
[root@oa ~]#

三小结

以上,是在CentOS 7上,扩文件系统的一个操作流程和记录,多年不处理和操作Linux操作系统的基本管理和底层配置任务了,还是以前玩儿Oracle的时候,经常处理类似的case。

四 补充

将LV所在VG剩余空间全部扩到当前LV上。

##查看当前VG可用空间,Free  PE / Size       153599 / <600.00 GiB
[root@primarydb ~]# vgdisplay
 --- Volume group ---
VG Name               centos
System ID            
Format               lvm2
Metadata Areas        3
Metadata Sequence No  5
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                3
Open LV               2
Max PV                0
Cur PV                3
Act PV                3
VG Size               <898.99 GiB
PE Size               4.00 MiB
Total PE              230141
Alloc PE / Size       76542 / 298.99 GiB
Free PE / Size       153599 / <600.00 GiB
VG UUID               yKW3id-ZnAP-ymQ2-s7Dq-NrKe-BH1d-Y43lG0
 
[root@primarydb ~]#

##查看LV信息:
[root@primarydb ~]# lvdisplay /dev/centos/root

 --- Logical volume ---
LV Path               /dev/centos/root
LV Name               root
VG Name               centos
LV UUID               BkhSq6-06vY-j8bo-Hb2x-EiMT-PPkF-9H9W2o
LV Write Access       read/write
LV Creation host, time localhost, 2021-11-24 16:53:22 +0800
LV Status             available
 # open                 1
LV Size                50.00 GiB
Current LE             12800
Segments               1
Allocation             inherit
Read ahead sectors     auto
 - currently set to     8192
Block device           253:0
 
[root@primarydb ~]#
###把底层名为centos的VG上的所有可用空间,都划给名为/dev/centos/root的lv:
[root@primarydb ~]# lvresize -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to <650.00 GiB (166399 extents).
Logical volume centos/root successfully resized.
[root@primarydb ~]#

然后,看到的信息如下:

[root@primarydb ~]# vgdisplay 
 --- Volume group ---
VG Name               centos
System ID            
Format               lvm2
Metadata Areas        3
Metadata Sequence No  6
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                3
Open LV               2
Max PV                0
Cur PV                3
Act PV                3
VG Size               <898.99 GiB
PE Size               4.00 MiB
Total PE              230141
Alloc PE / Size       230141 / <898.99 GiB
Free PE / Size       0 / 0  
VG UUID               yKW3id-ZnAP-ymQ2-s7Dq-NrKe-BH1d-Y43lG0
 
[root@primarydb ~]# lvdisplay /dev/centos/root
 --- Logical volume ---
LV Path               /dev/centos/root
LV Name               root
VG Name               centos
LV UUID               BkhSq6-06vY-j8bo-Hb2x-EiMT-PPkF-9H9W2o
LV Write Access       read/write
LV Creation host, time localhost, 2021-11-24 16:53:22 +0800
LV Status             available
 # open                 1
LV Size               <650.00 GiB
Current LE             166399
Segments               3
Allocation             inherit
Read ahead sectors     auto
 - currently set to     8192
Block device           253:0
 
[root@primarydb ~]#

五 参考

https://www.thegeekdiary.com/how-to-grow-extend-xfs-filesytem-in-centos-rhel-using-xfs_growfs-command/

https://github.com/getnas/getnas/blob/master/storage/case-two.md

https://blog.csdn.net/wangsong_it/article/details/107509170

留言