Advanced Disk Management-LVM Logical Volume Management

1. LVM logical volume management

1. Concept

LVM (Logical Volume Manager) is a mechanism for managing disks and partitions in the Linux environment.

Ordinary disk partition management methods cannot generally change their size after the partition is divided (risk)

LVM is to merge the object pv into one or several large vgs, and divide the vg space according to our needs. Since it is a virtual storage pool, you can freely adjust the size when dividing the space

Insert picture description here

The focus of LVM is that the capacity of the file system can be adjusted flexibly, not the efficiency and safety of data storage **

2. Components of LVM

Physical volume (PV, Physical Volume): converted from disk or partition

Volume group (VG, Volume Group): Multiple physical volumes are combined together to form a volume group. The same volume group can be composed of different partitions of the same hard disk or different partitions on different hard disks. We usually put Volume group is understood as a hard disk.

Logical Volume (LV, Logical Volume): If the volume group is understood as a hard disk, then our logical volume is a partition on the hard disk, and the logical volume can be formatted to store data.

Physical extension (PE, Physical Extend): The smallest storage unit of PE volume group. The location of PE is VG volume group, that is, on the hard disk, then we can understand PE as a sector on the hard disk, the default is 4MB, can be freely configured .

Second, create an LVM logical volume

(1) Partition the disk, or the entire disk is not partitioned
(2) Establish the partition or the entire disk on the disk into a physical volume (PV)
(3) Combine the created physical volumes into a volume group (VG), pass Add or delete a physical volume to adjust the volume group size.
(4) Finally, the volume group is divided into logical volumes (LV). The logical volume can also be adjusted at will. The logical volume is equivalent to a real partition, so it must be formatted and mounted.

Use fdisk to partition, the system ID of the partition needs to be modified to 8e00 using the t option
Insert picture description here

1. Physical volume management (pv)

1.1 Create Physical Volume-pvcreate

Format: pvcreate disk name / partition name

Partition:
Insert picture description here
entire hard drive:
Insert picture description here

1.2 View physical volume—pvscan & pvdisplay

Format:
pvs is short for
pvscan
pvdisplay pvdisplay specifies the disk name / partition name

[root@xxx ~]# :pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <19.00g     0 
  /dev/sdd1         lvm2 ---   10.00g 10.00g
  /dev/sde          lvm2 ---   20.00g 20.00g
[root@xxx ~]# :pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <19.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4863
  Free PE               0
  Allocated PE          4863
  PV UUID               CQu05U-dzdQ-QkTT-W4OR-EaIW-f7EV-cC9sJi
   
  "/dev/sdd1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdd1
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               wvT7tu-mSmu-OzjE-oNrQ-4ILN-S38J-hscKsx
   
  "/dev/sde" is a new physical volume of "20.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sde
  VG Name               
  PV Size               20.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               fF8Umg-JrkA-NAMu-QTMI-ebds-NFTD-HS1osw

1.3 Delete the physical volume-pvremove

Format:
pvremove disk name / partition name

[root@xxx ~]# :pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <19.00g     0 
  /dev/sdd1         lvm2 ---   10.00g 10.00g
  /dev/sde          lvm2 ---   20.00g 20.00g
  /dev/sdf          lvm2 ---   20.00g 20.00g
[root@xxx ~]# :pvremove /dev/sdf
  Labels on physical volume "/dev/sdf" successfully wiped.
You have new mail in /var/spool/mail/root
[root@xxx ~]# :pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <19.00g     0 
  /dev/sdd1         lvm2 ---   10.00g 10.00g
  /dev/sde          lvm2 ---   20.00g 20.00g

2. Volume group management (vg)

2.1 Create Volume Group-vgcreate

Format:
vgcreate option volume group name disk name / partition name (you can write multiple, separated by spaces)
-s size: specify the PE size of VG

[root@xxx ~]# :vgcreate -s 2M vg1 /dev/sdd1 /dev/sde
  Volume group "vg1" successfully created

2.2 View volume group—vgscan & vgdisplay

Format:
vgs is short for vgscan
vgdisplay
vgdisplay specifies the disk name / partition name

[root@xxx ~]# :vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n- <19.00g      0 
  vg1      2   0   0 wz--n- <30.00g <30.00g
