How LVM2 works in Linux and how to create LVM2

Source: Internet
Author: User

How LVM2 works in Linux and how to create LVM2

How LVM2 works in Linux and how to create LVM2

I. LVM principles

[MD]: Multi Device Multi-Device

Mdadm is a user space tool and a RAID management tool, which has little to do with RAID. Real RAID integration in Linux Kernel

[DM] Device Mapper Device ing is also a common management mechanism in linux.

Similar to MD, DM can also provide a logical device.

DM is more powerful than MD. Because DM not only provides the RAID function of MD, it is also the basis of the logical volume (LVM2), and we can also implement RAID0 and RAID1 Based on the logical volume. Such a function. Some functions of all DM and MD overlap, but not completely. Therefore, we usually use MD for RAID and DM for LVM2.

Is the kernel architecture of DM. That is, how DM works.

Like MD, DM also has a core module, which is composed of many sub-modules. For example, in MD, the cores of MD include RAID0, RAID1, RAID5, and RAID1 + 0. Different Levels are completed by multiple sub-modules. DM also has multiple sub-modules to complete different organization methods.

For example:

Linear: similar to Jbod. The storage space of multiple disks can be spliced together and used as one disk.

Mirror: similar to an image.

Snapshot: Like a VM, a Snapshot can retain the status of data when a Snapshot is created. A snapshot is another path for accessing the same data. (Mainly used for data backup)

Multipath: multi-path. The multi-path must be configured by the DM module, so that the path of the data storage device can be searched through multiple and different lines.

RAID: only to avoid service termination or data loss caused by hardware damage. Human error operations cannot be ensured, resulting in data loss. Generally, data is backed up even after RAID.

In addition to snapshots, DM also supports Dynamic Disk size reduction.

For example, how fast is the physical disk available? DM can organize multiple disks into a large logical device. if it finds that the memory is insufficient, you can add a hard disk to expand the device, if it is found that the space is too large, you can remove a hard disk to reduce the space. For example:

Note: This logical device is just equivalent to an extended partition. We can create a new partition on this extended partition. Therefore, we want to use this logical device to store data, you must create a logical partition on this logical device. This logical partition is a truly scalable partition. For example

 

[PV]: Physical Volume

[VG]: Volume Group

[LV]: Logical Volume

The size of the VG (volume group) Boundary depends on the size of the PV (physical volume) combination.

Although PV is combined into multiple VG, LV (logical volume) can be divided into multiple LV (logical volume) on VG, however, the sum of these multiple LV values cannot exceed VG. If the VG is not enough, you can increase the PV (physical volume). If the space in the LV is insufficient, you can expand the LV.

Creating a snapshot for a volume is actually creating a snapshot for a logical volume, and the snapshot volume must be in the same volume group as the logical volume. This means that if you want to create a snapshot of an LV logical volume, you must set aside space in the same volume group to create a snapshot volume for a logical volume.

Why can we implement VG extension and LV extension? In fact, when PV is put into VG, We need to divide all the space of the physical volume of PV into one physical block and one storage unit. This block is not a file system block, but a PE (PhysicalExtend physical disk area). As long as the physical volume is added to the VG, the PE size in the new volume must be the same as that in the VG. This document describes how much PE the VG uses when creating the VG.

[PE] (Physical Extend) Physical disk area, referred to as Disk Area

Therefore, the VG volume group is composed of a large number of PES. Then, the creation of the LV logical volume is how many PES are allocated to the LV by the VG. The LV size depends on how many PES the VG assigns to the LV. On LV, these PES are called LE (LoglcalExtend Logical Disk Area ). Therefore, the extension on LV is equivalent to adding several PEs, which can achieve the purpose of expanding PES.

If RAID1 + 0 is used to establish a logical volume on the basis of RAID, even if the hardware is damaged, the LVM performance will not be affected. This ensures data security, the disk area can also be expanded.

Ii. Create LVM

[Operating system:] Red Hat 6.3

[Related commands :]

Pvcreate: create a physical volume as pv

Vgcreate: combines pv into vg

Lvcreate: Create lv on vg

First, create VG.

In this example, create a 10g vg.

