运维之基础命令--逻辑卷管理

一、LVM介绍

​ 逻辑卷管理LVM是一个多才多艺的硬盘系统工具。无论在Linux或者其他类似的系统,都是非常的好用。传统分区使用固定大小分区,重新调整大小十分麻烦。但是,LVM可以创建和管理“逻辑”卷,而不是直接使用物理硬盘。可以让管理员弹性的管理逻辑卷的扩大缩小,操作简单,而不损坏已存储的数据。可以随意将新的硬盘添加到LVM,以直接扩展已经存在的逻辑卷。LVM并不需要重启就可以让内核知道分区的存在。

LVM使用分层结构,如下如所示:

img

​ 图中顶部,首先是实际的物理磁盘及其划分的分区和其上的物理卷(PV)。一个或多个物理卷可以用来创建卷组(VG)。然后基于卷组可以创建逻辑卷(LV)。只要在卷组中有可用空间,就可以随心所欲的创建逻辑卷。文件系统就是在逻辑卷上创建的,然后可以在操作系统挂载和访问

二、LVM使用

2.1 磁盘准备

加装两块硬盘,sdb-10G与sdc-5G

[root@lb01 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0   47G  0 part /
sdb      8:16   0   10G  0 disk
sdc      8:32   0    5G  0 disk
sr0     11:0    1  792M  0 rom

给sbd创建LVM分区

[root@lb01 ~]# fdisk /dev/sdb
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): n  # 新建分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +2G
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): t  # 将分区改为LVM分区
Selected partition 1
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1 80  Old Minix
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x0005122b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   8e  Linux LVM

重复添加,再加一个3G,一个2G的分区,可见分区表如下:

