


How to use LVM disk operation commands in Linux disk management
May 23, 2023 pm 01:58 PMLVM, Logical Volume Manger, is a logical volume management function provided by the Linux kernel. It is composed of kernel drivers and application layer tools. It creates a logical layer based on the hard disk partition, which can be very flexible and very Conveniently manage storage devices.
LVM uses the device-mapper function of the Linux kernel to implement virtualization of the storage system (the system partition is independent of the underlying hardware). Through LVM, storage space can be abstracted and virtual partitions can be created on it. Partitions can be expanded and reduced more easily. When adding or deleting partitions, there is no need to worry about not having enough contiguous space on a certain hard disk to avoid being in use. It eliminates the trouble of disk repartitioning and the inconvenience of having to move other partitions to adjust partitions. It can manage disks more flexibly than traditional partitioning systems.
1. The basic components of LVM
1. Physical volume (PV, Physical Volume)
A block device that can store LVM. Such as hard disk partition (MBR or GPT Partition), SAN hard disk, RAID or LUN, a loopback file, a kernel-mapped device (such as dm-crypt), which contains a special LVM header, which is the actual hardware or storage system built by LVM.
2. Volume Group (VG, Volume Group)
A volume group is a collection of one or more physical volumes and is displayed as /dev/VG_NAME in the device file system.
3. Logical Volume (LV, Logical Volume)
Logical volumes are the final meta-devices available for use by the system. They are created and managed in volume groups and are composed of physical blocks. In fact It is a virtual partition and appears as /dev/VG_NAME/LV_NAME, on which file systems can usually be created.
4. Physical Blocks (PE, Physical Extends)
If a logical volume needs to allocate multiple physical blocks, they will become the smallest contiguous area in a volume group (default is 4 MiB ). You can think of it as part of a physical volume that can be assigned to a logical volume.
Below I drew a picture of the location of lvm in Linux disk management:
- Treat multiple hard disks as one large hard disk
- Using logical volumes (LV), you can create partitions that span many hard disk spaces.
- You can create a small logical volume (LV) and dynamically adjust its size when there is insufficient space.
- When adjusting the size of a logical volume (LV), you don’t need to consider the location of the logical volume on the hard disk, and you don’t have to worry about no available contiguous space.
- It is feasible to create, delete, resize and other operations online for logical volumes and volume groups. For file systems on LVM, resizing is required, but some file systems (such as ext4) support online operations.
- Without restarting the service, the logical volume (LV) used in the service can be migrated online/live to another hard disk.
- Allows the creation of snapshots, which can save backups of the file system while minimizing the downtime of the service.
- Supports various device-mapper targets, including transparent file system encryption and caching of frequently used data. This will allow you to create a system with one or more disks, encrypted with LUKS, and use LVM on top to easily manage and adjust these independent encrypted volumes (e.g. /, /home, /backup, etc.) without booting the trouble of entering the key multiple times.
- Requires more complicated extra steps during system setup.
- Windows system does not support LVM. If you use dual systems, you will not be able to access the LVM partition on Windows.
- If you are using MBR, set the partition type to 8e.
- If you are using GPT, set the partition type to E6D6D379-F507-44C2-A23C-238F2A3DF928.
2. Physical volume (PV) related operations
①. Use the lvmdiskscan command to list the devices that can be used as PV
Note: If the system boot program does not support LVM, /boot
cannot be placed in LVM. At this moment, an independent /boot
partition must be created and directly formatted and mounted to /boot. The only known bootloader that supports LVM is GRUB.
As you can see from the above figure, //dev/sda2 is already a PV, so only dev/sda1, /dev/sdb1, and /dev/sdb2 can be used to create PV, and because /dev/sda1 is boot boot area, so below we can create PV
② for /dev/sdb1 and /dev/sdb2, and use the pvcreate command to create pv
root# pvcreate device1 device2 ...
③ , View all current PV information
You can view PV information through the three commands pvs, pvscan, and pvdisplay
Note: If you are using an SSD that has not been formatted and the erase block size is less than 1M, please use the following command
pvcreate --dataalignment 1m /dev/sda
to set the alignment. .
3. Volume group (VG) related operations
①. Create a volume group
Use the command vgcreate to create a volume group
root# vgcreate vg_name pv1 pv2 ...
Create the volume group vg_fedora_yg and add pv /dev/sdb1 to the volume group.
②. View volume group information
At this time, you can also view the volume group where each physical volume is located through pvs:
4. Logical volume (LV) related operations
①.Create LV
Use the lvcreate command
root# lvcreate -L <lv_size> <vg_name> -n <lv_name> # 將卷組vg_name下所有剩余空間給創(chuàng)建的lv_name邏輯卷 root# lvcreate -l +100%FREE <vg_name> -n <lv_name>
root# lvcreate -L <lv_size> <vg_name> -n <lv_name>
After the logical volume is created, you can access it through /dev/mapper/vg_fedora_yg-lv_yg01
or /dev/vg_fedora_yg/lv_yg01
:
②、View lv
Command lvs, lvscan, lvdisplay to view
root# lvextend -L <extend_size> <lv_path>
Note: If the expanded logical volume has been mounted to a specific file system, you need to execute resize2fs or xfs_growfs (for xfs files System) command to make the modification effective, You can use df -Th or blkid to check the file system type mounted by lv.
5. Format and mount LV (logical volume) After the above logical volume LV is created, it can usually be found under /dev/mapper/ or /dev/vg_name/ If the logical volume cannot be found, you can execute the following command:# modprobe dm-mod # vgscan # vgchange -ayFinally, you can see the following:
# mkfs.<filesystem_type> /dev/mapper/<vg_name>-<lv_name> # # mkfs.xfs /dev/mapper/vg_fedora_yg-lv_yg01
# mount /dev/mapper/<vg_name>-<lv_name> <mount_point>
/dev/mapper/vg_fedora_yg-lv_yg01),
Do not use the actual partition device where the logical volume is located (That is, do not use: /dev/sdb1)
illustrate:① In the picture, /dev/sda1 is the boot boot area and cannot be managed by lvm, so it is directly formatted and mounted to the directory /boot. In addition, /dev/sdb2 is also mounted to the directory without being directly formatted by lvm. directory.
②. The capacity of volume group vg_fedora1 is 139G, from which 40 5 45 = 90G is allocated, and there is still 49G free. These free capacities can be lvextended to the following lv; you can also create another lv and allocate go out.
③. Disk settings /dev/sdc still has 50G free space that is not partitioned and can be used after partitioning.
The above is the detailed content of How to use LVM disk operation commands in Linux disk management. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Linux's cost of ownership is usually lower than Windows. 1) Linux does not require license fees, saving a lot of costs, while Windows requires purchasing a license. 2) Linux has low hardware requirements and can extend the service life of the device. 3) The Linux community provides free support to reduce maintenance costs. 4) Linux is highly secure and reduces productivity losses. 5) The Linux learning curve is steep, but Windows is easier to use. The choice should be based on specific needs and budget.

