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

Creation and expansion of LVM logical volumes in Linux system (detailed description of commands, illustrated with pictures and texts)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Introduction to LVM:

LVM (Logical Volume Manager) Chinese name: logical volume manager, which is essentially a virtual device driver, is a new level of abstraction added between block devices and physical devices in the kernel, which can combine several disks (physical volumes, PhysicalVolume) to form a storage pool or volume group (VolumeGroup).

LVM can create new logical devices by dividing logical volumes of different sizes (LogicalVolume) from the volume group each time. The underlying raw disk is no longer directly controlled by the kernel, but by the LVM layer. For upper-layer applications, volume groups replace disk blocks as the basic unit of data storage.

LVM manages the physical extents of all physical volumes and maintains the mapping between logical and physical extents. LVM logic devices provide the same functions as physical disks to upper-layer applications, such as file system creation and data access. However, LVM logical devices are not subject to physical constraints, logical volumes do not have to be contiguous space, it can span many physical volumes, and can be resized at any time. It is easier to manage disk space than physical disks.

Advantages and disadvantages of LVM:

We usually use computers, when there is not enough disk space, generally there is no way to expand the disk space, we can only add new hard drives, partition creation of new hard drives, and then format to use. Of course, this is certainly enough for our personal computers, except for some trouble. However, in the working environment, it is difficult for us to shut down the server and add a new hard disk. We should know that the server running important services is generally running 24 hours a day. At this time, we can dynamically manage the disk of the system through LVM.

The basic concept of LVM mechanism: volume group (VG) schematic diagram:

Explanation of the term:

Physical storage medium (The physical media): in this case, the storage device of the system: the hard disk, such as / dev/hda1, / dev/sda, etc., is the storage unit at the lowest level of the storage system.

Physical volume (physical volume) PV: a physical volume refers to a hard disk partition or a device that logically has the same function as a disk partition (such as RAID). It is the basic storage logic block of LVM, but compared with the basic physical storage media (such as partitions, disks, etc.), it contains management parameters related to LVM.

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

Logical volumes (logical volume) logical volumes of LV:LVM are similar to hard disk partitions in non-LVM systems, and file systems can be established on top of logical volumes (such as / home or / usr, etc.).

Physical block (physical extent) PE: each physical volume is divided into basic units called PE (Physical Extents), and PE with a unique number is the smallest unit that can be addressed by LVM. The size of PE is configurable and defaults to 4MB.

Logical extent LE: logical volumes are also divided into basic addressable units called LE (Logical Extents). In the same volume group, the size of LE and PE are the same and correspond one to one. Administrative commands for LVM:

Main commands:

Pvcreate command: create a new physical volume (format: pvcreate... Device name 1. [device name 2])

Vgcreate command: create a new volume group (format: vgcreate... Volume group name. Physical volume name 1. Physical volume name 2)

Lvcreate command: create a new logical volume (format: lvcreate...-L... Capacity size.-n. Logical volume name. Volume group name)

Lvextend command: add storage space for logical volumes (format: lvextend...-L...+ capacity size... / dev/ volume group name / logical volume name)

LVM process for creating a logical volume (actual operation): step 1: create a physical volume (PV).

1. Add three 20G hard drives to the virtual machine, and then restart the Linux system (restart the system to recognize the hard drive, restart with the "reboot" or "init6" command).

two。 Use the fdisk command to check that the hard drive has been added successfully.

Enter: fdisk-l

