LVM Logical Volume management in CentOS

Source: Internet
Author: User
Related Knowledge Point LVM: LogicalVolumeManager logical volume management. it is a disk management method and 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. Volume group volumegroup Knowledge Point
LVM: Logical VolumeManager Logical volume management. it is a disk management method and 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.

Volume group (VG)
A volume Group is the highest abstraction layer in LVM and is a memory pool composed of one or more physical volumes.

Physical volume (PV)
A typical physical volume is a hard disk partition, but it can also be an entire hard disk or a created Software RAID volume.

Logical volume (LV)
A logical volume is equivalent to a partition in a non-LVM system. it is created on a volume group and is a standard block device on which a file system can be created.

Physical Block physical extent (PE)
Physical volumes are stored in blocks of the same size. the block size is the same as the logical volume block size in the volume group.

Logical Block logical extent (LE)
Logical volumes are stored in blocks. the block size of all logical volumes in a volume Group is the same.

Job environment
Note: The following operations are performed in the VirtualBox4.1 virtual machine environment.
System version: CentOS 5.8
Hard disk size (virtual hard disk): 20 GB

1. First, check the system space usage.
Use different missions to view the usage of system space based on different concerns.

View the partition information of the disk. we can see that all the space except the boot partition (100 MB) is in LVM.
[Root @ sqing ~] # Fdisk-l
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 2610 20860402 + 8e Linux LVM

View node usage
[Root @ sqing ~] # Df-h
Filesystem Size Used Avail Use % Mounted on
/Dev/mapper/VolGroup00-lv_root
9.7G 926 M 8.3G 10%/
/Dev/sda1 99 M 13 M 82 M 13%/boot
Tmpfs 506 M 0 506 M 0%/dev/shm

View the logical volume group information. you can see that vg has 7.88G of free space.
[Root @ sqing ~] # Vgscan
Reading all physical volumes. This may take a while...
Found volume group "VolGroup00" using metadata type lvm2
[Root @ sqing ~] # Vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
Max lv 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE/Size 448/12 .00 GB
Free PE/Size 188/7 .88 GB
Vg uuid I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH

View the logical volume information. you can see that the root partition size is 10 GB and the swap partition size is 2 GB.
[Root @ sqing ~] #LvsCan
ACTIVE '/dev/VolGroup00/lv_root' [10.00 GB] inherit
ACTIVE '/dev/VolGroup00/lv_swap' [2.00 GB] inherit

II. during installation, the swap partition size is set to 2 GB. it is not enough and needs to be expanded to 4 GB.

Expand the swap logical volume
Step: cancel the activation of the swap space, modify the swap partition size, set the swap partition to the swap area, and then activate the swap space.
[Root @ sqing ~] # Swapoff
Usage: swapoff [-hV]
Swapoff-a [-v]
Swapoff [-v] special...
[Root @ sqing ~] # Lvresize-L 4G/dev/VolGroup00/lv_swap # increase the size to 4G
Extending logical volume lv_swap to 4.00 GB
Logical volume lv_swap successfully resized
[Root @ sqing ~] # Mkswap-f/dev/VolGroup00/lv_swap
Setting up swapspace version 1, size = 4294963 kB
[Root @ sqing ~] # Swapon
Usage: swapon [-hV]
Swapon-a [-e] [-v]
Swapon [-v] [-p priority] special | LABEL = volume_name...
Swapon [-s]
(Note: If the swap logical volume is reduced, the procedure is the same as that described above .)

After the operation is complete, check the current lv and vg information. you can see that lv_swap is 4 GB, and the free size of the logical volume changes from 7.88 GB to 5.88 GB.
[Root @ sqing ~] # Lvscan
ACTIVE '/dev/VolGroup00/lv_root' [10.00 GB] inherit
ACTIVE '/dev/VolGroup00/lv_swap' [4.00 GB] inherit
[Root @ sqing ~] # Vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 6
VG Access read/write
VG Status resizable
Max lv 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE/Size 448/14 .00 GB
Free PE/Size 188/5 .88 GB
Vg uuid I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH

3. use the remaining disk space.
Add a logical partition on/dev/VolGroup00, named lv_data, and the size is the remaining space (5.88 GB) in the logical volume group VolGroup00 ).

