LinuxLVM logical volume management and usage

Source: Internet
Author: User
Abstract: The most common hard-to-decide problem that Linux users encounter when installing the Linux operating system is how to correctly evaluate the size of each partition to allocate suitable hard disk space. When a partition space is exhausted, the solution is usually to use a symbolic link or a tool to adjust the partition size (such as PatitionMagic), but this is only

Abstract: The most common hard-to-decide problem that Linux users encounter when installing the Linux operating system is how to correctly evaluate the size of each partition to allocate suitable hard disk space. When a partition space is exhausted, the solution is usually to use a symbolic link or a tool to adjust the partition size (such as PatitionMagic), but this is only a temporary solution, the problem is not fundamentally solved. With the appearance of the logical disk volume management function in Linux, these problems have been solved. This article discusses LVM technology in depth, so that users can easily adjust the size of each partition without downtime.

I. Preface

Every Linux user will encounter this dilemma when installing Linux: how to accurately evaluate and allocate the capacity of each hard disk partition when partitioning the system, because the system administrator should not only consider the capacity required by a certain partition, but also anticipate the maximum capacity that may be required after the partition. If the estimation is inaccurate, the administrator may even need to back up the entire system, clear the hard disk, re-partition the hard disk, and restore the data to the new partition when a partition is insufficient.

Although there are many tools available for dynamic disk adjustment, such as PartationMagic, it cannot completely solve the problem because a partition may be exhausted again; in addition, this requires a reboot of the system. for many critical servers, shutdown is unacceptable and new hard disks are added, if you want a file system that can span multiple hard drives, the partition adjustment program cannot solve the problem.

Therefore, the perfect solution should be to adjust the file system size without downtime, so that the file system can easily span across different disks and partitions. Fortunately, the logical disk volume management (LVM, LogicalVolume Manager) mechanism provided by Linux is a perfect solution.

LVM is short for Logical volume management (Logical VolumeManager). It is a mechanism for managing disk partitions in Linux. LVM is a Logical layer built on hard disks and partitions, to improve the flexibility of disk partition management. The LVM system administrator can easily manage disk partitions. for example, you can connect several disk partitions to an entire volume Group (volumegroup) to form a storage pool. The administrator can create a logical volume Group (logicalvolumes) and create a file system on the logical volume group. Through LVM, administrators can easily adjust the size of storage volume groups and name, manage, and allocate disk storage by group. for example, they can define disk storage by usage: "development" and "sales" instead of using the physical disk names "sda" and "sdb ". When a new disk is added to the system, the LVM administrator does not need to move the disk files to the new disk to make full use of the new storage space. Instead, the file system can be directly expanded to span the disk.

II. basic terms of LVM

As mentioned above, LVM is a logical layer added between the disk partition and the file system to shield the file system from the underlying disk partition layout and provide an abstract disk volume, create a file system on the disk. First, we will discuss the following LVM terms:

