LVM基础操作步骤梳理


1. 理解LVM工作原理

什么是LVM?

LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。

为什么要使用LVM?

在日常工作或者学习中,随着电脑的不断被使用,我们的磁盘空间可能会越来越小,这时候,我们不能直接更换大硬盘,因为硬盘有价,数据无价,这时候我们就可以使用LVM,LVM将存储虚拟化,使用逻辑卷,你不会受限于物理磁盘的大小,另外,和硬件相关的存储设置被其隐藏,你能不用停止应用或卸载文件系统来调整卷大小或数据迁移.这样能减少操作成本,LVM最大的特点就是可以对磁盘进行动态管理。因为逻辑卷的大小是可以动态调整的,而且不会丢失现有的数据。如果我们新增加了硬盘,其也不会改变现有上层的逻辑卷。作为一个动态磁盘管理机制,逻辑卷技术大大提高了磁盘管理的灵活性。

LVM工作机制?

LVM就是通过将底层的物理硬盘抽象的封装起来,然后以逻辑卷的方式呈现给上层应用。在传统的磁盘管理机制中,我们的上层应用是直接访问文件系统,从而对底层的物理硬盘进行读取,而在LVM中,其通过对底层的硬盘进行封装,当我们对底层的物理硬盘进行操作时,其不再是针对于分区进行操作,而是通过一个叫做逻辑卷的东西来对其进行底层的磁盘管理操作。

逻辑卷管理概念?

物理卷(PV, Physical Volume)
物理卷就是指磁盘,磁盘分区或从逻辑上和磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有和LVM相关的管理参数。当前LVM允许你在每个物理卷上保存这个物理卷的0至2份元数据拷贝.默认为1,保存在设备的开始处.为2时,在设备结束处保存第二份备份.

卷组(VG, Volume Group)
LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。能在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。

逻辑卷(LV, Logical Volume)
LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上能建立文件系统(比如/home或/usr等)。

LVM原理图

2. LVM操作案例分析

 添加磁盘并创建一个分区


[root@localhost ~]# fdisk -l        //查看磁盘情况
[root@localhost ~]# fdisk /dev/sdb        //进入磁盘(可以依次按键n-->e-->回车-->回车-->w,即用此磁盘空闲空间创建分区)
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 0xeed8cf69.

Command (m for help): p         //查看分区列表

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: 0xeed8cf69

   Device Boot      Start         End      Blocks   Id  System

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): +20G
Partition number (1-4, default 1):    
First sector (2048-104857599, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): +20G
Partition 1 of type Linux and of size 20 GiB is set

Command (m for help): t            //更改分区类型
Selected partition 1
Hex code (type L to list all codes): l            //分区类型列表  
Hex code (type L to list all codes): 8e        //将新添加的分区标注成LVM,只要是‘8e’(LVM)类型的磁盘分区都可以用来扩展卷组。
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p    //查看分区表

Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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: 0xeed8cf69

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41945087    20971520   8e  Linux LVM

Command (m for help): w        //保存
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

创建逻辑卷lv01并挂载到/lvm01目录上。

[root@localhost ~]# pvcreate /dev/sdb1      //创建物理卷
  Physical volume "/dev/sdb1" successfully created
[root@localhost ~]# mkfs.ext4 /dev/sdb1        //格式化新添加的分区
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@localhost ~]# pvcreate /dev/sdb1        //创建pv
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@localhost ~]# pvdisplay /dev/sdb1        //查看pv信息
  "/dev/sdb1" is a new physical volume of "20.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               20.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               cL1S1N-nUGj-DuYa-9RP0-ened-A4ZZ-FTlOWX

[root@localhost ~]# vgcreate vg01 /dev/sdb1
  Volume group "vg01" successfully created

[root@localhost ~]# vgdisplay vg01
  --- Volume group ---
  VG Name               vg01
  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               lghbtI-4usq-GFEj-Zr26-7NrP-UUE3-0RHMF5


[root@localhost ~]# lvcreate -L 10M -n lv01 vg01    //从卷组vg01中创建一个大小为10M名字为lv01的逻辑卷, 如果要使用卷组剩下的所有空间创建逻辑卷 lvcreate -n lv-name -l 100%FREE vg-name
  Rounding up size to full physical extent 12.00 MiB
  Logical volume "lv01" created.

