Linux 逻辑卷管理

环境准备

虚拟机添加3块20G硬盘,sdb sdc sdd

[root@centos7 ~]# lsblk /dev/sd{b..d}
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb    8:16   0  20G  0 disk 
sdc    8:32   0  20G  0 disk 
sdd    8:48   0  20G  0 disk

逻辑卷基本管理

硬盘分区管理方案缺点

  • 硬盘分区空间必须使用连续空间,所以硬盘分区扩容很麻烦,特别是要扩容的分区后没有空间。
  • 使用硬盘分区创建的文件系统不同跨多个硬盘,受单个硬盘容量限制。
  • 如果硬盘损坏,则文件系统中数据也会丢失。

逻辑卷管理硬盘方案优点

  • 使用逻辑卷可以更加灵活地扩展和缩减文件系统空间。
  • 使用逻辑卷创建的文件系统可以跨多个硬盘,适合创建超大容量文件系统。
  • 使用逻辑卷可以创建镜像卷,保证数据的冗余性。即使单个硬盘损坏,文件系统中数据也会丢失。
  • 使用逻辑卷还可以创建快照,保留某一时刻数据,就像虚拟机快照一样。

逻辑卷基本概念

LVM是 Logical Volume Manager(逻辑卷管理)的简写,LVM将若干个磁盘或者磁盘分区连接为一个整块的卷组,形成一个存储池。管理员可以在卷组上任意创建逻辑卷,并进一步在逻辑卷上创建文件系统。

  • 物理卷(PV, Physical Volume),是LVM的基本存储逻辑块,通过块设备创建。块设备是指磁盘、磁盘分区或从逻辑上和磁盘分区具有同样功能的设备(如RAID)。与基本的物理存储介质(如分区、磁盘等)相比,物理卷包含有和LVM相关的管理参数。
  • 卷组(VG, Volume Group),是一个逻辑的存储空间,由一个或多个物理卷组成。
  • 逻辑卷(LV, Logical Volume),是从卷组中创建出来的一个逻辑空间。如果把一个卷组类比为一个逻辑硬盘,那么逻辑卷则可以类比为一个硬盘分区,所以也可以在逻辑卷之上创建文件系统。

简单地说:卷组由多个物理卷组成,可以在卷组中创建多个逻辑卷,对逻辑卷创建文件系统给用户使用。

LVM管理基本流程:

  1. 创建物理卷
  2. 创建卷组
  3. 创建逻辑卷

创建物理卷

# 创建单个PV
[root@server ~ 13:36:00]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.

# 此次创建多个PV
[root@server ~ 13:36:22]# pvcreate /dev/sd{c,d}
  Physical volume "/dev/sdc" successfully created.
  Physical volume "/dev/sdd" successfully created.
  
# 查看PV列表
[root@server ~ 13:36:38]# pvs
  PV         VG     Fmt  Attr PSize    PFree 
  /dev/sda2  centos lvm2 a--  <199.00g  4.00m
  /dev/sdb          lvm2 ---    20.00g 20.00g
  /dev/sdc          lvm2 ---    20.00g 20.00g
  /dev/sdd          lvm2 ---    20.00g 20.00g

# 查看单个PV详细信息
[root@server ~ 13:36:41]# pvdisplay /dev/sdb
  "/dev/sdb" is a new physical volume of "20.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb
  VG Name               
  PV Size               20.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               1Kov2E-FV7J-hWP4-lZOy-Y1EP-BNAx-u69jcV

创建卷组

# 创建包涵单个PV的VG
[root@server ~ 13:37:46]# vgcreate webapp /dev/sdb
  Volume group "webapp" successfully created
  
# 创建包涵多个PV的VG
[root@server ~ 13:38:28]# vgcreate dbapp /dev/sd{c,d}
  Volume group "dbapp" successfully created

[root@server ~ 13:38:51]# pvs
  PV         VG     Fmt  Attr PSize    PFree  
  /dev/sda2  centos lvm2 a--  <199.00g   4.00m
  /dev/sdb   webapp lvm2 a--   <20.00g <20.00g
  /dev/sdc   dbapp  lvm2 a--   <20.00g <20.00g
  /dev/sdd   dbapp  lvm2 a--   <20.00g <20.00g

