How to Configure LVM in Linux (pvcreate, lvcreate, vgcreate)

December 2, 2013 | By
| Reply More

linux lvm configuration

Logical Volume Management (LVM) creates a layer of abstraction over physical storage, allowing you to create logical storage volumes. With LVM in place, you are not bothered with physical disk sizes because the hardware storage is hidden from the software so it can be resized and moved without stopping applications or unmounting file systems.

For example if you are running out of disk space on your server, you can just add another disk and extend the logical volume on the fly.

Below are some advantages of using Logical volumes over using physical storage directly:

* Resize storage pools

You can extend the logical space as well as reduce it without reformatting the disks.

* Flexible storage capacity

You can add more space by adding more disks and adding them to the pool of physical storage, thus you have a flexible storage capacity.

* Use of striped,mirrored and snapshot volumes

Striped logical volume that stripes data across two or more disks can dramatically increase throughput. Mirrored Logical volumes provide a convenient way to configure a mirror for your data. And you can take device snapshots for backups or to test the effect of changes without affecting the real data.

Logical Volume Management 3 parts

a) Physical Volume (PV)

b) Volume Group (VG)

c) Logical Volume (LV)

Create Physical Volume

Physical volume is the actual storage device that will be used in the LVM configuration. It can be an entire disk, a partition on disk or a LUN on the SAN. You can use pvcreate to create the physical volume. In this example I have added two disks (/dev/sdb and /dev/sdc) of 1 GB each. I will be using these for examples.

pvcreate command initialise these disks so that they can be a part in forming volume groups.

# pvcreate /dev/sdb /dev/sdc
Writing physical volume data to disk "/dev/sdb"
Physical volume "/dev/sdb" successfully created
Writing physical volume data to disk "/dev/sdc"
Physical volume "/dev/sdc" successfully created

Display the physical volumes:

# pvdisplay
"/dev/sdb" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID FJGgGD-c6h3-88v3-mIh5-Y9XL-oPbe-YUlgAl

"/dev/sdc" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Nsb7iS-uR6f-eBe2-KmjQ-U1Iy-lnrp-pKbZwX

You can also use pvs command that will display the output in a configurable form.

pvcreate command

Create Volume Group

Physical volumes are combined into volume groups (VGs). It creates a pool of disk space out of which logical volumes can be allocated. The disk space available for allocation in Volume Group is divided into units of a fixed-size called extents. An extent is the smallest unit of storage that can be allocated. Within a physical volume, extents are referred to as physical extents.

A logical volume is allocated into logical extents of the same size as the physical extents. The extent size is thus the same for all logical volumes in the volume group. The volume group maps the logical extents to physical extents.

# vgcreate vg-01 /dev/sdb /dev/sdc
Volume group "vg-01" successfully created

Display information about VG's

# vgdisplay
--- Volume group ---
VG Name vg-01
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 1.99 GiB
PE Size 4.00 MiB
Total PE 510
Alloc PE / Size 0 / 0
Free PE / Size 510 / 1.99 GiB
VG UUID W8G3vJ-ImqE-nR0z-e5ND-fiWe-rHCo-VB9KV1

You can also use vgs command that will display the output in a configurable form.

vgcreate command

Create Logical Volume

A volume group is divided up into logical volumes. So if you have created vg-01 earlier then you can create logical volumes from that VG. The amount of space you want to allocate depend on your requirement. The two disks are having 2GB of space collectively. You might want to create LV of 200MB, 1GB etc.  In this example I will discuss three type of logical volumes.

a) Linear Volume
b) Striped Volume
c) Mirrored Volume

Linear Logical Volumes

A linear volume aggregates space from one or more physical volumes into one logical volume. For example, if you have two 2GB disks, you can create a 4GB logical volume. The physical storage is concatenated. The application will see one device that is 4 GB in size. By default we create linear volumes in LVM. Consider the following examples:

# lvcreate -L 1G -n lv_linear vg-01
Logical volume "lv_linear" created

Display Information about Logical Volumes.

# lvdisplay
--- Logical volume ---
LV Name /dev/vg-01/lv_linear
VG Name vg-01
LV UUID pUHbzH-DmWX-deOL-GhXx-f6mm-C0ld-1xdwQv
LV Write Access read/write
LV Status available
# open 0
LV Size 1.00 GiB
Current LE 256
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3

You can also use lvs command that will display the output in a configurable form.

lvcreate command