[root@localhost ~]# lvdisplay vg01     //查看在vg01组创建的逻辑卷
  --- Logical volume ---
  LV Path                /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                A1CBRX-wLOF-2d7D-SPvg-sBhD-3wzO-WtbmeH
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2020-01-14 17:08:48 +0800
  LV Status              available
  # open                 0
  LV Size                12.00 MiB
  Current LE             3
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3

[root@localhost ~]# mkfs.ext4 /dev/vg01/lv01    //逻辑卷已经准备好了,我们可以格式化和挂载逻辑卷,就像其它ext2/3/4分区一样!
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
3072 inodes, 12288 blocks
614 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=12582912
2 block groups
8192 blocks per group, 8192 fragments per group
1536 inodes per group
Superblock backups stored on blocks: 
    8193

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

[root@localhost ~]# mkdir /lvm01
[root@localhost ~]# mount /dev/vg01/lv01 /lvm01/
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  845M   50G   2% /
devtmpfs                 3.9G     0  3.9G   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    3.9G  8.6M  3.9G   1% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/centos-home   42G   33M   42G   1% /home
/dev/sda1                497M  123M  375M  25% /boot
tmpfs                    781M     0  781M   0% /run/user/0
/dev/mapper/vg01-lv01     11M  204K  9.6M   3% /lvm01

扩展LVM空间

逻辑卷lv01只有16MB的空间,我们需要对其进行扩展空间,扩展空间并不会影响到之前存储的数据。

实际情景1:卷组vg01有空闲的空间,直接进行扩展就可以。

[root@localhost ~]# lvextend -L +200M /dev/vg01/lv01     //使用lvextend命令直接对逻辑卷进行扩展200M
  Size of logical volume vg01/lv01 changed from 52.00 MiB (13 extents) to 252.00 MiB (63 extents).
  Logical volume lv01 successfully resized.                                                 
[root@localhost ~]# df -Th    //发现分区大小并没有改变
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        50G  845M   50G   2% /
devtmpfs                devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs                   tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs                   tmpfs     3.9G  8.6M  3.9G   1% /run
tmpfs                   tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/centos-home xfs        42G   33M   42G   1% /home
/dev/sda1               xfs       497M  123M  375M  25% /boot
tmpfs                   tmpfs     781M     0  781M   0% /run/user/0
/dev/mapper/vg01-lv01   ext4       50M  396K   46M   1% /lvm01
[root@localhost ~]# resize2fs /dev/vg01/lv01     //激活
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg01/lv01 is mounted on /lvm01; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/vg01/lv01 is now 258048 blocks long.

[root@localhost ~]# df -Th            //可以看到/lvm01正常了
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        50G  845M   50G   2% /
devtmpfs                devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs                   tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs                   tmpfs     3.9G  8.6M  3.9G   1% /run
tmpfs                   tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/centos-home xfs        42G   33M   42G   1% /home
/dev/sda1               xfs       497M  123M  375M  25% /boot
tmpfs                   tmpfs     781M     0  781M   0% /run/user/0
/dev/mapper/vg01-lv01   ext4      245M  768K  233M   1% /lvm01

实际情景2:卷组vg01空间不足,先扩展vg,扩展vg就是往vg中加pv

使用fdisk命令创建一个分区

[root@localhost ~]# fdisk /dev/sdc
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 

Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# 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.


Command (m for help): p

Disk /dev/sdc: 85.9 GB, 85899345920 bytes, 167772160 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: 0xeeb944e3

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048   167772159    83885056   8e  Linux LVM

添加物理卷并添加到卷组vg01中,然后再进行逻辑卷扩容操作

[root@localhost ~]# pvcreate /dev/sdc1        //创建物理卷
  Physical volume "/dev/sdc1" successfully created
[root@localhost ~]# mkfs.ext4 /dev/sdc1
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
5242880 inodes, 20971264 blocks
1048563 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2168455168
640 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, 7962624, 11239424, 20480000

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