# 查看VG列表
[root@server ~ 13:38:55]# vgs
  VG     #PV #LV #SN Attr   VSize    VFree  
  centos   1   3   0 wz--n- <199.00g   4.00m
  dbapp    2   0   0 wz--n-   39.99g  39.99g
  webapp   1   0   0 wz--n-  <20.00g <20.00g

# 查看单个VG详细信息
[root@server ~ 13:39:42]# vgdisplay dbapp 
  --- Volume group ---
  VG Name               dbapp
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               39.99 GiB
  PE Size               4.00 MiB
  Total PE              10238
  Alloc PE / Size       0 / 0   
  Free  PE / Size       10238 / 39.99 GiB
  VG UUID               fH4nMX-f0MZ-Y41F-1dgN-vrDD-Cile-98YDgj

创建逻辑卷

# 在卷组webapp中创建一个逻辑卷:名称为webapp01,大小为5G。
[root@server ~ 13:40:05]# lvcreate -n webapp01 -L 5G webapp 
  Logical volume "webapp01" created.

# 在卷组dbapp中创建一个跨硬盘逻辑卷:名称为data01,大小为25G。
[root@server ~ 13:41:08]# lvcreate -n data01 -L 25G dbapp 
  Logical volume "data01" created.

# 查看LV列表
[root@server ~ 13:41:25]# lvs
  LV       VG     Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home     centos -wi-ao---- <145.12g                                                    
  root     centos -wi-ao----   50.00g                                                    
  swap     centos -wi-ao----   <3.88g                                                    
  data01   dbapp  -wi-a-----   25.00g                                                    
  webapp01 webapp -wi-a-----    5.00g                                                    

逻辑卷设备名支持以下三种格式:

  • /dev/vname/lvame
  • /dev/mapper/vname-lvame
  • /dev/dm-N
[root@server ~ 13:41:29]# ll /dev/dbapp/data01 /dev/mapper/dbapp-data01 
lrwxrwxrwx 1 root root 7 Nov 14 13:41 /dev/dbapp/data01 -> ../dm-4
lrwxrwxrwx 1 root root 7 Nov 14 13:41 /dev/mapper/dbapp-data01 -> ../dm-4
# 查看单个LV详细信息
[root@server ~ 13:42:51]# lvdisplay /dev/dbapp/data01 
  --- Logical volume ---
  LV Path                /dev/dbapp/data01
  LV Name                data01
  VG Name                dbapp
  LV UUID                tcO9Ru-d73m-JZpE-oCD9-f0MM-mtqa-c0nJKQ
  LV Write Access        read/write
  LV Creation host, time server.centos.cloud, 2025-11-14 13:41:25 +0800
  LV Status              available
  # open                 0
  LV Size                25.00 GiB
  Current LE             6400
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:4

# 可以看到:物理卷/dev/sdc空间已使用完,物理卷/dev/sdd空间已使用5G
[root@server ~ 13:43:12]# pvs
  PV         VG     Fmt  Attr PSize    PFree  
  /dev/sda2  centos lvm2 a--  <199.00g   4.00m
  /dev/sdb   webapp lvm2 a--   <20.00g <15.00g
  /dev/sdc   dbapp  lvm2 a--   <20.00g      0 
  /dev/sdd   dbapp  lvm2 a--   <20.00g  14.99g

# 可以看到逻辑卷/dev/dbapp/data01空间横跨2个硬盘
[root@server ~ 13:43:45]# lsblk /dev/sd{b..d}
NAME              MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb                 8:16   0  20G  0 disk 
└─webapp-webapp01 253:3    0   5G  0 lvm  
sdc                 8:32   0  20G  0 disk 
└─dbapp-data01    253:4    0  25G  0 lvm  
sdd                 8:48   0  20G  0 disk 
└─dbapp-data01    253:4    0  25G  0 lvm

