【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:
- pt_info= "/data/lv01##lv01##10g /data/lv02##lv02##5g" #Format : mount point ## logical volume name # size, multiple logical volume information, separated by spaces
- vg_name=data_vg #volume group name
- #Determine whether the mount point has been mounted by other devices
- function is_exist_mount_point(){
- status=0
- mount_point=$(df -h|grep "/dev/"|awk '{print $NF}')
- cus_dir=$1
- for j in ${mount_point};do
- if [ "$cus_dir" = "$j" ];then
- status=1
- break
- fi
- done
- return $status
- }
- disk_info=$(lsblk|tail -2|head -1)
- disk_type=$(echo $disk_info|awk '{print $NF}')
- if [ "$disk_type" = "disk" ];then
- disk_name="/dev/$(echo $disk_info|awk '{print $1}')"
- else
- echo " There is no new disk "
- fi
- pv_name="${disk_name}1"
- echo "n
- p
- t
- 8th
- w
- "|fdisk ${disk_name}
- pvcreate ${pv_name}
- vgcreate ${vg_name} ${pv_name}
- for i in ${pt_info};do
- dir=$(echo $i|awk -F'##' '{print $1}')
- lv_name=$(echo $i|awk -F'##' '{print $2}')
- size=$(echo $i|awk -F'##' '{print $3}')
- is_exist_mount_point $dir
- #Return 0 , the mount point is available, return 1 , the mount point is not available
- if [ $? -eq 0 ];then
- lvcreate -L $size -n $lv_name $vg_name
- if [ ! -d $dir ];then
- mkdir -p $dir
- fi
- mkfs.xfs /dev/$vg_name/$lv_name
- mount /dev/$vg_name/$lv_name $dir
- echo "/dev/$vg_name/$lv_name $dir $fs defaults 0 0">> /etc/fstab
- elif [ $? -eq 1 ];then
- continue
- fi
- 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.