Detailed explanation of the use of Linux LVM

Abstract:  One of the most common and difficult problems encountered by Linux users when installing the Linux operating system is how to correctly evaluate the size of each partition to allocate suitable hard disk space. When the space of a certain partition is exhausted, the solution is usually to use a symbolic link, or use a tool to adjust the partition size (such as Patition Magic, etc.), but these are only temporary solutions and do not fundamentally solve the problem. With the emergence of Linux's logical volume management function, these problems are easily solved. This article discusses LVM technology in depth, allowing users to easily adjust the size of each partition without downtime.

 

I. Introduction

 

    Every Linux user will encounter such a 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 must not only consider the capacity required by a current partition, but also To anticipate the maximum capacity that the partition may need in the future. Because if the estimate is not accurate, the administrator may even have to backup the entire system, clear the hard disk, repartition the hard disk, and then restore the data to the new partition when a partition is insufficient.

 

    Although there are many tools to dynamically adjust the disk, such as Partation Magic, etc., but it can not completely solve the problem, because a certain partition may be exhausted again; another aspect, this requires a reboot of the system to achieve, for many Critical servers, where downtime is unacceptable, and for adding new hard drives, where a file system that spans multiple hard drives is desired, a partition resizer will not solve the problem.

 

    Therefore, the perfect solution should be to adjust the size of the file system freely under the premise of zero downtime, which can facilitate the realization of the file system across different disks and partitions. Fortunately, the logical volume management (LVM, Logical Volume Manager) mechanism provided by Linux is a perfect solution.

 

    LVM is the abbreviation of Logical Volume Manager. It is a mechanism for managing disk partitions in the Linux environment. LVM is a logical layer built on hard disks and partitions to improve the flexibility of disk partition management. sex. Through LVM system administrators can easily manage disk partitions, such as: connecting several disk partitions into a whole volume group (volume group) to form a storage pool. Administrators can freely create logical volume groups (logical volumes) on the volume groups, and further create file systems on the logical volume groups. The administrator can easily adjust the size of the storage volume group through LVM, and can name, manage and allocate the disk storage according to the group, for example, define according to the usage: "development" and "sales" instead of using physical disks Names "sda" and "sdb". And when a new disk is added to the system, the LVM administrator does not have to move the disk's files to the new disk to make full use of the new storage space, but directly expand the file system across the disk.

 