创建文件系统

[root@server ~ 13:44:16]# mkfs.xfs /dev/webapp/webapp01 
meta-data=/dev/webapp/webapp01   isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@server ~ 13:45:56]# mkdir -p /webapp/webapp01
[root@server ~ 13:47:04]# mount /dev/webapp/webapp01 /webapp/webapp01/
# 基于lvm的文件系统持久化挂载,需要修改/etc/fstab文件。

清理

# 卸载文件系统
[root@server ~ 13:48:19]# umount /webapp/webapp01 

# 删除LV
[root@server ~ 13:52:04]# lvremove /dev/webapp/webapp01 /dev/dbapp/data01 
Do you really want to remove active logical volume webapp/webapp01? [y/n]: y
  Logical volume "webapp01" successfully removed
Do you really want to remove active logical volume dbapp/data01? [y/n]: y
  Logical volume "data01" successfully removed

# 删除VG
[root@server ~ 13:52:43]# vgremove webapp dbapp 
  Volume group "webapp" successfully removed
  Volume group "dbapp" successfully removed

# 删除PV
[root@server ~ 13:52:53]# pvremove /dev/sd{b..d}
  Labels on physical volume "/dev/sdb" successfully wiped.
  Labels on physical volume "/dev/sdc" successfully wiped.
  Labels on physical volume "/dev/sdd" successfully wiped.

扩展和缩减卷组

环境准备

# 创建卷组
[root@server ~ 13:53:08]# vgcreate webapp /dev/sdb
  Physical volume "/dev/sdb" successfully created.
  Volume group "webapp" successfully created
# 创建卷组的时候,如果指定的块设备不是物理卷,则会先将块设备创建为物理卷。

# 创建逻辑卷
[root@server ~ 14:09:27]# lvcreate -n webapp01 -L 10G webapp 
WARNING: xfs signature detected on /dev/webapp/webapp01 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/webapp/webapp01.
  Logical volume "webapp01" created.

扩展卷组

如果卷组空间不足,可以通过添加新的物理卷到卷组中,以扩展卷组容量。

[root@server ~ 14:09:52]# vgextend webapp /dev/sd{c,d}
  Physical volume "/dev/sdc" successfully created.
  Physical volume "/dev/sdd" successfully created.
  Volume group "webapp" successfully extended

缩减卷组

如果卷组空间中有多余的物理卷或者物理卷容量小需要更换,则可以将物理卷从卷组中剔除。

删除物理卷前确保物理卷不在使用中。

# 查看物理卷使用状态
[root@server ~ 14:10:37]# pvs
  PV         VG     Fmt  Attr PSize    PFree  
  /dev/sda2  centos lvm2 a--  <199.00g   4.00m
  /dev/sdb   webapp lvm2 a--   <20.00g <10.00g # /dev/sdb正在使用
  /dev/sdc   webapp lvm2 a--   <20.00g <20.00g
  /dev/sdd   webapp lvm2 a--   <20.00g <20.00g

# 将物理卷/dev/sdb从卷组webapp中剔除,则会报错
[root@server ~ 14:10:52]# vgreduce webapp /dev/sdb
  Physical volume "/dev/sdb" still in use

# 解决方法:将物理卷/dev/sdb中数据移动到卷组中其他物理卷
[root@server ~ 14:11:58]# pvmove /dev/sdb
  /dev/sdb: Moved: 0.35%
  /dev/sdb: Moved: 100.00%
# 或者移动到卷组中特定物理卷
[root@centos7 ~]# pvmove /dev/sdb /dev/sdc

# 查看物理卷使用状态
[root@server ~ 14:12:56]# pvs
  PV         VG     Fmt  Attr PSize    PFree  
  /dev/sda2  centos lvm2 a--  <199.00g   4.00m
  /dev/sdb   webapp lvm2 a--   <20.00g <20.00g # /dev/sdb不在使用
  /dev/sdc   webapp lvm2 a--   <20.00g <10.00g # /dev/sdc在使用
  /dev/sdd   webapp lvm2 a--   <20.00g <20.00g

