Linux LVM逻辑卷

需求:服务器新添加一个挂载盘,为防止以后存储空间不够需要扩容时格式化磁盘,故使用LVM逻辑卷管理。

LVM简介

LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现,目前最新版本为:稳定版1.0.5,开发版 1.1.0-rc2,以及LVM2开发版。Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不下某个文件时,这个文件因为受上层文件系统的限制,也不能跨越多个分区来存放,所以也不能同时放到别的磁盘上。而遇到出现某个分区空间耗尽时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。随着Linux的逻辑卷管理功能的出现,这些问题都迎刃而解,用户在无需停机的情况下可以方便地调整各个分区大小。

LVM基本术语

物理存储介质(The physical media):这里指系统的存储设备:硬盘,如:/dev/hda1、/dev/sda等等,是存储系统最低层的存储单元。

物理卷(physical volume):物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。

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

逻辑卷(logical volume):LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。

PE(physical extent):每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。

LE(logical extent):逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。

简单来说就是:

PV:是物理的磁盘分区

VG:LVM中的物理的磁盘分区,也就是PV,必须加入VG,可以将VG理解为一个仓库或者是几个大的硬盘。

LV:也就是从VG中划分的逻辑分区

 

Linux LVM逻辑卷

 

 

 

1. 创建分区【8e为LVM分区格式】

[root@cqsjxw-nydsjpt01 ~]# fdisk /dev/vdc 
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 0x72334656.

Command (m for help): p

Disk /dev/vdc: 1073.7 GB, 1073741824000 bytes, 2097152000 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: 0x72334656

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

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

Command (m for help): p

Disk /dev/vdc: 1073.7 GB, 1073741824000 bytes, 2097152000 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: 0x72334656

   Device Boot      Start         End      Blocks   Id  System
/dev/vdc1            2048  2097151999  1048574976   8e  Linux LVM

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

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

2. 创建PV

[root@cqsjxw-nydsjpt01 ~]# pvcreate /dev/vdc1
  Physical volume "/dev/vdc1" successfully created.
[root@cqsjxw-nydsjpt01 ~]# pvs
  PV         VG      Fmt  Attr PSize     PFree    
  /dev/vda3  centos7 lvm2 a--     39.80g        0 
  /dev/vdc1          lvm2 ---  <1000.00g <1000.00g
