Linux Pathfinder: Linux Howtos and Tutorials

LVM Disk Management in RHEL 6

LVM Disk Management in RHEL 6
LVM Disk Management in RHEL 6 

  • Introduction
  • General information
  • Physical volumes
  • Volume groups
  • Logical volumes
  • Add space to a volume group
  • Reduce space from an existing volume group
  • Create a Snapshot of LVM

Introduction:

In this tutorial, I will show some LVM disk management commands in RHEL 6. LVM stands for logical volume manager. LVM allows you to create a logical storage volume using multiple physical disks. This has been tested in Red Hat Enterprise Linux 6.

General information

[asiface@linuxpathfinder ~]$ rpm -qa | grep -i lvm
lvm2-libs-2.02.98-9.el6.i686
system-config-lvm-1.1.12-16.el6.noarch
lvm2-2.02.98-9.el6.i686

[asiface@linuxpathfinder ~]$ sudo /sbin/fdisk -l | grep Disk
[sudo] password:
Disk /dev/sda: 16.1 GB, 16106127360 bytes
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
Disk /dev/sdc: 5368 MB, 5368709120 bytes

Display the local file system disk space usage.
[asiface@linuxpathfinder ~]$ df -lh

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              14G   11G  2.4G  83% /
tmpfs                 250M  228K  250M   1% /dev/shm
/dev/sda1             291M   52M  224M  19% /boot
/dev/sr1              3.0G  3.0G     0 100% /media/RHEL_6.4 i386 Disc 1

Physical volumes

/dev/sdb and /dev/sdc will make physical volumes.

[root@linuxpathfinder /]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xfd272a95

Device Boot      Start         End      Blocks   Id  System

Command (m for help): m
Command action
a   toggle a bootable flag
b   edit bsd disklabel
c   toggle the dos compatibility flag
d   delete a partition
l   list known partition types
m   print this menu
n   add a new partition
o   create a new empty DOS partition table
p   print the partition table
q   quit without saving changes
s   create a new empty Sun disklabel
t   change a partition’s system id
u   change display/entry units
v   verify the partition table
w   write table to disk and exit
x   extra functionality (experts only)

Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1305, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305):
Using default value 1305

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xfd272a95

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1305    10482381   83  Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xfd272a95

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1305    10482381   8e  Linux LVM

Command (m for help): w

The partition table has been altered!

Reboot needed after below mentioned command.

[root@linuxpathfinder /]# partprobe

Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.

[root@linuxpathfinder /]# pvcreate /dev/sdb1

Physical volume “/dev/sdb1” successfully created

[root@linuxpathfinder /]# pvs
PV         VG   Fmt  Attr PSize  PFree
/dev/sdb1       lvm2 a–  10.00g 10.00g

[root@linuxpathfinder /]# pvdisplay
“/dev/sdb1” is a new physical volume of “10.00 GiB”
— NEW Physical volume —
PV Name               /dev/sdb1
VG Name
PV Size               10.00 GiB
Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               1L2w2r-gIwi-QerY-8wo2-BLYp-pZPX-9qjs7H

Another Physical Volume as shown below:

[root@linuxpathfinder /]# pvs
PV         VG   Fmt  Attr PSize  PFree
/dev/sdb1       lvm2 a–  10.00g 10.00g
/dev/sdc1       lvm2 a–   4.99g  4.99g

[root@linuxpathfinder /]# pvdisplay
“/dev/sdb1” is a new physical volume of “10.00 GiB”
— NEW Physical volume —
PV Name               /dev/sdb1
VG Name
PV Size               10.00 GiB
Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               1L2w2r-gIwi-QerY-8wo2-BLYp-pZPX-9qjs7H

“/dev/sdc1” is a new physical volume of “4.99 GiB”
— NEW Physical volume —
PV Name               /dev/sdc1
VG Name
PV Size               4.99 GiB
Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               dWQprr-Hagl-Vlf0-YLtL-paYn-Eeif-YmchY1

Volume groups

We have 2 physical volume which are sdb1 and sdc1. Now we will create Volume Group of both physical volumes.

[root@linuxpathfinder /]# vgcreate vg01 /dev/sdb1 /dev/sdc1
Volume group “vg01” successfully created

Attributes of volume group in which vg name is (VG01) as shown below.

