LVM Logical Volume Management

Keywords: Linux RHEL Fragment Anaconda

From Wechat Public Number Let the Code Fly

Rhel Topic 2: LVM Logical Volume Management - Disk Management (2)

2017-05-07 Let the Code Fly

1. Add 20G hard disk and create three new main partitions, size 5G, 5G and 10G respectively.
If you are not familiar with partitioning, you can take a look at basic storage management.
Change the system id of the partition
Because we have just finished partitioning, we are still in the disk management interface, t key to modify the partition format.
After entering, enter the partition id that needs to be modified and enter it, then enter the format 8e (Linux LVM format) that we need to change to make the change.
If you forget the code 8e, you can use the l parameter in the fdisk view to see the corresponding code in a known format.

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

You can see that the format was changed successfully! Modify other partitions in the same way. Check it out after modifying p.

Device Boot Start End Blocks Id System
sdb1 1 654 5253223+ 8e Linux LVM
sdb2 655 1308 5253255 8e Linux LVM
sdb3 1309 2610 10458315 8e Linux LVM
You can see that the partition format has been changed to Linux LVM format! Then w saves and exits!

3. Creating Physics Volume
Command: pvcreate/dev/sdb{1,2,3}

[root@localhost dev]# pvcreate sdb{1,2,3}
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
  Physical volume "/dev/sdb3" successfully created

You can see that the physical volume was created successfully! Let's take a look at it with pvscan (or short spelling pvs)

[root@localhost dev]# pvs
  PV         VG   Fmt  Attr PSize PFree
  /dev/sdb1       lvm2 a--  5.01g 5.01g
  /dev/sdb2       lvm2 a--  5.01g 5.01g
  /dev/sdb3       lvm2 a--  9.97g 9.97g

This is the details of the physical volume we created!
IV. Creating Volume Groups
We can add physical volumes sdb1 and sdb2 to a volume group
Command: vgcreate myvg/dev/sdb{1,2}

[root@localhost dev]# vgcreate myvg /dev/sdb{1,2}
  Volume group "myvg" successfully created

You can see that the vg group "myvg" was created successfully. Check it out with vgscan (or short spelling vgs)

[root@localhost dev]# vgs
  VG   #PV #LV #SN Attr    VSize  VFree 
  myvg   2        0     0 wz--n- 10.02g 10.02g

We can see that there are two PVs in our myvg volume group, that is, two physical volumes, sdb1 and sdb2, and the space is the sum of the space of the two physical volumes.
V. Creating Logical Volumes
We divide a logical volume mylv from the myvg volume group:
Command: lvcreate-n mylv-L+5G myvg

[root@localhost dev]# lvcreate -n mylv -L +5G myvg

- n refers to the name of the new lvm logical volume - L to be capitalized to specify the logical volume size followed by the vg group name
Logical volume "mylv" created
lvm logical volume creation success! Check it out with lvscan (or lvs)

[root@localhost dev]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  mylv myvg -wi-a----- 5.00g   

You can see that LVM logical volume named mylv belongs to myvg volume group, size 5G!
Formatting Logic Volume
Command: mkfs-t ext4/dev/myvg/mylv

[root@localhost dev]# mkfs -t ext4 /dev/myvg/mylv
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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

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

This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Successful formatting!
7. Auto-mounting for mounting settings
Create a new folder of lv1 under the root directory as the mount directory, and then mount it.
Command: mount/dev/myvg/mylv/lv1

[root@localhost dev]# cd /
[root@localhost /]# mkdir lv1
[root@localhost /]# mount /dev/myvg/mylv /lv1
[root@localhost /]# cd lv1
[root@localhost lv1]# ls
lost+found

You can see that our lvm logical volume has been mounted successfully. Here is a new file to test whether it can be used.

[root@localhost lv1]# cat >test.txt<<eof
> lvm
> xiexie
> eof
[root@localhost lv1]# cat test.txt 
lvm
xiexie

Can use! Next step is to set up automatic mounting

