Automatically create linux logical volume and expand capacity

【Summary】

For every operation and maintenance personnel, creating linux logical volumes is the most basic knowledge. In recent projects, it is necessary to automatically create logical volumes and realize boot mounting. Therefore, this article will explain how to create logic, and attach automatic creation Script for logical volumes.

 

  • Knowledge about LVM

The full name of LVM is Logical Volume Manager. LVM is a mechanism for managing disk partitions in the Linux environment. It is a logical layer built on the hard disk and partitions and under the file system. The biggest feature of LVM is that it can dynamically manage disks. Because the size of the logical volume can be adjusted dynamically without losing existing data. For example: our daily office computer, the file system is built on the disk, and the size of the disk limits how many files we can store. If we create a logical volume, the storage size is limited by the lv logical volume, and the logical volume can be dynamically expanded. , and the existing data will not be lost. Even if we add a new hard disk, the existing upper logical volume will not be changed, which greatly improves the flexibility of the disk.

Three concepts that need to be understood: physical volume (pv), volume group (vg), logical volume (lv)

PV: Physical volumes are created by partitions. It can be understood that there are as many physical volumes as there are partitions.

VG: Multiple physical volumes form a volume group, which can be dynamically expanded

LV: Logical volumes are divided from volume groups and can be dynamically expanded

This article explains in detail how to create logical volumes and the expansion of volume groups and logical volumes according to the following process: create physical partitions --> create physical volumes --> create volume groups --> create logical volumes --> volume group expansion --> logical volumes expansion

Experimental environment: centos7.5, file system xfs

 

  • create physical partition

First, check the partition status of the current system through the lsblk command or fdisk -l:

It can be seen from the picture that there is no partitioned disk for sdb at present, and then use the following command:

fdisk /dev/sdb partitions the disk. This example creates two partitions, /dev/sdb1 and /dev/sdb2.

 

  • create physical volume

In the second step, two partitions sdb1 and sdb2 have been created, and then the partitions are created as physical volumes

pvcreate partition name   #create physical volume

pvdisplay    #View created physical volumes

pvremove physical volume name   #delete physical volume eg: pvremove /dev/sdb1

 

  • create volume group

In the third step, two physical volumes /dev/sdb1 and /dev/sdb2 have been created, and then the physical volumes are created as a volume group

vgcreate volume group name multiple physical volume names  #create volume group

vgdisplay   #View created volume groups

vgremove volume group name  #delete volume group

 

  • create logical volume

Divide logical volumes in the volume group, assuming the names of the logical volumes to be created are lv01 and lv02

l vcreate -L size -n logical volume name volume group name   #Create logic of specified size

lvcreate -l 100% free -n logical volume name volume group name    #Create a logical volume and allocate all the remaining space of the volume group to the logical volume

lvdisplay    #View the created logical volume

lvremove /dev/ volume group name/logical volume name  #delete logical volume

 

  • Format file system and mount

mkfs.xfs /dev/ Volume Group Name/Logical Volume Name   #Format

mount device name mount point   #mount device

In addition, you can also write the mount operation into the configuration file /etc/fstab to achieve boot mount

Use df -h to check whether the logical volume hangs up successfully

 

  • automation script

 The above process is realized through the script as follows:

  1. pt_info= "/data/lv01##lv01##10g /data/lv02##lv02##5g"   #Format : mount point ## logical volume name # size, multiple logical volume information, separated by spaces  
  2. vg_name=data_vg #volume group name  
  3.   
  4. #Determine whether the mount point has been mounted by other devices  
  5. function is_exist_mount_point(){  
  6.     status=0  
  7.     mount_point=$(df -h|grep "/dev/"|awk '{print $NF}')  
  8.     cus_dir=$1  
  9.     for j in ${mount_point};do  
  10.         if [ "$cus_dir" = "$j" ];then  
  11.             status=1  
  12.             break  
  13.         fi  
  14.     done  
  15.     return $status  
  16. }  
  17.   
  18.   
  19. disk_info=$(lsblk|tail -2|head -1)  
  20. disk_type=$(echo $disk_info|awk '{print $NF}')  
  21. if [ "$disk_type" = "disk" ];then  
  22.     disk_name="/dev/$(echo $disk_info|awk '{print $1}')"  
  23. else  
  24.     echo  " There is no new disk "  
  25. fi  
  26.   
  27. pv_name="${disk_name}1"  
  28.   
  29. echo "n  
  30. p  
  31.   
  32.   
  33.   
  34. t  
  35. 8th  
  36. w  
  37. "|fdisk ${disk_name}  
  38.   
  39.   
  40. pvcreate ${pv_name}  
  41. vgcreate ${vg_name} ${pv_name}  
  42.   
  43.   
  44. for i in ${pt_info};do  
  45.     dir=$(echo $i|awk -F'##' '{print $1}')  
  46.     lv_name=$(echo $i|awk -F'##' '{print $2}')  
  47.     size=$(echo $i|awk -F'##' '{print $3}')  
  48.     is_exist_mount_point $dir  
  49.     #Return 0 , the mount point is available, return 1 , the mount point is not available   
  50.     if [ $? -eq 0 ];then  
  51.         lvcreate -L $size -n $lv_name $vg_name  
  52.         if [ ! -d $dir ];then  
  53.             mkdir -p $dir  
  54.         fi  
  55.         mkfs.xfs /dev/$vg_name/$lv_name  
  56.         mount /dev/$vg_name/$lv_name $dir  
  57.         echo "/dev/$vg_name/$lv_name  $dir  $fs   defaults   0 0">> /etc/fstab  
  58.     elif [ $? -eq 1 ];then  
  59.         continue  
  60.     fi  
  61.   
  62. done  
  • Extend volume group

In the above volume group data-vg, add an sdc with a disk space of 5G, first create a physical volume /dev/sdc1 through steps 2 and 3, and then put the physical volume into the volume group data-vg

vgextend volume group name physical volume  #extend volume group

 

  • Extend Logical Volume

Expand the original 10G lv01 to 15G

lvextend -L size /dev/volume group name/logical volume name    # Extended volume group

xfs_growfs /dev/ Volume Group Name/Logical Volume Name   # Adjust the size of the xfs file system, note: ext series and xfs file system commands are different, the test environment file system of this article is xfs

 

 

  • Summarize

The above is the whole process of creating a logical volume, as well as the expansion of the volume group and the expansion of the logical volume. After focusing on understanding the three concepts of pv, vg, and lv, the operation process is relatively simple.

 

Guess you like

Origin blog.csdn.net/qq_20663229/article/details/101612852