[root@localhost ~]# mkfs.ext4 /dev/sdc1
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
5242880 inodes, 20971264 blocks
1048563 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2168455168
640 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, 7962624, 11239424, 20480000

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

[root@localhost ~]# vgextend vg01 /dev/sdc1     //将物理卷/dev/sdc1添加到vg01卷组里面
WARNING: ext4 signature detected on /dev/sdc1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sdc1.
  Physical volume "/dev/sdc1" successfully created
  Volume group "vg01" successfully extended
[root@localhost ~]# lvextend -L +15G /dev/vg01/lv02    //扩容
  Size of logical volume vg01/lv02 changed from 19.75 GiB (5056 extents) to 34.75 GiB (8896 extents).
  Logical volume lv02 successfully resized.
[root@localhost ~]# resize2fs /dev/vg01/lv02
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg01/lv02 is mounted on /lvm02; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 5
The filesystem on /dev/vg01/lv02 is now 9109504 blocks long.

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  845M   50G   2% /
devtmpfs                 3.9G     0  3.9G   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    3.9G  8.6M  3.9G   1% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/mapper/centos-home   42G   33M   42G   1% /home
/dev/sda1                497M  123M  375M  25% /boot
tmpfs                    781M     0  781M   0% /run/user/0
/dev/mapper/vg01-lv01    245M  768K  233M   1% /lvm01
/dev/mapper/vg01-lv02     35G   48M   33G   1% /lvm0

缩小逻辑卷

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

第一步:卸载文件系统

[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 3.9G 276K 3.9G 1% /dev/shm
tmpfs tmpfs 3.9G 12M 3.8G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 19G 2.8G 16G 15% /
/dev/sda1 xfs 497M 168M 329M 34% /boot
tmpfs tmpfs 781M 0 781M 0% /run/user/0
/dev/mapper/vg1-lv1 ext4 93M 1.6M 85M 2% /data
[root@localhost ~]# umount /data

第二步:检查文件系统的完整性。

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

第三步:把逻辑卷缩小到50M

[root@localhost ~]# resize2fs /dev/vg1/lv1 50M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 51200 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 51200 blocks long.

[root@localhost ~]# lvreduce -L 50M /dev/vg1/lv1 
Rounding size to boundary between physical extents: 52.00 MiB.
WARNING: Reducing active logical volume to 52.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lv1? [y/n]: y
Size of logical volume vg1/lv1 changed from 100.00 MiB (25 extents) to 52.00 MiB (13 extents).
Logical volume vg1/lv1 successfully resized.

第四步:重新挂载逻辑卷

[root@localhost ~]# mount /dev/vg1/lv1 /data
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 3.9G 300K 3.9G 1% /dev/shm
tmpfs tmpfs 3.9G 12M 3.8G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 19G 2.8G 16G 15% /
/dev/sda1 xfs 497M 168M 329M 34% /boot
tmpfs tmpfs 781M 0 781M 0% /run/user/0
/dev/mapper/vg1-lv1 ext4 45M 1.1M 40M 3% /data

第五步:设置开启自动挂载

[root@localhost ~]# blkid
/dev/sda1: UUID="d9692c2f-a923-4ad0-bec4-6dd857bd3605" TYPE="xfs" 
/dev/sda2: UUID="7ohdUl-beCh-ld4I-5aET-kpfI-36DU-H0Ocfv" TYPE="LVM2_member" 
/dev/sdb5: UUID="MrqzV2-0RWe-obBo-90XK-BuGX-p9og-CjNUPJ" TYPE="LVM2_member" 
/dev/mapper/centos-root: UUID="f8cc13e4-cab2-45a4-8405-a32f8d0954d0" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="bbbe0095-76b6-4c3c-9f91-7592b3437b5a" TYPE="swap" 
/dev/mapper/vg1-lv1: UUID="37ccb234-3952-4935-94ca-9468c10ade26" TYPE="ext4" 
/dev/sdb1: PTTYPE="dos" 
[root@localhost ~]# echo "UUID=37ccb234-3952-4935-94ca-9468c10ade26 /data ext4 defaults 0 0" >> /etc/fstab
lvm