LinuxoftenoutperformsWindowsinI/Operformanceduetoitscustomizablekernelandfilesystems,whileWindowsoffersmoreuniformperformanceacrosshardware.1)LinuxexcelswithcustomizableI/OschedulerslikeCFQandDeadline,enhancingperformanceinhigh-throughputapplications

The key to installing dual systems in Linux and Windows is partitioning and boot settings. 1. Preparation includes backing up data and compressing existing partitions to make space; 2. Use Ventoy or Rufus to make Linux boot USB disk, recommend Ubuntu; 3. Select "Coexist with other systems" or manually partition during installation (/at least 20GB, /home remaining space, swap optional); 4. Check the installation of third-party drivers to avoid hardware problems; 5. If you do not enter the Grub boot menu after installation, you can use boot-repair to repair the boot or adjust the BIOS startup sequence. As long as the steps are clear and the operation is done properly, the whole process is not complicated.

The key to enabling EPEL repository is to select the correct installation method according to the system version. First, confirm the system type and version, and use the command cat/etc/os-release to obtain information; second, enable EPEL through dnfinstallepel-release on CentOS/RockyLinux, and the 8 and 9 version commands are the same; third, you need to manually download the corresponding version of the .repo file and install it on RHEL; fourth, you can re-import the GPG key when encountering problems. Note that the old version may not be supported, and you can also consider enabling epel-next to obtain the test package. After completing the above steps, use dnfrepolist to verify that the EPEL repository is successfully added.

Newbie users should first clarify their usage requirements when choosing a Linux distribution. 1. Choose Ubuntu or LinuxMint for daily use; programming and development are suitable for Manjaro or Fedora; use Lubuntu and other lightweight systems for old devices; recommend CentOSStream or Debian to learn the underlying principles. 2. Stability is preferred for UbuntuLTS or Debian; you can choose Arch or Manjaro to pursue new features. 3. In terms of community support, Ubuntu and LinuxMint are rich in resources, and Arch documents are technically oriented. 4. In terms of installation difficulty, Ubuntu and LinuxMint are relatively simple, and Arch is suitable for those with basic needs. It is recommended to try it first and then decide.

Linux usually performs better in web server performance, mainly due to its advantages in kernel optimization, resource management and open source ecosystem. 1) After years of optimization of the Linux kernel, mechanisms such as epoll and kqueue make it more efficient in handling high concurrent requests. 2) Linux provides fine-grained resource management tools such as cgroups. 3) The open source community continuously optimizes Linux performance, and many high-performance web servers such as Nginx are developed on Linux. By contrast, Windows performs well when handling ASP.NET applications and provides better development tools and commercial support.

The steps to add a new hard disk to the Linux system are as follows: 1. Confirm that the hard disk is recognized and use lsblk or fdisk-l to check; 2. Use fdisk or parted partitions, such as fdisk/dev/sdb and create and save; 3. Format the partition to a file system, such as mkfs.ext4/dev/sdb1; 4. Use the mount command for temporary mounts, such as mount/dev/sdb1/mnt/data; 5. Modify /etc/fstab to achieve automatic mount on the computer, and test the mount first to ensure correctness. Be sure to confirm data security before operation to avoid hardware connection problems.

Logs in Linux systems are usually stored in the /var/log directory, which contains a variety of key log files, such as syslog or messages (record system logs), auth.log (record authentication events), kern.log (record kernel messages), dpkg.log or yum.log (record package operations), boot.log (record startup information); log content can be viewed through cat, tail-f or journalctl commands; application logs are often located in subdirectories under /var/log, such as Apache's apache2 or httpd directory, MySQL log files, etc.; at the same time, it is necessary to note that log permissions usually require s