Create logical volume lv_data
Step: create a new partition, format the new partition, and mount the new partition. (partitions cannot be directly used in Linux. they are stored in directories, therefore, you need to mount lv_data to a directory)
(Note: In the following operations, we wanted to use all the remaining space of 5.88 GB at once. However, due to the discrepancy between the system data and the display data (G-level bytes), the input number 5.88G is too large, then change to 5.86 GB)
[Root @ sqing ~] # Lvcreate-L 5.88G-n lv_data/dev/VolGroup00
Rounding up size to full physical extent 5.91 GB
Volume group "VolGroup00" has insufficient free space (188 extents): 189 required.
[Root @ sqing ~] # Lvcreate-L 5.86G-n lv_data/dev/VolGroup00
Rounding up size to full physical extent 5.88 GB
Logical volume "lv_data" created
[Root @ sqing ~] # Mkfs. ext3/dev/VolGroup00/lv_data
Mke2fs 1.39 (29-may-2006)
Filesystem label =
OS type: Linux
Block size = 4096 (log = 2)
Fragment size = 4096 (log = 2)
770048 inodes, 1540096 blocks
77004 blocks (5.00%) reserved for the super user
First data block = 0
Maximum filesystem blocks = 1577058304
47 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768,983 04, 163840,229 376, 294912,819 200, 884736

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 31 mountsor
180 days, whichever comes first. Use tune2fs-c or-I tooverride.

View the vg information and find that the remaining space is used up.
[Root @ sqing ~] # Vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 7
VG Access read/write
VG Status resizable
Max lv 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE/Size 636/19 .88 GB
Free PE/Size 0/0
Vg uuid I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH
[Root @ sqing ~] # Mkdir/home/data
[Root @ sqing ~] # Mount/dev/VolGroup00/lv_data/home/data
[Root @ sqing ~] # Df-h
Filesystem Size Used Avail Use % Mounted on
/Dev/mapper/VolGroup00-lv_root
9.7G 926 M 8.3G 10%/
/Dev/sda1 99 M 13 M 82 M 13%/boot
Tmpfs 506 M 0 506 M 0%/dev/shm
/Dev/mapper/VolGroup00-lv_data
5.8G 140 M 5.4G 3%/home/data

4. assume that a part of space (only 3 GB) needs to be squeezed out on the lv_data logical volume for convenient management to create a logical volume lv_develop. Therefore, you need to reduce lv_data and then create lv_develop. For the sake of data security, we recommend that you unmount the logical volume from the directory and then adjust the size.

To verify the test, create some files in the/home/data directory before uninstalling.

[Root @ sqing ~] # Cd/home/data/
[Root @ sqing data] # touch 001.txt 002.txt 003.txt
[Root @ sqing data] # ls # After lv_data is uninstalled, data exists in the/home/data Directory.
001. txt 002.txt 003.txt lost + found
[Root @ sqing data] # cd
[Root @ sqing ~] # Umount/home/data/
[Root @ sqing ~] # Ls/home/data/# After lv_data is uninstalled, the/home/data directory is empty.
[Root @ sqing ~] #

Narrow down the logical volume lv_data
Step: run the e2fsck command to check the partition, change the file system size, and then reduce the logical volume size.
[Root @ sqing ~] # E2fsck-f/dev/VolGroup00/lv_data
E2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/Dev/VolGroup00/lv_data: 14/770048 files (7.1% non-contiguous), 59982/1540096 blocks
[Root @ sqing ~] # Resize2fs-p/dev/VolGroup00/lv_data
Resize2fs 1.39 (29-May-2006)
The filesystem is already 1540096 blocks long. Nothing to do!
[Root @ sqing ~] # Lvresize-L 3G/dev/VolGroup00/lv_data
WARNING: switching active logical volume to 3.00 GB
This may destroy your data (filesystem etc .)
Do you really want to reduce lv_data? [Y/n]: y
Elastic Cing logical volume lv_data to 3.00 GB
Logical volume lv_data successfully resized

View the vg information and find that the 2.88G space is available.
[Root @ sqing ~] # Vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 8
VG Access read/write
VG Status resizable
Max lv 0
Cur LV 3
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE/Size 544/17 .00 GB
Free PE/Size 92/2 .88 GB
Vg uuid I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH

The logical volume of lv_data is 3 GB.
[Root @ sqing ~] # Lvscan
ACTIVE '/dev/VolGroup00/lv_root' [10.00 GB] inherit
ACTIVE '/dev/VolGroup00/lv_swap' [4.00 GB] inherit
ACTIVE '/dev/VolGroup00/lv_data' [3.00 GB] inherit