The physical media)
This refers to the storage device of the system, such as/dev/hda and/dev/sda. it is the storage unit at the lowest layer of the storage system.
Physical volume (physical volume)
A physical volume refers to a hard disk partition or a device (such as RAID) with the same functions as a disk partition logically. it is the basic storage logical block of LVM, however, compared with basic physical storage media (such as partitions and disks), it contains management parameters related to LVM.
Volume Group)
An LVM volume group is similar to a physical hard disk in a non-LVM system and consists of physical volumes. You can create one or more "LVM partitions" (logical volumes) on the volume group. The LVM volume group consists of one or more physical volumes.
Logical volume)
The logical volume of LVM is similar to the hard disk partition in a non-LVM system. a file system (such as/home or/usr) can be created on the logical volume ).
PE (physical extent)
Each physical volume is divided into basic units called PhysicalExtents. PES with unique numbers are the smallest units that can be addressed by LVM. The PE size is configurable. the default value is 4 MB.
LE (logical extent)
Logical volumes are also divided into the addressable basic units called LE (Logical Extents. In the same volume group, the LE size and PE are the same and one-to-one correspondence.

First, we can see that the physical volume (PV) is composed of an equivalent basic unit PE.

A volume group consists of one or more physical volumes,

As you can see, PE and LE have a one-to-one relationship. The logical volume is created on the volume group. A logical volume is equivalent to a non-LVM system disk partition. you can create a file system on it.

Is the logical relationship between disk partitions, volume groups, logical volumes, and file systems:

The non-LVM system stores the metadata containing the partition information in the partition table at the starting position of the partition, the logical volume and volume group-related metadata are also stored in the VGDA (volume group descriptor area) at the beginning of the physical volume. VGDA includes PV descriptor, VG descriptor, LV descriptor, and PE descriptor.

When LVM is started, the system activates the VG and loads the VGDA to the memory to identify the actual physical storage location of the LV. When the system performs I/O operations, it accesses the actual physical location based on the ing mechanism established by VGDA.

3. install LVM
First, determine whether the lvm tool is installed in the system:

[Root @ www root] # rpm? Qa | grep lvm
Lvm-1.0.3-4

If the command result is similar to the preceding example, the LVM management tool is installed. if the command is not output, the LVM management tool is not installed, you need to download or install the LVMrpm toolkit from the network.

After installing the RPM Package of LVM, you must configure kernel support for LVM to use LVM. RedHat supports LVM by default. if you need to recompile the kernel, you must go to the Multi-deviceSupport (RAID and LVM) sub-menu when configuring the kernel and select the following two options:

[*] Multiple devices driver support (RAID and LVM)
<*> Logical volume manager (LVM) Support

Then re-compile the kernel to add LVM support to the new kernel.

To use LVM, make sure that LVM is activated when the system is started. Fortunately, in versions later than RedHat7.0, the system startup script already supports activating LVM in/etc/rc. d/rc. sysinit has the following content:

# LVM initialization
If [-e/proc/lvm-a-x/sbin/vgchange-a-f/etc/lvmtab]; then
Action $ "Setting up Logical Volume Management:"/sbin/vgscan &/sbin/vgchange-a y
Fi

The key is two commands. the vgscan command scans all disks to obtain the volume group information and creates the file volume group data file/etc/lvmtab and/etc/lvmtab. d/*; vgchange-a y command to activate all the volume groups of the system.

IV. create and manage LVM
To create an LVM system, follow these steps:

1. create a partition
Use a partitioning tool (such as fdisk) to create LVM partitions. the method is the same as that for creating other general partitions. The difference is that the LVM partition type is 8e.

2. create a physical volume
The command for creating a physical volume is pvcreate, which is used to create all partitions or disks to be added to the volume group as physical volumes. The command to create the entire disk as a physical volume is:

# Pvcreate/dev/hdb

The command to create a single partition as a physical volume is:

# Pvcreate/dev/hda5

3. create a volume Group
The command for creating a volume Group is vgcreate, and the physical volume created using pvcreate is created as a complete volume group:

# Vgcreateweb_document/dev/hda5/dev/hdb

The first parameter of the vgcreate command is to specify the logical name of the volume group: web_document. The following parameter specifies all partitions and disks that you want to add to the volume group. In addition to creating a volume group web_document, vgcreate also sets a 4 mb pe (4 MB by default ), this indicates that all logical volumes created on the volume group are expanded or reduced in 4 MB increments. Because of the kernel, the PE size determines the maximum size of the logical volume. the 4 mb pe determines that the maximum size of a single logical volume is 256 GB, if you want to use a logical volume larger than GB, specify a larger PE when creating the volume group. The PE size ranges from 8 KB to 512 MB, and must always be a multiple of 2 (use-s to specify. for details, see man vgcreate ).

4. activate the volume Group
To immediately use a volume group instead of restarting the system, you can use vgchange to activate the volume group:

# Vgchange-a yweb_document

5. add a new physical volume to the volume group.
When the system installs a new disk and creates a new physical volume, you need to use the vgextend command to add it to an existing volume group:

# Vgextendweb_document/dev/hdc1

Here/dev/hdc1 is the new physical volume.

6. delete a physical volume from the volume Group
To delete a physical volume from a volume group, make sure that the physical volume to be deleted is not in use by any logical volume. run the pvdisplay command to view the physical volume information:

If a physical volume is being used by a logical volume, you need to back up the data of the physical volume to another place and then delete it. The command to delete a physical volume is vgreduce:

# Vgreduceweb_document/dev/hda1

7. create a logical volume
The command for creating a logical volume is lvcreate:

# Lvcreate-L1500? Nwww1 web_document

This command creates a logical volume named www1 with a size of MB on the volume group web_document, and the device entry is/dev/web_document/www1 (web_document is the volume group name, www1 is the name of the logical volume ). If you want to create a logical volume that uses all the volume groups, you must first check the number of PES in the volume group, and then specify:

# Vgdisplay web_document | grep "Total PE"
Total PE 45230
# Lvcreate-l 45230 web_document-n www1

8. create a file system
I recommend using the reiserfs file system to replace ext2 and ext3:

After creating a file system, you can load and use it:

# Mkdir/data/wwwroot
# Mount/dev/web_document/www1/data/wwwroot

If you want to automatically load the file system when the system starts, you also need to add the following content in/etc/fstab:

/Dev/web_document/www1/data/wwwroot reiserfs defaults 1 2

9. delete a logical volume
Before deleting a logical volume, you must detach it and then delete it:

# Umount/dev/web_document/www1
# Lvremove/dev/web_document/www1
Lvremove -- do you really want to remove "/dev/web_document/www1 "? [Y/n]: y
Lvremove -- doing automatic backup of volume group "web_document"
Lvremove -- logical volume "/dev/web_document/www1" successfullyremoved

10. extend the logical volume size
Those who are used to the traditional hard drive tool may think that the file system and the hard disk partition are unified, in fact, they are not a complete whole (that is why BSD can create multiple file system partitions in a hard disk partition ). Imagine a bookcase with a mobile shelf. You can adjust the location of books in the bookcase through a mobile bookshelf. Using books as a file system, you can fill the cupboard with books, or adjust the space of the bookcase through mobile bookshelves. Of course, you can also adjust the storage location of books in the bookcase through the bookshelf. As shown in:

| <-- Hda1 ----> | <-- hda2 ------> |
+ --------------- +
| Xxxxxxxxxxx | xxxxxx |
| Xxxxxxxxxxx | xxxxxx |
+ --------------- +

The file system in hard disk partition/dev/hda1 occupies the entire partition, while the file system in/dev/hda2 only occupies about 2/3 of the partitions.

Therefore, when adjusting the logical volume capacity, you must also adjust the size of the file system on the volume. to increase the capacity, first adjust the capacity and then adjust the file system, to reduce the capacity, the opposite is true.

LVM provides the ability to easily adjust the logical volume size. the command to expand the logical volume size is lvextend:

# Lvextend-L12G/dev/web_document/www1
Lvextend -- extending logical volume "/dev/web_document/www1" to 12 GB
Lvextend -- doing automatic backup of volume group "web_document"
Lvextend -- logical volume "/dev/web_document/www1" successfullyextended

The preceding command expands the size of the logical volume www1 to 12 GB.

# Lvextend-L + 1G/dev/web_document/www1
Lvextend -- extending logical volume "/dev/web_document/www1" to 13 GB
Lvextend -- doing automatic backup of volume group "web_document"
Lvextend -- logical volume "/dev/web_document/www1" successfullyextended

The preceding command increases the size of the logical volume www1 by 1 GB.

After the capacity of the logical volume is increased, you need to modify the file system size to use the expanded space. We recommend that you use the reiserfs file system to replace ext2 or ext3. Therefore, we will only discuss the reiserfs situation here. The Reiserfs file tool provides the file system size adjustment tool: resize_reiserfs. To adjust the size of the file system to be loaded:

# Resize_reiserfs-f/dev/web_document/www1

We recommend that you uninstall the file system, resize the file system, and load the file again:

# Umount/dev/web_document/www1
# Resize_reiserfs/dev/web_document/www1
# Mount-treiserfs/dev/web_document/www1/data/wwwroot

You can consider using tools for ext2 or ext3 file systems.

Ext2resize. Http://sourceforge.net/projects/ext2resize

11. reduce logical volume size

You can use lvreduce to achieve logical volume capacity. you also need to first uninstall the file system:

# Umount/data/wwwroot
# Resize_reiserfs-s-2G/dev/web_document/www1
# Lvreduce-L-2G/dev/web_document/www1
# Mount-treiserfs/dev/web_document/www1/data/wwwroot

V. Summary
According to the above discussion, we can see that LVM has good scalability and is very convenient to use. You can easily adjust the volume Group and logical volume size to further adjust the file system size. For more information, see LVM-HOWTO.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.