[root@cqsjxw-nydsjpt01 ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/vda3
  VG Name               centos7
  PV Size               39.80 GiB / not usable 0   
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              10189
  Free PE               0
  Allocated PE          10189
  PV UUID               VVXMV7-zVhi-NSVz-frrY-YKFg-BoJH-ir33p3
   
  "/dev/vdc1" is a new physical volume of "<1000.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdc1
  VG Name               
  PV Size               <1000.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               N6wQJb-K6Au-wto7-SR2O-jGrD-oZtQ-IcRlLI

3. 创建VG

[root@cqsjxw-nydsjpt01 ~]# vgcreate vgdata /dev/vdc1 
  Volume group "vgdata" successfully created
[root@cqsjxw-nydsjpt01 ~]# vgs
  VG      #PV #LV #SN Attr   VSize     VFree    
  centos7   1   1   0 wz--n-    39.80g        0 
  vgdata    1   0   0 wz--n- <1000.00g <1000.00g
[root@cqsjxw-nydsjpt01 ~]# vgdisplay 
  --- Volume group ---
  VG Name               vgdata
  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               <1000.00 GiB
  PE Size               4.00 MiB
  Total PE              255999
  Alloc PE / Size       0 / 0   
  Free  PE / Size       255999 / <1000.00 GiB
  VG UUID               MZjnmV-Gukd-XaH2-76jQ-wjik-LaWp-yqoHTQ
   
  --- Volume group ---
  VG Name               centos7
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  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
  VG Size               39.80 GiB
  PE Size               4.00 MiB
  Total PE              10189
  Alloc PE / Size       10189 / 39.80 GiB
  Free  PE / Size       0 / 0   
  VG UUID               QTfxI7-AXVG-dGoO-zCQ8-wsFb-QlOe-HPSWPI

4. 物理卷存储划分给新的逻辑卷

[root@cqsjxw-nydsjpt01 ~]# lvcreate -L 999.90G -n lvdata01 vgdata
  Rounding up size to full physical extent 999.90 GiB
  Logical volume "lvdata01" created.
[root@cqsjxw-nydsjpt01 ~]# lvs
  LV       VG      Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root     centos7 -wi-ao----  39.80g                                                    
  lvdata01 vgdata  -wi-a----- 999.90g                                                    
[root@cqsjxw-nydsjpt01 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vgdata/lvdata01
  LV Name                lvdata01
  VG Name                vgdata
  LV UUID                r83fGv-U3ju-Yn9k-yuXN-pEK6-ltTL-eSB2AO
  LV Write Access        read/write
  LV Creation host, time cqsjxw-nydsjpt01, 2021-07-29 11:11:09 +0800
  LV Status              available
  # open                 0
  LV Size                999.90 GiB
  Current LE             255975
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos7/root
  LV Name                root
  VG Name                centos7
  LV UUID                npjBgn-XPHe-dNS2-bJGd-kYp2-RayO-dWttAc
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-06-30 14:35:21 +0800
  LV Status              available
  # open                 1
  LV Size                39.80 GiB
  Current LE             10189
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

5. 格式化逻辑卷文件系统

[root@cqsjxw-nydsjpt01 ~]# mkfs.ext4 /dev/vgdata/lvdata01 
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
65536000 inodes, 262118400 blocks
13105920 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2409627648
8000 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, 23887872, 71663616, 78675968, 
    102400000, 214990848

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

6. 挂载逻辑卷

[root@cqsjxw-nydsjpt01 ~]# mount /dev/vgdata/lvdata01 /home
[root@cqsjxw-nydsjpt01 ~]# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/centos7-root      40G  2.4G   38G   6% /
devtmpfs                      32G     0   32G   0% /dev
tmpfs                         32G   56K   32G   1% /dev/shm
tmpfs                         32G  282M   32G   1% /run
tmpfs                         32G     0   32G   0% /sys/fs/cgroup
/dev/vda2                    197M  120M   77M  61% /boot
tmpfs                        6.3G     0  6.3G   0% /run/user/0
/dev/mapper/vgdata-lvdata01  985G   77M  934G   1% /home
[root@cqsjxw-nydsjpt01 ~]# cat /etc/fstab 
UUID=97f655c8-0ae8-489c-90be-594feaceaa27 /home  ext4    defaults        0 0

7. 扩展逻辑卷【转载】

给逻辑卷增加空间并不会影响以前空间的使用,所以无需卸载文件系统,直接通过命令lvextend –L +500M /dev/vgdata/lvdata01或者lvextend –l 2.5G /dev/vgdata/lvdata01 给lvdata01增加500M空间(lvdata01目前是15G空间)设置完成之后,记得使用resize2fs命令来同步文件系统。

[root@DB02 ~]# lvextend -L +500M /dev/vgdata/lvdata01 
  Extending logical volume lvdata01 to 15.49 GiB
  Logical volume lvdata01 successfully resized
[root@DB02 ~]# lvs
  LV       VG     Attr       LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  lvdata01 vgdata -wi-ao---- 15.49g                                             
[root@DB02 ~]# resize2fs /dev/vgdata/lvdata01 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vgdata/lvdata01 is mounted on /u01; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vgdata/lvdata01 to 4060160 (4k) blocks.
The filesystem on /dev/vgdata/lvdata01 is now 4060160 blocks long.

 

参考文章:

LVM_百度百科 (baidu.com)

Linux 挂载新添加磁盘&LVM配置_ITPUB博客

LVM简介 - 滴滴滴 - 博客园 (cnblogs.com)

 

Linux LVM逻辑卷

上一篇:Jenkins 部署报错 error fsevents@2.3.2: The platform "linux" is incompatible with this module


下一篇:linux基础之仓库、软件包管理、sed常见实战用法