[root@linuxpathfinder /]# vgs
VG   #PV #LV #SN Attr   VSize  VFree
vg01   2   0   0 wz–n- 14.98g 14.98g

[root@linuxpathfinder /]# vgdisplay
— Volume group —
VG Name               vg01
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               14.98 GiB
PE Size               4.00 MiB
Total PE              3836
Alloc PE / Size       0 / 0
Free  PE / Size       3836 / 14.98 GiB
VG UUID               larW0I-dris-BCNU-2Pb1-nNjv-wzEx-viGwry

Logical volumes

We will create 8 gigabyte logical volume in the following step.

[root@linuxpathfinder /]# lvcreate -L 8G -n lv01 vg01
Logical volume “lv01” created

[root@linuxpathfinder /]# lvscan
ACTIVE            ‘/dev/vg01/lv01’ [8.00 GiB] inherit

Some commands to see logical volumes.

[root@linuxpathfinder /]# lvs
LV   VG   Attr      LSize Pool Origin Data%  Move Log Cpy%Sync Convert
lv01 vg01 -wi-a—- 8.00g
[root@localhost /]#

[root@linuxpathfinder /]# lvdisplay
— Logical volume —
LV Path                /dev/vg01/lv01
LV Name                lv01
VG Name                vg01
LV UUID                Kz8Wxq-66hX-XUSC-t28z-Rkd7-f7yR-Y1h114
LV Write Access        read/write
LV Creation host, time localhost.localdomain, 2014-01-11 23:55:59 -0800
LV Status              available
# open                 0
LV Size                8.00 GiB
Current LE             2048
Segments               1
Allocation             inherit
Read ahead sectors     auto
– currently set to     256
Block device           253:0

Now we will create a file system on /dev/vg01/lv01 from the following steps.

[root@linuxpathfinder /]# mkfs.ext4 /dev/vg01/lv01
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
524288 inodes, 2097152 blocks
104857 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Now create a folder which name let suppose (new_drive) and mount the logical volume on this folder.

[root@linuxpathfinder /]# mount -t ext4 /dev/vg01/lv01 /new_drive/

It will be successfully mounted.

[root@linuxpathfinder /]# df -ahl
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              14G   11G  2.3G  83% /
proc                     0     0     0   –  /proc
sysfs                    0     0     0   –  /sys
devpts                   0     0     0   –  /dev/pts
tmpfs                 250M  232K  250M   1% /dev/shm
/dev/sda1             291M   52M  224M  19% /boot
/dev/mapper/vg01-lv01
7.9G  146M  7.4G   2% /new_drive

After that we will add the following entry into /etc/fstab file.

[root@linuxpathfinder /]# vim /etc/fstab

/dev/vg01/lv01          /new_drive              ext4    defaults        0 0

Add space to a volume group

We will go through an example of adding more space to a volume group which name lv01. Steps are as follows:

[root@linuxpathfinder new_drive]# lvresize -L 11G /dev/vg01/lv01
Extending logical volume lv01 to 11.00 GiB
Logical volume lv01 successfully resized
[root@linuxpathfinder new_drive]#

But disk space will not extend or increase yet…. we will make with further more step.

[root@linuxpathfinder new_drive]# resize2fs /dev/vg01/lv01
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg01/lv01 is mounted on /new_drive; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg01/lv01 to 2883584 (4k) blocks.
The filesystem on /dev/vg01/lv01 is now 2883584 blocks long.

Now see below the disk size is resized and increased space from 8G to 11G.

[root@linuxpathfinder new_drive]# df -ahl

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              14G   11G  2.3G  83% /
proc                     0     0     0   –  /proc
sysfs                    0     0     0   –  /sys
devpts                   0     0     0   –  /dev/pts
/dev/sda1             291M   52M  224M  19% /boot
/dev/mapper/vg01-lv01
11G  148M   11G   2% /new_drive

Reduce space From aN Existing Volume group

Now we will go through how to reduce the volume disk space. But before doing these steps please make sure you have taken backup of your data from your production server.

First we will unmount the file system and then run fsck command.

[root@linuxpathfinder /]# umount /new_drive/

[root@linuxpathfinder /]# fsck -f /dev/vg01/lv01
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/vg01-lv01: 12/720896 files (0.0% non-contiguous), 83136/2883584 blocks

