LINUX Logical Volume Management: LVM

Source: Internet
Author: User

1. What is LVM

LVM is a logical volume management tool that has two versions, LVM1 and LVM2. Native support in 2.4 kernal and 2.6kernal respectively.

LVM provides management capabilities for higher logical levels of disk partitioning. The storage volumes can be re-partitioned and moved through LVM.

LVM can name a logical volume custom named development, sales, etc., instead of "SDA" "SDB"

2. A case description of the benefits of LVM: Joe's PC

As an Example:joe buys a PC with an 8.4 Gigabyte disk on it and installs Linux using the following partitioning system:

/BOOT/DEV/HDA1 megabytesswap/dev/hda2 megabytes//dev/hda3 2 Gigabytes/home/dev/hda 4 6 gigabytes

When Joe needs to install Office suit, there is not enough 2G space in the root partition. There are only a few things Joe can do at this time

1. Format the hard disk, repartition it

2. Buy a new hard drive, create a new partition, move the data to complete the installation 3. Make a link to the root partition, point to/home, and complete the installation in/home

What's the difference if you use LVM?

/BOOT/DEV/HDA1 megabytesswap/dev/vg00/swap megabytes//dev/vg00/root 2 gigabytes/ho Me/dev/vg00/home 6 gigabytes

At this point when you need to install Office suit, you need to reduce/dev/vg00/home space, the space allocated to/root.

650) this.width=650; "src=" Http://img.baidu.com/hi/face/i_f42.gif "alt=" i_f42.gif "/> Boot is not included on the LV because bootloaders don ' t understand LVM volumes yet. It's possible boot on LVM would work and you run the risk of have an unbootable system.

Joe's home directory is full, so he bought a new 20G hard disk, need to format the hard drive to get/dev/sdb1 and then the home/home data full copy past, then the new disk mounted to/home, and then the past home/home data link such as/home/joe/ Old-mp3s

But if he uses LVM, he can directly add the disk to the existing volume group, and then directly expand the/home logical volume space, do not need to do such a troublesome thing.

Structure of the 2.LVM (PV VG LV)


    hda1   hdc1       (pv:s on  Partitions or whole disks)                                  \   /                                                                                \ /                                                                                diskvg         ( VG)                                                                  /  |  \                                                                            /   |   \                                                                       usrlv rootlv varlv  (lv:s)     |      |     |                                                                   ext3     ext4  xfs  (filesystems)

VG Volume Group:

The VG is the abstraction level at the highest level of LVM, which puts logical volumes and phsical volumes in a storage unit

PV Physical Volume:

A physical volume, usually a hard disk, or a raid group that looks like a hard disk

LV Logical Volume (LV):

Similar to disk partitioning, for system-visible standard block devices, which can include file systems such as/home

PE physical Extent

Each physical volume PV is divided into a basic unit called the PE (physical extents), with a uniquely numbered PE being the smallest unit that can be addressed by LVM. The size of the PE is configurable and defaults to 4MB.

logical extent (LE)

Le and PE are the corresponding addressing units, consistent with the size of the PE in the VG.

650) this.width=650; "src=" Http://img.baidu.com/hi/face/i_f42.gif "alt=" I_f42.gif "/> The process of a hard disk corresponding to the LV file system: HDD → Partition → (pv→vg→lv) → format (LV for ext file system)→ mount

We have a volume group VG1, which has a physical extent size of 4MB. In this volume group we introduce 2 hard disk partitions,/dev/hda1 and/dev/hdb1. These 2 partitions will become physical volumes PV1 and PV2. PV corresponds to the PE is divided into 4MB chunks, assuming PV1 divided into 99, PV2 to 248. At this point we can create a logical volume. It can be any size in 1-347 (248+99). For example, the creation of a logical volume LV, actually do is the map of Le and PE, for example, you can map le 1 to the 51 of the PE, it means that the first 4MB chunk of the LV write, is actually written from the 51st chunk of PV1.

650) this.width=650; "src=" Http://img.baidu.com/hi/face/i_f42.gif "alt=" i_f42.gif "/> Mapping mode: Two

linear mapping: corresponds a group of PE to an area le 1-99 map to PV1 , le 100-347 map onto PV2

