Linux disk storage management LVM logical volumes

Basic concepts of LVM logical volumes
  • Logically add different hard disks or partitions to a unified volume group
  • The VG volume group is equivalent to a large logical hard disk
  • LV logical volume, equivalent to partition, takes out a certain space from the volume group

The creation and management of logical volumes and hard disk partitions are similar

  • establish
  • format
  • partition

Implementation process of logical volume

  • The same or different hard disks or partitions become physical volumes pv
    Pvcreate is equivalent to a label
  • Put the labeled physical volume into the volume group vg
    Vgcreate specifies the volume group name and determines which PVS are added to the volume group
  • Create a logical volume lv on the volume group
    Lvcreate creates one or more logical volumes

IBM minicomputers have not used partitions for a long time, and all use volume group technology

Advantages and disadvantages of logical volumes

advantage

  • No user perception
  • Logical volumes are easy to expand or shrink
  • It can automatically monitor the size of the logical volume and automatically expand the capacity when it reaches a certain value

shortcoming

  • The data security in case of hardware storage problems cannot be guaranteed

Install logical volume tool

yum -y install lvm2
Repository AppStream is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository PowerTools is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
 Last metadata expiration check: 0:06:26 Before, it was executed at 21:12:27 on Sunday, April 11, 2021.
software package lvm2-8:2.03.05-5.el8.0.1.x86_64 Installed.
Dependency resolution.
=======================================================================================================
 software package                             framework             edition                         Warehouse            size
=======================================================================================================
upgrade:
 device-mapper                      x86_64           8:1.02.171-5.el8             base           373 k
 device-mapper-event                x86_64           8:1.02.171-5.el8             base           268 k
 device-mapper-event-libs           x86_64           8:1.02.171-5.el8             base           267 k
 device-mapper-libs                 x86_64           8:1.02.171-5.el8             base           406 k
 lvm2                               x86_64           8:2.03.09-5.el8              base           1.6 M
 lvm2-libs                          x86_64           8:2.03.09-5.el8              base           1.1 M

Transaction summary
=======================================================================================================
Upgrade 6 package

Total downloads: 4.0 M
 Download package:
(1/6): device-mapper-event-libs-1.02.171-5.el8.x86_64.rpm              1.2 MB/s | 267 kB     00:00    
(2/6): device-mapper-libs-1.02.171-5.el8.x86_64.rpm                    5.3 MB/s | 406 kB     00:00    
(3/6): device-mapper-1.02.171-5.el8.x86_64.rpm                         1.2 MB/s | 373 kB     00:00    
(4/6): lvm2-2.03.09-5.el8.x86_64.rpm                                   5.8 MB/s | 1.6 MB     00:00    
(5/6): lvm2-libs-2.03.09-5.el8.x86_64.rpm                              2.4 MB/s | 1.1 MB     00:00    
(6/6): device-mapper-event-1.02.171-5.el8.x86_64.rpm                   119 kB/s | 268 kB     00:02    
-------------------------------------------------------------------------------------------------------
total                                                                   1.8 MB/s | 4.0 MB     00:02     
Run transaction check
 Transaction check succeeded.
Run transaction test
 Transaction test succeeded.
Run transaction
  In preparation  :                                                                                        1/1 
  Run script: device-mapper-libs-8:1.02.171-5.el8.x86_64                                             1/1 
  upgrade    : device-mapper-libs-8:1.02.171-5.el8.x86_64                                            1/12 
  upgrade    : device-mapper-8:1.02.171-5.el8.x86_64                                                 2/12 
  upgrade    : device-mapper-event-libs-8:1.02.171-5.el8.x86_64                                      3/12 
  upgrade    : device-mapper-event-8:1.02.171-5.el8.x86_64                                           4/12 
  Run script: device-mapper-event-8:1.02.171-5.el8.x86_64                                           4/12 
  upgrade    : lvm2-libs-8:2.03.09-5.el8.x86_64                                                      5/12 
  upgrade    : lvm2-8:2.03.09-5.el8.x86_64                                                           6/12 
  Run script: lvm2-8:2.03.09-5.el8.x86_64                                                           6/12 
  Run script: lvm2-8:2.03.05-5.el8.0.1.x86_64                                                       7/12 
  clear    : lvm2-8:2.03.05-5.el8.0.1.x86_64                                                       7/12 
  Run script: lvm2-8:2.03.05-5.el8.0.1.x86_64                                                       7/12 
  clear    : lvm2-libs-8:2.03.05-5.el8.0.1.x86_64                                                  8/12 
  Run script: device-mapper-event-8:1.02.163-5.el8.0.1.x86_64                                       9/12 
  clear    : device-mapper-event-8:1.02.163-5.el8.0.1.x86_64                                       9/12 
  clear    : device-mapper-event-libs-8:1.02.163-5.el8.0.1.x86_64                                 10/12 
  clear    : device-mapper-libs-8:1.02.163-5.el8.0.1.x86_64                                       11/12 
  clear    : device-mapper-8:1.02.163-5.el8.0.1.x86_64                                            12/12 
  Run script: device-mapper-8:1.02.163-5.el8.0.1.x86_64                                            12/12 
  verification    : device-mapper-8:1.02.171-5.el8.x86_64                                                 1/12 
  verification    : device-mapper-8:1.02.163-5.el8.0.1.x86_64                                             2/12 
  verification    : device-mapper-event-8:1.02.171-5.el8.x86_64                                           3/12 
  verification    : device-mapper-event-8:1.02.163-5.el8.0.1.x86_64                                       4/12 
  verification    : device-mapper-event-libs-8:1.02.171-5.el8.x86_64                                      5/12 
  verification    : device-mapper-event-libs-8:1.02.163-5.el8.0.1.x86_64                                  6/12 
  verification    : device-mapper-libs-8:1.02.171-5.el8.x86_64                                            7/12 
  verification    : device-mapper-libs-8:1.02.163-5.el8.0.1.x86_64                                        8/12 
  verification    : lvm2-8:2.03.09-5.el8.x86_64                                                           9/12 
  verification    : lvm2-8:2.03.05-5.el8.0.1.x86_64                                                      10/12 
  verification    : lvm2-libs-8:2.03.09-5.el8.x86_64                                                     11/12 
  verification    : lvm2-libs-8:2.03.05-5.el8.0.1.x86_64                                                 12/12 

Upgraded:
  device-mapper-8:1.02.171-5.el8.x86_64                device-mapper-event-8:1.02.171-5.el8.x86_64    
  device-mapper-event-libs-8:1.02.171-5.el8.x86_64     device-mapper-libs-8:1.02.171-5.el8.x86_64     
  lvm2-8:2.03.09-5.el8.x86_64                          lvm2-libs-8:2.03.09-5.el8.x86_64               

complete!

Prepare space

Change the ID of the partition

swap 82
Logical volume 8e
fdisk l t

The partition ID is changed, and the whole hard disk does not need to be changed

Change a physical disk or partition to a physical volume

  • pvs simple display of physical volumes
  • pvdisplay displays existing physical volumes
  • Pvcreate create physical volume
    pvcreat /dev/sd{b1,c}

Create volume group

  • vgs
  • vgcreate volume group name physical volumes
    The default pe size is 4M. You can use - s space to specify the pe size

Create one or more logical volumes

  • lvs
  • lvcreate -n volume group name - L how many PES or - L capacity (integer multiple in pe)

Using logical volumes

  • The real name is not lv's namecho
  • Devices requiring / dev/dm - * to use logical volumes
    There are three names that can access a logical volume

Create a file system on a volume group

mkfs. File system / dev / logical volume / logical volume name

Permanent mount

Mount point must exist
Modify / etc/fstab
Mount logical mount point must exist