3. Configure disk sdb to allocate all space to a primary partition, and then convert the partition format to LVM format. (I have written about disk management in my previous blog. If you don't know how to do it, you can read it.)

Input: fdisk / dev/sdb- management disk sdb input: nmury-new partition input: Pmure-select partition type primary partition enter- -default setting partition number is 1 enter-default setting starting sector enter-default allocation of all space to partition 1 enter: t- -convert partition format input: 8e---LVM format code (if you don't know, you can enter L query) input: WMurmuri-save exit

4. Using the above method, configure disk sdc and sdd in the same way as disk sdb, and save and exit the disk configuration panel. (because the process is the same, I won't repeat it.)

5. Use the fdisk command again to check that the disk partition is configured successfully.

Enter: fdisk-l

6. Use the pvcreate command to create three disk partitions into three physical volumes.

(I use regular expression input, which is easier and the result is the same as typing "pvcreate / dev/sdb1 / dev/sdc1 / dev/sdd1" command line.)

Enter: pvcreate / dev/sd {b.. d} 1

Step 2: create a new volume group (VG).

1. Add the physical volumes sdb1, sdc1, sdd1 to the volume group mailbox.

Enter: vgcreate mailbox / dev/sd {b.. d} 1-create a new volume group mailbox and add physical volumes sdb1, sdc1, sdd1 input: pvscan--- to view physical volume information

two。 Check the details of the volume group mailbox.

Input: vgdisplay- displays volume group details

Step 3: create a new logical volume (LV)

1. Create a new logical volume mbox01 for the mailbox volume group. (my logical volume with a disk size of 20g and 25g can obviously be built across disks.)

Input: lvcreate-L 25g-n mbox01 mailbox- create a new logical volume named mbox01 for the mailbox volume group with a capacity of 25g input: lvdisplay--- displays logical volume details

two。 Then create a logical volume mbox02 for the mailbox volume group, with the capacity set to 15G.

Input: lvcreate-L 15G-n mbox02 mailbox input: lvdisplay

3. If we feel that there is less space for the logical volume mbox01, we can extend it to 10G.

Input: lvextend-L + 10G / dev/mailbox/mbox01- to expand 10G space for mbox01. "+" indicates how much capacity to increase, and if it does not take "+", it means how much capacity to expand. Input: lvdisplay

Step 4: format the logical volume and mount it

1. Format logical volumes mbox01 and mbox02.

Input: mkfs.xfs / dev/mailbox/mbox01- format logical volume mbox01 to xfs file format input: mkfs.xfs / dev/mailbox/mbox02- format logical volume mbox02 to xfs file format

two。 Create two new directories under the / mnt/ directory as mount points for the two logical volumes.

Input: mkdir / mnt/mbox0 {1.. 2}-create two new directories named mbox01 and mbox02 under the / mnt/ directory input: ls / mnt/-- View the files or directories under the / mnt/ directory

3. Add automatic mount commands for logical volumes mbox01 and mbox02 to the / etc/fstab file. (automatically upload my previous blog, friends who won't can have a look.)

Input: vim / etc/fstab---- use vim editor to edit fstab file input: / dev/mailbox/mbox01 / mnt/mbox01 xfs defaults 0-add auto-mount command input: / dev/mailbox/mbox02 / mnt/mbox02 xfs defaults 00-add auto-mount command input: mount-a Murray- -- Mount all devices. Input: df-hT -View mount information

LVM expansion: logical volume (LV) expansion: ext2/ext3/ext4 file system, adjust the command is resize2fs, or directly use "- r" to automatically expand the file system size:

1. Unmount the mbox01 logical volume, and then format the file into ext4 format.

Input: umount / dev/mailbox/mbox01- unmount mbox01 input: mkfs.ext4 / dev/mailbox/mbox01- format file is ext4

two。 Remount the mbox01 and view the mount details.

Enter: mount / dev/mailbox/mbox01 / mnt/mbox01- mount mbox01 enter: df-hT--- to view mount details

3. We add 1G capacity to mbox01, and then check the mount details and find that the capacity has not increased.

Input: lvextend-L + 1G / dev/mailbox/mbox01- to increase capacity input: df-hT--- to view mount details

4. Use the resize2fs command to perform the capacity adjustment, and then check to find that the capacity increase is successful.

Input: resize2fs / dev/mailbox/mbox01- perform adjustment input: df-hT--- to view mount details

5. Use the "- r" option to extend the file system size directly.

Input: lvextend-L + 1G / dev/mailbox/mbox01-r-increase mbox01 capacity 1G and expand automatically

Enter: df-hT--- to view mount details

The adjustment command for the xfs file system is xfs_growfs, or automatically extend the file system size with "- r" directly:

1. If there is not enough capacity in the process of using mbox02, we can expand the capacity directly, but only if there is enough capacity in the volume group.

Enter: lvextend-L + 5G / dev/mailbox/mbox02- to add 5G capacity to mbox02 logical volume

Enter: lvdisplay / dev/mailbox/mbox02- to display mbox02 logical volume details

two。 Let's take a look at the mount information again with the df-hT command and find that the capacity of the mbox02 has not increased. This is because the file system is not synchronized yet, so we just need to synchronize it with the xfs_growfs command.

Input: df-hT--- view mount details input: xfs_growfs / dev/mailbox/mbox02- perform adjustment input: df-hT-- -View mount details

3. Automatically extend the file system size directly with "- r".

Input: lvextend-L + 1G / dev/mailbox/mbox02-r-increase mbox02 capacity 1G and expand automatically input: df-hT--- to view mount details

Expansion of volume group (VG):

1. If we find that there is not enough space left in the volume group, let's first add a hard drive.

two。 After adding the hard disk, the system is usually rebooted, but the server in the enterprise cannot be restarted at will. Here we use the echo "--> / sys/class/scsi_host/host0/scan command to rescan the scsi bus to add the disk.

Enter: echo "- -" > / sys/class/scsi_host/host0/scan- scan scsi bus to add hard disk input: fdisk-l / dev/sde--- to view the new disk sde

3. Format the new disk, and then add it to the volume group mailbox to see if the space has been added successfully.

Input: pvcreate / dev/sde--- to create a new physical volume / dev/sde input: vgextend mailbox / dev/sde- add disk / dev/sde to volume group mailbox to enter: vgs--- to view volume group information

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