First, we need to create a 10G vi. We can use 10 1G or a 2G, 3G, or 5G pv.

In order to simulate, the disk partition is used here to simulate the hard disk to create the vg

First, prepare a 2, 3, and 5g partition. To simulate three hard disks.

You can use fdisk to create partitions. This document does not describe how to create partitions. For details, see "How to Create RAID in linux".

Here, we have created sdb10 for 2G, sdb11 for 3G, and sdb12 for 5G for other demos. Here we also created a 7G sdb13.

Note: After creation, you must change the partition type to Linux LVM. the Linux LVM type in the system is 8e.

Then

# Partprobe/dev/sdb

Re-read the sdb partition table. If it fails, use partx-a/dev/sdb to re-read it.

Enter cat/proc/partitions to verify whether the partition can be used.

# Cat/proc/partitions

Then, create these partitions, that is, the physical volume, as pv.

Step 1: Create a PV

# Pvcreate/dev/sdb {10, 11, 12, 13}

Create sdb10, sdb11, sdb12, and sdb13 as pv

Use pvs to view the pvs on the current system

# Pvs

Here we can see that we have just created the pv, in which sda2 was created by the system itself.

The above equipment has some errors. These errors are normal as long as they are within 20%.

You can use pvdisplay to view the detailed information of a pv. You can add the device name to view the information of a pv.

# Pvdisplay/dev/sdb10

Step 2: Create vg

Create a vg and use the vgcreate command

# Vgcreate myvg/dev/sdb {10, 11, 12}

Create sdb10, sdb11, and sdb13 as a vg named myvg

You can use vgs to view the vg devices on the system.

# Vgs

Vg_gdy is created by the system itself .; Myvg is the vg we just created

You can use vgdisplay to view the detailed information of vg.

# Vgdisplay/dev/myvg

Here we can see that the size of myvg is 10.02 GB, which is just the same as the size of the pv of the 2G, 3g, 5g that we created earlier.

Here we can see that the pe size of myvg is 4 mb, and the total number of PES is [total pe]. Currently, the idle PE [Free pe]

You can use-s to specify the size of the disk PE when creating the vg.

 

You can use vgremove to delete a vg.

# Vgremove myvg

You can delete the created myvg.

Using vgs, you can no longer find the created myvg.

Create a vg with a pe size of 8 Mb.

# Vgcreate myvg-s 8 M/dev/sdb {10, 11, 12}

Use vgdisplay to view the details of myvg

# Vgdisplay myvg

The PE size is 8 Mb.

Reduce vg. Note that the process of reducing vg is equivalent to removing a pv (physical volume ). If the vg that is just removed has data, the system will be damaged. Generally, you need to use pvmove to move the data in the pv to other devices when reducing the vg. For example, to remove the sdb11 pv, you must first use pvmove and then use vgreduce to remove the sdb11 device.

Move data on sdb11 to another device

# Pvmove/dev/sdb11

Because no logical partition is created on pv, no data is found on mypv. Therefore, no data is displayed when the partition is removed. Then, we can remove this pv from myvg.

Remove sdb11

# Vgreduce myvg/dev/sdb11

Use vgs to view the vg size again

# Vgs

Now we can see that the vg size has been reduced by 10 Gb by a 3G sdb11, and then only 7 GB.

View with pvs

# Pvs

At this time, sdb11 does not belong to any vg.

When a pv does not belong to any vg, you can use pvremove to delete the pv.

Delete sdb11

# Pvremove/dev/sdb11

If you input pvs again, the previous sdb11 cannot be found.

Expand myvg

# Vgextend myvg/dev/sdb13

Add the previously created sdb13 to myvg.

Then use vgs to view the results. We can see that the pv of sdb13 reduced to 7 GB by adding a 7G myvg, and then the pv size changed to 14 GB.

Note: Once a pv is added to an existing vg, the PE size of the physical disk area on the pv is automatically determined to keep the size consistent with that of the physical disk area in the vg.

View the physical disk size of sdb13

# Pvdisplay/dev/sdb13

We can see that the physical disk size on the pv is 8 M in the same size as the physical disk size on the previous vg.

 

Step 3: Create an lv on vg