Isometric Correspondence: Staggered Distribution

1st chunk of le[1], pv1[1],2nd chunk of le[1], pv2[1],3rd chunk of le[1], pv3[1],4th chunk of le[1], P V1[2],

650) this.width=650; "src=" Http://img.baidu.com/hi/face/i_f42.gif "alt=" I_f42.gif "/>Snapshot

Snapshot (snapshot) is an extremely beautiful feature provided by LVM. Its principle is the "copy-on-write (Cow-copy-on-write)" technique, which replicates the original volume's metadata (metadata) to create a logical volume, and does not replicate any data on the physical volume, so its creation process is instantaneous in real time. A snapshot is a special type of logical volume that contains the full data of the original logical volume specified at the time of creation, and you can manipulate the snapshot without worrying about changes to the data to invalidate the backup.

LVM snapshots Track and maintain data using a technique called write-time replication (cow-copy-on-write)
The consistency. Its simple principle is to track changes in the block on the original volume, before the data is changed
It is copied to the snapshot's own reserved space (as the name implies, copy-on-write). When the snapshot is read, the
The modified data is read from the snapshot's reservation space, and unmodified data is redirected to the original volume for reading, so
There is a layer of cow devices between the file system of the snapshot and the device.

This snapshot, though it looks pretty good, actually has a lot of limitations. The first is performance.

"So-a write to a normal LV was just a write, but a write-to-a snapshotted LV (or an LV snapshot) involves reading the Origi NAL data, writing it elsewhere and then writing some metadata on it all.
This quite obviously impacts performance, and due to device mapper have a very basic implementation, it is particularly  Bad. My tests show synchronous sequential writes to a snapshotted LV is around 90% slower than writes to a normal LV. "

What a painful insight! Write performance decreased by 90%

See a CentOS Kernel 2.6.18 test Result:

 #without  snapshot:sync; time sh -c  "dd if= /dev/zero of=/scratch/moo bs=1m count=1000; sync "1000+0 records in1000+0  records out1048576000 bytes  (1.0 GB)  copied, 10.3395 seconds, 101  MB/sreal    0m20.285suser    0m0.000ssys      0m1.084s#with snapshot:sync; time sh -c  "dd if=/dev/zero of=/scratch/ Moo bs=1m count=1000; sync "1000+0 records in1000+0 records out1048576000  bytes  (1.0 GB)  copied, 796.627 seconds, 1.3 MB/sreal     16m44.222suser    0m0.000ssys     0m1.132s 

Change chunk size will have a certain performance improvement, many experts recommend using 8K 16K these small units, reduce the impact of copy-on-wirte on performance.

For backup purposes, it is recommended that you do not use the active snapshot, but instead treat the snapshot as a freezing point. (for space comparison fee)

For example, my database runs at the moment of the snapshot, and I execute the script every 00:01, creating a snapshot volume and recording the status. Copy the data from the snapshot volume to the DD command or TAR for block backup or file backup.

You can use scripts to implement backups

# VI snapshot_backup.sh; #备份脚本------------------------------------------------------------------------------#!/bin/bashtoday= ' Date ' +%y %m%d "'; lvcreate-l1g-s-n logvol02s/dev/volgroup00/logvol02;mount/dev/volgroup00/logvol02s/disk/volgroup00/ Logvol02s;tar-zcvf/tmp/snapshot_backup_$today.tar.gz/disk/volgroup00/logvol02s/*;umount/dev/volgroup00/ Logvol02s;lvremove-f/dev/volgroup00/logvol02s;---------------------------------------------------------------- --------------chmod 755 snapshot_backup.sh; #运行权限设置;

650) this.width=650; "src=" Http://img.baidu.com/hi/face/i_f27.gif "alt=" I_f27.gif "/>

Reference documents:

Http://tldp.org/HOWTO/LVM-HOWTO/index.html

Https://www.redhat.com/archives/linux-lvm/2013-July/msg00044.html

Http://chengkinhung.blogspot.com/2012/09/linuxlvmsnapshot.html


This article is from "The Old Artisan" blog, reprint please contact the author!

LINUX Logical Volume Management: 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.