目录

创建和扩容LVM

创建和扩容LVM

背景

之前老师说企业里LVM用的少,就没怎么学。巧了不是,刚好新公司有用到。

LVM简介

LVM即Logical Volume Manager,其将一个或多个硬盘的分区在逻辑上进行集合,相当于一个大硬盘,当其空间不足时,可以将其他硬盘的分区加入其中

LVM基本术语

物理卷(pv):即物理的磁盘分区
卷组(vg):若干个物理卷组成的一个集合
逻辑卷(lv):从vg中划分的逻辑分区

LVM的安装

  1. 检查系统有无安装lvm:rpm -qa | grep lvm
  2. 假设现系统新添加了一块硬盘,对此硬盘做lvm,先对此硬盘进行分区
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
[root@danco_1 ~]# fdisk /dev/sdb

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):
    
    m:获取帮助  
    p:显示当前分区  
    n:新建分区  
    d:删除分区  
    t:设置分区类型  
    w:保存当前分区设置并写入硬盘  

执行完分区操作后要使用命令:partprobe使分区表生效!
查看确认分区是否成功:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fdisk -l
磁盘 /dev/sdb:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 4096 字节
I/O 大小(最小/最佳):4096 字节 / 4096 字节
磁盘标签类型:dos
磁盘标识符:0x0e02e706

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   8e  Linux LVM
/dev/sdb2        20973568    41945087    10485760   8e  Linux LVM
  1. 创建物理卷pv
1
2
3
[root@danco_1 ~]# pvcreate /dev/sdb1 /dev/sdb2
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdb2" successfully created.

查看所有pv:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@danco_1 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               cl_fedora
  PV Size               62.41 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              15977
  Free PE               0
  Allocated PE          15977
  PV UUID               VdxBr8-ZeYh-k0LD-Oj1r-LV1g-yY0f-IVG2Sw

  "/dev/sdb1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               atxthc-ls8E-IzAd-wFof-gbtf-VGXu-NzGL3L

  "/dev/sdb2" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               k6GY0D-fIWN-VKKf-IECP-tcue-NGtW-KHOfdM
  1. 创建卷组vg
1
2
3
4
5
6
[root@danco_1 ~]# vgcreate vg01 /dev/sdb1 /dev/sdb2
Volume group "vg01" successfully created
[root@danco_1 ~]# vgs
VG        #PV #LV #SN Attr   VSize  VFree
cl_fedora   1   3   0 wz--n- 62.41g     0
vg01        2   0   0 wz--n- 19.99g 19.99g
  1. 创建逻辑卷lv
1
2
3
4
[root@danco_1 ~]# lvcreate  -l +100%FREE -n lv01 vg01
WARNING: ext4 signature detected on /dev/vg01/lv01 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/vg01/lv01.
Logical volume "lv01" created.

这里我把vg01卷组的所有空间都拿去做lv01了 查看系统所有逻辑卷组:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
[root@danco_1 ~]# lvdisplay
--- Logical volume ---
LV Path                /dev/vg01/lv01
LV Name                lv01
VG Name                vg01
LV UUID                kbHmMc-HNqB-tKcJ-4ZsI-ZIFy-VPfp-yuIAIB
LV Write Access        read/write
LV Creation host, time danco_1, 2021-11-22 16:56:47 +0800
LV Status              available
# open                 0
LV Size                19.99 GiB
Current LE             5118
Segments               2
Allocation             inherit
Read ahead sectors     auto
- currently set to     8192
Block device           253:3
  1. 创建并挂载文件系统
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@danco_1 ~]# mkfs.ext4 /dev/vg01/lv01
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: 完成
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5240832 blocks
262041 blocks (5.00%) reserved for the super user
第一个数据块=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: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

这里选用ext4格式。
然后进行挂载:

1
2
[root@danco_1 ~]# mkdir /media/lv01
[root@danco_1 ~]# mount /dev/vg01/lv01 /media/lv01/

或者进行永久挂载:

1
2
#vim /etc/fstab
/dev/vg01/lv01    /media/lv01  ext4   defaults  1   2