[root@xxx ~]# :vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <19.00 GiB
  PE Size               4.00 MiB
  Total PE              4863
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               d4wUf7-yU7O-0hdk-ohdD-4UME-TIBw-qQMQyr
   
  --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        2
  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                2
  Act PV                2
  VG Size               <30.00 GiB
  PE Size               2.00 MiB
  Total PE              15358
  Alloc PE / Size       0 / 0   
  Free  PE / Size       15358 / <30.00 GiB
  VG UUID               1U8tJu-cHBt-B8TV-Ao2j-t29G-ULc3-DlHB04

2.3 increase volume group capacity-vgextend

Format:
vgextend volume group name disk name / partition name

[root@xxx ~]# :vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n- <19.00g      0 
  vg1      1   0   0 wz--n- <10.00g <10.00g
[root@xxx ~]# :vgextend vg1 /dev/sde
  Volume group "vg1" successfully extended
You have new mail in /var/spool/mail/root
[root@xxx ~]# :vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n- <19.00g      0 
  vg1      2   0   0 wz--n- <30.00g <30.00g

2.4 Reduce volume group capacity-vgreduce

Be careful when deleting

Format:
vgreduce volume group name physical volume name

[root@xxx ~]# :vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n- <19.00g      0 
  vg1      2   0   0 wz--n- <30.00g <30.00g
[root@xxx ~]# :vgreduce vg1 /dev/sde
  Removed "/dev/sde" from volume group "vg1"
You have new mail in /var/spool/mail/root
[root@xxx ~]# :vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n- <19.00g      0 
  vg1      1   0   0 wz--n- <10.00g <10.00g

2.5 delete volume group-vgremove

Format:
vgremove volume group name

Seeing the VG column, the volume group is deleted

[root@xxx ~]# :pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  centos lvm2 a--  <19.00g      0 
  /dev/sdd1  vg1    lvm2 ---   10.00g 10.00g
  /dev/sde   vg1    lvm2 ---   20.00g 20.00g
[root@xxx ~]# :vgremove vg1
  Volume group "vg1" successfully removed
[root@xxx ~]# :pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <19.00g     0 
  /dev/sdd1         lvm2 ---   10.00g 10.00g
  /dev/sde          lvm2 ---   20.00g 20.00g

3. Logical volume management (lv)

3.1 Create Logical Volume --lvcreate

Format:
lvcreate parameter -n logical volume name volume group name

Parameters:
-L capacity: specify the size of the created logical volume, unit: MB, GB, TB, etc.
-l (small) number: specify the size of the
logical volume according to the number of PE -n logical volume name: specify the name of the logical volume

[root@xxx ~]# :lvcreate -L 15G -n lv1 vg1
  Logical volume "lv1" created.

Note: Because the logical volume is equivalent to a partition, it needs to be formatted and mounted before it can be used, and self-starting

[root@xxx ~]# :mkfs.xfs /dev/vg1/lv1
meta-data=/dev/vg1/lv1           isize=512    agcount=4, agsize=983040 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=3932160, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
You have new mail in /var/spool/mail/root
[root@xxx ~]# :mkdir /disk/guazai5
[root@xxx ~]# :mount /dev/vg1/lv1 /disk/guazai5
[root@xxx ~]# :df -h | grep movie
You have new mail in /var/spool/mail/root
[root@xxx ~]# :df -h | grep /disk/guazai5
/dev/mapper/vg1-lv1       15G   33M   15G   1% /disk/guazai5
[root@xxx ~]# :echo "/dev/vg1/lv1 /disk/guazai5 xfs defaults 0 0" >> /etc/fstab
[root@xxx ~]# :mount -a
[root@xxx ~]# :df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  2.7G   15G  16% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sdc1                 20G  483M   20G   3% /disk/guazai4
/dev/sdb1                 10G   33M   10G   1% /disk/guazai3
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0
/dev/mapper/vg1-lv1       15G   33M   15G   1% /disk/guazai5

3.2 View Logical Volume—lvscan & lvdisplay

Format:
lvs is short for lvscan
lvdisplay

[root@xxx ~]# :lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <17.00g                                                    
  swap centos -wi-ao----   2.00g                                                    
  lv1  vg1    -wi-ao----  15.00g                                                    
