You must know the Linux dynamic management storage mechanism LVM

1. What is LVM?

  LVM (Logical Volume Manager), logical volume management.It is a management mechanism for disk partitions in Linux. I believe everyone will have questions, why should there be such a management mechanism? That's because :

  Many Linux users will encounter such a dilemma when installing the operating system : how to accurately assess and allocate the capacity of each hard disk partition, if it is too large, it will waste hard disk space; if it is too small, it will face insufficient use. Once the system partition is not enough, we have to back up and delete related data, and even forced to re-plan the storage plan and reinstall the operating system to meet the needs of the application system.
  Based on this requirement, LVM provides us with a mechanism for dynamically managing storage .

Second, the principle of LVM

  LVM provides usLogical disk, So that the file system no longer cares about the concept of the underlying physical disk. To understand the principles of LVM, we must first understand several basic LVM terms .

Name Meaning
PV(Physical Volume) Physical volume, real physical hard drive or partition
VG(Volume Group) Volume group, a large logical hard disk composed of multiple physical volumes
LV(Logical Volume) Logical volume, multiple available partitions divided by volume group
PE(Physical Extend) Physical extension block, the smallest storage unit available for allocation in the physical volume, the default is 4MB
LE(Logical Extend) Logical extension block, the smallest storage unit available for allocation in a logical volume, in the same volume group, the size of LE and PE are the same, and one-to-one correspondence

  Then let's take a look at how LVM works ? Combine this picture to explain for everyone!
Insert picture description here
  LVM is to create a logical layer on top of the hard disk partition. This logical layer makes multiple hard disks or partitions look like a logical hard disk, and then divide this logical hard disk into logical volumes for use, which greatly improves the flexibility of the partition.

3. LVM management operation?

1. Create (delete, view) physical volume (PV)

  ① Create a physical volume

pvcreate command syntax : vgcreate device file name

[root@localhost ~]# yum install -y lvm2    //安装LVM2(LVM的管理工具)
[root@localhost ~]# pvcreate /dev/sdb{1,2,3,4}    //将分区建立成为物理卷
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
  Physical volume "/dev/sdb3" successfully created
  Physical volume "/dev/sdb4" successfully created

  ② Delete the physical volume

pvremove command syntax : vgcreate physical volume name

//删除物理卷时,物理卷必须不属于任何卷组
[root@localhost ~]# pvmove /dev/sdb4    //转移数据到其他PV
[root@localhost ~]# pvremove /dev/sdb4    //删除物理卷sdb4
  Labels on physical volume "/dev/sdb4" successfully wiped

  ③ View physical volume

[root@localhost ~]# pvs    //查看物理卷的概要信息
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sdb1  myvg lvm2 a--u 108.00m   8.00m
  /dev/sdb2  myvg lvm2 a--u 108.00m 108.00m
  /dev/sdb3  myvg lvm2 a--u 108.00m 108.00m
[root@localhost ~]# pvdisplay    //查看物理卷的详细信息
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               myvg
  PV Size               109.79 MiB / not usable 1.79 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              27
  Free PE               2
  Allocated PE          25
  PV UUID               PXC8sh-tCv4-KQ5S-Hwo4-FrT1-erxf-0QtmZ8
  ...    //省略/dev/sdb{2,3}的详细信息
[root@localhost ~]# pvscan    //扫描所有硬盘上的物理卷
  PV /dev/sdb1   VG myvg            lvm2 [108.00 MiB / 8.00 MiB free]
  PV /dev/sdb2   VG myvg            lvm2 [108.00 MiB / 108.00 MiB free]
  PV /dev/sdb3   VG myvg            lvm2 [108.00 MiB / 108.00 MiB free]
  Total: 3 [324.00 MiB] / in use: 3 [324.00 MiB] / in no VG: 0 [0   ]

2. Create (increase / decrease capacity, delete, view) volume group (VG)

  ① Create a volume group

vgcreate command syntax : vgcreate [-s PE size] Volume group name physical volume name
  Among them, -s is used to specify the size of PE, the unit can be MB, GB, TB, etc. The default is 4MB.

[root@localhost ~]# vgcreate myvg /dev/sdb1 /dev/sdb2    //将物理卷sdb{1,2}加入卷组myvg
  Volume group "myvg" successfully created
  

  ② Increase the volume group capacity

vgextend command syntax : vgextend volume group name physical volume name

[root@localhost ~]# vgextend myvg /dev/sdb3    //将物理卷sdb3加入卷组myvg
  Volume group "myvg" successfully extended

  ③ Reduce volume group capacity

vgreduce command syntax : vgreduce volume group name physical volume name

[root@localhost ~]# vgreduce myvg /dev/sdb3    //将物理卷sdb3从卷组myvg中移除
  Removed "/dev/sdb3" from volume group "myvg"

  ④ Delete volume group

vgremove command syntax : vgremove volume group name

//删除卷组时,卷组中必须没有任何逻辑卷
[root@localhost ~]# vgcreate myvg1 /dev/sdb3    //建立测试卷组myvg1
  Volume group "myvg1" successfully created