# 再次剔除,成功
[root@server ~ 14:13:03]# vgreduce webapp /dev/sdb
  Removed "/dev/sdb" from volume group "webapp"
[root@server ~ 14:14:22]# pvs
  PV         VG     Fmt  Attr PSize    PFree  
  /dev/sda2  centos lvm2 a--  <199.00g   4.00m
  /dev/sdb          lvm2 ---    20.00g  20.00g
  /dev/sdc   webapp lvm2 a--   <20.00g <10.00g
  /dev/sdd   webapp lvm2 a--   <20.00g <20.00g

扩展和缩减逻辑卷

扩展逻辑卷

如果逻辑卷空间不足,在卷组空间充足的情况下,也可以扩展逻辑卷空间。

# 逻辑卷增加2G空间
[root@server ~ 14:14:31]# lvextend -L +2G /dev/webapp/webapp01 
  Size of logical volume webapp/webapp01 changed from 10.00 GiB (2560 extents) to 12.00 GiB (3072 extents).
  Logical volume webapp/webapp01 successfully resized.

[root@server ~ 14:15:05]# lvs /dev/webapp/webapp01 
  LV       VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  webapp01 webapp -wi-a----- 12.00g

缩减逻辑卷

如果逻辑卷空间充足,希望将多余的空间缩减出来它用,也可以缩减逻辑卷空间。

[root@server ~ 14:15:16]# lvreduce -L -2G /dev/webapp/webapp01 
  WARNING: Reducing active logical volume to 10.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce webapp/webapp01? [y/n]: y
  Size of logical volume webapp/webapp01 changed from 12.00 GiB (3072 extents) to 10.00 GiB (2560 extents).
  Logical volume webapp/webapp01 successfully resized.

[root@server ~ 14:15:58]# lvs /dev/webapp/webapp01 
  LV       VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  webapp01 webapp -wi-a----- 10.00g

扩展和缩减文件系统

扩展 XFS 文件系统

XFS文件系统只支持扩展,不支持缩减

环境准备:格式化文件系统,并创建数据

[root@server ~ 14:49:17]# mkfs.xfs /dev/webapp/webapp01 
meta-data=/dev/webapp/webapp01   isize=512    agcount=4, agsize=655360 blks
      =                       sectsz=512   attr=2, projid32bit=1
      =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2621440, imaxpct=25
      =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
      =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@server ~ 14:49:37]# mkdir /var/www/html/
[root@server ~ 14:49:51]# mount /dev/webapp/webapp01 /var/www/html/
[root@server ~ 14:50:08]# cp /etc/host* /var/www/html/
[root@server ~ 14:50:25]# ls /var/www/html/
host.conf  hostname  hosts  hosts.allow  hosts.deny
# 第一步:扩展逻辑卷
[root@server ~ 14:51:05]# lvextend -L 15G /dev/webapp/webapp01 
  Size of logical volume webapp/webapp01 changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
  Logical volume webapp/webapp01 successfully resized.
[root@server ~ 14:53:11]# lvs /dev/webapp/webapp01 
  LV       VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  webapp01 webapp -wi-ao---- 15.00g
  
# 第二步:扩展文件系统
[root@server ~ 14:53:20]# xfs_growfs /var/www/html/
meta-data=/dev/mapper/webapp-webapp01 isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2621440, 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 2621440 to 3932160
[root@server ~ 14:53:51]# df -h /var/www/html/
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/webapp-webapp01   15G   33M   15G   1% /var/www/html
[root@server ~ 14:53:57]# ls /var/www/html/
host.conf  hostname  hosts  hosts.allow  hosts.deny

# 块设备和文件系统一并扩展
[root@server ~ 14:54:05]# lvextend -rL 20G /dev/webapp/webapp01
  Size of logical volume webapp/webapp01 changed from 15.00 GiB (3840 extents) to 20.00 GiB (5120 extents).
  Logical volume webapp/webapp01 successfully resized.