The advantage of a logical volume is not in merging space

Test the speed of logical volumes

dd if=/dev/zero of=/date/test.ing bs=1M count=800

If you write the same content again, you will use the cache instead of writing to the hard disk
You can clean up the cache and modify kernel parameters

find /proc/ -name 'drop'

echo 3 > /proc.sys/vm/drop_cache

Do not release the cache in production, and use clean cache in testing
Cleaning up the cache is slow

The performance of logical volume is better than that of traditional partition

Logical volume expansion and reduction

The expansion of logical volume is to take the capacity from the volume group

How much capacity does lvextend -l expand to? How much capacity does pe -L expand to
lvextend -l 50free expand to 50% of the volume group
lvextend -l +50%free capacity expansion increases the volume group by 50%

  • Although the space is added, the new space has no file system

  • The newly added space needs to be the same as the source space system

  • Synchronize the newly added space file system, and users will not be affected
    resize2fs /dev/vg0 / volume group name is only for ext series file systems
    xfs_ The growfs / dev / vg0 / volume group name is for XFS file systems only

  • Add - r no matter what file system it is, synchronize immediately
    lvextend -r -L +1G /dev/vg0/log

The volume group space is full. Expand the volume group

Volume group space comes from physical volumes
Add physical volume

Back up the data before shrinking

xfs does not support volume reduction
Only ext4 supports volume reduction

  1. Cancel mount
  2. Check file system
  3. resize2fs shrink file system
  4. Shrink logical volume

tune2fs partition view file system

fsck /dev/vg0/log fix file system

If you can't fix it, you can only format it

mkfs.ext4 /dev/vg0/log

Remove physical volumes from volume groups

First remove the occupied space from the volume group
Remove the occupied pe

The number of pe before moving should be less than the number of pe remaining in the whole vg

pvmove /dev/sdc moves the pe occupied on the pv to other PVS on the same vg

Delete pv from vg after moving
vgreduce vof /dev/sdc

Then delete the pv attribute
pvremove /dev/sdc

Logical volume snapshot

  • Outstanding advantages of logical volumes for fast backup
  • Snapshot logical volume, occupying volume group space
  • The control created is not a real backup
  • Only the version before snapshot
  • If not, it will be pushed to the snapshot as soon as it is changed
  • The snapshot space can be much smaller than the original data

ext4 create snapshot command

lvcreate -s create snapshot

lvcreate -s -n mysqlshot -L 100M -p -r /dev/vg0/mysql
-p -r add read-only attribute to prevent tampering

mount /dev/vg0/mysql-snapshot /mnt/sanp

Mount and view. Although there is nothing, what you see comes from the original logical volume

df view, the snapshot volume and the original volume have the same size and occupation

ext4 restore snapshot

  • Cancel the mount first
    umount
  • Restore logical volumes with snapshots
    lvconvert --merge /dev/vg0/mysql-snapshot

After restoring, the snapshot will be deleted automatically

Create snapshot of xfs system

lvcreate -s -n log-snapshot -L 100M /dev/vg0/log

Add the - o ro read-only attribute when mounting
xfs feature: if there are two file systems using the same uuid, it is not allowed to mount directly, and nouuid needs to be added

mount -o ro,nouuid /dev/vg0/log /mnt/snap

xfs system restore snapshot

Cancel the mount first

Restore snapshot again
lvconvert --merge /dev/vg0/log-snapshot

Delete logical volume

  • Cancel the mount first
    umount

  • Delete logical volume
    lvremove

  • Delete volume group again
    vgremove

  • Deleting a physical volume
    pvremove

  • Then delete the partition or hard disk
    fdisk d

blkid view uuid

The snapshot is the same as the source volume uuid

Scripted monitoring utilization

Keywords: Linux Operation & Maintenance Big Data server

Added by harkonenn on Tue, 08 Mar 2022 09:26:47 +0200