Create a logical volume of about 50 MB)

# Lvcreate-L 50M-n testlv myvg

That is, create a 50 m lv named testlv on myvg

Use lvs to view the lv ON THE SYSTEM

# Lvs

We can see that the actual size of testlv we created is 56 mb. This is because of the PE size. The system must allocate the lv size according to the whole PE, therefore, the size of testlv must be an integer multiple of PE.

You can use lvdisplay to view detailed lv information.

# Lvdisplay/dev/myvg/testlv

Use mke2fs to create a file system for testlv

# Mke2fs-j/dev/myvg/testlv

# Mount textlv to the/ftpftp/gandian/gz2 directory

# Mount/dev/myvg/testlv/ftpftp/gandian/gz2

Now, testlv has been mounted and can be used.

Use mount to view mounting information

# Mount

Here we can see that the mounted testlv is/dev/mapper/myvg-testlv. In fact,/dev/mapper/is the real device file, in/dev/myvg/testlv, it is just a link file.

# Ls-la/dev/myvg/testlv

The lv cannot be mounted before the lv is removed,

Uninstall testlv

# Umount/dev/myvg/testlv

Remove testlv

# Lvremove/dev/myvg/testlv

You will be prompted that there may be files on the device. Are you sure you want to remove them? Enter y.

# Lvs

Textlv just now cannot be found.

The lv creation and basic operations are complete.

 

Iv. lv Extension

First, the process of creating a partition is actually the process of creating a physical border. Creating a file system for a partition is equivalent to creating a logical boundary for the partition. In fact, the logical boundary is close to the physical boundary. To expand a lv, you must first expand its physical boundary, and then expand its logical boundary, that is, the file system boundary. Otherwise, you must first reduce the logical boundary (that is, the boundary of the file system) and then the physical boundary.

Generally, the risk of expansion is small, and the risk of reduction is large. Expansion and reduction steps cannot be reversed. Otherwise, the file system will be damaged.

Create a 2 GB logical Volume lv named testlv

# Lvcreate-L 2G-n testlv myvg

Format testlv

# Mke2fs-j/dev/myvg/testlv

Mount testlv

# Mount/dev/myvg/testlv/ftpftp/gandian/gz2

View mounting information

# Mount

Copy an object to this directory.

# Cp/etc/inittab/ftpftp/gandian/gz2

View disk Information

# Df-lh

We can see that the size of testlv is 2 GB. Now we can extend it to 5 GB.

Step 1: Expand the logical border of lv

# Lvextend-L 5G/dev/myvg/testlv

Note: Before expansion, make sure that there is sufficient space available in vg.

Use df-lh and lvs to view and compare

We can find that the size of testlv has been extended to 5 GB, but the information on the mount is still 2 GB. This is because the file system has not been extended yet. That is, the logic boundary has not been extended.

Step 2: Expand the logical boundary (that is, extend the boundary of the file system)

# Resize2fs-p/dev/myvg/testlv

This is to extend the file system to the maximum available range.

Now use df-lh and lvs to view and compare

5G is displayed here, that is, the expansion is complete.

Therefore, the expansion of the logical volume does not affect the use of the original files, and the risk is very small, and the file system can be expanded even if it is in the Mount state without uninstalling.

 

Fifth, lv reduction

As explained above, the order of lv reduction is the opposite of that of lv extension. To reduce the logical boundary, you can use resize2fs.

Note: the risk of reducing logical volumes is very high.

First, do not scale down online. uninstall it first;

2. Ensure that the reduced space size can still store all the original data;

Third, check the file system to ensure that the file system is in a consistent state.

After the file system is reduced, you can use lvreduce to reduce the lv (logical boundary) size.

In this example, the 5Gtestlv is reduced to 3 GB.

Step 1: uninstall testlv

# Umount/dev/myvg/testlv

X you can use mount to check whether the unmount is complete.

Step 2: forcibly check the file system of testlv

# E2fsck-f/dev/myvg/testlv

If you do not ask after the check, you can continue the next step:

Step 3: Reduce the file system boundary to 3 GB

# Resize2fs/dev/myvg/testlv 3G