meta-data=/dev/mapper/webapp-webapp01 isize=512    agcount=6, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=3932160, 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 3932160 to 5242880
[root@server ~ 14:54:50]# lvs /dev/webapp/webapp01 
  LV       VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  webapp01 webapp -wi-ao---- 20.00g

[root@server ~ 14:55:38]# df -Th /var/www/html/
Filesystem                  Type  Size  Used Avail Use% Mounted on
/dev/mapper/webapp-webapp01 xfs    20G   33M   20G   1% /var/www/html

扩展 EXT4 文件系统

EXT4文件系统即支持扩展,也支持缩减。

环境准备:格式化文件系统,并创建数据

[root@server ~ 14:55:51]# umount /var/www/html 
[root@server ~ 14:56:13]# mkfs.ext4 /dev/webapp/webapp01 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   

[root@server ~ 14:56:27]# mount /dev/webapp/webapp01 /var/www/html/
[root@server ~ 14:57:22]# df -Th /var/www/html/
Filesystem                  Type  Size  Used Avail Use% Mounted on
/dev/mapper/webapp-webapp01 ext4   20G   45M   19G   1% /var/www/html

[root@server ~ 14:57:29]# cp /etc/host* /var/www/html/
[root@server ~ 14:58:04]# ls /var/www/html/
host.conf  hostname  hosts  hosts.allow  hosts.deny 
# 第一步:扩展逻辑卷
[root@server ~ 14:58:10]# lvextend -L 25G /dev/webapp/webapp01 
  Size of logical volume webapp/webapp01 changed from 20.00 GiB (5120 extents) to 25.00 GiB (6400 extents).
  Logical volume webapp/webapp01 successfully resized.
[root@server ~ 14:58:42]# lvs /dev/webapp/webapp01 
  LV       VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  webapp01 webapp -wi-ao---- 25.00g
  
# 第二步:扩展文件系统
[root@server ~ 14:58:48]# resize2fs /dev/webapp/webapp01 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/webapp/webapp01 is mounted on /var/www/html; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/webapp/webapp01 is now 6553600 blocks long.

[root@server ~ 14:59:10]# df -Th /var/www/html/
Filesystem                  Type  Size  Used Avail Use% Mounted on
/dev/mapper/webapp-webapp01 ext4   25G   44M   24G   1% /var/www/html
[root@server ~ 14:59:17]# ls /var/www/html/
host.conf  hostname  hosts  hosts.allow  hosts.deny

# 块设备和文件系统一并扩展
[root@server ~ 14:59:25]# lvextend -rL 30G /dev/webapp/webapp01
  Size of logical volume webapp/webapp01 changed from 25.00 GiB (6400 extents) to 30.00 GiB (7680 extents).
  Logical volume webapp/webapp01 successfully resized.
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/webapp-webapp01 is mounted on /var/www/html; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 4
The filesystem on /dev/mapper/webapp-webapp01 is now 7864320 blocks long.

[root@server ~ 15:00:01]# lvs /dev/webapp/webapp01 
  LV       VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  webapp01 webapp -wi-ao---- 30.00g                                                    
[root@server ~ 15:00:51]# df -Th /var/www/html/
Filesystem                  Type  Size  Used Avail Use% Mounted on
/dev/mapper/webapp-webapp01 ext4   30G   44M   28G   1% /var/www/html

缩减 EXT4 文件系统

缩减EXT4文件系统注意事项:

  • 不支持在线缩减,必须卸载后缩减。
  • 缩减后的容量,不能小于当前使用的容量。

ext4 减容流程:

  1. 卸载文件系统
  2. e2fsck 文件系统
  3. 缩减文件系统
  4. 缩减lv
  5. 挂载测试

具体操作如下:

# 第一步:卸载文件系统
[root@server ~ 15:00:59]# umount /var/www/html

# 第二步:检测文件系统
[root@server ~ 15:01:27]# e2fsck -f /dev/webapp/webapp01 
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/webapp/webapp01: 16/1966080 files (0.0% non-contiguous), 167447/7864320 blocks

