For a new hard disk, the method of “partition > format > mount” is convenient and fast, but it is not convenient for subsequent management. The partition or disk space that has been divided cannot be expanded when the disk space is used up. The logical volume can solve this problem.

First, clear thinking

  • First, we added a hard disk to the server with Linux system. In order to make better use of this hard disk, we decided to use logical volume to manage the disk.

  • Volume groups are the largest in scope, with the largest number of physical volumes, but they are created in the following order:Physical Volume (PV) > Volume Group (VG) > Logical Volume (LV) > Format > MountThe size of a logical volume is not directly defined, but the number of pes. The size of a PE can be changed. The default size is 4MB.

2. Preparation of experimental environment

  • Software: VMware 15

  • Operating system: Centos 7

  • The topology

  • Objective: Create a logical volume of 800M and expand it to 1G

3. Add hard disks and partitions

  • To add a 20 GB hard disk to the VM, click ok at the end

  • The newly added disk is detected

    /sys/class/scsi_host/ echo “–” > /sys/class/scsi_host/host0/scan echo “–” > /sys/class/scsi_host/host1/scan echo “- – – ” > /sys/class/scsi_host/host2/scan

    LSBLK fdisk -l /dev/sdb

  • New partition; Change the partition type and re-read the partition

    Fdisk /dev/sdb p # display partition table n # create partition +10G p t # change partition ID 8e wq # Save write and exit partprobe

    “”” M show help D delete partition L list partition type “””

Logical volume management

  • Creating a Logical Volume

    Fdisk -l /dev/sdb1 pvcreate /dev/sdb1 vgcreate vg_sdb1 /dev/sdb1 -s 8M The unit is 8M lvcreate -n LV_sdb1 -L 100 vg_sdb1 # Create a logical volume of 800M

  • Format the file and create a mount directory

    Ext4 /dev/vg_sdb1/lv_sdb1 / ext4 mkdir -p /vg_sdb1/lv_sdb1

    Common Linux formats: XFS, ext3, ext4, vFAT, FAT

  • Automatic mount to achieve permanent storage

    Vim /etc/fstab # Enter the configuration file and modify it

    /dev/vg_sdb1/lv_sdb1 /vg_sdb1/lv_sdb1 ext4 defaults 0 0

    esc :wq

    “”” Automatic mount can also be implemented by changing the file /dev/vg_sdb1/lv_sdb1 to UUID (blkid check UUID);

    In addition to automatic mounting, manual mounting is also available, but will not be effective after restart, for example, mount /dev/vg_sdb1/lv_sdb /vg_sdb1/lv_sdb1 “””

  • mount

    Mount a # To mount df-th

5. Extend partitions

  • Check whether the volume group has sufficient free space to expand the logical volume to 1 GB

    Vgdisplay vg_sdb1 # Extend the logical volume to 1G lvextend -l 1G /dev/vg_sdb1/lv_sdb1 # Check the logical volume size lvdisplay /dev/vg_sdb1/lv_sdb1

    -l -l +1 +1 +1 +1

  • Update the size of a logical volume

    Resize2fs /dev/vg_sdb1/lv_sdb1

    For ext3 or ext4 use resize2fs for XFS use xfs_growfs

6. Shrink the logical volume

  • The operation of shrinking is basically not done, so I won’t take screenshots here

    E2fsck -f /dev/vg_sdb1/lv_sdb1 # Resize the file resize2fs /dev/vg_sdb1/lv_sdb1 200M # Shrink logical volume lvreduce-l 200M /dev/vg_sdb1/lv_sdb1 remount mount -a # Check the mounting information df-th

Seven, other

  • The command script of this experiment has been saved to baidu web disk, you can download by yourself through the link

Link: pan.baidu.com/s/1vh0RehuM… Extraction code: DSVE