Linux Logical Disk volume management LVM details

Source: Internet
Author: User
Please note that this article has been published in "The World of open systems". The copyright of this article belongs to this magazine. Do not repost it at will. repost this article and keep this statement.
Linux Logical Disk volume management LVM details

Keywords:Linux LVM disk management Disk File System

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 patition magic), 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 partation magic, 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, logical volume manager) mechanism provided by Linux is a perfect solution.

LVM is short for logical volume manager. 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 to form a storage pool. The administrator can create a logical volume group (logical volumes) 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/hda1 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 physical extents. 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 lvm rpm 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 enter the multi-Device Support (raid and LVM) Sub-menu When configuring the kernel. 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 taken $ "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:

    # Vgcreate web_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 y web_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:

    # Vgextend web_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:

    # Vgreduce web_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" successfully removed

    10. Extend the logical volume size

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

    # 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" successfully extended

    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" successfully extended

    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.

  •  

    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.