Linux LVM根目录扩容操作指南

发布于:2025-07-06 ⋅ 阅读:(13) ⋅ 点赞:(0)

#作者:张桐瑞

1 环境示例

  • 系统:CentOS
  • 文件系统:XFS
  • 根分区使用LVM管理
  • 新加磁盘:/dev/sdb

2 查看现有磁盘

[root@centos01 ~]# lsblk -f  #列出所有块设备和文件系统类型。
NAME            FSTYPE      LABEL UUID                                   MOUNTPOINT
sda                                                                      
├─sda1          xfs               40fe2536-61bc-4764-9953-1b598364860e   /boot
└─sda2          LVM2_member       12dSdc-5ffT-U6SQ-1rbj-DEBl-B31m-ZvTdIy 
  ├─centos-root xfs               8e5d2ada-0e2d-457c-a069-b5cf0f6012fd   /
  ├─centos-swap swap              8868ce34-3dbf-44b4-9c37-7d67a05700aa   [SWAP]
  └─centos-home xfs               5c9b7c5b-bc6a-4500-8160-7638650ebef9   /home
sdb                                                                      
sdc                                                                      
sr0                                                                      
[root@centos01 ~]#   #可以看到 sdb、sdc 是新加的裸盘,没有分区或文件系统。

3 确认现有空间

[root@centos01 ~]# df -h  #显示已挂载文件系统的使用情况。
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.6M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   50G   14G   37G  27% /
/dev/sda1               1014M  162M  853M  16% /boot
/dev/mapper/centos-home   47G   33M   47G   1% /home
tmpfs                    182M     0  182M   0% /run/user/0
[root@centos01 ~]# #根分区是LVM逻辑卷/dev/mapper/centos-root,初始50G。

4 磁盘分区

[root@centos01 ~]# fdisk /dev/sdb  #以 /dev/sdb 为例,使用 fdisk 分区
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x09736fa8.

Command (m for help): n  # 创建新分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p  # 选择主分区
Partition number (1-4, default 1):   # 回车,默认分区号
First sector (2048-209715199, default 2048):  # 回车,默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): # 回车,默认
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set

Command (m for help): p  # 回车,默认

Disk /dev/sdb: 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: 0x09736fa8
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209715199   104856576   83  Linux
Command (m for help): w    # 写入分区表并退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@centos01 ~]#  #分区完成后,生成/dev/sdb1

5 扩容lvm

[root@centos01 ~]# lvm  #进入 lvm 命令行
lvm> pvcreate /dev/sdb1  #创建物理卷
  Physical volume "/dev/sdb1" successfully created.
lvm> pvdisplay  #查看物理卷
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <99.00 GiB / not usable 3.00 MiB  
  "/dev/sdb1" is a new physical volume of "<100.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               <100.00 GiB
   
lvm> vgdisplay  #查看卷组
  --- Volume group ---
  VG Name               centos
  System ID             
      
lvm> vgextend centos /dev/sdb1 #扩展卷组
  Volume group "centos" successfully extended
lvm> lvdisplay #查看逻辑卷
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  --- Logical volume ---
  LV Path                /dev/centos/home
  LV Name                home
  VG Name                centos
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
 
lvm> lvextend -l +100%FREE /dev/centos/root #扩展根分区的逻辑卷
  Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 150.00 GiB (38400 extents).
  Logical volume centos/root successfully resized.
#命令说明
#   -l +100%FREE:把卷组中所有未分配的空间都给根分区
#   /dev/centos/root:指定要扩展的逻辑卷
lvm> exit #退出 lvm 交互命令行
  Exiting.
[root@centos01 ~]#

6 拓展文件系统

[root@centos01 ~]# xfs_growfs /dev/mapper/centos-root #分区是 XFS 格式,使用 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@centos01 ~]# df -h  #查看挂载空间
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.6M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root  150G   14G  137G   9% /
/dev/sda1               1014M  162M  853M  16% /boot
/dev/mapper/centos-home   47G   33M   47G   1% /home
tmpfs                    182M     0  182M   0% /run/user/0
[root@centos01 ~]# lsblk -f  #检查磁盘布局
NAME            FSTYPE      LABEL UUID                                   MOUNTPOINT
sda                                                                      
├─sda1          xfs               40fe2536-61bc-4764-9953-1b598364860e   /boot
└─sda2          LVM2_member       12dSdc-5ffT-U6SQ-1rbj-DEBl-B31m-ZvTdIy 
  ├─centos-root xfs               8e5d2ada-0e2d-457c-a069-b5cf0f6012fd   /
  ├─centos-swap swap              8868ce34-3dbf-44b4-9c37-7d67a05700aa   [SWAP]
  └─centos-home xfs               5c9b7c5b-bc6a-4500-8160-7638650ebef9   /home
sdb                                                                      
└─sdb1          LVM2_member       KDbLDH-djf0-GGpl-N0le-Fv37-1ZMZ-pxttFy 
  └─centos-root xfs               8e5d2ada-0e2d-457c-a069-b5cf0f6012fd   /
sdc                                                                      
sr0                                           

7 参考命令速查

lsblk -f
df -h
fdisk /dev/sdb
lvm
  pvcreate /dev/sdb1
  pvdisplay
  vgextend centos /dev/sdb1
  lvdisplay
  lvextend -l +100%FREE /dev/centos/root
  exit
xfs_growfs /dev/mapper/centos-root