Disk partition management mechanism ---- LVM

Source: Internet
Author: User

I don't know if my friends have ever experienced this kind of thing. You only gave/home a certain amount of space when planning hosts at the beginning. However, as the number of users and constantly store data in it, the directory space is not enough. What should you do?

You may choose to format a larger disk and copy the data in/home to the/home directory, but don't you think it will be very troublesome? Yes, LVM is a disk management mechanism that can help you solve this problem. Let's take a look at it.


LVM Introduction

LVM is short for logical volume manager. It is a tool that organizes one or more underlying devices into one advanced device. One of its important features is the ability to elastically adjust the capacity of the file system, rather than improving the performance of Data Reading. LVM can integrate multiple physical partitions to make them look like a disk partition, and can add or delete physical partitions at any time.

To better understand LVM, it is important to understand the LVM architecture:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/A0/wKiom1PLcOGxQtOEAABBcT0b7xo774.gif "Title =" lvm.gif "alt =" wkiom1plcogxqtoeaabbct0b7xo774.gif "/>


Physical Volume(Physical volume, PV)

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(Volume group, VG)

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.

Logical volume(Logical volume, LV)

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(Physical extent, PE)

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.


LVM commands


Physical Volume command

Pvchange changes the properties of physical volumes in a volume group.

Pvcreate creates a physical volume that can be used by the volume group.

Pvdisplay displays information about one or more physical volumes in a volume group.

Pvmove transfers the allocated physical area from one physical volume to another physical volume.

Volume Group Command

Vgcfgbackup saves the LVM configuration of the volume group

Vgcfgrestore restores LVM configurations to the volume group

Status of the vgchange switch volume group

Vgcreate creates a volume group

Vgdisplay displays the volume group information

Vgextend extends a volume group by adding physical volumes

Vgexport outputs a volume group from the system

Vgimport input a volume group to the System

System physical volume of the vgscan volume group

Vgreduce reduces the volume group by deleting one or more physical volumes

Vgremove deletes the definition of one or more volume groups from the system.

Logical image of vgsync synchronized to one or more failed volume groups

Logical volume command

Lvchange changes the attributes of a logical volume.

Lvcreate creates a logical volume in the volume group

Lvdisplay displays logical volume information

Lvextend adds the number of physical areas allocated to the logical volume

Lvlnboot sets the logical volume as startup, swap, or memory image volume

Lvmerge combines the volumes of previous images into a logical image volume

Lvreduce reduces the number of physical areas allocated to logical volumes

Lvremove remove one or more logical volumes from the volume group

Lvrmboot deletes logical volumes connected to boot, swap, or memory image volumes

Lvsplit divides the logical volume of the image into two logical volumes.

Lvsync synchronizes logical volume images on one or more Invalid logical volumes


Creating LVM

Next, I will introduce how to create LVM in practice.

Create a disk partition

Create a disk partition by using fdisk. When creating a partition, the size of a single partition is not required, as long as their sum can meet the requirements. Note that you must specify the system type of the partition when partitioning. Select 8e, Which is LVM.

Here we will create a 10g LVM as an example. Here we use the/dev/SDB disk for partitioning.

# Fdisk/dev/SDB command (M for help): n command Action E extended P primary partition (1-4) P specifies the primary partition (but LVM does not require this, both primary and logical partitions are supported.) Partition Number (1-4): 1 first cylinder (1-1044, default 1): Last cylinder, + cylinders or + size {K, M, g} (1-1044, default 1044): + 3G command (M for help): t change type selected partition 1hex code (type L to list codes ): 8e changed system type of partition 1 to 8E (Linux LVM)

Create another 7g partition in the same way and save and exit. Then, you can use fdisk-L to view partitions.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/A2/wKioL1PLft-RoVYpAAFStsnYVig459.jpg "Title =" partition success .jpg "alt =" wKioL1PLft-RoVYpAAFStsnYVig459.jpg "/>


Create physical volume

The created partition is used to store physical volumes. LVM can use physical volumes of different sizes.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/A2/wKioL1PLf3byCpgVAACAOD8phuU124.jpg "Title =" pv.jpg "alt =" wkiol1plf3bycpgvaacaod8phu124.jpg "/>

Run the following command to check the creation of a physical volume.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/A2/wKiom1PLfrDAFMa-AAGZisL2nC4732.jpg "Title =" pv.jpg "alt =" wKiom1PLfrDAFMa-AAGZisL2nC4732.jpg "/>


