Oracle 8i | Oracle 9i | Oracle 10g | Oracle 11g | Oracle 12c | Miscellaneous | PL/SQL | SQL | Oracle RAC | Oracle Apps | Linux

Linux Logical Volume Management

This article gives an introduction to Logical Volume Management (LVM) in Linux, with specific reference to the information needed for the RHCSA EX200 and RHCE EX300 certification exams.

Remember, the exams are hands-on, so it doesn't matter which method you use to achieve the result, so long as the end product is correct.

Related articles.

Why use logical volume management?

Logical volume management provides a level of abstraction between a file system and the physical disks or partitions.

Without a volume manager, once you have used all the space on a disk your file system can not be extended any further. Using logical volume management, a file system is built on a logical volume. A logical volume is built on top of a volume group, which in turn is created using one or more physical volumes (disks or partitions). This way a single file system can span many disks. If a file system becomes full it can be extended by adding a new physical volume (disk or partition) to the volume group, then simply extending the logical volume.

This flexibility in space allocation is only one aspect of the usefulness of logical volume management, but it is the only one considered in the Red Hat certification exams.

If you are completely new to the concept of logical volume management, you may wish to read more about it in the RHEL6 Logical Volume Manager Administration.

Physical Volumes (pvcreate, pvremove)

The pvcreate command initializes a disk or partition as a physical volume for use with the volume manager. When using whole disks you don't need any special preparation.

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

The pvremove command wipes the device so it is no longer considered a physical volume.

# pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped
#

When using partitions, the system id of the partitions should be set to "Linux LVM" (8e). The following output shows the creation of two 5G partitions on the "/dev/sdb" device, both with system ids set to "Linux LVM".

# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xcf1c9c9c.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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): c
DOS Compatibility flag is not set

Command (m for help): u
Changing display/entry units to sectors

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

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): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First sector (10487808-20971519, default 10487808): 
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-20971519, default 20971519): 
Using default value 20971519

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

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
#

With the partitions present, we can initialize them as physical volumes.

# pvcreate /dev/sdb1
  Writing physical volume data to disk "/dev/sdb1"
  Physical volume "/dev/sdb1" successfully created
# pvcreate /dev/sdb2
  Writing physical volume data to disk "/dev/sdb2"
  Physical volume "/dev/sdb2" successfully created
#

These physical volumes will be used for the remaining examples in the article.

Other physical volume commands are available. Typically, they begin with "pv*" and are present in the "/sbin/" directory. Not all the commands listed below are physical volume commands though.

# ls /sbin/pv*
/sbin/pvchange  /sbin/pvcreate   /sbin/pvmove    /sbin/pvresize  /sbin/pvscan
/sbin/pvck      /sbin/pvdisplay  /sbin/pvremove  /sbin/pvs
#

Volume Groups (vgcreate, vgextend, vgreduce, vgdisplay)

A volume group is a collection of physical volumes. A volume group can be created using the vgcreate command, passing the group name and a physical volume as parameters.

# vgcreate DataVolGroup /dev/sdb1
  Volume group "DataVolGroup" successfully created
#

The vgextend command allows you to add physical volumes to an existing volume group.

# vgextend DataVolGroup /dev/sdb2
  Volume group "DataVolGroup" successfully extended
#

The vgdisplay command shows information about the specified volume group, or all volume groups if no parameter is specified.

# vgdisplay DataVolGroup
  --- Volume group ---
  VG Name               DataVolGroup
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  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               9.99 GiB
  PE Size               4.00 MiB
  Total PE              2558
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2558 / 9.99 GiB
  VG UUID               ZexOo9-kO0j-wEUP-2IhM-5IGf-dAio-AuKlDf
   
#

Physical volumes are removed from the volume group using the vgreduce command.

# vgreduce DataVolGroup /dev/sdb2
  Removed "/dev/sdb2" from volume group "DataVolGroup"
#

Other volume group commands are available. Typically, they begin with "vg*" and are present in the "/sbin/" directory. Not all the commands listed below are volume group commands though.

# ls /sbin/vg*
/sbin/vgcfgbackup   /sbin/vgcreate   /sbin/vgimportclone  /sbin/vgrename
/sbin/vgcfgrestore  /sbin/vgdisplay  /sbin/vgmerge        /sbin/vgs
/sbin/vgchange      /sbin/vgexport   /sbin/vgmknodes      /sbin/vgscan
/sbin/vgck          /sbin/vgextend   /sbin/vgreduce       /sbin/vgsplit
/sbin/vgconvert     /sbin/vgimport   /sbin/vgremove
#

