LVM Disk Management

Keywords: Linux Windows Operation & Maintenance

1. Introduction to LVM

LVM is an abbreviation of Logical Volume Manager (Logical Volume Management), which connects several disks or disk partitions into a single block volume group to form a storage pool.Administrators can arbitrarily create logical volumes on volume groups and further create file systems on logical volumes.Administrators can easily adjust the size of storage volume groups through LVM, and can name, manage, and assign disk storage in a group manner.

2. What is a disk?

The main storage device that stores information in the computer is the hard disk, but the hard disk can not be used directly. The hard disk must be divided into a single hard disk area, which is the disk partition.In traditional disk management, a hard disk is divided into two types of partitions: primary partition and extended partition.A primary partition is a partition that has the ability to install an operating system and boot a computer, so that the partition can be formatted directly, then the system can be installed to store files directly.

3. Disk Partitions

1. How many partitions does Linux need to create?
Installing Linux requires only two basic partitions, the root partition and the swap partition. If you have enough disk space, you can partition more space into the root partition. You can also create new directories to your desktop, such as downloaded software packages, which will not affect your speed of entering the Linux system. Of course, this requires you to have a large root partition.

2. Why create partitions?

  • Favorable for management, the system is generally placed in a separate zone, so as the system zone only places the system, other zones will not be affected by the performance of disk fragmentation on the system disk.

  • Due to technical constraints (e.g. old versions of the micro-FAT file system cannot access more than a certain amount of disk space;Old PC BIOS does not allow operating system boot from more than 1024 cylinders on the hard disk)

  • If a partition is logically corrupted, only the corrupted partition, not the entire hard disk, is affected.

  • Exchanging files on some operating systems, such as Linux, is usually a partition of its own.In this case, a dual-boot configuration allows several operating systems to use the same swap partition to save disk space.

  • Avoid overfilling your computer with logs or other files that cause the entire computer to fail, and place them in separate partitions where only that partition may run out of space.

  • Often, two operating systems cannot exist on the same partition or use different "local" disk formats.Separate disks into logical disks for different operating systems.

  • Many file systems write files to disk using fixed-size clusters that are directly proportional to the size of their partitioned file system.If a file size is not an integer multiple of the cluster size, the last one in the cluster will have free space that cannot be used by other files.In this way, using clustered file systems causes files to take up more space on disk than they do in memory, and larger partitions mean larger cluster sizes and greater waste of space.Therefore, using a few smaller partitions instead of a larger one can save space.

  • Each partition can be customized to different needs.For example, if a partition rarely writes data to it, it can be loaded as read-only.If you want many small files, you need to use file system partitions with many nodes.

  • On a multiuser system running Unix, there may be a need to guard against user hard-link attacks.To do this, /home and/tmp paths must be separated from system files such as/var and/etc.

Disk - --- Base Unit Sector - --- 512 data block = 4KB
Disk type: ide had sata scis nvme

4. Extend partitions

For example, p4 in the figure is an extended partition, which can be further divided into logical partitions that can be formatted.Extended partitions store partition information for logical partitions in a location within the extended partition.If the p4 extended partition is divided into three logical partitions, the first logical partition is named sda5, the second sda6, the third sda7, and so on.
Above, a disk is divided into four partitions, the fourth partition is set as an extended partition, and the first logical partition is sda5.However, when the entire disk is divided into three partitions, the first logical partition of the extended partition partition partition is still named sda5, not something else.To summarize, no matter how many primary partitions the disk has, the first logical partition must start with sda5.

5. Logical Volume Management Concepts

Physical Volume
Physical volumes are disks, partitions, or devices (such as RAIDs) that logically share the same functionality as disk partitions. They are the basic storage logic blocks of LVM, but they contain management parameters related to LVM when compared with basic physical storage media (such as partitions, disks, etc.).The current LVM allows you to save between 0 and 2 copies of metadata for each physical volume. The default is 1, which is saved at the beginning of the device. At 2, the second backup is saved at the end of the device.

Volume Group (VG, Volume Group)
LVM volume groups are similar to physical hard disks in non-LVM systems and consist of physical volumes.You can create one or more LVM partitions (logical volumes) on a volume group that consists of one or more physical volumes.

Logical Volume
LVM's logical volumes are similar to hard disk partitions in non-LVM systems, on which file systems (such as/home or/usr) can be built.

6. Partitions of disks:

Note: The sum of the number of disk partitions, base partitions, and extended partitions cannot be greater than 4

