Introduction to LVM Logical Volume management in CentOS Linux
Source: Internet
Author: User
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, when a partition is incorrect
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, LogicalVolumeManager) mechanism provided by Linux is a perfect solution.
LVM is short for logical disk volume management (LogicalVolumeManager). 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:
* Physical storage media (Thephysicalmedia)
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 (physicalvolume)
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 (VolumeGroup)
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.
* Logicalvolume)
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 (physicalextent)
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 (logicalextent)
Logical volumes are also divided into the addressable basic units called LE (LogicalExtents. In the same volume group, the LE size and PE are the same and one-to-one correspondence. Introduce LVM. next we will conduct an experiment, add 3 8 Gb hard disks to the virtual machine, and start
Centos5.1 [root @ myadcls ~] # Fdisk-l // view all attached hard Disk/dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065*512 = 8225280 bytes Device Boot Start End Blocks Id System
/Dev/sda1*1 13 104391 83 Linux
/Dev/sda2 14 268 2048287 + 82 Linux swap/Solaris
/Dev/sda3 269 2610 18812115 83 Linux Disk/dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065*512 = 8225280 bytes Disk/dev/sdbdoesn't contain a valid partition table Disk/dev/sdc: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065*512 = 8225280 bytes Disk/dev/sdcdoesn't contain a valid partition table Disk/dev/sdd: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065*512 = 8225280 bytes Disk/dev/sdddoesn't contain a valid partition table
[Root @ myadcls ~] # Pvcreate/dev/sdb/dev/sdc/dev/sdd // Create a pv physical volume
Physical volume "/dev/sdb" successfully created
Physical volume "/dev/sdc" successfully created
Physical volume "/dev/sdd" successfully created
[Root @ myadcls ~] #
[Root @ myadcls ~] # Pvcreate/dev/sdb/dev/sdc/dev/sdd
Physical volume "/dev/sdb" successfully created
Physical volume "/dev/sdc" successfully created
Physical volume "/dev/sdd" successfully created
[Root @ myadcls ~] # Vgcreate vgtest/sdb/dev/sdc/dev/sdd // Create a volume group named vgtest
Volume group "vgtest" successfully created
[Root @ myadcls ~
[Root @ myadcls ~] # Lvcreate-I 3-I 4-L 1000 M-n lvtest1 vgtest
Rounding size (250 extents) up to stripe boundary size (252 extents)
Logical volume "lvtest1" created
// Create an lv. the-I parameter is used to create a logical volume by using the strip mode ing. The value of this parameter is used to specify that the created logical volume will be mapped to several PVS.
-The I parameter uses the fast size when using the strip mode. unit: KB. The value must be 2N (N> = 2). The-L parameter specifies the size of the logical volume to be created, unit: K, M, G, T indicates KB, MB. gb tb.
-N is used to specify the name of the created logical volume, which can be defined as needed. When using-I, make sure that the specified pv is not completely allocated to any logical volume. Otherwise, the creation will fail. if the PV size is not the same, the created logical volume can only take the minimum value. The following uses the remaining space to create the second logical volume. the vgdisplay command shows the remaining space of the volume group as follows: [root @ myadcls ~] # Vgdisplay
--- Volume group ---
VGName vgtest
SystemID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
VGAccess read/write
VGStatus resizable
MAXLV 0
CurLV 1
OpenLV 0
MaxPV 0
CurPV 3
ActPV 3
VGSize 23.99 GB
PESize 4.00 MB
TotalPE 6141
Alloc PE/Size 252/1008 .00 MB
Free PE/Size 5889/23 .00 GB
VGUUID 0GGQBE-kBOl-QD22-6eWx-J3eW-fGB1-a9ME8F
5889 PE for free
Divide the free pe space into another logical volume. [Root @ myadcls ~] # Lvcreate-l 5889-n lvtest2vgtest
Logical volume "lvtest2" created
// The-l parameter specifies the number of LE logical volumes. the default value is 4 MB, so the size is 4 MB x 5889 = 23556 MB.
Next, create a file system: create a file system on a logical volume and create a file system on a partition. for example, create an ext3 log file system on a logical volume, the command is as follows [root @ myadcls ~] # Lvcreate-l 5889-n lvtest2 vgtest
Logical volume "lvtest2" created
[Root @ myadcls ~] # Mkfs. ext3/dev/vgtest/lvtest1
Mke2fs 1.39 (29-may-2006)
Filesystem label =
OS type: Linux
Block size = 4096 (log = 2)
Fragment size = 4096 (log = 2)
129024 inodes, 258048 blocks
12902 blocks (5.00%) reserved for the super user
First data block = 0
Maximum filesystem blocks = 264241152
8 block groups
32768 blocks per group, 32768 fragments per group
16128 inodes per group
Superblock backups stored on blocks:
32768,983 04, 163840,229376 Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 23 mountsor
180 days, whichever comes first. Use tune2fs-c or-I tooverride. the above is the entire process for creating logical volumes in Linux.
LVM maintenance
During routine LVM maintenance, operations such as logical volume resizing and adding new physical volumes are usually required. 1. activate the volume group. if it is not activated, it cannot be accessed, but it is activated by default. manually activate the following command: [root @ myadcls ~] # Vgchange-ay vgtest // parameter description-a is used to set whether the specified volume Group is available. the parameter y indicates that the volume Group is activated. if it is set to n, the volume Group is suspended. 2 logical volume (s) in volume group "vgtest" now active
2. add a new physical volume to the volume group. when the volume group space is insufficient, you can add a new physical volume to expand the capacity of the volume group. in this case, you can use the vgextend command to achieve this, as shown below:
[Root @ myadcls ~] Vgextend vgtest/dev/sde
Volume
Group "vgtest" successfully extended
"/Dev/sde" is the added physical volume, which must be initialized by pvcreate. 3. remove the physical volume from the volume group.
To remove a physical volume from a volume group, first make sure that the physical volume to be removed is not used by any logical volume. run the pvdisplay command to view the physical volume.
[Root @ myadcls ~] # Pvdisplay/dev/sde
--- Physical volume ---
PVName/dev/sde
VGName vgtest
PVSize 8.00 GB/not usable 0
Allocatable yes (but full)
PE Size (kbytes) 4096
TotalPE 2047
FreePE 0
AllocatedPE 2047
PVUUID NrrGyd-y0j1-06cD-ZA5l-ipxV-LvvP-YeIB9q4. if a physical volume is being used by a logical volume, you need to move the data of the physical volume elsewhere and then remove it. Command for removing physical volumes: vgreduce vgtest/dev/sde 5. transfer data between physical volumes
When a disk in a logical volume is found to be faulty, or other data needs to be transferred to the physical volume (such as replacing a large hard disk or faster physical device ), you can use pvmove to transfer data between physical volumes. for example, to move data from physical volumes to/dev/sde, take the following steps: pvmove/dev/sdd/dev/sde
The size of/dev/sde must be greater than or equal to/dev/sdd, and the modprobe dm-mirror command is required before pvmove is used to load the dm-mirror module, this is because pvmove needs to use this module when transferring data. by default, this module is loaded in the system. 6. extend the logical volume
As mentioned above, lvm provides a convenient function to adjust the logical volume size. it can be used to expand the lvextend command of the logical volume. for example, it can be used to adjust the space of lvtest2 to 1600 MB.
Lvextend-L 1600 M/dev/vgtest/lvtest2 can also be added to the original base, as shown below:
Lvextend-L + 40 M/dev/vgtest/lvtest2 logical volume does not take effect immediately after expansion. you need to use the resize2fs command to re-load the logical volume size, this command is only applicable to ext2/ext3 file systems (if the reiserfs file system is used, use the resize_reiserfs command). if the logical volume is in use, you should first uninstall the logical volume and then execute it. the resize2fs command is as follows: umount/dev/vgtest/lvtest2
Resize2fs/dev/vgtest/lvtest2 mount/dev/vgtest/lvtest2/mnt/lvtest2/the above operations can be directly implemented through the ext2online command, in this way, you do not need to uninstall the logical volume, as shown in the following code: ext2online/dev/vgtest/lvtest2 7. delete logical volume
Before deleting a logical volume, run the following command to detach it: lvremove/dev/vgtest/lvtest2
Y to unmount successfully 8. remove the volume Group
Before removing a volume group, make sure that no logical volume exists in the volume group. Alternatively, you can use vgchange to manually stop the volume group. to remove the volume group, perform the following operations: vgremove vgtest 9. before deleting a physical volume, you must confirm that the physical volume has been removed from the volume group. The command to delete the physical volume is as follows:
Pvremove/dev/sdeLVM can also provide the snapshot function, in which snapshots can quickly back up data in the current logical volume, thus greatly reducing the burden on backup data, however, the technology is still immature.
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