Step 4: Reduce the physical border of testlv to 3 GB

# Lvreduce-L 3G/dev/myvg/testlv

The prompt here is that the file may be damaged. If the file is damaged, you are responsible for it. Enter y here.

The device can be mounted if no problem occurs.

Step 5: Mount testlv

# Mount/dev/myvg/testlv/ftpftp/gandian/gz2

After mounting, you can use ls to view the file and find that the previously copied file still exists and is not damaged.

Use df-lh and lvs to view information about testlv.

The size of the testlv disk is 3 GB, that is, the lv is successfully reduced.

 

Vi. Create an lv snapshot volume

This example uses testlv as an example to create a snapshot volume for testlv.

Note: Because snapshot volumes cannot be modified, they are generally created in read-only mode.

When creating a snapshot volume, you still use lvcreate. However, you must use the-s parameter to specify the created snapshot volume.-p can specify the permission.

Note: The snapshot volume must be in the same volume group as the source volume. Snapshot volumes are used for backup. After a snapshot volume is created, it is backed up. Generally, the snapshot volume is meaningless. Can be deleted.

The lifecycle of the snapshot volume is the entire data duration. During this period, the data growth cannot exceed the snapshot volume size. If the snapshot volume size is exceeded, the changed data of the file exceeds the size of the snapshot volume. The changed data cannot exist in the snapshot volume. If this happens, the snapshot volume will discard all the operations and cause self-corruption. In this way, your previous backup will be suspended. To ensure security, the snapshot volume size can be as large as the original volume size. In this way, the data volume is too large to exceed the size of the original volume, but it is generally not recommended to do so.

 

Create a 50 m snapshot volume for textlv

# Lvcreate-L 50 M-ntestlv-snap-s-p r/dev/myvg/testlv

Use lvs to view snapshot volumes

# Lvs

Mount the snapshot volume to/ftpftp/gandian/kz/

# Mount/dev/myvg/testlv-snap/ftpftp/gandian/kz/

After mounting, you can find that the files in the snapshot volume are the same as those in the testlv directory.

For example, if you create a file in testlv, the file in the testlv-snap snapshot volume is not changed.

Back up snapshot volumes

# Tar-jcf/ftpftp/gandian/gz2.tar.bz2/ftpftp/gandian/kz/

View backup file information

# Tar -jtfgz2.tar.bz2

Detach a snapshot volume

# Umount/dev/myvg/testlv-snap

Delete snapshot volumes

# Lvremove/dev/myvg/testlv-snap

You can use lvs for verification after deleting the snapshot volume.

Now, the snapshot volume creation and backup are demonstrated.

--- Add ----------------------------------------------------

Lvm details

[Pv] Physical Volume

[Vg] Volume Group VG Volume Group

[Lv] logical Volume

Create pv and view pv commands

Pv: note that pv is composed of Disk Partitions. The partition format is (8e (linux ))

[Pvcreate] Create pv

For example:

Pvcreate/dev/sda1/dev/sda2 // you can create sda1 and sda2 devices as pv devices.

[Pvremove] Delete pv

[Pvscan] scan pv (scan the number of physical PVS on the System)

[Pvdisplay] view pv details on the System

[Pvs] view pv devices on the current system (simple view)

[Pvmove] Move pv

Vg: Note: vg is composed of pv, which is equivalent to a disk to the system.

[Vgcreate] Create vg and create pv as vg

For example:

Vgcreatemyvg/dev/sdb1/dev/sdb2 // create the disks sdb1 and sdb2 that have been created with pv into vg named myvg

Parameters:

[-S #] the size of the specified Disk Area (PESize) is 4 MB.

For example:

Gcreate-s 8 M myvg/dev/sdb {5, 6} // create a device named myvg using sdb5, which has been created by pv, consisting of sdb6, and the size of the PE in the disk area is 8 M

[Vgremove] Remove vg

Example: # vgremovemyvg // Delete myvg

[Vgmove] Move the physical disk area to another device # pvmove/dev/sdb5 // remove the data on the physical disk area sdb5 (this physical disk area will be unmounted after convenience)

[Vgdisplay] view the detailed information of the vg Device

[Vgs] view the basic information of the vg Device

[Vgscan] How many vg devices are scanned on the System

Vgreduce: scale down vg # vgreduce myvg/dev/sdb5 // remove sda5 from myvg (that is, the space of myvg will be reduced)

# Pvremove/dev/sdb5 (you can then remove the pv disk device sdb5)

[Vgxtend] expand vg # vgextendmyvg/dev/sdb7 // expand the disk sdb7 with the pv of the myvg Device

# Pvcreate/dev/sdb7 (you must create one before the above)

 

Lv: Note: The lv belongs to vg and must be created in vg.

[Lvcreate] Create lv on vg

 

Parameters:

-N: Specifies the name.

[-L]: Specifies the space size.

For example, lvcreate-L 50 M-n testlv myvg // create an lv in myvg named testlv and the size is 50 M

Mke2fs-j/dev/myvg/testlv // format testlv. The formatted file system is ext3

Mount/dev/myvg/testlv/mnt // mount testlv to the mnt directory

[-P]: Specifies the permission.

-S: Specifies the snapshot volume.

[Create a snapshot volume]

1. The lifecycle is the entire data duration. During this duration, the data growth cannot exceed the snapshot volume size:

2. the snapshot volume should be read-only:

3. the snapshot volume and the original volume are in the same volume group.

Syntax [lvcreate-L #-n SLV_NAME-p r/PATH/TO/LV]

# Lvcreate-L 50 M-n lv1-snap-s-p r/dev/myvg/lv1 // create a size of 50 M for the lv1 device named: lv1-snap, snapshot volume with read-only permission

# Mount/dev/myvg/lv1-snap/ftpftp/gandian // mount the snapshot volume under the gandian folder

# Tar-jcf/beifeng.tar.bz2/ftpftp/gandian // set the content in the Quick View to the beifeng.tar.bz2 File

# Umount/dev/myvg/lv1-snap // uninstall the snapshot volume

# Lvremove/dev/myvg/lv1-snap // remove the snapshot volume (to delete the snapshot volume)

Related commands

Lv: Note: The lv belongs to vg and must be created in vg.

[Lvremove] Remove # lvremove/dev/myvg/testlv // remove testlv

# Lvremovelvremove/dev/mapper/myvg-testlv // You can also remove testlv (Note:/dev/myvg/testlv is only a connection file, real file in/dev/mapper/myvg-testlv)

[Lvs]

[Lvdisplay]

Lvcan]

Lvextend

For example: # lvextend-L 7G/dev/myvg/testlv // extend the physical boundaries of testlv to 7G

# Resize2fs-p/dev/myvg/testlv // extend the file system boundary of testlv to the maximum (that is, the physical boundary is used as much as possible)

[Lvreduce] Zoom out

Note: 1. It cannot be reduced online and must be uninstalled first;

2. Make sure that the reduced space size can still store all the original data:

3. Check the file before reducing the size to ensure that the file system is in a state of perfection.

For example:

# Df-lh // view the file system size and used size

# Umount/dev/myvg/testlv // uninstall the testlv Device

# E2fsck-k/dev/myvg/testlv // forcibly check the file system of the testlv Device

# Resize2fs/dev/myvg/testlv 1G // reduce the file system size to 1G

# Lvreduce-L 1G/dev/myvg/testlv // reduce the physical boundary of testlv to 1G

# Mount/dev/myvg/testlv/users // mount

[Lvresize] can be expanded or reduced.

1. Extend the logical volume:

[Lvextend] logical volume of the extended Disk

-L [+] #/PATH/TO/LV

[Resize2fs] logical editing of extended file systems

Resize2fs/PATH/TO/LV 5G

For example: # lvextend-L 7G/dev/myvg/testlv // extend the physical boundary of testlv to 7G

# Resize2fs-p/dev/myvg/testlv // extend the file system boundary of testlv to the maximum (that is, the physical boundary is used as much as possible)

Ii. logical volume reduction:

Note: 1. It cannot be reduced online and must be uninstalled first;

2. Make sure that the reduced space size can still store all the original data:

3. Check the file before reducing the size to ensure that the file system is in a state of perfection.


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.