[root@lb01 ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x0005122b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4196351     2097152   8e  Linux LVM
/dev/sdb2         4196352    10487807     3145728   8e  Linux LVM
/dev/sdb3        10487808    14682111     2097152   8e  Linux LVM

2.2 创建物理卷(PV)

安装LVM工具

[root@lb01 ~]# yum install lvm2 

将准备好的LVM分区生成为物理卷

[root@lb01 ~]# pvcreate /dev/sdb1
WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.
[root@lb01 ~]# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created.
[root@lb01 ~]# pvcreate /dev/sdb3
  Physical volume "/dev/sdb3" successfully created.

查看物理卷信息

[root@lb01 ~]# pvdisplay
  "/dev/sdb1" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               YUHdrd-AH1x-ew0m-Mp9i-3GHp-S4zc-XxlDJM

  "/dev/sdb2" is a new physical volume of "3.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name
  PV Size               3.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               wGDQvx-7zfs-g018-wqx4-Y8rh-1kWT-PnLuFu

  "/dev/sdb3" is a new physical volume of "2.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb3
  VG Name
  PV Size               2.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               fykcuR-jooY-WKbi-yM8j-7fQ5-mhnH-JCkXC8

补充:如何删除物理卷

[root@lb01 ~]# pvremove /dev/sdb1

2.3 创建卷组(VG)

将/dev/sdb{1,2,3}合并为卷组vg01

[root@lb01 ~]# vgcreate vg01 /dev/sdb1 /dev/sdb2 /dev/sdb3

查看卷组信息

[root@lb01 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg01
  System ID
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               <6.99 GiB
  PE Size               4.00 MiB
  Total PE              1789
  Alloc PE / Size       0 / 0
  Free  PE / Size       1789 / <6.99 GiB
  VG UUID               gzeouH-16qO-n6xv-V6VM-A9ep-eK6E-ODuIwU

补充:如何删除卷组

[root@lb01 ~]# vgremove vg01
  Volume group "vg01" successfully removed

2.4 创建逻辑卷(LV)

创建一个名为lv01,大小为100M的逻辑卷

[root@lb01 ~]# lvcreate -L 100M -n lv01 vg01
  Logical volume "lv01" created.

查看逻辑卷

[root@lb01 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                xFksnb-ENc2-mEo4-5gEb-yaJj-wP2c-oFuBeT
  LV Write Access        read/write
  LV Creation host, time lb01, 2021-09-01 20:39:16 +0800
  LV Status              available
  # open                 0
  LV Size                100.00 MiB
  Current LE             25
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

补充:删除逻辑卷

[root@lb01 ~]# lvremove /dev/vg01/lv02
Do you really want to remove active logical volume vg01/lv02? [y/n]: y
  Logical volume "lv02" successfully removed

2.5 逻辑卷挂载

格式化逻辑卷

[root@lb01 ~]# mkfs.ext4 /dev/vg01/lv01
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729

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

挂载逻辑卷

[root@lb01 ~]# mkdir /mnt/test-lv01
[root@lb01 ~]# mount /dev/vg01/lv01 /mnt/test-lv01
[root@lb01 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda3               47G  1.9G   46G   4% /
devtmpfs               479M     0  479M   0% /dev
tmpfs                  489M     0  489M   0% /dev/shm
tmpfs                  489M  6.8M  482M   2% /run
tmpfs                  489M     0  489M   0% /sys/fs/cgroup
/dev/sda1             1014M  119M  896M  12% /boot
tmpfs                   98M     0   98M   0% /run/user/0
/dev/mapper/vg01-lv01   93M  1.6M   85M   2% /mnt/test-lv01

2.6 扩展逻辑卷

卸载逻辑卷

[root@lb01 ~]# umount /dev/vg01/lv01

将逻辑卷lv01调整为200M

[root@lb01 ~]# lvresize -L 200M /dev/vg01/lv01 
  Size of logical volume vg01/lv01 changed from 100.00 MiB (25 extents) to 200.00 MiB (50 extents).
  Logical volume vg01/lv01 successfully resized.

检查磁盘错误

[root@lb01 ~]# e2fsck -f /dev/vg01/lv01
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/vg01/lv01: 11/25688 files (9.1% non-contiguous), 8896/102400 blocks

调整文件系统大小

[root@lb01 ~]# resize2fs /dev/vg01/lv01

查看逻辑卷信息

[root@lb01 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                svm5Xk-B8jZ-QhZw-egCm-x6b2-e5Jt-wvMi2F
  LV Write Access        read/write
  LV Creation host, time lb01, 2021-09-01 20:41:53 +0800
  LV Status              available
  # open                 0
  LV Size                200.00 MiB
  Current LE             50
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
  ...

重新挂载使用

[root@lb01 ~]# !mount
mount /dev/vg01/lv01 /mnt/test-lv01
[root@lb01 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda3               47G  1.9G   46G   4% /
devtmpfs               479M     0  479M   0% /dev
tmpfs                  489M     0  489M   0% /dev/shm
tmpfs                  489M  6.8M  482M   2% /run
tmpfs                  489M     0  489M   0% /sys/fs/cgroup
/dev/sda1             1014M  119M  896M  12% /boot
tmpfs                   98M     0   98M   0% /run/user/0
/dev/mapper/vg01-lv01  190M  1.6M  175M   1% /mnt/test-lv01

2.7 缩减逻辑卷

卸载逻辑卷

[root@lb01 ~]# umount /dev/vg01/lv01

检查磁盘错误

[root@lb01 ~]# e2fsck -f /dev/vg01/lv01
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/vg01/lv01: 11/49400 files (9.1% non-contiguous), 11884/204800 blocks

缩减文件系统大小,更新ext4信息

[root@lb01 ~]# resize2fs /dev/vg01/lv01 100M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg01/lv01 to 102400 (1k) blocks.
The filesystem on /dev/vg01/lv01 is now 102400 blocks long.

完成后,减小逻辑卷大小

[root@lb01 ~]# lvresize -L 100M /dev/vg01/lv01
  WARNING: Reducing active logical volume to 100.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg01/lv01? [y/n]: y
  Size of logical volume vg01/lv01 changed from 200.00 MiB (50 extents) to 100.00 MiB (25 extents).
  Logical volume vg01/lv01 successfully resized.

查看逻辑卷信息

[root@lb01 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                svm5Xk-B8jZ-QhZw-egCm-x6b2-e5Jt-wvMi2F
  LV Write Access        read/write
  LV Creation host, time lb01, 2021-09-01 20:41:53 +0800
  LV Status              available
  # open                 0
  LV Size                100.00 MiB
  Current LE             25
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
  ...

挂载使用

[root@lb01 ~]# !mo
mount /dev/vg01/lv01 /mnt/test-lv01
[root@lb01 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda3               47G  1.9G   46G   4% /
devtmpfs               479M     0  479M   0% /dev
tmpfs                  489M     0  489M   0% /dev/shm
tmpfs                  489M  6.8M  482M   2% /run
tmpfs                  489M     0  489M   0% /sys/fs/cgroup
/dev/sda1             1014M  119M  896M  12% /boot
tmpfs                   98M     0   98M   0% /run/user/0
/dev/mapper/vg01-lv01   93M  1.6M   85M   2% /mnt/test-lv01

2.8 VG增加一块磁盘

新盘创建lvm分区

[root@lb01 ~]# fdisk /dev/sdc
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 0x05ff27fa.

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

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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: 0x05ff27fa

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     8390655     4194304   8e  Linux LVM

创建物理卷

[root@lb01 ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.

扩容VG

[root@lb01 ~]# vgextend vg01 /dev/sdc1
  Volume group "vg01" successfully extended

查看VG信息

[root@lb01 ~]# vgdisplay vg01
  --- Volume group ---
  VG Name               vg01
  System ID
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  10
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               1
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               10.98 GiB
  PE Size               4.00 MiB
  Total PE              2812
  Alloc PE / Size       50 / 200.00 MiB
  Free  PE / Size       2762 / <10.79 GiB
  VG UUID               2Tjzna-XmXI-9MQ7-zeyc-AARV-84r5-jyymkf

PS:尽管我们使用一个单独的磁盘做示范,其实只要是‘8e’类型的磁盘分区都可以用来扩展卷组。

在VG中创建LV,可以看到磁盘都用上了

[root@lb01 ~]# lvcreate -L 10G -n lv03 vg01
  Logical volume "lv03" created.
[root@lb01 ~]# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda             8:0    0   50G  0 disk
├─sda1          8:1    0    1G  0 part /boot
├─sda2          8:2    0    2G  0 part [SWAP]
└─sda3          8:3    0   47G  0 part /
sdb             8:16   0   10G  0 disk
├─sdb1          8:17   0    2G  0 part
│ ├─vg01-lv01 253:0    0  100M  0 lvm  /mnt/test-lv01
│ ├─vg01-lv02 253:1    0  100M  0 lvm
│ └─vg01-lv03 253:2    0   10G  0 lvm
├─sdb2          8:18   0    3G  0 part
│ └─vg01-lv03 253:2    0   10G  0 lvm
└─sdb3          8:19   0    2G  0 part
  └─vg01-lv03 253:2    0   10G  0 lvm
sdc             8:32   0    5G  0 disk
└─sdc1          8:33   0    4G  0 part
  └─vg01-lv03 253:2    0   10G  0 lvm
sr0            11:0    1  792M  0 rom

运维之基础命令--逻辑卷管理
http://gsproj.github.io/2022/07/06/01_运维/01-基础命令/day19-磁盘分区/02_逻辑卷管理/
作者
GongSheng
发布于
2022年7月6日
许可协议