查看是否挂载成功:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[root@danco_1 ~]# df -h
文件系统                    容量  已用  可用 已用% 挂载点
devtmpfs                    716M     0  716M    0% /dev
tmpfs                       730M     0  730M    0% /dev/shm
tmpfs                       730M  8.4M  722M    2% /run
tmpfs                       730M     0  730M    0% /sys/fs/cgroup
/dev/mapper/cl_fedora-root   41G  3.4G   38G    9% /
/dev/sda2                  1014M  155M  860M   16% /boot
/dev/sda1                   599M  9.0M  590M    2% /boot/efi
/dev/mapper/cl_fedora-home   20G  174M   20G    1% /home
tmpfs                       146M     0  146M    0% /run/user/0
/dev/mapper/vg01-lv01        20G   45M   19G    1% /media/lv01

LVM扩容

上面的例子中,我们新加的硬盘一共30G,分了两个区出来一共20G并加入了卷组vg01,现在我们需要对逻辑卷lv01进行扩容。
先将sdb剩余空间进行分区并设为pv:

1
2
fdisk /dev/sdb
pvcreate /dev/sdb3

查看pv使用情况:

1
2
3
4
5
6
[root@danco_1 ~]# pvs
PV         VG        Fmt  Attr PSize   PFree
/dev/sda3  cl_fedora lvm2 a--   62.41g      0
/dev/sdb1  vg01      lvm2 a--  <10.00g      0
/dev/sdb2  vg01      lvm2 a--  <10.00g      0
/dev/sdb3            lvm2 ---  <10.00g <10.00g

查看vg使用情况,将新添加的sdb3加入到vg01卷组:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
[root@danco_1 ~]# pvs
PV         VG        Fmt  Attr PSize   PFree
/dev/sda3  cl_fedora lvm2 a--   62.41g      0
/dev/sdb1  vg01      lvm2 a--  <10.00g      0
/dev/sdb2  vg01      lvm2 a--  <10.00g      0
/dev/sdb3            lvm2 ---  <10.00g <10.00g
[root@danco_1 ~]# vgs
VG        #PV #LV #SN Attr   VSize  VFree
cl_fedora   1   3   0 wz--n- 62.41g    0
vg01        2   1   0 wz--n- 19.99g    0
[root@danco_1 ~]# vgextend vg01 /dev/sdb3
Volume group "vg01" successfully extended
[root@danco_1 ~]# vgs
VG        #PV #LV #SN Attr   VSize   VFree
cl_fedora   1   3   0 wz--n-  62.41g      0
vg01        3   1   0 wz--n- <29.99g <10.00g

可看到vg01现在还有剩余10G的空间,现将这些空间全部扩容到lv01

1
2
3
[root@danco_1 ~]# lvextend -l +100%FREE /dev/vg01/lv01
Size of logical volume vg01/lv01 changed from 19.99 GiB (5118 extents) to <29.99 GiB (7677 extents).
Logical volume vg01/lv01 successfully resized.

如果是扩容指定大小,使用lvextend -L +5G /dev/vg01/lv01
使用resize2fs命令扩容逻辑卷文件系统:

1
2
3
4
5
[root@danco_1 ~]# resize2fs /dev/vg01/lv01
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg01/lv01 is mounted on /media/lv01; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/vg01/lv01 is now 7861248 blocks long.

验证扩容是否成功:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[root@danco_1 ~]# df -h
文件系统                    容量  已用  可用 已用% 挂载点
devtmpfs                    716M     0  716M    0% /dev
tmpfs                       730M     0  730M    0% /dev/shm
tmpfs                       730M  8.4M  722M    2% /run
tmpfs                       730M     0  730M    0% /sys/fs/cgroup
/dev/mapper/cl_fedora-root   41G  3.4G   38G    9% /
/dev/sda2                  1014M  155M  860M   16% /boot
/dev/sda1                   599M  9.0M  590M    2% /boot/efi
/dev/mapper/cl_fedora-home   20G  174M   20G    1% /home
tmpfs                       146M     0  146M    0% /run/user/0
/dev/mapper/vg01-lv01        30G   44M   28G    1% /media/lv01