Create logical volume lv_develop
Step: create a new partition and format the new partition.
(Note: to absorb the lessons above, the size of the logical volume should not be set to 2.88 GB at once, but smaller. set it to 2.8 GB here)
[Root @ sqing ~] # Lvcreate-L 2.8G-n lv_develop/dev/VolGroup00
Rounding up size to full physical extent 2.81 GB
Logical volume "lv_develop" created
[Root @ sqing ~] # Mkfs. ext3/dev/VolGroup00/lv_develop
Mke2fs 1.39 (29-may-2006)
Filesystem label =
OS type: Linux
Block size = 4096 (log = 2)
Fragment size = 4096 (log = 2)
368736 inodes, 737280 blocks
36864 blocks (5.00%) reserved for the super user
First data block = 0
Maximum filesystem blocks = 754974720
23 block groups
32768 blocks per group, 32768 fragments per group
16032 inodes per group
Superblock backups stored on blocks:
32768,983 04, 163840,229 376, 294912

Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 33 mountsor
180 days, whichever comes first. Use tune2fs-c or-I tooverride.

Lv_develop is created and the size is 2.81 GB.
[Root @ sqing ~] # Lvscan
ACTIVE '/dev/VolGroup00/lv_root' [10.00 GB] inherit
ACTIVE '/dev/VolGroup00/lv_swap' [4.00 GB] inherit
ACTIVE '/dev/VolGroup00/lv_data' [3.00 GB] inherit
ACTIVE '/dev/VolGroup00/lv_develop' [2.81 GB] inherit

View the vg information at this time and find that there is still 64 MB of space available.
[Root @ sqing ~] # Vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 9
VG Access read/write
VG Status resizable
Max lv 0
Cur LV 4
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE/Size 634/19 .81 GB
Free PE/Size 2/64.00 MB
Vg uuid I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH

To make full use of space resources, add 64 MB of free space to lv_develop,

Expand the logical volume lv_develop
Step: first increase the size of the logical partition and then change the file system for resizing
[Root @ sqing ~] # Lvresize-L + 64 M/dev/VolGroup00/lv_d # add 64 M. Note that there is a plus sign before
Lv_data lv_develop
[Root @ sqing ~] # Lvresize-L + 64 M/dev/VolGroup00/lv_develop
Extending logical volume lv_develop to 2.88 GB
Logical volume lv_develop successfully resized
[Root @ sqing ~] # Resize2fs-p/dev/VolGroup00/lv_develop
Resize2fs 1.39 (29-May-2006)
Resizing the filesystem on/dev/VolGroup00/lv_develop to 753664 (4 k) blocks.
The filesystem on/dev/VolGroup00/lv_develop is now 753664 blockslong.
[Root @ sqing ~] # Lvscan
ACTIVE '/dev/VolGroup00/lv_root' [10.00 GB] inherit
ACTIVE '/dev/VolGroup00/lv_swap' [4.00 GB] inherit
ACTIVE '/dev/VolGroup00/lv_data' [3.00 GB] inherit
ACTIVE '/dev/VolGroup00/lv_develop' [2.88 GB] inherit

Check the vg information and find that all the remaining space is used up.
[Root @ sqing ~] # Vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 10
VG Access read/write
VG Status resizable
Max lv 0
Cur LV 4
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE/Size 636/19 .88 GB
Free PE/Size 0/0
Vg uuid I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH

Mount two logical volumes to the specified directory.
[Root @ sqing ~] # Mkdir/home/develop
[Root @ sqing ~] # Mount/dev/VolGroup00/lv_data/home/data/
[Root @ sqing ~] # Mount/dev/VolGroup00/lv_develop/home/develop/

Check the data in lv_data.
(Note: the space after lv_data is reduced must be greater than the space used before it is reduced)
[Root @ sqing ~] # Ls/home/data/#/home/data
001. txt 002.txt 003.txt lost + found
[Root @ sqing ~] # Ls/home/develop/#/home/develop directory is empty.
Lost + found

So far, LVM-related operations have been completed.
To enable the system to automatically mount logical volumes after restart, you need to mount lv_data and lv_develop information to/etc/fstab. the modified content of/etc/fstab is as follows:
[Root @ sqing ~] # Cat/etc/fstab
/Dev/VolGroup00/lv_root/ext3 defaults 1 1
LABEL =/boot ext3 defaults 1 2
Tmpfs/dev/shm tmpfs defaults 0 0
Devpts/dev/pts devpts gid = 5, mode = 620 0 0
Sysfs/sys sysfs defaults 0 0
Proc/proc defaults 0 0
/Dev/VolGroup00/lv_swap swap defaults 0 0
/Dev/VolGroup00/lv_data/home/data ext3 defaults 0 0
/Dev/VolGroup00/lv_develop/home/develop ext3 defaults 0 0

The above operations have been learned a year ago, but they are not often used, so they are unfamiliar. now I have to repeat and write this note.
In actual work, when a hard disk is added, you must first create a partition (primary partition, extended partition), format, and Mount before using the above commands for LVM management.

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.