[root@localhost lv1]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu May  4 03:33:31 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9eb6c3c2-ea46-4453-9454-e7e95a99640c /                       ext4    defaults        1 1
UUID=ebe3134e-f959-4468-9c6e-19c850dec400 /boot                   ext4    defaults        1 2
UUID=c9e0a4d4-1288-4809-8868-7c0901ff6fae swap                    swap    defaults        0 0
tmpfs                    /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                     /sys                          sysfs   defaults        0 0
proc                      /proc                        proc    defaults        0 0
/dev/myvg/mylv    /lv1                           ext4    defaults        0 0
//Copy and paste the last line and edit it. Change the mount point, mount directory, and format, respectively. Save and exit! Restart for testing!
[root@localhost ~]# cd /lv1/
[root@localhost lv1]# ls
lost+found  test.txt
[root@localhost lv1]# cat test.txt 
lvm
xiexie
[root@localhost lv1]# 

The mounting point is still there! The test was successful! Thank you!

It's not over yet. The focus just begins.
The purpose of introducing lvm is to dynamically expand and reduce the capacity of partitions, while ensuring the availability of data in partitions. Let's take a look at how lvm works.

1. Addition and removal of pv in vg

In the previous case, we added / dev/sdb1 and / dev/sdb2 PVS to vg: myvg and separated a 5G lv: mylv from myvg.
On this basis, we can add pv to myvg or remove pv from myvg.
Let's first see how to add a pv to myvg:
Scanning the information of vg before adding, using the vgs command

[root@localhost dev]# vgs
  VG   #PV #LV #SN Attr    VSize  VFree 
  myvg   2        1     0 wz--n- 10.02g 5.02g

Using vgextend to extend vg: vgextend myvg/dev/sdb3

[root@localhost ~]# vgextend myvg /dev/sdb3
Volume group "myvg" successfully extended 

Scan vg again

[root@localhost dev]# vgs
  VG   #PV #LV #SN Attr    VSize  VFree 
  myvg   3       1     0 wz--n- 19.99g 14.99g

You can see that the vg extension was successful.
Remove pv from vg and use the vgreduce command

[root@localhost ~]# vgreduce myvg /dev/sdb3
Removed "/dev/sdb3" from volume group "myvg" 

[root@localhost dev]# vgs
  VG   #PV #LV #SN Attr    VSize  VFree 
  myvg   2        1     0 wz--n- 10.02g 5.02g
vg Neither expansion nor removal will affect it. lv Contents

[root@localhost /]# cat /lv1/test.txt 
lvm
xiexie

Great. Encourage me. Come on.

II. Expansion of lv

The expansion of LV needs to be modified in two places, one is the size of LV itself, the other is the size of mounted directory. Now let's expand lv's own space, first look at lv's own space.

[root@localhost ~]# lvs
  LV       VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  mylv myvg -wi-ao---- 5.00g

Now we can see that the size of lv is 5 G. Now we will expand it.
Command: lvextend/dev/myvg/mylv-L+1G

[root@localhost ~]# lvextend /dev/myvg/mylv -L +1G
  Extending logical volume mylv to 6.00 GiB
  Logical volume mylv successfully resized
[root@localhost ~]# lvs
  LV       VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  mylv myvg -wi-ao---- 6.00g  

You can see that the size of LV has changed to 6G. Here I want to explain that the "+" in front of the number is very important when expanding the size of lv. There is a big difference between adding "+" and not adding "+". Belt "+" indicates how much space is added on the original basis, without "+" means to change from the original basis to the size you specify, so if you don't add "+", you must specify a larger size than the original basis, otherwise the command will not succeed. Here is the following. Let's show you how to write commands without adding "+"!
Command: lvextend/dev/myvg/mylv-L 8G

[root@localhost ~]# lvextend /dev/myvg/mylv -L 8G
  Extending logical volume mylv to 8.00 GiB
  Logical volume mylv successfully resized
[root@localhost ~]# lvs
  LV       VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  mylv myvg -wi-ao---- 8.00g  

You can see that the size of lv has changed from 6G to 8G instead of increasing 8G, so students must learn to use it when practicing! Let's see if the size of the mount directory has changed.
Command: df-h

