Linux LVM logical volume, linuxlvm Logic

Source: Internet
Author: User

Linux LVM logical volume, linuxlvm Logic
Overview

LVM is generated because the traditional partitions cannot be expanded online once the partitions are well organized. There are also some tools that can expand the space online, but still face the risk of data corruption; when the traditional partition space is insufficient, the general solution is to create a larger partition to unmount the original partition and then copy the data to the new partition, however, in enterprise production systems, the downtime is often not allowed or the downtime is allowed for a short period of time. LVM can effectively solve the problem of online space expansion without affecting data, LVM can also use snapshots to ensure consistency between log files and tablespace files at the same time point during backup. This article will not provide a detailed introduction to concepts, this section describes how to resize, contract, and remove LVM instances.

 

Principle

PE (Physical Extend ):The minimum unit of the volume. The default size is 4 MB. Just as our data is stored as pages, the volume is stored as PE.

PV (Physical Volume): Physical volume. If you want to use a logical volume, the first step is to format the disk into a PV. It can be seen that PV protects PE, the number of PES in PV depends on the disk capacity/4 M.

VG (Volume Group): Volume group, VG is to combine many PES to generate a volume group, of course, the PE here can be cross-disk, if the current server disk space is insufficient, you can add a new disk without affecting the current system.

LV (Logical Volume ):The logical volume is used by the user. The first few are preparations for creating the logical volume. The size of the created logical volume must not exceed the remaining space of the VG.

Steps:

1. PV: format the disk

2. VG: Create a volume group

3. LV: Create a logical volume

4. mkfs: format the logical volume

5. mount: mount the logical volume.

Create LV

1. PV (format disk ):Pvcreate command

The first step is to format the disk. do not perform this operation on the existing partitioned disk.

First, check the current system disk information. I just added two 20 GB disks through the Virtual Machine, sdb and sdc, and used fdisk-l to view the information.

fdisk -l

Format/dev/sdb

pvcreate /dev/sdb /dev/sdc

View pv information using pvdisplay or pvs

You can obtain the PV size and remaining space information from the available information.

2. VG (create volume group): vgcreate command

vgcreate vggroup /dev/sdb /dev/sdc

Vggroup is the volume group name.

Use vgdisplay and vgs to view vg Information

 

You can view the vg size and PE size through vgdisplay. The default size of a single PE is 4 MB. The current VG has 10238 PES in total, and the current vg uses two PVS in total.

3. LV (create logical volume): lvcreate-n logical volume name-L logical volume size volume group name

Remember that the size of the created logical volume cannot exceed the remaining size of the volume group. I will create a 30 GB logical volume here.

lvcreate -n lv -L 30G vggroup

Use lvdisplay and lvs to view lv Information

 

The lv information obtained by lvdisplay includes the lv name and path, the vg group name of the lv, the lv size, and the number of PES contained in the current lv.

The lv file path is saved under vg.

4. mkfs (format lv)

mkfs.ext4 /dev/vggroup/lv

5. mount (mount LV)

mount -t ext4 /dev/vggroup/lv /mnt

Use df-h to view mounting information

Expand logical volume (LV)

Before expanding the capacity of the LV (logical volume), check the remaining space in the VG (volume group). The Expanded size cannot exceed the remaining space of the VG.

vgdisplay

The remaining space of the current VG is 9.99 GB.

1. lvextend (Extended logical volume)

To demonstrate that online expansion does not affect files, I first created a text test file in the/mnt directory with the content "1"

lvextend -L +9.99G /dev/vggroup/lv

All the remaining space of VG is divided into LV

The remaining space of vg is changed to 0.

The LV size is changed to 39.99 GB.

2. resize2fs: update the File System

resize2fs  /dev/vggroup/lv

Check whether text files in the/mnt directory are affected

File is not affected.

Expand volume group (VG)

At present, my system VG has no space left. If the system space is insufficient at this time, we need to add a disk to increase the VG size. In the virtual machine, I add a new 10 GB disk sdd.

1. format the new disk (pvcreate)

pv /dev/sdd

2. Add the formatted PV to VG (vgextend)

vgextend vggroup /dev/sdd

3. view the current vg size (vgdisplay)

 

The size of the current VG is changed to 50 GB, the remaining space is also changed to 10 Gb, And the PV Number of the current VG is changed to 3

Shrink logical volume (LG)

Note: Before shrinking a logical volume, you must unmount the logical volume and then shrink the file system. The logical volume is reduced, and the size of the logical volume cannot exceed the remaining space.

1. unmount a logical volume)

umount /mnt

2. shrink the File System

This step must be performed before the LV contraction.

First check the file system (e2fsck)

e2fsck -f /dev/vggroup/lv

Resize2fs)

Reduce the file system to 30 GB. If you have been careful, you will find that the lvdisplay file system is 39.9 GB, And the df-h file system is 40 GB, here, the size of df-h is used as the next step is to reduce the LV, so the next step of Data Consistency is-10G.

-- Reduce the file system to 30Gresize2fs/dev/vggroup/lv 30G

3. LV (lvreduce) Reduction)

lvreduce -L -10G /dev/vggroup/lv

Note: The three steps cannot be confused.

Next, mount the lV volume to check whether the space is modified.

mount /dev/vggroup/lv /mnt/

We can see that the text file still exists after the remount, And the lv size is changed to 30 GB.

 

Although the LV can be reduced, it is not recommended to do so, especially for enterprise systems, because there will be many unknown results.

Shrink volume group (VG)

You can only perform operations on those that do not use the pv. If your current PV is already used in the logical volume, the PV cannot be removed, for example, if the newly added sdd disk is not used, it can be removed from the VG.

View the PVS used by PVS.

pvs

Currently, only/dev/sdd is not used.

1. Uninstall the logical volume

The logical volume corresponds to the volume group rather than the PV. Although the operation before sdd is not added to the LV, the LV needs to be detached first.

umount /mnt

2. Remove PV (vgreduce)

vgreduce vggroup /dev/sdd

3. Delete pv

pvremove /dev/sdd

We can see that/dev/sdd has been removed.

 

Delete lv, vg, and pv

1. Uninstall all the lv instances before deleting them. The current system has only one lv.

umount /mnt

 

2. lvremove, vgreomve, pvremove

-- Delete LVlvremove/dev/vggroup/lv -- delete VGvgremove vggroup -- delete PVpvremove/dev/sdb/dev/sdc

These Delete Operations ensure that they are not used. For example, to delete a LV, copy the data before deleting it. To delete a VG, ensure that no LV exists in the VG group. Delete PV as mentioned earlier, PV needs not to be used.

 

Mount content reference: http://www.cnblogs.com/chenmh/p/5097530.html

Partition related content reference: http://www.cnblogs.com/chenmh/p/5096592.html

This chapter content reference from: http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/24/3097785.html

Summary

LVM not only brings about online resizing, but also uses LVM to ensure consistency between log files and tablespace files during the backup process, we will discuss in detail the backup-related content later.

 

 

 Don't try to vote for your sacred vote !!!

Note:

Author: pursuer. chen

Blog: http://www.cnblogs.com/chenmh

All essays on this site are original. You are welcome to repost them. However, you must indicate the source of the article and clearly provide the link at the beginning of the article. Otherwise, you will be held accountable.

Welcome to discussion

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.