Logical Volumes (lvcreate, lvextend, lvreduce, lvremove, lvdisplay)

Logical volumes are created with the lvcreate command by specifying a size and volume group.

# lvcreate --size 1G DataVolGroup
  Logical volume "lvol0" created
#

The resulting logical volume will have device path of "/dev/vol-group-name/lv-name", where the "vol-group-name" is that specified in the lvcreate command and the "lv-name" is that displayed in the command output. In this case, the device path is "/dev/DataVolGroup/lvol0". It can also be accessed using its mapper path "/dev/mapper/DataVolGroup-lvol0". The lvcreate command has many available settings, including the "-n" or "--name", allowing you to explicitly name the logical volume it creates.

Once the logical volume has been created, you can create a file system on it.

# mkfs.ext4 /dev/DataVolGroup/lvol0
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
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376

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

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

The size of a specified logical volume can be safely increased using the lvextend command.

# lvextend --size 2G /dev/DataVolGroup/lvol0
  Extending logical volume lvol0 to 2.00 GiB
  Logical volume lvol0 successfully resized
#

You can then extend the file system on the logical volume using the resize2fs command. The following example shows the file system being extended twice. The first time to 1500M, only using part of the logical volume, then to fill the logical volume.

# resize2fs /dev/DataVolGroup/lvol0 1500M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/DataVolGroup/lvol0 to 384000 (4k) blocks.
The filesystem on /dev/DataVolGroup/lvol0 is now 384000 blocks long.

# resize2fs /dev/DataVolGroup/lvol0
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/DataVolGroup/lvol0 to 524288 (4k) blocks.
The filesystem on /dev/DataVolGroup/lvol0 is now 524288 blocks long.

#

Logical volumes can have their size reduced using the lvreduce command, but this can be very destructive so it's probably best to only do it when you don't care about the contents of the volume.

# lvreduce --size 1G /dev/DataVolGroup/lvol0
  WARNING: Reducing active logical volume to 1.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvol0? [y/n]: y
  Reducing logical volume lvol0 to 1.00 GiB
  Logical volume lvol0 successfully resized
#

Logical volumes are dropped using the lvremove command. The output below shows a new logical volume being added then immediately removed.

# lvcreate --size 1G DataVolGroup
  Logical volume "lvol1" created
# lvremove /dev/DataVolGroup/lvol1
Do you really want to remove active logical volume lvol1? [y/n]: y
  Logical volume "lvol1" successfully removed
#

The lvdisplay command displays information about logical volumes. When called with no parameters, it displays information for all logical volumes on the system. Passing a volume group name limits the output to logical volumes in the specified volume group. Passing logical volume name limits the output to just that logical volume.

# lvdisplay /dev/DataVolGroup/lvol0
  --- Logical volume ---
  LV Name                /dev/DataVolGroup/lvol0
  VG Name                DataVolGroup
  LV UUID                4Z0XDY-sMFP-QyPb-xZo5-ycOJ-dBNe-cet4ye
  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     256
  Block device           253:2
   
#

Other logical volume commands are available. Typically, they begin with "lv*" and are present in the "/sbin/" directory. Not all the commands listed below are logical volume commands though.

# ls /sbin/lv*
/sbin/lvchange   /sbin/lvm          /sbin/lvmsadc   /sbin/lvresize
/sbin/lvconvert  /sbin/lvmchange    /sbin/lvmsar    /sbin/lvs
/sbin/lvcreate   /sbin/lvmconf      /sbin/lvreduce  /sbin/lvscan
/sbin/lvdisplay  /sbin/lvmdiskscan  /sbin/lvremove
/sbin/lvextend   /sbin/lvmdump      /sbin/lvrename
#

Logical Volume Manager GUI (system-config-lvm)

The system-config-lvm package provides a GUI tool to perform most of the LVM operations. It can be installed using one of the following command.

# # From a YUM repository.
# yum install system-config-lvm

# # From an RPM
# rpm -Uvh system-config-lvm*

Once installed, the GUI is started from the menu (System > Administration > Logical Volume Management) or from the command line by issuing system-config-lvm as the "root" user.

Logical Volume Manager GUI

Personally, I find the GUI tool incredibly slow and a little confusing at times, but if you are not a command line fan, you might get better mileage with it than me.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.