Check to see if the system has LVM:rpm-qa | grep LVM installed

If the command does not output, the LVM management tool is not installed, then you need to download the LVM rpm toolkit from the network or from a CD-ROM.

When we partition a disk, first check the current partition status
View command: fdisk-l

The start and end represent the sector location where the partition begins and ends.
blocks: Represents information about a block that has multiple sectors in it.
Id: Indicates the disk number

View device block: lsblk

Create a 3G Extended Partition

Formatting: mkfs.xfs/dev/sda1
Mount: mount/sda1/mnt
View file system usage: df -h

1:Start creating the physical volume PV first:
Ready for use: two disks
[root@bogon ~]# pvcreate /dev/sda /dev/sdb


View the new PV using pvs

2:Create Volume Group vg

Commands for vg:

Vgcreate [-s xM] vgName/dev/sd...Create a new vg, -s followed by the size of pe, in M, G, you can put more than one pv
Commands for vg:

        vgextend extend vg,Is to add pv

        vgs/vgscan View what's in the system vg Disk of

        vgdisplay Show the top of the system vg state

        vgremove delete vg

        vgreduce stay vg Delete in pv

Create a vgqjc and view:


3. Create Logical Volume lv

Commands for lv:

        lvcreate -l pe num /-L size -n lvname vgName  Newly build lv,-l Appoint pe Number of,-L Specify capacity, M,G

        lvextend Increase capacity

        lvs/lvscan View what's in the system lv Disk of

        lvdisplay Show the top of the system lv state

        lvremove delete lv

        lvreduce stay lv Limit capacity

Create lvqjc and view:

Format speech LV
mkfs.ext4 /dev/vgqjc/lvqjc

Mount Use

4. Online Extension of LVM

Looking at the actual disk capacity, it is found that it has not changed and the file system needs to be expanded


Resize2fs/dev/vgtest/lvtest Update the size of the file system, that is, activate

Resize2fs-f/dev/vgtest/lvtest 500M Force size

Dump2fs/dev/vgtest/lvtest View ext family file system

3:Reduction of LVM:

1:umount file system

2: Reduce file system

[root@localhost ~]# lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvqjc  vgqjc  -wi-a----- 8.00g                                                    
  lvtest vgtest -wi-a----- 1.00g   
[root@localhost ~]# 
[root@localhost ~]#                                                
[root@localhost ~]# resize2fs /dev/vgqjc/lvqjc 5G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vgqjc/lvqjc to 1310720 (4k) blocks.
The filesystem on /dev/vgqjc/lvqjc is now 1310720 blocks long.
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# lvreduce -L 5G /dev/vgqjc/lvqjc 
  WARNING: Reducing active logical volume to 5.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vgqjc/lvqjc? [y/n]: y
  Size of logical volume vgqjc/lvqjc changed from 8.00 GiB (512 extents) to 5.00 GiB (320 extents).
  Logical volume vgqjc/lvqjc successfully resized.
[root@localhost ~]#
[root@localhost ~]# lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvqjc  vgqjc  -wi-a----- 5.00g                                                    
  lvtest vgtest -wi-a----- 1.00g                                                    
[root@localhost ~]# ls #Check for missing files

Mount View

[root@localhost ~]# mount /dev/vgqjc/lvqjc /mnt
[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/sda5               xfs        20G  3.9G   16G  20% /
devtmpfs                devtmpfs  472M     0  472M   0% /dev
tmpfs                   tmpfs     488M     0  488M   0% /dev/shm
tmpfs                   tmpfs     488M  8.7M  479M   2% /run
tmpfs                   tmpfs     488M     0  488M   0% /sys/fs/cgroup
/dev/sda2               xfs        10G  114M  9.9G   2% /app
/dev/sda1               xfs       197M  133M   65M  68% /boot
tmpfs                   tmpfs      98M  4.0K   98M   1% /run/user/42
tmpfs                   tmpfs      98M   32K   98M   1% /run/user/1000
/dev/sr0                iso9660   8.8G  8.8G     0 100% /run/media/qijunchao/CentOS 7 x86_64
/dev/mapper/vgqjc-lvqjc ext4      4.8G   32M  4.5G   1% /mnt

  

View the test file:

Delete LVM

If you want to remove the LVM completely, you need to reverse the steps you created.

1: Uninstall the file system

2: Delete lv

3:Delete vg

4:Delete pv

Delete succeeded

Posted by toplay on Mon, 06 Sep 2021 18:08:58 -0700