[root@localhost ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda2               18G  2.5G   15G  15% /
tmpfs                       931M   76K  931M   1% /dev/shm
/dev/sda1               291M   39M  238M  14% /boot
/dev/sr0                   3.6G  3.6G     0 100% /media/RHEL_6.5 x86_64 Disc 1
/dev/mapper/myvg-mylv  5.0G  138M  4.6G   3% /lv1
 You can see that the size of the mount directory hasn't changed, so we need to change the size of the file system of lv.
Command: resize 2fs-f/dev/myvg/mylv
[root@localhost ~]# resize2fs -f /dev/myvg/mylv 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mylv is mounted on /lv1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/myvg/mylv to 2097152 (4k) blocks.
The filesystem on /dev/myvg/mylv is now 2097152 blocks long.
View again with df-h
[root@localhost ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda2               18G  2.5G   15G  15% /
tmpfs                  931M   76K  931M   1% /dev/shm
/dev/sda1              291M   39M  238M  14% /boot
/dev/sr0               3.6G  3.6G     0 100% /media/RHEL_6.5 x86_64 Disc 1
/dev/mapper/myvg-mylv  7.9G  140M  7.4G   2% /lv1
 You can see that the size of the lv mount directory has changed to 8G, and the expansion is successful!
After the expansion is successful, go to / lv1 and see if your stuff is still there? Is it handsome?
[root@localhost /]# cat /lv1/test.txt 
lvm
xiexie

3. Volume reduction of lv

Now let's learn about volume reduction of lv. The steps of volume reduction and volume reduction of LV are not the same.
First remove the mount directory
Command: umount/dev/myvg/mylv
Then check the disk
Command: e2fsck-f/dev/myvg/mylv

[root@localhost ~]# e2fsck -f /dev/myvg/mylv 
e2fsck 1.41.12 (17-May-2010)
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: 12/327680 files (0.0% non-contiguous), 55903/1310720 blocks(Here, if you forget the disk check command, you can reduce the capacity first!

Then reduce the capacity of the file system
Command: resize2fs/dev/myvg/mylv 5G

[root@localhost ~]# resize2fs /dev/myvg/mylv 5G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/mylv to 1310720 (4k) blocks.
The filesystem on /dev/myvg/mylv is now 1310720 blocks long.
//Then the volume reduction of lv is carried out.
//Command: lvreduce-L 5G /dev/myvg/mylv 
[root@localhost ~]# lvreduce -L 5G /dev/myvg/mylv 
  WARNING: Reducing active logical volume to 5.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv? [y/n]: y(The question here is to remind you whether to zoom in, because changing the size may cause file loss!!!
  Reducing logical volume mylv to 5.00 GiB
  Logical volume mylv successfully resized
[root@localhost ~]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  mylv myvg -wi-a----- 5.00g  

You can see that our lv becomes 5G, and then we can reload it.
Command: mount/dev/myvg/mylv/lv1
View lv mount directory size after mounting
Command: df-h

[root@localhost /]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda2               18G  2.5G   15G  15% /
tmpfs                      931M   76K  931M   1% /dev/shm
/dev/sda1              291M   39M  238M  14% /boot
/dev/sr0                  3.6G  3.6G     0 100% /media/RHEL_6.5 x86_64 Disc 1
/dev/mapper/myvg-mylv  5.0G  138M  4.6G   3% /lv1
 You can see that the mount directory has also become 5G.
Let's take another look at the files in the mount directory.
[root@localhost /]# cat /lv1/test.txt 
lvm
xiexie

You can see that the file is still there, so do you have to look at the available space in the zoom or it will damage the files in the mount directory? Be careful when you shrink! First cancel the mount directory, then check the disk, then change the size of the file, and finally go to the lv for scaling!!!

4. Removal of pv, vg and lv

When removing pv, VG and lv, install the opposite order created to delete. First delete lv, then remove vg, and finally delete pv.
1. Delete lv
If the lv has been mounted, uninstall it using the umount command first, and then execute lvremove to delete it.

[root@localhost /]# lvremove -f /dev/myvg/mylv

Do you really want to remove active logical volume "mylv"? [y/n]: y 
Logical volume "lvol0" successfully removed

2. Delete vg

[root@localhost /]# vgremove myvg

Volume group "myvg" successfully removed

3. Delete pv

[root@localhost /]# pvremove /dev/sdb3
Labels on physical volume "/dev/sdb3" successfully wiped 

Posted by public-image on Tue, 02 Jul 2019 15:53:27 -0700