2. Basic terms of LVM

 

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

 

 

  • The physical media
    refers to the storage devices of the system: hard disks, such as: /dev/hda1, /dev/sda, etc., are the lowest-level storage units of the storage system. 
  • Physical volume
    refers to a hard disk partition or a device (such as RAID) that logically has the same function as a disk partition. It is the basic storage logical block of LVM, but is different from basic physical storage media (such as partitions, disks, etc. ), but contains management parameters related to LVM. 
  • Volume Group
    LVM volume groups are similar to physical hard disks in non-LVM systems and consist of physical volumes. One or more "LVM partitions" (logical volumes) can be created on a volume group, which consists of one or more physical volumes. 
  • Logical volume (logical volume)
    The logical volume of LVM is similar to the hard disk partition in the non-LVM system, and a file system (such as /home or /usr, etc.) can be established on the logical volume. 
  • PE (physical extent)
    Each physical volume is divided into basic units called PE (Physical Extents), and a PE with a unique number is the smallest unit that can be addressed by LVM. The size of the PE is configurable and defaults to 4MB. 
  • LE (logical extent)
    logical volumes are also divided into addressable basic units called LE (Logical Extents). In the same volume group, the size of LE and PE are the same and correspond one-to-one.

 

 

    First you can see that a physical volume (PV) is composed of basic units PE of equal size.

 

 

    A volume group consists of one or more physical volumes,

 

 

    As can be seen from the above figure, PE and LE have a one-to-one correspondence. Logical volumes are built on volume groups. A logical volume is equivalent to a disk partition of a non-LVM system on which a file system can be created.

 

    The following figure is a schematic diagram of the logical relationship between disk partitions, volume groups, logical volumes and file systems:

 

 

    Just as non-LVM systems store metadata containing partition information in the partition table at the beginning of the partition, the metadata related to logical volumes and volume groups are also stored in the VGDA (Volume Group Descriptor) located at the beginning of the physical volume. region). VGDA includes the following: PV descriptor, VG descriptor, LV descriptor, and some PE descriptors.

 

    When the system starts LVM, VG is activated, and VGDA is loaded into memory to identify the actual physical storage location of LV. When the system performs I/O operations, it will access the actual physical location according to the mapping 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 input is similar to the above example, the system has installed the LVM management tool; if the command does not output, it means that the LVM management tool is not installed, you need to download from the network or install the LVM rpm toolkit from the CD.

 

    After installing the LVM RPM package, you need to configure the kernel to support LVM to use LVM. The default kernel of RedHat supports LVM. If you need to recompile the kernel, you need to enter the Multi-device Support (RAID and LVM) submenu when configuring the kernel, and select the following two options:

 

  

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

     


        Then recompile the kernel to add LVM support to the new kernel.


        In order to use LVM, make sure that LVM is activated at system startup. Fortunately, in versions after RedHat 7.0, the system startup script already has support for activating LVM. The following content in /etc/rc.d/rc.sysinit :



    # 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 get the volume group information, and creates the file volume group data files /etc/lvmtab and /etc/lvmtab.d/*; the vgchange -ay command activates all volume groups in the system.


    Fourth, create and manage LVM


    To create an LVM system, you generally need to go through the following steps:


    1. Create a partition


        Use a partition tool (such as fdisk, etc.) to create an LVM partition. The method is the same as that of other general partitions, except that the LVM partition type is 8e.


    2. Create a physical volume


        The command to create a physical volume is pvcreate, which is used to create all partitions or disks that you want to add to the volume group as physical volumes. The command to create an 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 to create a volume group is vgcreate, which creates a physical volume created with pvcreate 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 latter parameter specifies all partitions and disks that you wish to add to the volume group. In addition to creating the volume group web_document, vgcreate also sets a PE with a size of 4 MB (4MB by default), which means that all logical volumes created on the volume group are expanded or shrunk in 4 MB increments. Due to the kernel, the PE size determines the maximum size of the logical volume. The 4 MB PE determines the maximum capacity of a single logical volume is 256 GB. If you want to use a logical volume larger than 256G, specify a larger PE when creating a volume group. The PE size ranges from 8 KB to 512 MB and must always be a multiple of 2 (specified with -s, see man vgcreate for details).


    4. Activate the volume group


        In order to use the volume group immediately instead of rebooting the system, vgchange can be used to activate the volume group:


            # vgchange -a y web_document


    5. Add a new physical volume to the volume group


        When the system mounts a new disk and creates a new physical volume, to add it to an existing volume group, use the vgextend command:


            # vgextend web_document /dev/hdc1


        Here /dev/hdc1 is the new physical volume.


    6. Remove a physical volume from the volume group


        To delete a physical volume from a volume group, first make sure that the physical volume to be deleted is not in use by any logical volume, and use the pvdisplay command to view information about the physical volume:



        If a physical volume is being used by a logical volume, the data of the physical volume needs to be backed up elsewhere and then deleted. The command to delete a physical volume is vgreduce:


            # vgreduce web_document /dev/hda1


    7. Create a logical volume


        The command to create a logical volume is lvcreate:


            # lvcreate -L1500 –nwww1 web_document


        This command creates a logical volume named www1 with a size of 1500M 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 logical volume name). If you want to create a logical volume that uses all volume groups, you need to first check the number of PEs in the volume group, and then specify when creating the logical volume:



    # vgdisplay web_document| grep "Total PE"
    Total PE 45230
    # lvcreate -l 45230 web_document -n www1


    8. Create a file system


        The author recommends using the reiserfs file system instead of ext2 and ext3:



        Once the filesystem is created, it can be loaded and used:




    # mkdir /data/wwwroot
    # mount /dev/web_document/www1 /data/wwwroot


    If you want the file system to be automatically mounted when the system starts, you also need to add something to /etc/fstab:




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


    9. Delete a logical volume


    Before deleting a logical volume it is first necessary to unmount it, then delete:



    # 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 logical volume size


    LVM provides the ability to easily adjust the size of logical volumes. The command to expand the size of logical volumes 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 above command will expand the size of the logical volume www1 to 12G.



    # 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 above command will increase the size of the logical volume www1 by 1G.


        After increasing the capacity of the logical volume, it is necessary to modify the file system size to utilize the expanded space. The author recommends using the reiserfs file system instead of ext2 or ext3. Therefore, only the case of reiserfs is discussed here. The Reiserfs file tool provides a file system resizing tool: resize_reiserfs. For filesystems wishing to resize mounted:


        # resize_reiserfs -f /dev/web_document/www1


        It is generally recommended that the filesystem is best unmounted, resized, and then mounted:



    # umount /dev/web_document/www1
    # resize_reiserfs /dev/web_document/www1
    # mount -treiserfs /dev/web_document/www1 /data/wwwroot


    For users with ext2 or ext3 filesystems, consider using tools


         ext2resize。http://sourceforge.net/projects/ext2resize


    11. Reduce logical volume size


    The capacity of the logical volume can be achieved by using lvreduce, and the file system needs to be unmounted first:



    # 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


        As can be seen from the above discussion, LVM has good scalability and is very convenient to use. You can easily adjust the size of the volume group and logical volume, and further adjust the size of the file system. For more information, please refer to the LVM-HOWTO.

https://my.oschina.net/qihh/blog/59948

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326116047&siteId=291194637