search
HomeOperation and MaintenanceLinux Operation and MaintenanceHow to use LVM disk operation commands in Linux disk management

LVM, 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:

How to use LVM disk operation commands in Linux disk management

## The order is: disk -> partition -> PV - > VG -> LV -> fs, that is, disk->partition->physical volume->volume group->logical volume->file system.

The creation is also in this order, which will be introduced in detail below.

2. Advantages and Disadvantages of LVM

1. Advantages

Compared with the traditional hard disk partition management method, LVM is more flexible:

  • 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.

2. Disadvantages

  • 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.

3. Use of LVM

1. Create a partition

Before configuring lvm, the storage device must be partitioned. You can use Use fdisk or parted tools. When creating a partition, pay attention to the partition type setting (type is linux lvm):

  • 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.

I added a new disk /dev/sdb to my virtual machine. Let’s create an 8G partition:

How to use LVM disk operation commands in Linux disk management

How to use LVM disk operation commands in Linux disk management

In the same way, create another 10G linux lvm type partition:

How to use LVM disk operation commands in Linux disk management

①. Use the lvmdiskscan command to list the devices that can be used as PV

How to use LVM disk operation commands in Linux disk management

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 ...

How to use LVM disk operation commands in Linux disk management

③ , View all current PV information

You can view PV information through the three commands pvs, pvscan, and pvdisplay

How to use LVM disk operation commands in Linux disk management

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. .

①. Create a volume group

Use the command vgcreate to create a volume group

root# vgcreate vg_name pv1 pv2 ...

How to use LVM disk operation commands in Linux disk management

Create the volume group vg_fedora_yg and add pv /dev/sdb1 to the volume group.

②. View volume group information

How to use LVM disk operation commands in Linux disk management

At this time, you can also view the volume group where each physical volume is located through pvs:

How to use LVM disk operation commands in Linux disk management

①.Create LV

Use the lvcreate command

root# lvcreate -L <lv_size>  <vg_name> -n <lv_name>
 
# 将卷组vg_name下所有剩余空间给创建的lv_name逻辑卷
root# lvcreate -l +100%FREE  <vg_name> -n <lv_name>
root# lvcreate -L <lv_size> <vg_name> -n <lv_name>

How to use LVM disk operation commands in Linux disk management

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:

How to use LVM disk operation commands in Linux disk management

②、View lv

Command lvs, lvscan, lvdisplay to view

How to use LVM disk operation commands in Linux disk management

##③、Expand logical volume
By command lvextend:

root# lvextend -L <extend_size> <lv_path>

How to use LVM disk operation commands in Linux disk management

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 -ay

Finally, you can see the following:

How to use LVM disk operation commands in Linux disk management

①. Format the logical volume
Now you can create a file system on this logical volume:

# mkfs.<filesystem_type> /dev/mapper/<vg_name>-<lv_name>
 
# 
# mkfs.xfs /dev/mapper/vg_fedora_yg-lv_yg01

How to use LVM disk operation commands in Linux disk management

②, mount
# mount /dev/mapper/<vg_name>-<lv_name> <mount_point>

How to use LVM disk operation commands in Linux disk management

Note: Please select the new logical volume you created for the mount point (for example:

/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)

Finally, I drew a picture to show linux lvm:

How to use LVM disk operation commands in Linux disk management

You can refer to the above content to understand the disk management mechanism of Linux lvm.

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

什么是linux交叉编译什么是linux交叉编译Apr 29, 2022 pm 06:47 PM

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

linux中rpc是什么意思linux中rpc是什么意思May 07, 2022 pm 04:48 PM

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools