Linux Logical Disk Volume management LVM detailed __linux

Source: Internet
Author: User

Http://www.cnblogs.com/jianggest/archive/2009/12/21/1628768.html

Summary: One of the most common and difficult decisions that Linux users encounter when installing a Linux operating system is how to properly assess the size of each partition to allocate appropriate hard disk space. When a partition space is exhausted, the solution is usually to use symbolic links, or to adjust the size of the partition tool (such as patitionmagic, etc.), but this is only a temporary solution, there is no fundamental solution to the problem. With the advent of Linux Logical Disk volume management functions, these problems are solved, this article in-depth discussion of LVM technology, so that users can easily adjust the size of each partition without downtime.
  
   First, the preface
  
Every Linux user has 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 should not only consider the capacity required for a current partition, but also anticipate the maximum capacity that the partition might need later. Because if the estimate is inaccurate, administrators may even want to back up the entire system, clear the hard disk, repartition the hard disk, and then restore the data to the new partition when a partition is not enough.
  
Although there are a lot of dynamic disk-tuning tools available now, such as partationmagic, it doesn't completely solve the problem because one partition may be depleted again, and in another, it will require a reboot of the system to implement, and for many critical servers, downtime is unacceptable, And for adding a new hard disk, you want a file system that spans multiple hard drives, the partitioning adjustment program does not solve the problem.
  
Therefore, the perfect solution should be 0 downtime under the premise of the file system can easily adjust the size, can facilitate the implementation of file systems across different disks and partitions. Fortunately, the Logical Disk volume management (lvm,logicalvolumemanager) mechanism provided by Linux is a perfect solution.
  
LVM is the abbreviation for Logical Disk volume management (logicalvolumemanager), which is a mechanism for managing disk partitions in a Linux environment, and LVM is a logical layer built on the hard disk and partitions to improve disk partition management flexibility. The LVM system administrator makes it easy to manage disk partitions, such as connecting several disk partitions into an entire block of volume groups (volumegroup) to form a storage pool. Administrators can create logical volume groups (logicalvolumes) randomly on volume groups and further create file systems on logical volume groups. Administrators can easily adjust the size of storage volume groups through LVM, and can name, manage, and distribute disk storage by group, for example, by definition by usage: "Development" and "sales" instead of using the physical disk name "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 the most of the new storage space, but directly extends the file system across the disk.
  
   Ii. The basic terminology of LVM
  
As mentioned earlier, LVM is a logical layer that is added between the disk partition and the file system to shield the file system from the lower disk partition layout, providing an abstract disk volume, and creating a filesystem on the disk volume. First we discuss the following LVM terms:
  
* Physical storage media (Thephysicalmedia)
This refers to the system storage device: Hard disk, such as:/DEV/HDA1,/DEV/SDA, etc., is the storage system at the lowest level of storage units.
  
* Physical Volume (Physicalvolume)
A physical volume is a hard disk partition or a device (such as RAID) that has the same functionality as a disk partition, and is the basic storage logic block of LVM, but is compared to basic physical storage media (such as partitions, disks, and so on) but contains administrative parameters related to LVM.
  
* Volume Group (volumegroup)
An LVM volume group is similar to a physical hard disk in a non-LVM system, which consists of a physical volume. 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 Volume (Logicalvolume)
The LVM logical volume is similar to a hard disk partition in a non-LVM system, where a file system (such as/home or/usr, etc.) can be built on top of a logical volume.
  
*pe (physicalextent)
Each physical volume is divided into basic units called PE (physicalextents), with a unique number of PE being the smallest unit that can be addressed by LVM. The size of the PE is configurable and the default is 4MB.
  
*le (logicalextent)
The logical volume is also divided into the addressable base unit called LE (logicalextents). In the same volume group, Le's size and PE are the same, and one by one correspond.
  
   

  
First you can see that the physical volume (PV) is made up of basic unit PE of equal size.
  
   

  
A volume group consists of one or more physical volumes.
  
   

  
As you can see from the above picture, PE and Le have one by one corresponding relationships. Logical volumes are built on volume groups. Logical volumes are the equivalent of a non-LVM system's disk partition, where you can create a file system.
  
The following illustration is a schematic diagram of the logical relationship between a disk partition, a volume group, a logical volume, and a file system:
  
   

  
and non-LVM systems store metadata that contains partition information in a partitioned table at the start of a partition, as well as in the Vgda (Volume Group descriptor area) that is associated with the logical volume and the volume group. Vgda includes the following: PV descriptor, VG descriptor, LV descriptor, and some PE descriptors.
  
When the system starts LVM, it activates the VG and loads the Vgda into memory to identify the actual physical storage location of the LV. When the system is doing I/O, the actual physical location is accessed according to the mapping mechanism established by VGDA.
  
   third, the installation of LVM
  
First determine if the LVM tool is installed in the system:
  
[Root@wwwroot] #rpm –QA|GREPLVM
Lvm-1.0.3-4
  
If the command result input is similar to the previous example, then the LVM administration tool is installed, and if the command does not have an output, the LVM administration tool is not installed, and the lvmrpm kit needs to be downloaded from the network or installed from the CD.
  
After installing the LVM RPM package, it is also necessary to configure the kernel to support LVM in order to use LVM. The Redhat default kernel is LVM-enabled, and if you need to recompile the kernel, you need to enter the Multi-devicesupport (RAIDANDLVM) submenu When you configure the kernel, selecting the following two options:

Multipledevicesdriversupport (RAIDANDLVM)
Logicalvolumemanager (LVM) Support
  
Then recompile the kernel to add LVM support to the new kernel.
  
In order to use LVM, be sure to activate LVM at system startup, fortunately in later versions of RedHat7.0, the system boot script already has support for activating LVM, with the following in/etc/rc.d/rc.sysinit:
  
#LVMinitialization
If[-e/proc/lvm-a-x/sbin/vgchange-a-f/etc/lvmtab];then
action$ "Settinguplogicalvolumemanagement:"/sbin/vgscan&&/sbin/vgchange-ay
Fi
  
The key is two commands, the Vgscan command implementation scans all disks for volume group information, and creates file volume group data Files/etc/lvmtab and/etc/lvmtab.d/*;vgchange-ay commands to activate all volume groups on the system.
  
   Iv. Creating and managing LVM
  
To create an LVM system, you typically need to go through the following steps:
  
1. Create partitions
Creating an LVM partition using partitioning tools such as Fdisk is the same way as creating 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 uses this command to create all partitions or disks that you want to add 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 Volume Group
The command to create a volume group is vgcreate, and the physical volume established using Pvcreate is created as a complete volume group:
  
#vgcreateweb_document/dev/hda5/dev/hdb
  
Vgcreate command The first argument is to specify the logical name of the volume group: web_document. The following argument specifies all partitions and disks that you want to add to the volume group. Vgcreate to create a volume group web_document, you also set up a PE with a size of 4MB (the default is 4MB), which means that all logical volumes created on the volume group are expanded or scaled in 4MB units. Because of the kernel reason, the PE size determines the maximum size of the logical volume, 4MB of PE determines the maximum capacity of a single logical volume of 256GB, if you want to use a logical volume greater than 256G, then specify a larger PE when you create the volume group. The PE size range is 8KB to 512MB and must always be a multiple of 2 (specified using-S, refer to manvgcreate).
  
4. Activate Volume group
In order to use the volume group immediately instead of restarting the system, you can use Vgchange to activate the volume group:
  
#vgchange-ayweb_document
  
5, add a new physical volume to the volume group
When a new disk is installed and a new physical volume is created, and you want to add it to an existing volume group, you need to use the Vgextend command:
  
#vgextendweb_document/DEV/HDC1
  
Here/DEV/HDC1 is the new physical volume.
  
6. Remove a physical volume from the volume group
To remove a physical volume from a volume group, first make sure that the physical volume you want to delete is not being used by any logical volume, and you must use the Pvdisplay command to view a physical volume information:
  
   

  
If a physical volume is being used by a logical volume, you need to back up the data for that physical volume somewhere else, and then delete it. The command to delete a physical volume is vgreduce:
  
#vgreduceweb_document/dev/hda1
  
7. Create logical Volumes
The command to create a logical volume is lvcreate:
  
#lvcreate-l1500–nwww1web_document
  
This command creates a logical volume with the name WWW1 on the volume group web_document, a size of 1500M, and the device entry is/DEV/WEB_DOCUMENT/WWW1 (Web_document is the volume group name and WWW1 is the logical volume name). If you want to create a logical volume that uses all volume groups, you need to first look at the number of PE for that volume group, and then specify when the logical volume is created:
  
#vgdisplayweb_document |grep "Totalpe"
TotalPE45230
#lvcreate-L45230WEB_DOCUMENT-NWWW1
  
8. Create File system
The author recommends the use of ReiserFS file system to replace ext2 and ext3:
  


  
Once you have created the file system, you can load and use it:
  
#mkdir/data/wwwroot
#mount/dev/web_document/www1/data/wwwroot
  
If you want the file system to load automatically when the system starts, you also need to add content to the/etc/fstab:
  
/dev/web_document/www1/data/wwwrootreiserfsdefaults12
  
9. Delete a logical volume
Before you delete a logical volume, you first need to uninstall it and then delete it:
  
#umount/DEV/WEB_DOCUMENT/WWW1
#lvremove/DEV/WEB_DOCUMENT/WWW1
Lvremove--doyoureallywanttoremove "/DEV/WEB_DOCUMENT/WWW1"? [Y/n]:y
Lvremove--doingautomaticbackupofvolumegroup "Web_document"
Lvremove--logicalvolume "/DEV/WEB_DOCUMENT/WWW1" successfullyremoved
  
10. Extended Logical Volume size
LVM provides the ability to easily resize logical volumes, and the command to extend the size of logical volumes is lvcreate:
  
#lvextend-L12G/DEV/WEB_DOCUMENT/WWW1
Lvextend--extendinglogicalvolume "/DEV/WEB_DOCUMENT/WWW1" TO12GB
Lvextend--doingautomaticbackupofvolumegroup "Web_document"
Lvextend--logicalvolume "/DEV/WEB_DOCUMENT/WWW1" successfullyextended
  
The above command is implemented to enlarge the size of the logical volume WWW1 to 12G.
  
#lvextend-L+1G/DEV/WEB_DOCUMENT/WWW1
Lvextend--extendinglogicalvolume "/DEV/WEB_DOCUMENT/WWW1" TO13GB
Lvextend--doingautomaticbackupofvolumegroup "Web_document"
Lvextend--logicalvolume "/DEV/WEB_DOCUMENT/WWW1" successfullyextended
  
The above command increases the size of the logical volume WWW1 by 1G.
  
After increasing the capacity of the logical volume, you need to modify the file system size to enable the use of expanded space. The author recommends using ReiserFS file system to replace ext2 or ext3. So this is just a discussion of reiserfs. The ReiserFS file tool provides the file system sizing tool: Resize_reiserfs. For the file system size that you want to adjust the load:
  
#resize_reiserfs-F/DEV/WEB_DOCUMENT/WWW1
  
It is generally recommended that the file system be uninstalled, resized, and then loaded:
  
#umount/DEV/WEB_DOCUMENT/WWW1
#resize_reiserfs/DEV/WEB_DOCUMENT/WWW1
#mount-treiserfs/dev/web_document/www1/data/wwwroot
  
For users who use EXT2 or ext3 file systems, you can consider using tools
  
Ext2resize. Http://sourceforge.net/projects/ext2resize
  
11. Reduce Logical Volume size
  
The capacity of a logical volume can be implemented using Lvreduce, and the file system needs to be uninstalled 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 you can see from the above discussion, LVM is very scalable and easy to use. You can easily adjust the size of the volume group, logical volume, and further resize the file system. Please refer to Lvm-howto if you would like to see more information.

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.