# 第三步:缩减文件系统
[root@server ~ 15:01:39]# resize2fs /dev/webapp/webapp01 10G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/webapp/webapp01 to 2621440 (4k) blocks.
The filesystem on /dev/webapp/webapp01 is now 2621440 blocks long.

# 第四步:缩减逻辑卷
[root@server ~ 15:02:13]# lvreduce -L 10G /dev/webapp/webapp01 
  WARNING: Reducing active logical volume to 10.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce webapp/webapp01? [y/n]: y
  Size of logical volume webapp/webapp01 changed from 30.00 GiB (7680 extents) to 10.00 GiB (2560 extents).
  Logical volume webapp/webapp01 successfully resized.

# 第五步:挂载文件系统验证
[root@centos7 ~]# mount /dev/webapp/webapp01 /var/www/html

[root@server ~ 15:04:33]# mount /dev/webapp/webapp01 /var/www/html/
[root@server ~ 15:05:01]# df -Th /var/www/html/
Filesystem                  Type  Size  Used Avail Use% Mounted on
/dev/mapper/webapp-webapp01 ext4  9.8G   37M  9.2G   1% /var/www/html
[root@server ~ 15:05:09]# ls /var/www/html/
host.conf  hostname  hosts  hosts.allow  hosts.deny  lost+found

补充说明: 如果文件系统是构建在磁盘分区上,扩展文件系统容量和缩减文件系统容量操作相同。

练习:缩减/home磁盘空间100G给/使用

  1. 备份 home
  2. 卸载
  3. 缩减 /dev/mapper/centos-home 100G
  4. 格式化 /dev/mapper/centos-home xfs
  5. 恢复home的数据
  6. /dev/mapper/centos-root 扩容100
[root@client ~ 15:38:09]# xfsdump -L "Home Directory Backup" -M "Backup_Drive_20241114" -f /tmp/home_backup_full.dump /home
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsdump: level 0 dump of client.centos.cloud:/home
xfsdump: dump date: Fri Nov 14 15:38:12 2025
xfsdump: session id: 1aa610d6-3ff1-4dc0-9bfc-bc3924e1b1f2
xfsdump: session label: "Home Directory Backup"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 34368 bytes
xfsdump: /var/lib/xfsdump/inventory created
xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 25608 bytes
xfsdump: dump size (non-dir files) : 1632 bytes
xfsdump: dump complete: 0 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /tmp/home_backup_full.dump OK (success)
xfsdump: Dump Status: SUCCESS

[root@client ~ 15:39:13]# umount /home 
[root@client ~ 15:39:21]# lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   200G  0 disk 
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0   199G  0 part 
  ├─centos-root 253:0    0    50G  0 lvm  /
  ├─centos-swap 253:1    0   3.9G  0 lvm  [SWAP]
  └─centos-home 253:2    0 145.1G  0 lvm  
sr0              11:0    1   4.4G  0 rom  

[root@client ~ 15:39:25]# lvreduce -L 100G /dev/centos/home 
  WARNING: Reducing active logical volume to 100.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/home? [y/n]: y
  Size of logical volume centos/home changed from <145.12 GiB (37150 extents) to 100.00 GiB (25600 extents).
  Logical volume centos/home successfully resized.

[root@client ~ 15:44:30]# lvreduce -L 50G /dev/centos/home 
  WARNING: Reducing active logical volume to 50.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/home? [y/n]: y
  Size of logical volume centos/home changed from 100.00 GiB (25600 extents) to 50.00 GiB (12800 extents).
  Logical volume centos/home successfully resized.

[root@client ~ 15:44:51]# mkfs.xfs -f /dev/centos/home 
meta-data=/dev/centos/home       isize=512    agcount=4, agsize=3276800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=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 log           bsize=4096   blocks=6400, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@client ~ 15:46:27]# mount /dev/centos/home /home/

