LVM 相关知识
阅读原文时间:2023年07月09日阅读:3

LVM 相关知识

名词

全称

释义

PV

Physical Volume

物理硬盘、硬盘分区或者RAID磁盘阵列,先要创建pv

VG

Volume Group

卷组建立在物理卷之上,一个卷组可以包含多个物理卷,而且在卷组创建之后也可以继续向其中添加新的物理卷。

LV

Logical Volume

逻辑卷是用卷组中空闲的资源建立的,并且逻辑卷在建立后可以动态地扩展或缩小空间。

PE

Physical Extent

基本单元,就像是存储数据的块大小

1.如果没有需要安装

[root@hdss7-200 ~]# yum -y install lvm2

功能/命令

物理卷管理

卷组管理

逻辑卷管理

扫描

pvscan

vgscan

lvscan

建立

pvcreate

vgcreate

lvcreate

显示

pvdisplay

vgdisplay

lvdisplay

删除

pvremove

vgremove

lvremove

扩展

vgextend

lvextend

缩小

vgreduce

lvreduce

1.第一步:初始化PV

[root@hdss7-200 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0      2:0    1    4K  0 disk
sda      8:0    0   20G  0 disk
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    1G  0 part [SWAP]
└─sda3   8:3    0 18.5G  0 part /
sdb      8:16   0   20G  0 disk

[root@hdss7-200 ~]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.

# 查看pv
[root@hdss7-200 ~]# pvdisplay
  "/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               xE8lvt-Rjmi-0PmE-K2O3-epdr-hJEx-XfXzr3

2.第二步:添加VG

[root@hdss7-200 ~]# vgcreate storage /dev/sdb
  Volume group "storage" successfully created

# 查看卷组
[root@hdss7-200 ~]# vgdisplay
  --- Volume group ---
  # 卷组的名字
  VG Name               storage
  System ID
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  # 卷组的大小
  VG Size               <20.00 GiB
  PE Size               4.00 MiB
  Total PE              5119
  Alloc PE / Size       0 / 0
  Free  PE / Size       5119 / <20.00 GiB
  VG UUID               PcjNur-WcsZ-dhzj-vmRf-kSQG-51Se-lilfzO

3.第三步:创建LV

# 创建一个逻辑卷名字叫做block1从卷组storage中去分容量。
[root@hdss7-200 ~]# lvcreate -n block1 -L 10G storage
  Logical volume "block1" created.

-n    # 指定块设备名称
-L    # 指定块设备大小
# 查看逻辑卷
[root@hdss7-200 ~]# lvdisplay
  --- Logical volume ---
  # 卷的位置
  LV Path                /dev/storage/block1
  LV Name                block1
  VG Name                storage
  LV UUID                UC72Oq-HkYP-Yg6G-PoU6-xGq3-pjjE-Ny6Xx6
  LV Write Access        read/write
  LV Creation host, time hdss7-200.host.com, 2021-05-21 01:51:38 +0800
  LV Status              available
  # open                 0
  # 卷的大小
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

# 原理
[root@hdss7-200 ~]# ls /dev/storage/block1 -l
lrwxrwxrwx 1 root root 7 May 21 01:51 /dev/storage/block1 -> ../dm-0

4.格式化挂载使用

[root@hdss7-200 ~]# mkfs.ext4 /dev/storage/block1
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
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 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

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

[root@hdss7-200 ~]# mount /dev/storage/block1 /mnt/

[root@hdss7-200 ~]# lsblk
NAME             MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0                2:0    1    4K  0 disk
sda                8:0    0   20G  0 disk
├─sda1             8:1    0  500M  0 part /boot
├─sda2             8:2    0    1G  0 part [SWAP]
└─sda3             8:3    0 18.5G  0 part /
# 可以看到大小有20G只分了10G
sdb                8:16   0   20G  0 disk
└─storage-block1 253:0    0   10G  0 lvm  /mnt

1.在已有的卷组中还有剩余容量

如果说现在的block1 卷容量快满了,要进行扩容,首先查看VG的信息,看看vg中有没有剩余的空间,如果有就可以直接扩容如果没有就需要添加新的物理磁盘了。

[root@hdss7-200 ~]# vgdisplay
  --- Volume group ---
  VG Name               storage
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  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
  # 本来是20G,但是实际容量是<20G的
  VG Size               <20.00 GiB
  PE Size               4.00 MiB
  Total PE              5119
  Alloc PE / Size       2560 / 10.00 GiB
  # 可用的<10G,创建block1的时候使用过10G
  Free  PE / Size       2559 / <10.00 GiB
  VG UUID               PcjNur-WcsZ-dhzj-vmRf-kSQG-51Se-lilfzO

扩容逻辑卷block1 就是扩容LV

# 把逻辑卷block1扩容到11G
[root@hdss7-200 ~]# lvextend -L 11G /dev/storage/block1
  Size of logical volume storage/block1 changed from 10.00 GiB (2560 extents) to 11.00 GiB (2816 extents).
  Logical volume storage/block1 successfully resized.

# 或者使用,这种方法在原有的基础上加2G,是在storage VG当中去自动的分取容量。
[root@hdss7-200 ~]# lvextend -L +2G /dev/storage/block1
  Size of logical volume storage/block1 changed from 11.00 GiB (2816 extents) to 13.00 GiB (3328 extents).
  Logical volume storage/block1 successfully resized.

查看容量

# 可以看到现在的容量是13G
[root@hdss7-200 ~]# lsblk
NAME             MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0                2:0    1    4K  0 disk
sda                8:0    0   20G  0 disk
├─sda1             8:1    0  500M  0 part /boot
├─sda2             8:2    0    1G  0 part [SWAP]
└─sda3             8:3    0 18.5G  0 part /
sdb                8:16   0   20G  0 disk
└─storage-block1 253:0    0   13G  0 lvm  /mnt
sr0               11:0    1 1024M  0 rom

# 但是在挂载中
[root@hdss7-200 ~]# df -h
Filesystem                  Size  Used Avail Use% Mounted on
*******************
# 只有9.8G
/dev/mapper/storage-block1  9.8G   37M  9.2G   1% /mnt

同步更改的容量信息

# 如果是xfs的文件系统
[root@hdss7-200 ~]# xfs_growfs /dev/storage/block1

# 如果是ext4文件系统,先检查文件系统完整性
[root@hdss7-200 ~]# e2fsck -f /dev/storage/block1
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/storage/block1: 11/655360 files (0.0% non-contiguous), 83137/2621440 blocks
# 再更改扩容的容量
[root@hdss7-200 ~]# resize2fs /dev/storage/block1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/storage/block1 to 3407872 (4k) blocks.
The filesystem on /dev/storage/block1 is now 3407872 blocks long.

[root@hdss7-200 ~]# df -h
Filesystem                  Size  Used Avail Use% Mounted on
*******************
/dev/mapper/storage-block1   13G   41M   12G   1% /mnt

2.新加磁盘或分区的扩容

扩容逻辑卷首先要知道该逻辑卷是用的哪个卷组中的容量。比如扩容根分区。扩容的步骤都差不多

  • 初始化PV
  • 扩展VG
  • 扩展逻辑卷
  • 更新磁盘容量

直接扩容/分区,这种就是经常要扩容的方式,显示的信息是在centos的VG中有一个root的逻辑卷,挂载到了根目录。

一般思路就是先扩展VG centos
再扩展逻辑卷LV root
[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0               2:0    1    4K  0 disk
sda               8:0    0   20G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]me

# 现在的/目录挂载的容量只有17G

查看现在有的VG

[root@localhost ~]# vgdisplay
  --- Volume group ---
  # VG的名字叫做centos,要对它进行扩容。
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  # 看得出这个VG已经被使用完了
  VG Size               <19.00 GiB
  PE Size               4.00 MiB
  Total PE              4863
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               X32zoj-u7lp-f8TX-0DGK-CZyv-CX1p-vN3qPd

扩容VG

# 先初始化一个PV,一个分区或者是一个磁盘
[root@localhost ~]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.

# 扩展VG
[root@localhost ~]# vgextend centos /dev/sdb
  Volume group "centos" successfully extended

# 查看信息
[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  # 现在已经扩容成功,差不多大了2倍的样子。这样扩展LV的时候就可以直接,从VG中分区容量。
  VG Size               38.99 GiB
  PE Size               4.00 MiB
  Total PE              9982
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       5119 / <20.00 GiB
  VG UUID               X32zoj-u7lp-f8TX-0DGK-CZyv-CX1p-vN3qPd

扩容逻辑卷

# 扩容至25G
[root@localhost ~]# lvextend -L 25G /dev/centos/root
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to 25.00 GiB (6400 extents).
  Logical volume centos/root successfully resized.

# 或者
# 在现有容量的基础上再增加5G
[root@localhost ~]# lvextend -L +5G /dev/centos/root
  Size of logical volume centos/root changed from 25.00 GiB (6400 extents) to 30.00 GiB (7680 extents).
  Logical volume centos/root successfully resized.

# 现在有30G了

更新卷大小

[root@localhost ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, 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 4455424 to 7864320

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0               2:0    1    4K  0 disk
sda               8:0    0   20G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part
  ├─centos-root 253:0    0   30G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk
└─centos-root   253:0    0   30G  0 lvm  /

# 现在可以看到根目录有30G了。

在对逻辑卷进行缩容操作时,其丢失数据的风险更大。所以在生产环境中执行相应操作时,一定要提前备份好数据。另外Linux系统规定,在对LVM逻辑卷进行缩容操作之前,对于ext文件系统要先检查文件系统的完整性(当然这也是为了保证数据安全)。在执行缩容操作前记得先把文件系统卸载掉。

[root@localhost ~]# lvreduce -L 20G /dev/centos/root
  WARNING: Reducing active and open logical volume to 20.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/root? [y/n]: y
  Size of logical volume centos/root changed from 30.00 GiB (7680 extents) to 20.00 GiB (5120 extents).
  Logical volume centos/root successfully resized.

# 或者
lvreduce -L -8G /dev/mapper/centos-home
xfs 文件系统只支持增大分区空间的情况,不支持减小的情况。

# 最后使用即可
e2fsck -f /dev/centos/root

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器