Create a volume group

The command to create a volume group first is very simple.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/3F/A2/wKioL1PLgTvSE9jQAABK4O8gZ90531.jpg "Title =" create vg.jpg "alt =" wkiol1plgtvse9jqaabk4o8gz90531.jpg "/>

View the basic information of the created VG

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/A2/wKioL1PLgVqRv1HxAAFGory4SEs849.jpg "Title =" 20.vg8.jpg "alt =" wkiol1plgvqrv1hxaafgory4ses849.jpg "/>

From the output, we can see the volume group usage/total. The physical volume provides space for the volume group. As long as there is space available in this volume group, we can create logical volumes at will.


Create logical volume

You can specify some options when creating a logical volume.

Lvcreate

Name-N lv_name

Size-L # unit {mmggtt}

LV access path:

1./dev/vg_name/lv_name

/Dev/myvg/mylv


2./dev/mapper/VG_NAME-LV_NAME

/Dev/mapper/myvg-mylv

Here we will create a 5g LV, and then view it.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/A2/wKiom1PLhDuBuUUZAAGWIqgSTUE756.jpg "Title =" lv.jpg "alt =" wkiom1plhdubuuzaagwiqgstue756.jpg "/> so that our logical volume is ready, you can format and mount the logical volume. Once the logical volume is mounted, we can read and write data on the mount point. Note that if you want to delete this logical volume, you must uninstall it first.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/A2/wKioL1PLhsqTATpzAAIbfmfMevM153.jpg "Title =" lattice mounted 1.jpg "alt =" wkiol1plhsqtatpzaaibfmfmevm153.jpg "/>


LVM Extension

The most useful feature of LVM is to adjust the logical volume size. We must unmount the extension before it is available.

Note that we must first expand the physical boundary before expanding the logical boundary.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/A3/wKioL1PLiLvDyQ08AAIr9XOB-iA931.jpg "Title =" exten.jpg "alt =" wKioL1PLiLvDyQ08AAIr9XOB-iA931.jpg "/> continue to use lvdisplay to view the status of the logical volume, you can find that its capacity has changed to 6 GB.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/A3/wKioL1PLiV2g5CacAAEP-DhznA0177.jpg "Title =" extended 2.jpg "alt =" wKioL1PLiV2g5CacAAEP-DhznA0177.jpg "/>

Now, this logical volume can be mounted again, and this method can also be used for other partitions.


LVM reduction(You also need to unmount the package first)

Reduction is a very dangerous operation, so you must be careful. Below are several necessary steps.

1. Determine the target size after the reduction, and make sure that the target logical volume has enough space to accommodate all the original data;

2. First, sanction the file system and perform forced detection.

E2fsck-F

3. Reduce logical boundaries

Resize2fs Device

4. Reduce physical boundaries

Lvreduce

Note: If the size of the logical volume is smaller than the size of the stored data, the data stored in the logical volume will be lost.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/A3/wKioL1PLizmyT4RvAAKjpqqUl1I341.jpg "Title =" ..jpg "alt =" wkiol1plizmyt4rvaakjpqqul1i341.jpg "/>

Continue viewing to confirm

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/A3/wKioL1PLi7aSnGUSAAEvh3WmDpk862.jpg "Title =" thumbnail 0.jpg "alt =" wkiol1pli7asngusaaevh3wmdpk862.jpg "/>

In this way, our reduction is successful.


LVM snapshot function

A very important feature of LVM is system snapshot. A snapshot records the system information at a certain time point, just like a photo record. If any data changes in the future, the original data will be moved to the snapshot area. if no changes are made, the data will still be in the original LVM.

Therefore, the snapshot function of LVM plays an important role in data backup. Of course, the snapshot area must be able to accommodate the size of the original data that has changed for a certain period of time, so it is necessary to estimate the data.

Lvcreate

-L: Size

-N: Name

-S: Description

-P: access permission

Note: The snapshot volume is a logical volume. Therefore, it must be in the same volume group as the target logical volume. You do not need to specify a volume group;

650) This. length = 650; "src =" http://s3.51cto.com/wyfs02/M02/3F/A3/wKioL1PLkviTmG4TAACo4XVwYEY144.jpg "Title =" sp.jpg "alt =" wkiol1plkvitmg4taaco 4xvwyey144.jpg "/> note that after a snapshot volume is created, it is in the same directory as the logical volume it points ,, both mysnap and mylv are under myvg.



Disk partition management mechanism ---- LVM

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.