Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use LVM Management commands in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use LVM management commands in Linux. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

In Linux operating system, device file is a special type of file. Most of these files are located in the / dev directory and are used to represent a specific hardware device detected by the Linux host.

For common block storage devices such as disks (ATA, SATA, SCSI, SAS, SSD, etc.) and flash drives, the device files are mainly named in the form of sd*. For example, sda represents the first hard disk, sdb2 represents the second partition of the second hard disk, and so on.

Therefore, you can view the disk devices in the system directly using the ls-l / dev/sd* command:

$ls-l / dev/sd*brw-rw---- 1 root disk 8, August 7 00:47 / dev/sdabrw-rw---- 1 root disk 8, August 7 00:47 / dev/sda1

That is, there is only one hard disk (/ dev/sda) connected to the current system, and the hard disk has only one partition (/ dev/sda1).

II. Zoning

Partitioning can be understood as dividing an entire hard disk into one or more independent storage areas.

For example, the first hard disk of the system can be divided into three partitions, namely sda1, sda2, and sda3. Sda1 is used to mount the root directory (/), sda2 mount / var, and sda3 mount / home directory. Even if the log files in the / var directory take up all the storage space of sda2, it will not affect the use of the other two partitions.

You can use the fdisk-l command to view disk and partition information on the system:

Sudo fdisk-lDisk / dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectorsDisk model: VBOX HARDDISKUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x20985120Device Boot Start End Sectors Size Id Type/dev/sda1 2048 20964824 20962777 10G 83 Linux

Create a disk partition

The fdisk command can also be used to partition the hard disk, including creating new partitions, deleting existing partitions, creating partition tables, and so on.

Here I add a blank virtual hard disk to the Linux system in the virtual machine through the VirtualBox software. Use the fdisk-l command to view the hard disk devices detected by the system:

$sudo fdisk-lDisk / dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectorsDisk model: VBOX HARDDISKUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x20985120Device Boot Start End Sectors Size Id Type/dev/sda1 2048 20964824 20962777 10G 83 LinuxDisk / dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectorsDisk model: VBOX HARDDISKUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes

Use the fdisk / dev/sdb command to partition the new hard drive:

Only) Script I load disk layout from sfdisk script file O dump disk layout to sfdisk script file Save & Exit w write table to disk and exit q quit without saving changes Create a new label g create a new empty GPT partition table G create a new empty SGI (IRIX) partition table o create a new empty DOS partition table s create a new empty Sun partition table

After entering the fdisk program interface, press the m key and enter, you can print help information and obtain the interactive commands supported in this interface.

For example, enter p to output the partition information of the current hard disk, type n to create a new partition, and type d to delete the existing partition.

After doing anything to the partition, you finally need to use w to write all previous changes to the hard disk.

Here, press n to start the creation of a new partition, select the partition type according to the prompts (p represents the main partition, e represents the expanded partition), further select the partition number and the location of the first sector (usually the default), and finally enter the location of the last sector in the new partition (you can also specify the partition size directly) in the format + /-sectors or + /-size. Enter + 3G to create a new partition with a size of 3 GB. The specific steps are as follows:

Command (m for help): nPartition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): pPartition number (1-4, default 1): First sector (2048-10485759, default 2048): Last sector, + /-sectors or + /-size {Kjorie Glemagne Tcot P} (2048-10485759, default 10485759): + 3GCreated a new partition 1 of type 'Linux' and of size 3 GiB.Command (m for help): nPartition type p primary (1 primary, 0 extended) 3 free) e extended (container for logical partitions) Select (default p): pPartition number (2-4, default 2): First sector (6293504-10485759, default 6293504): Last sector, + /-sectors or + /-size {K sectors or Greco T P} (6293504-10485759, default 10485759): Created a new partition 2 of type 'Linux' and of size 2 GiB.

Then use the same steps to divide the remaining space of the disk into another partition. At this point, check the partition information, the original blank 5GB new hard disk sdb has been divided into two partitions sdb1 and sdb2:

Command (m for help): pDisk / dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectorsDisk model: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0xce119026Device Boot Start End Sectors Size Id Type/dev/sdb1 2048 6293503 6291456 3G 83 Linux/dev/sdb2 6293504 10485759 4192 256 2G 83 Linux

It should be noted that if you press the Q key to exit the fdisk program directly at this time, all previous actions will not be saved.

If you confirm that there is no problem with the previous operation of the hard disk, you should use the w command to write the new partition information to the disk. Similar to saving and exiting when editing a file.

III. File system

Disk and other storage devices can be regarded as a small library, in which the books are the data in the hard disk, and the role of partition is similar to the bookshelves stored in different categories of books, forming a relatively independent area.

But the books on the shelves are not placed at random. Each book needs to be arranged regularly according to certain rules and order, and sometimes the exact location is recorded.

File system is a system that organizes and allocates the space of storage devices, is responsible for file access, and protects and retrieves stored files. For the operating system, the reading and writing of files does not directly affect the hard disk sectors, but processes and organizes file data according to specific rules through the file system.

Common file systems such as NTFS in Windows and Ext4 in Linux systems.

In Windows systems, what is commonly called a "partition" operation involves the process of creating a partition and establishing a file system. In Linux systems, these two steps require two separate commands to complete.

You can use the mkfs.ext4 / dev/sdb1 command to create a file system in Ext4 format on the first partition of the previously added hard disk.

$sudo mkfs.ext4 / dev/sdb1mke2fs 1.44.6 (5-Mar-2019) Creating filesystem with 786432 4k blocks and 196608 inodesFilesystem UUID: d5e21599-12e9-44da-ae51-124d89fe5edaSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912Allocating group tables: doneWriting inode tables: doneCreating journal (16384blocks): doneWriting superblocks and filesystem accounting information: done

Swap partition

A swap partition in a Linux system can be thought of as a "memory device" located on a hard disk. Linux temporarily swaps some of the data in memory that does not need to be used immediately to the swap partition on the hard disk to alleviate the lack of memory.

My Linux virtual machine was installed without a swap partition. Here, the 2G sdb2 partition is divided into swap space through the mkswap command:

$sudo mkswap / dev/sdb2Setting up swapspace version 1, size = 2 GiB (2146430976 bytes) no label, UUID=47006330-810c-4321-8d73-d52a5f70bc88

$sudo swapon / dev/sdb2 $free-h total used free shared buff/cache availableMem: 983Mi 223Mi 168Mi 4.0Mi 590Mi 597MiSwap: 2.0Gi 0B 2.0Gi

Partition mount

In Windows systems, after inserting a divided hard disk or flash drive, one or more of the added partitions are automatically assigned drive letters (such as D:, E:, F:, etc.), and then you can read or write files on the new partition directly through the drive letter.

There is no concept of drive letter in Linux system, its file hierarchy is a tree structure (directory) starting from the root directory (/), which extends all the way down, and each branch is a specific path pointing to a specific file. For example, / usr, / root, / var, / var/log, etc.

A directory can be said to be an abstract logical structure independent of hardware storage devices that is used to specify a specific location in the file system hierarchy. The corresponding relationship between the disk partition and the directory structure needs to be specified by mounting.

Generally, when you install the system, you can mount the sda1 partition to the root directory, then all files in that directory will be saved on the sda1. If a new data disk sdb is added later, the hard disk has only one partition sdb1. To save some files on the sdb1 partition, you can create a new blank branch in the directory tree (such as / mnt/data) and mount the sdb1 under that branch. Any subdirectories, files, and other data created under the / mnt/data directory will then be saved on sdb1.

The specific commands are as follows:

$sudo mkdir-p / mnt/data$ sudo mount / dev/sdb1 / mnt/data

Use the df-h command to view the disk space consumed by the file system:

$df-hFilesystem Size Used Avail Use% Mounted onudev 456M 0456m 0% / devtmpfs 99m 1.1m 98m 2% / run/dev/sda1 9.8G 5.2G 4.2G 56% / tmpfs 492M 0492M 0% / dev/shmtmpfs 5.0M 4.0K 5.0M 1% / run/locktmpfs 492M 0492M 0% / sys/fs/cgrouptmpfs 99m 099m 0% / run/user/117tmpfs 99m 099m 0% / run/user/1000/dev/sdb1 2.9G 9.0M 2.8G 1% / mnt/data

You can see that the newly added partition / dev/sdb1 has been mounted to the / mnt/data directory.

Alternatively, you can use the lsblk command to view the capacity and mount points of block storage devices (that is, disks and partitions):

$lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsda 8:0 0 10G 0 disk └─ sda1 8:1 0 10G 0 part / sdb 8:16 0 5G 0 disk ├─ sdb1 8:17 0 3G 0 part / mnt/data └─ sdb2 8:18 0 2G 0 part [SWAP] sr0 11:0 1 1024M 0 rom

It is important to note that manually mounted partitions are automatically unmounted after the system reboot. If you want to mount the partition automatically every time the system starts up, like the root directory, you can modify the / etc/fstab configuration file, as shown in the following example:

# UUID=f3435713-b2cd-4196-b07b-2ffb116a028d / ext4 defaults 0 1/dev/sdb1 / mnt/data ext4 defaults 0 1/dev/sdb2 none swap sw 0 0

PS: compared to / dev/sda1, it is often safer to mount a partition using UUID. You can check the UUID of the disk partition by using the blkid command:

$sudo blkid/dev/sda1: UUID= "f3435713-b2cd-4196-b07b-2ffb116a028d" TYPE= "ext4" PARTUUID= "20985120-01" / dev/sdb1: UUID= "d5e21599-12e9-44da-ae51-124d89fe5eda" TYPE= "ext4" PARTUUID= "ce119026-01" / dev/sdb2: UUID= "47006330-810c-4321-8d73-d52a5f70bc88" TYPE= "swap" PARTUUID= "ce119026-02"

4. LVM (logical volume management)

For disk partition schemes that do not include logical volume management (LVM), the location, size, and number of partitions are generally fixed, making it difficult to extend current partitions and add new ones.

At this time, if you add additional hard drives and partitions, you need to create new branches in the directory tree as mount points, and the file data is scattered to multiple complex locations, which is not convenient for merging, backing up and managing data.

LVM allows you to merge single or multiple partitions into a logical volume group that contains logical volumes that can be dynamically added, resized, or deleted.

The lowest layer of the LVM system is the physical volume (pv), that is, disks, partitions, RAID arrays, and so on. Physical volumes can be used to create logical volume groups (vg), which in turn can contain any number of logical volumes (lv), which functionally correspond to partitions on physical disks.

Create volume groups and logical volumes

You can use the pvcreate command to mark a storage device (disk, partition, etc.) as a physical volume.

Here I add another blank virtual hard disk with a size of 5G through VirtualBox, and the system detects that the device is / dev/sdc:

LsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsda 8:0 0 10G 0 disk └─ sda1 8:1 0 10G 0 part / sdb 8:16 0 5G 0 disk ├─ sdb1 8:17 0 3G 0 part / mnt/data └─ sdb2 8:18 02G 0 part [SWAP] sdc 8:32 05G 0 disksr0 11:0 1 1024M 0 rom

Create a physical volume:

$sudo pvcreate / dev/sdc Physical volume "/ dev/sdc" successfully created.

List all physical volumes through the pvs command:

$sudo pvs PV VG Fmt Attr PSize PFree / dev/sdc lvm2-5.00g 5.00g

Create a logical volume group based on the physical volume through the vgcreate command:

$sudo vgcreate data-volume / dev/sdc Volume group "data-volume" successfully created

Use the vgs command to list all current logical volume groups:

$sudo vgs VG # PV # LV # SN Attr VSize VFree data-volume 1 00 wz--n-

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report