[root@client ~ 15:46:32]# xfsrestore -f /tmp/home_backup_full.dump /home/
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description: 
xfsrestore: hostname: client.centos.cloud
xfsrestore: mount point: /home
xfsrestore: volume: /dev/mapper/centos-home
xfsrestore: session time: Fri Nov 14 15:38:12 2025
xfsrestore: level: 0
xfsrestore: session label: "Home Directory Backup"
xfsrestore: media label: "Backup_Drive_20241114"
xfsrestore: file system id: c266ce41-65ca-4780-a0c5-0a771c38b850
xfsrestore: session id: 1aa610d6-3ff1-4dc0-9bfc-bc3924e1b1f2
xfsrestore: media id: 53196597-8bc1-42fa-9cf4-fe51ee4f226e
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 2 directories and 4 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /tmp/home_backup_full.dump OK (success)
xfsrestore: Restore Status: SUCCESS

[root@client ~ 15:50:52]# lvextend -r -l +100%FREE /dev/centos/root 
  Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 145.12 GiB (37151 extents).
  Logical volume centos/root successfully resized.
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 38042624
[root@client ~ 15:51:04]# lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   200G  0 disk 
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0   199G  0 part 
  ├─centos-root 253:0    0 145.1G  0 lvm  /
  ├─centos-swap 253:1    0   3.9G  0 lvm  [SWAP]
  └─centos-home 253:2    0    50G  0 lvm  /home
sr0              11:0    1   4.4G  0 rom  

逻辑卷快照

我们可以使用快照记录了lv中数据,后续可以用来恢复数据。

# 创建快照:快照的容量不能小于lv容量
[root@centos7 ~]# lvcreate -s -n webapp01-snap1 -L 10G /dev/webapp/webapp01
  Logical volume "webapp01-snap1" created.

# 挂载快照
[root@centos7 ~]# mount /dev/webapp/webapp01-snap1 /webapp/webapp01/

# 查看数据
[root@centos7 ~]# ls /webapp/webapp01/
host.conf  hostname  hosts  hosts.allow  hosts.deny

# 创建新数据
[root@centos7 ~]# echo hello world > /webapp/webapp01/hello.txt
[root@centos7 ~]# cat /webapp/webapp01/hello.txt
hello world

raid 逻辑卷

准备环境

[root@centos7 ~]# umount /webapp/webapp0
[root@centos7 ~]# umount /var/www/html
[root@centos7 ~]# lvremove /dev/webapp/webapp01*

创建 raid1 逻辑卷

[root@centos7 ~]# lvcreate --type raid1 -n webapp01 -L 15G webapp
[root@centos7 ~]# mkfs.xfs /dev/webapp/webapp01 
[root@centos7 ~]# mount /dev/webapp/webapp01 /var/www/html/
[root@centos7 ~]# cp /etc/ho* /var/www/html/

模拟故障

[root@centos7 ~]# dd if=/dev/zero of=/dev/sdd bs=1M count=256

此时文件系统仍可以正方访问

[root@centos7 ~]# ls /var/www/html/
host.conf  hostname  hosts  hosts.allow  hosts.deny
[root@centos7 ~]# umount /var/www/html
[root@centos7 ~]# mount /dev/webapp/webapp01 /var/www/html/

修复raid

# 删除丢失的物理卷
[root@centos7 ~]# vgreduce --removemissing webapp --force

# 扩容回来
[root@centos7 ~]# vgextend webapp /dev/sdd

# 修复raid1 逻辑卷
[root@centos7 ~]# lvconvert --repair /dev/webapp/webapp01

# 扫描物理卷变化
[root@centos7 ~]# pvscan

# 验证结果
[root@centos7 ~]# pvs|grep webap
  /dev/sdb   webapp lvm2 a--  <20.00g   4.99g
  /dev/sdc   webapp lvm2 a--  <20.00g   4.99g
  /dev/sdd   webapp lvm2 a--  <20.00g <20.00g

其他示例:创建 raid5 逻辑卷

# Create  a  RAID5  LV  with a usable size of 5GiB, 3 stripes, a stripe size of 64KiB, using a total of 4 devices (including one for parity).
lvcreate --type raid5 -L 5G -i 3 -I 64 -n mylv vg00
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