[root@localhost ~]# vgremove myvg1    //删除测试卷组myvg1
  Volume group "myvg1" successfully removed

  ⑤ View volume group

[root@localhost ~]# vgs    //查看卷组的概要信息
  VG   #PV #LV #SN Attr   VSize   VFree
  myvg   2   1   0 wz--n- 216.00m 64.00m
[root@localhost ~]# vgdisplay    //查看卷组的详细信息
  --- Volume group ---
  VG Name               myvg
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               216.00 MiB
  PE Size               4.00 MiB
  Total PE              54
  Alloc PE / Size       38 / 152.00 MiB
  Free  PE / Size       16 / 64.00 MiB
  VG UUID               jSVIZS-MLeY-fOox-pAJZ-hLLd-vtM3-YaK2Hu
[root@localhost ~]# vgscan    //扫描所有硬盘上的卷组
  Reading all physical volumes.  This may take a while...
  Found volume group "myvg" using metadata type lvm2

3. Create (increase / decrease capacity, delete, view) logical volume (LV)

  ① Create a logical volume

lvcreate command syntax : lvcreate [options] [-n logical volume name] volume group name
Among them, common options:
  -n logical volume name: specify the logical volume name;
  -L size: specify the logical volume size, directly specify the capacity size, the unit can It is MB, GB, TB, etc .;
  -l quantity: specify the logical volume size, determine the logical volume size by specifying the number of PE;
  -s: create a snapshot;

[root@localhost ~]# lvcreate -L 100M -n mylv myvg    //使用卷组myvg建立大小为100M的逻辑卷mylv
  Logical volume "mylv" created.                                               

  ② Increase the logical volume capacity

lvresize command syntax : lvresize [options] device file name of logical volume
Among them, commonly used options:
  -L size: adjust the size of the logical volume, directly specify the size of the capacity;
  -l quantity: adjust the size of the logical volume, determine the logical volume by specifying the number of PEs Size;
they can be set in both absolute and relative ways:
  #: means to adjust to # size or # PE size capacity;
  +/- #: means increase / decrease # size or increase / decrease # PE size capacity.

  A large enough VG can increase the LV capacity.

[root@localhost ~]# lvresize -L +50M /dev/myvg/mylv    //增加逻辑卷mylv的大小到150M
  Rounding size to boundary between physical extents: 52.00 MiB.
  Size of logical volume myvg/mylv changed from 100.00 MiB (25 extents) to 152.00 MiB (38 extents).
  Logical volume mylv successfully resized.
[root@localhost ~]# df -h /dev/myvg/mylv    //查看硬盘信息
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/myvg-mylv
                       93M  1.6M   87M   2% /mnt/mylv    //mylv分区的大小并未改变
//逻辑卷的大小虽然改变了,但是挂载磁盘大小依然是100M,如果要让分区使用这个新的逻辑卷,必须给新增的LV创建文件系统。
[root@localhost ~]# resize2fs /dev/myvg/mylv    //调整文件系统大小
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mylv is mounted on /mnt/mylv; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/myvg/mylv to 155648 (1k) blocks.
The filesystem on /dev/myvg/mylv is now 155648 blocks long.

[root@localhost ~]# df -h /dev/myvg/mylv
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/myvg-mylv
                      144M  1.6M  135M   2% /mnt/mylv

  ③ Reduce the logical volume capacity

  Reducing the LV capacity is a relatively dangerous operation, so it is generally not recommended to reduce the capacity of the logical volume because it may cause data loss. The following steps need to be used to reduce the LV capacity:

  ① Unmount the logical volume;
  ② Use the e2fsck command to forcibly detect the remaining space of the logical volume;
  ③ Use the resize2fs command to reduce the file system;
  ④ Use the lvresize command to reduce the capacity of the logical volume;
  ⑤ Mount for use.

[root@localhost ~]# umount /dev/myvg/mylv    //卸载逻辑卷mylv
umount: /dev/myvg/mylv: not mounted
[root@localhost ~]# e2fsck -f /dev/myvg/mylv    //强制检测逻辑卷剩余空间
e2fsck 1.41.12 (17-May-2010)
/dev/myvg/mylv: recovering journal
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/myvg/mylv: ***** FILE SYSTEM WAS MODIFIED *****
/dev/myvg/mylv: 11/37544 files (9.1% non-contiguous), 10390/155648 blocks
[root@localhost ~]# resize2fs /dev/myvg/mylv 100M    //减少逻辑卷文件系统大小
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/mylv to 102400 (1k) blocks.
The filesystem on /dev/myvg/mylv is now 102400 blocks long.
[root@localhost ~]# lvresize -L -50M /dev/myvg/mylv    //减少逻辑卷容量
  Rounding size to boundary between physical extents: 48.00 MiB.
  WARNING: Reducing active logical volume to 108.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myvg/mylv? [y/n]: y
  Size of logical volume myvg/mylv changed from 156.00 MiB (39 extents) to 108.00 MiB (27 extents).
  Logical volume mylv successfully resized.
[root@localhost ~]# mount /dev/myvg/mylv /mnt/mylv/    //挂载
[root@localhost ~]# df -h /dev/myvg/mylv    //查看硬盘信息
/dev/mapper/myvg-mylv
                       93M  1.6M   87M   2% /mnt/mylv

tips : The file system size and logical volume size must be consistent . in caseThe logical volume is larger than the file system, Because some areas are not formatted into a file systemWaste of space;in caseThe logical volume is smaller than the file system, ThenThere will be problems with the data

  ④ Delete logical volume

  If you delete the logical volume, all data in it will be lost. You need to delete the LV according to the following steps :

  ① Unmount the logical volume;
  ② If there is mounting information of the logical volume in / etc / fstab (you can ignore it if it has not been added before), you need to delete it first, otherwise the system may not start;
  ③ Use the lvremove command to delete the logical volume.

lvremove command syntax : lvremove logical volume name

[root@localhost ~]# umount /dev/myvg/mylv    //卸载逻辑卷mylv
[root@localhost ~]# lvremove /dev/myvg/mylv    //删除逻辑卷mylv
Do you really want to remove active logical volume mylv? [y/n]: y
  Logical volume "mylv" successfully removed

  ⑤ View logical volume

[root@localhost ~]# lvs            //查看逻辑卷的概要信息
  LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-ao---- 108.00m     
[root@localhost ~]# lvdisplay        //查看逻辑卷的详细信息
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                7xpEK7-ODNn-DnSX-Eshz-zo4I-zitd-2h1aSM
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2020-04-03 04:36:05 +0800
  LV Status              available
  # open                 1
  LV Size                108.00 MiB
  Current LE             27
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
[root@localhost ~]# lvscan    //扫描所有硬盘上的逻辑卷
  ACTIVE            '/dev/myvg/mylv' [108.00 MiB] inherit

3. How to build LVM?

  You need to follow the steps below to build an LVM:

  ① The physical hard disk is divided into partitions;
  ② The physical partition is established as a physical volume (PV);
  ③ The physical volume is integrated into a volume group (VG), the volume group can be dynamically resized, and the physical partition is added to or removed from the volume group Delete;
  ④ Divide the volume group into logical volumes (LV), and logical volumes can also be resized directly.
  ⑤ The logical volume can be imagined as a partition, so it also needs to be formatted and mounted.

1. Establish a physical partition

[root@localhost ~]# fdisk /dev/sdb    //在/dev/sdb硬盘中新建两个分区,每个分区大小为100M
Command (m for help): n    //创建分区
Command action
   e   extended
   p   primary partition (1-4)
p    //分区类型选择主分区
Partition number (1-4): 1    //设置分区号为1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +100M    //设置100M大小

...    //剩下一个分区创建过程省略

Command (m for help): t    //更改分区格式
Partition number (1-4): 1    //选择分区1
Hex code (type L to list codes): 8e    //8e为Linux LVM格式
Changed system type of partition 1 to 8e (Linux LVM)

...    //剩下一个分区格式修改过程省略

Command (m for help): p    //查询创建结果

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x2d354121

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  8e  Linux LVM
/dev/sdb2              15          28      112455   8e  Linux LVM

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partx -a /dev/sdb    //重读磁盘分区表信息

2. Create a physical volume

[root@localhost ~]# pvcreate /dev/sdb{1,2}    //将分区建立成为物理卷
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
[root@localhost ~]# pvs    //查看物理卷信息
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sdb1  myvg lvm2 a--u 108.00m 108.00m
  /dev/sdb2  myvg lvm2 a--u 108.00m 108.00m

3. Create a volume group

[root@localhost ~]# vgcreate myvg /dev/sdb{1,2}    //将物理卷sdb{1,2}加入卷组myvg
  Volume group "myvg" successfully created
[root@localhost ~]# vgs    //查看卷组信息
  VG   #PV #LV #SN Attr   VSize   VFree
  myvg   2   0   0 wz--n- 216.00m 216.00m

4. Create a logical volume

[root@localhost ~]# lvcreate -L 200M -n mylv myvg    //使用卷组myvg建立大小为200M的逻辑卷mylv
  Logical volume "mylv" created.
[root@localhost ~]# lvs    //查看逻辑卷信息
  LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-a----- 200.00m

5. Formatting and mounting of logical volumes

[root@localhost ~]# mkfs.ext4 /dev/myvg/mylv    //格式化
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729

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

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# mkdir /mnt/mylv    //创建挂载点
[root@localhost ~]# mount /dev/myvg/mylv /mnt/mylv/    //挂载
[root@localhost ~]# df -Th    //查看硬盘信息
[root@localhost ~]# df -Th
Filesystem           Type     Size  Used Avail Use% Mounted on
/dev/sda2            ext4     9.8G  814M  8.5G   9% /
tmpfs                tmpfs    491M     0  491M   0% /dev/shm
/dev/sda1            ext4     190M   31M  150M  17% /boot
/dev/sr0             iso9660  3.7G  3.7G     0 100% /media
`/dev/mapper/myvg-mylv
                     ext4     190M  1.6M  179M   1% /mnt/mylv`

Published 16 original articles · praised 363 · 30,000+ views

Guess you like

Origin blog.csdn.net/weixin_43898125/article/details/105380559