After that we will go through vice versa. And we will reduce disk size from 11G to 6G.

Using resize2fs command:

[root@linuxpathfinder /]# resize2fs /dev/vg01/lv01 6G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg01/lv01 to 1572864 (4k) blocks.
The filesystem on /dev/vg01/lv01 is now 1572864 blocks long.

Using lvresize command:

[root@linuxpathfinder /]# lvresize -L 6G /dev/vg01/lv01
WARNING: Reducing active logical volume to 6.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv01? [y/n]: y
Reducing logical volume lv01 to 6.00 GiB
Logical volume lv01 successfully resized

After successfully reduced the volume, we will again mount the lv file system.

[root@linuxpathfinder /]# mount -t ext4 /dev/vg01/lv01 /new_drive/

You can see the newly logical volume disk space from 11G to 6G.

[root@linuxpathfinder /]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              14G   11G  2.3G  83% /
/dev/sda1             291M   52M  224M  19% /boot
/dev/mapper/vg01-lv01
6.0G  144M  5.5G   3% /new_drive

Note: We can also increase and decrease the disk space with lvextend and lvreduce command. But here I am using resize command. You can get more information from their (man) pages.

Create A Snapshot of LVM

The purpose of snapshot is to make backup of the lvm. If you lost some from existing lvm, don’t worry about it…. You can take data from a LVM snapshot.

How to create Snapshot of Logical Volume:

[root@linuxpathfinder /]# lvcreate -L 4G -s -n lvmsnapshot /dev/vg01/lv01
Logical volume “lvmsnapshot” created

[root@linuxpathfinder /]# lvs
LV          VG   Attr      LSize Pool Origin Data%  Move Log Cpy%Sync Convert
lv01        vg01 owi-aos– 6.00g
lvmsnapshot vg01 swi-a-s– 4.00g      lv01     0.00

To retrieve a data from lvmsnapshot, first we will mount lvmsnapshot as shown below.

[root@linuxpathfinder /]# mount -t ext4 /dev/vg01/lvmsnapshot /snapshot/

[root@linuxpathfinder /]# df -ah
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              14G   11G  2.3G  83% /
proc                     0     0     0   –  /proc
/dev/sda1             291M   52M  224M  19% /boot
/dev/mapper/vg01-lv01
6.0G  145M  5.5G   3% /new_drive
/dev/mapper/vg01-lvmsnapshot
6.0G  145M  5.5G   3% /snapshot

[root@linuxpathfinder /]# cd /snapshot/
[root@linuxpathfinder snapshot]# ls
hosts  lost+found  services  test1.txt
[root@linuxpathfinder snapshot]# cp hosts /new_drive/
[root@linuxpathfinder snapshot]# cd /new_drive/
[root@linuxpathfinder new_drive]# ll
total 648
-rw-r–r– 1 root root    158 Jan 12 02:26 hosts
drwx—— 2 root root  16384 Jan 11 23:58 lost+found
-rw-r–r– 1 root root 641020 Jan 12 01:55 services
-rw-r–r– 1 root root      0 Jan 12 00:12 test1.txt

Using unmount and lvremove commands:

[root@linuxpathfinder new_drive]# umount /snapshot/

[root@linuxpathfinder new_drive]# lvremove /dev/vg01/lvmsnapshot
Do you really want to remove active logical volume lvmsnapshot? [y/n]: y
Logical volume “lvmsnapshot” successfully removed

[root@linuxpathfinder new_drive]# lvs
LV   VG   Attr      LSize Pool Origin Data%  Move Log Cpy%Sync Convert
lv01 vg01 -wi-ao— 6.00g

I hope you will be able to manage logical volume manager with above steps.

Share this article:

Asif Khan's picture
I am an experienced Linux System and Network Administrator with having more than 6 years of experience. My primary responsibilities are to keep up and running Linux system environment, backups important data with scripting and configuration management on AWS Linux server environments. My aim is to help people, in finding their path towards a solution to their problems. Well, I believe helping others can good for both parties, to them and to you as well. That day and today is the day that thousands of people seek help from my website to help people find answers to their questions. I am sure that everyone will get all the answers to their questions related to software and operating system, and this website will help you keep your system updated.

Add new comment

Image CAPTCHA

Enter the characters shown in the image.