You have new mail in /var/spool/mail/root
[root@xxx ~]# :lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg1/lv1
  LV Name                lv1
  VG Name                vg1
  LV UUID                Oxd3vD-Q0wD-QHb0-bf6o-3TEJ-VV08-DI01U9
  LV Write Access        read/write
  LV Creation host, time xxx, 2020-04-22 23:42:51 -0400
  LV Status              available
  # open                 1
  LV Size                15.00 GiB
  Current LE             7680
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                AmBZcm-EgKm-4Lx7-WSUf-KYvH-CtEO-zpIHjz
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-02-27 23:30:51 -0500
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                ZzVT9H-lHCv-lbjh-xsMU-uDM9-17Yp-ThoiF9
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-02-27 23:30:51 -0500
  LV Status              available
  # open                 1
  LV Size                <17.00 GiB
  Current LE             4351
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

3.3 Increase the logical volume capacity-lvextend

Note: To modify the size of lv and file system, you must first unmount lv and file system (umount)

格式:
lvextend -L +10G /dev/vg1/lv1		先增加lv的大小
xfs_growfs 	/dev/vg1/lv1	再修改文件系统的大小

3.3.1 Uninstall first, increase lv capacity

[root@xxx ~]# :umount /dev/mapper/vg1-lv1
You have new mail in /var/spool/mail/root
[root@xxx ~]# :df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  2.7G   15G  16% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sdc1                 20G  483M   20G   3% /disk/guazai4
/dev/sdb1                 10G   33M   10G   1% /disk/guazai3
/dev/sda1               1014M  133M  882M  14% /boot
tmpfs                     98M     0   98M   0% /run/user/0
[root@xxx ~]# :lvextend -L +5G /dev/vg1/lv1
  Size of logical volume vg1/lv1 changed from 15.00 GiB (7680 extents) to 20.00 GiB (10240 extents).
  Logical volume vg1/lv1 successfully resized.
You have new mail in /var/spool/mail/root
[root@xxx ~]# :mount /dev/vg1/lv1 /disk/guazai5
[root@xxx ~]# :df -h | grep /disk/guazai5
/dev/mapper/vg1-lv1       15G   33M   15G   1% /disk/guazai5

After unmounting the mount point, after we increase the size of lv, we mount and check, the actual capacity has not increased, so

3.3.2 Modify the size of the file system

[root@xxx ~]# :xfs_growfs /dev/vg1/lv1
meta-data=/dev/mapper/vg1-lv1    isize=512    agcount=4, agsize=983040 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=3932160, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 3932160 to 5242880
You have new mail in /var/spool/mail/root
[root@xxx ~]# :df -h | grep /disk/guazai5
/dev/mapper/vg1-lv1       20G   33M   20G   1% /disk/guazai5

You can see that the capacity has been updated (15G + 5G becomes 20G)

3.4 Reduce logical volume capacity—lvreduce

Note: lv modify the size and file system, you must uninstall (umount) First lv and file systems
Note: do a backup

Format:
lvreduce -L + 10G / dev / vg1 / lv1 Specifies how much the partition is reduced
mkfs.xfs -f / dev / vg1 / lv1 Then reformat, mount, view

Note: The xfs file system only supports expansion by default, and does not support shrinking, so, it cannot be truly lossless ... (forced demonstration)

3.4.1 First unmount the mount point and specify the reduced partition size

[root@xxx ~]# :df -h | grep /disk/guazai5
/dev/mapper/vg1-lv1       20G   33M   20G   1% /disk/guazai5
You have new mail in /var/spool/mail/root
[root@xxx ~]# :umount /dev/mapper/vg1-lv1
You have new mail in /var/spool/mail/root
[root@xxx ~]# :lvreduce -L 5G /dev/mapper/vg1-lv1
  WARNING: Reducing active logical volume to 5.00 GiB.
  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 20.00 GiB (10240 extents) to 5.00 GiB (2560 extents).
  Logical volume vg1/lv1 successfully resized.

3.4.2 Force reformatting, mounting, viewing

[root@xxx ~]# :mkfs.xfs -f /dev/mapper/vg1-lv1
meta-data=/dev/mapper/vg1-lv1    isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
You have new mail in /var/spool/mail/root
[root@xxx ~]# :mount /dev/vg1/lv1 /disk/guazai5
[root@xxx ~]# :df -h | grep /disk/guazai5
/dev/mapper/vg1-lv1      5.0G   33M  5.0G   1% /disk/guazai5

At this point, the size of the capacity we have checked becomes 5G.

Note: Restore the backup

Published 52 original articles · praised 5 · visits 1341

Guess you like

Origin blog.csdn.net/weixin_46669463/article/details/105704399