Striped Logical Volume

When you write data to a logical volume, the file system lays the data out across the underlying physical volumes. You can control the way the data is written to the physical volumes by creating a striped logical volume. For large sequential reads and writes, this can improve the efficiency of the data I/O.

With striping, I/O can be done in parallel. Below command is used to create striped volume:

# lvcreate -L 1G -i2 -I64 -n lv_stripe vg-01
Logical volume "lv_stripe" created

# lvdisplay
--- Logical volume ---
LV Name /dev/vg-01/lv_stripe
VG Name vg-01
LV UUID NEgVUL-6zAw-SFLd-yTc5-GWKl-CaUk-lxfjzi
LV Write Access read/write
LV Status available
# open 0
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 512
Block device 253:3

the i denotes the number of stripes. This determines over how many physical volumes the logical volume will be striped. The number of stripes cannot be greater than the number of physical volumes in the volume group. The I denotes the strip size. The above command creates a striped logical volume across 2 physical volumes with a stripe of 64kB.

Mirrored Logical Volume

A mirror maintains identical copies of data on different devices. When data is written to one device, it is written to a second device as well. This provides protection for device failures. When one leg of a mirror fails, the logical volume becomes a linear volume and can still be accessed.

An LVM mirror divides the device being copied into regions that are typically 512KB in size. LVM maintains a small log which it uses to keep track of which regions are in sync with the mirror. This log can be kept on disk, which will keep it persistent across reboots, or it can be maintained in memory. The following command will create a mirrored logical volume.

# lvcreate -L 200M -m1 -n lv_mirror vg-01
Logical volume "lv_mirror" created

# lvdisplay
--- Logical volume ---
LV Name /dev/vg-01/lv_mirror
VG Name vg-01
LV UUID HXE3lc-17vb-Ejy9-ME0Q-DUdx-ZKBi-nyCX4x
LV Write Access read/write
LV Status available
# open 0
LV Size 200.00 MiB
Current LE 50
Mirrored volumes 2
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:6

Activating LVM

The Logical Volumes can be made known to the kernel using the command ‘lvchange’. The -a option is used to activate or deactivate the Logical Volume. Using ‘y’ with -a option will make it known to kernel and ‘n’ will make it unavailable.

Note : When you create a volume group / logical volume it is, by default, activated. So only run this command based on specific requirement

# lvchange -ay /dev/VolGroup00/LogVol00

The similar command, ‘vgchange’ (with -ay option) is used for activating the Volume Group.

# vgchange -ay VolGroup00
2 logical volume(s) in volume group “VolGroup00″ now active

Create LVM On a New Hard Disk

More physical volumes can be added to an existing volume group thus increasing its size. In general using LVM, a partition can span more than one disk. The size of logical volumes can also be extended and reduced without any loss of data on that volume.

First using fdisk command make a partition and toggle that partition to LINUX LVM (8e) label.Then create a physical volume using pvcreate command.

#pvcreate /dev/hda10

Display the size of the physical volume

#pvdisplay /dev/hda10

Create volume group whose name test

#vgcreate test /dev/dha10

Display the details of volume group created

#vgdisplay test

Create logical volume of size 100 MB with name as data , /etc/test/data */

#lvcreate -L 100M -n data test

Display the information about logical volume

#lvdisplay /dev/test/data

Convert/format logical partition to ext3 filesystem

#mke2fs -j /dev/test/data

Mount the volume to any directory

#mount /dev/test/data /mnt

Or

If you have device mapper

#mount /dev/mapper/test-data /oracle

Commands to Scan PV's, LV's and VG's

The lvscan command scans for all logical volumes on the host

# lvscan
ACTIVE ‘/dev/VolGroup00/LogVol00′ [6.88 GB] inherit
ACTIVE ‘/dev/VolGroup00/LogVol01′ [1.00 GB] inherit

The vgscan command scans all the disks for volume groups and rebuilds the LVM cache file.

# vgscan
Reading all physical volumes. This may take a while…
Found volume group “VolGroup00″ using metadata type lvm2

The pvscan command scans all lvm block devices for physical volumes.

# pvscan
PV /dev/sda2 VG VolGroup00 lvm2 [7.88 GB / 0 free]
Total: 1 [7.88 GB] / in use: 1 [7.88 GB] / in no VG: 0 [0 ]

Note : All these commands, only work properly on Redhat/Centos systems

Filed Under : LINUX HOWTO, LVM

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.