Sample Chapter from the Book

Disk Partitioning Using Logical Volume Manager

The LVM solution is widely used for managing disk storage. LVM enables you to accumulate spaces taken from one or several disks (called physical volumes) to form a large logical container (called volume group), which can then be divided into partitions (called logical volumes). Figure 13-1 demonstrates LVM components.

LVM structure is made up of three key virtual objects called physical volume, volume group and logical volume that use physical extents (PEs) and logical extents (LEs) to further carve up the virtual objects. These components are explained in the following sub-sections.

Physical Volume

A physical volume (PV) is created when a standard partition, a software RAID partition or an entire disk is brought under LVM control by initializing the partition or disk, and constructing LVM data structures on it. A partition or disk must first be initialized before it can be used in a volume group.

Currently, there is one physical volume on the system, which was created during RHEL installation. Run the pvs command to view it:

# pvs
  PV  VG  Fmt Attr PSize PFree
  /dev/sda2 VolGroup00 lvm2 a- 29.00G 1.72G

The output indicates that there is one physical volume /dev/sda2 of size 29GB in the VolGroup00 volume group.

Volume Group

A volume group (VG) is created when at least one physical volume is added to it. The space from all physical volumes in a volume group is summed up to form a large pool of storage, which is then used to build one or more logical volumes. The default naming convention for volume groups is VolGroup00, VolGroup01, VolGroup02 and so on, however, you may use any naming scheme you wish. For example, a volume group can be called vg01, vgora, vgweb and so on.
Currently, there is one volume group on the system, which was created during RHEL installation. Run the vgs command to view it:

# vgs
  VG  #PV #LV #SN Attr VSize VFree
  VolGroup00 1 7 0 wz--n- 29.00G 1.72G

The output indicates that there is one volume group VolGroup01 containing one physical volume.

Physical Extent

When a volume group is created, the physical volume added to it is divided into several smaller logical pieces known as physical extents (PEs). An extent is the smallest allocatable unit of space in LVM. At volume group creation time, you can either define the size of the PE or leave it to the default value, which is 4MB. This means an 8GB disk would contain approximately 2000 PEs. All physical volumes in a volume group must use the same PE size.

The following command displays physical extent size used in VolGroup00 volume group:

# vgdisplay VolGroup00
. . . . . . . .
  Format  lvm2
  Metadata Areas 1
  Metadata Sequence No  8
  VG Access  read/write
  VG Status  resizable
  MAX LV  0
  Cur LV  7
  Open LV  7
  Max PV  0
  Cur PV  1
  Act PV  1
  VG Size  29.00 GB
  PE Size  32.00 MB
  Total PE  928
  Alloc PE / Size 873 / 27.28 GB
  Free  PE / Size 55 / 1.72 GB
  VG UUID  EPaUft-3G6K-riru-2txQ-rZCu-LQU7-25uvqz

Logical Volume

A volume group contains a pool of  space taken from one or more physical volumes added to it. This volume group space is divided into one or more partitions called logical volumes (LVs).

A logical volume can be increased or decreased in size and can use space taken from several physical volumes inside the volume group.
The default naming convention for logical volumes is lvol1, lvol2, lvol3 and so on, however, you may use any naming scheme you wish. For example, a logical volume can be called system, undo, table and so on. The following demonstrates device files for logical volumes in the VolGroup00 volume group.

# ll /dev/VolGroup00
total 0
lrwxrwxrwx 1 root root 28 Dec 11 07:50 lvol1 -> /dev/mapper/VolGroup00-lvol1
lrwxrwxrwx 1 root root 28 Dec 11 07:50 lvol2 -> /dev/mapper/VolGroup00-lvol2
lrwxrwxrwx 1 root root 28 Dec 11 07:50 lvol3 -> /dev/mapper/VolGroup00-lvol3
lrwxrwxrwx 1 root root 28 Dec 11 07:50 lvol4 -> /dev/mapper/VolGroup00-lvol4
lrwxrwxrwx 1 root root 28 Dec 11 07:50 lvol5 -> /dev/mapper/VolGroup00-lvol5
lrwxrwxrwx 1 root root 28 Dec 11 07:50 lvol6 -> /dev/mapper/VolGroup00-lvol6
lrwxrwxrwx 1 root root 28 Dec 11 07:50 lvol7 -> /dev/mapper/VolGroup00-lvol7

Currently, there are seven logical volumes on the system, which were created during RHEL installation. Run the lvs command to view them:

# lvs
  LV VG  Attr LSize Origin Snap% Move Log Copy% Convert
  lvol1 VolGroup00 -wi-ao 992.00M
  lvol2 VolGroup00 -wi-ao 1.94G
  lvol3 VolGroup00 -wi-ao 1.94G
  lvol4 VolGroup00 -wi-ao 4.88G
  lvol5 VolGroup00 -wi-ao 4.88G
  lvol6 VolGroup00 -wi-ao 4.88G
  lvol7 VolGroup00 -wi-ao 7.81G

The output indicates that there is one volume group VolGroup00 containing seven logical volumes.

Logical Extent

A logical volume is made up of extents called logical extents (LEs). Logical extents point to physical extents. The larger a logical volume is, the more LEs it will have.

The PE and LE sizes are usually kept the same within a volume group. A logical extent, however, can be smaller or larger than a physical extent. The default size for an LE is 4MB. The following command displays information about /dev/VolGroup00/lvol1 logical volume. The output does not indicate the LE size; however, you can divide the LV size by the Current LE count to get the LE size (which comes to 32MB in this example).

# lvdisplay /dev/VolGroup00/lvol1
  --- Logical volume ---
  LV Name  /dev/VolGroup00/lvol1
  VG Name  VolGroup00
  LV UUID  e2ZQcd-se0g-LvuQ-CepS-7WM9-wTFl-7J0Wse
  LV Write Access read/write
  LV Status  available
  # open  1
  LV Size  992.00 MB
  Current LE  31
  Segments  1
  Allocation  inherit
  Read ahead sectors auto
    - currently set to 256
  Block device 253:0

Managing Disk Space Using LVM

Managing disk space using LVM involves several tasks such as creating a physical volume, creating and displaying a volume group, creating and displaying a logical volume, extending a volume group, extending a logical volume, reducing a logical volume, renaming a logical volume, removing a logical volume, moving physical extents, reducing a volume group, removing a volume group and uninitializing a physical volume. You may either use LVM commands or the Red Hat LVM Configurator system-config-lvm to do the job.

Overview of LVM Configurator

The LVM Configurator is a graphical interface for you to perform LVM management tasks such as:

 Display volume group, logical volumes and physical volumes, and their properties.
 Extend or reduce a volume group.
 Create or remove a logical volume.
 Display PE to LE mapping.

As noted in section “Disk Partitioning Using fdisk”, there are four hard drives – /dev/sdb, /dev/sdc, /dev/sdd and /dev/sde – installed on the system. You will be able to use them to perform various tasks in this section. All LVM commands discussed in the remaining portion of this chapter support –v (or --verbose) option for verbosity. See man pages of commands for additional options and usage.

Creating a Physical Volume

A disk or partition must be initialized with the pvcreate command before it can be used in LVM. After initialization, the disk or partition is added to a volume group as a physical volume to create logical volumes. The following explains how a disk and a partition are initialized.

Make sure that LVM software packages are installed on the system:

# rpm –qa | grep lvm
system-config-lvm-1.1.3-2.0.el5
lvm2-2.02.32-4.el5

Initialize /dev/sdb disk:

# pvcreate /dev/sdb
  Physical volume “/dev/sdb” successfully created

Initialize /dev/sdc1 partition. It is assumed that /dev/sdc1 partition already exists and was created with either fdisk (partition ID “8e” (Linux LVM)) or parted (file system flag “lvm”).

# pvcreate /dev/sdc1
  Physical volume “/dev/sdc1” successfully created

You can use the LVM Configurator to create a physical volume. Follow the steps below:

 Start LVM Configurator and go to Uninitialized Entities  /dev/sdb. Click Initialize Entity and then Yes to initialize the disk.

Creating and Displaying a Volume Group

The next step is to create a volume group. Follow the steps below to create a volume group VolGroup01 with two physical volumes /dev/sdb and /dev/sdc1:

# vgcreate VolGroup01 /dev/sdb /dev/sdc1
  Volume group “VolGroup01” successfully created

Execute the vgdisplay command to view what vgcreate has done. Run this command with and without the –v option. With –v, it reports in detail.

# vgdisplay –v VolGroup01
    Using volume group(s) on command line
    Finding volume group “VolGroup01”
  --- Volume group ---
  VG Name   VolGroup01
  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   8.94 GB
  PE Size   4.00 MB
  Total PE   2288
  Alloc PE / Size  0 / 0
  Free  PE / Size  2288 / 8.94 GB
  VG UUID   ZFRYws-jY1P-UyVC-eAE3-QkAN-v5vM-ZDgS1H

  --- Physical volumes ---
  PV Name   /dev/sdb
  PV UUID   x3jrs8-tOXa-d6f1-0ZiZ-ElT2-8r3t-QC084s
  PV Status   allocatable
  Total PE / Free PE  2047 / 2047

  PV Name   /dev/sdc1
  PV UUID   nY17XC-oc0y-iyPv-D323-j0sV-xsNB-V7g8Kw
  PV Status   allocatable
  Total PE / Free PE  241 / 241

The vgdisplay command shows that there are two physical volumes in the VolGroup01 volume group with 2047 PEs in the first physical volume and 241 in the second. The PE size is 4MB. Currently, none of the PEs are allocated and no logical volumes exist. The last portion from the output under “Physical volumes” displays information about the physical volumes included in VolGroup01.

You can view detailed information about a physical volume using the pvdisplay command. Run it with and without the –v option on /dev/sdb:

# pvdisplay /dev/sdb
  --- Physical volume ---
  PV Name  /dev/sdb
  VG Name  VolGroup01
  PV Size  8.00 GB / not usable 4.00 MB
  Allocatable  yes
  PE Size (KByte) 4096
  Total PE  2047
  Free PE  2047
  Allocated PE 0
  PV UUID  x3jrs8-tOXa-d6f1-0ZiZ-ElT2-8r3t-QC084s

You can use the LVM Configurator to create a volume group. Follow the steps below to create VolGroup01:

 Start LVM Configurator and go to Unallocated Volumes  /dev/sdb  Partition 1. Click Create New Volume Group. Enter volume group name and leave other values to defaults. Click OK.
Creating and Displaying a Logical Volume

At this point, you have a pool of storage space available within VolGroup01 that you can use to construct logical volumes. Create a logical volume of size 2000MB using the lvcreate command:

# lvcreate –L 2000 VolGroup01
  Logical volume “lvol0” created

The size can be specified in KBs (kilobytes), MBs (megabytes), GBs (gigabytes), TBs (terabytes), PBs (petabytes), EBs (exabytes) or LEs (logical extents), however, MB is the default. In the above example, the size specified is 2000MB. Alternatively, you can use the –l option and specify the size in multiples of LEs. For example, to create another logical volume of size 2000MB (which is equal to 500 LEs), run the above command as follows:

# lvcreate –l 500 VolGroup01
  Logical volume “lvol1” created

Run the vgdisplay command to see what lvcreate has performed:

# vgdisplay –v VolGroup01
    Using volume group(s) on command line
    Finding volume group "VolGroup01"
  --- Volume group ---
  VG Name   VolGroup01
  System ID
  Format   lvm2
  Metadata Areas  2
  Metadata Sequence No 3
  VG Access   read/write
  VG Status   resizable
  MAX LV   0
  Cur LV   2
  Open LV   0
  Max PV   0
  Cur PV   2
  Act PV   2
  VG Size   8.94 GB
  PE Size   4.00 MB
  Total PE   2288
  Alloc PE / Size  1000 / 3.91 GB
  Free  PE / Size  1288 / 5.03 GB
  VG UUID   ZFRYws-jY1P-UyVC-eAE3-QkAN-v5vM-ZDgS1H

  --- Logical volume ---
  LV Name   /dev/VolGroup01/lvol0
  VG Name   VolGroup01
  LV UUID   t1KWaN-QBIu-t2GU-71cX-edjE-u7A7-xp6z6k
  LV Write Access  read/write
  LV Status   available
  # open   0
  LV Size   1.95 GB
  Current LE   500
  Segments   1
  Allocation   inherit
  Read ahead sectors  auto
  - currently set to  256
  Block device  253:7

  --- Logical volume ---
  LV Name   /dev/VolGroup01/lvol1
  VG Name   VolGroup01
  LV UUID   hHC2CO-Sz9J-pFr0-dHvA-yVNb-OHT2-h9ACgf
  LV Write Access  read/write
  LV Status   available
  # open   0
  LV Size   1.95 GB
  Current LE   500
  Segments   1
  Allocation   inherit
  Read ahead sectors  auto
  - currently set to  256
  Block device  253:8

  --- Physical volumes ---
  PV Name   /dev/sdb
  PV UUID   x3jrs8-tOXa-d6f1-0ZiZ-ElT2-8r3t-QC084s
  PV Status   allocatable
  Total PE / Free PE  2047 / 1047

  PV Name   /dev/sdc1
  PV UUID   nY17XC-oc0y-iyPv-D323-j0sV-xsNB-V7g8Kw
  PV Status   allocatable
  Total PE / Free PE  241 / 241

Notice the change in the vgdisplay output before and after the creation of the logical volumes. The logical volumes constructed above have used the default logical volume names which are lvol0 and lvol1.

The following example creates a 3150MB logical volume called oravol in VolGroup01:

# lvcreate –L 3150 –n oravol VolGroup01
  Rounding up size to full physical extent 3.08 GB
  Logical volume “oravol” created

To display oravol logical volume information, use the lvdisplay command:

# lvdisplay /dev/VolGroup01/oravol
  --- Logical volume ---
  LV Name   /dev/VolGroup01/oravol
  VG Name   VolGroup01
  LV UUID   LZWoh4-opWN-u8y7-ej81-dpya-c6xt-9rx0PI
  LV Write Access  read/write
  LV Status   available
  # open   0
  LV Size   3.08 GB
  Current LE   788
  Segments   1
  Allocation   inherit
  Read ahead sectors  auto
  - currently set to  256
  Block device  253:9

At this stage, you may want to view the file contents in the /etc/lvm/backup directory where volume group information, along with physical volumes and logical volumes, is stored. Each volume group in the system has a sub-directory corresponding to its name.

# ll /etc/lvm/backup
total 16
-rw------- 1 root root 2937 Dec   2 15:07 VolGroup00
-rw------- 1 root root 1931 Dec 11 13:11 VolGroup01

The contents of VolGroup01 file is displayed below:

# cat /etc/lvm/backup/VolGroup01
# Generated by LVM2 version 2.02.32-RHEL5 (2008-03-04): Thu Dec 11 13:11:29 2008
contents = "Text Format Volume Group"
version = 1
description = "Created *after* executing 'lvcreate -L 3150 -n oravol VolGroup01'"

creation_host = "rhel01"        # Linux rhel01 2.6.18-92.el5 #1 SMP Tue Apr 29 13:16:12 EDT 2008 i686
creation_time = 1229019089      # Thu Dec 11 13:11:29 2008

VolGroup01 {
        id = "ZFRYws-jY1P-UyVC-eAE3-QkAN-v5vM-ZDgS1H"
        seqno = 4
        status = ["RESIZEABLE", "READ", "WRITE"]
        extent_size = 8192              # 4 Megabytes
        max_lv = 0
        max_pv = 0

        physical_volumes {

                pv0 {
                        id = "x3jrs8-tOXa-d6f1-0ZiZ-ElT2-8r3t-QC084s"
                        device = "/dev/sdb"     # Hint only

                        status = ["ALLOCATABLE"]
                        dev_size = 16777216     # 8 Gigabytes
                        pe_start = 384
                        pe_count = 2047 # 7.99609 Gigabytes
                }
. . . . . . . .
        logical_volumes {

                lvol0 {
                        id = "t1KWaN-QBIu-t2GU-71cX-edjE-u7A7-xp6z6k"
                        status = ["READ", "WRITE", "VISIBLE"]
                        segment_count = 1
. . . . . . . .

You can use the LVM Configurator to create a logical volume. Follow the steps below to create lvol1:

 Start LVM Configurator and go to Volume Groups  VolGroup01  VolGroup01 Logical View. Click Create New Logical Volume. Enter the logical volume name and size, and leave other values to defaults. Click OK.

Extending a Volume Group

Extending a volume group adds one or more physical volumes to it. Currently, VolGroup01 includes two physical volumes /dev/sdb and /dev/sdc1. The following example adds to it another disk /dev/sdd.
Run pvcreate and then vgextend to achieve this:

# pvcreate /dev/sdd
  Physical volume "/dev/sdd" successfully created
# vgextend VolGroup01 /dev/sdd
  Volume group "VolGroup01" successfully extended

Do a vgdisplay with –v to see updated information about VolGroup01.
You can use the LVM Configurator to extend a volume group. Follow the steps below to extend VolGroup01:

 Start LVM Configurator and go to Volume Groups  VolGroup01  VolGroup01 Physical View. Click Extend Volume Group. Highlight the partition or disk that you wish to add to the volume group and click OK.

Extending a Logical Volume

Extending a logical volume increases the logical volume in size. Currently, three logical volumes – lvol0, lvol1 and oravol – with sizes 2000MB, 2000MB and 3150MB respectively, exist. The following example extends lvol0 to 5000MB. Use the lvextend command to perform this activity. You can specify the size in either MBs or LEs. All of the following will generate an identical result:

# lvextend –L 5000 /dev/VolGroup01/lvol0
  Extending logical volume lvol0 to 4.88 GB
  Logical volume lvol0 successfully resized
# lvextend –L +3000 /dev/VolGroup01/lvol0
# lvextend –l 1250 /dev/VolGroup01/lvol0
# lvextend –l +750 /dev/VolGroup01/lvol0

Alternatively, you can use the lvresize command to extend a logical volume. For example, to double the size of lvol1 to 4000MB, do any of the following:

# lvresize –L 4000 /dev/VolGroup01/lvol1
  Extending logical volume lvol1 to 3.91 GB
  Logical volume lvol1 successfully resized
# lvresize –L +2000 /dev/VolGroup01/lvol1
# lvresize –l 1000 /dev/VolGroup01/lvol1
# lvresize –l +500 /dev/VolGroup01/lvol1

Do a vgdisplay with –v to see updated information about VolGroup01. You can also use the lvdisplay command with –v option on the lvol0 and lvol1 logical volumes to view details.

You can use the LVM Configurator to extend a logical volume. Follow the steps below to extend lvol1:

 Start LVM Configurator and go to Volume Groups  VolGroup01  VolGroup01 Logical View  lvol1. Click Edit Properties. Specify a new size and click OK.

Reducing a Logical Volume

Reducing a logical volume decreases the logical volume in size. Currently, lvol0 is 5000MB. The following example reduces it to 1000MB. Use the lvreduce command to perform this activity. You can specify the size in either MBs or LEs. All of the following will do exactly the same:

# lvreduce –L 1000 /dev/VolGroup01/lvol0
  WARNING: Reducing active logical volume to 1000.00 MB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvol0? [y/n]: y
  Reducing logical volume lvol0 to 1000.00 MB
  Logical volume lvol0 successfully resized
# lvreduce –L –4000 /dev/VolGroup01/lvol0
# lvreduce –l 125 /dev/VolGroup01/vol0
# lvreduce –l –1000 /dev/VolGroup01/lvol0

There is a risk involved when reducing the size of a logical volume, critical data may be lost. Always perform a backup of data in the logical volume before reducing it. See Chapter 17 “Backup, Restore and Compression” for details on how to perform backups.

You can use the LVM Configurator to reduce a logical volume. Follow the steps below to reduce lvol1:

 Start LVM Configurator and go to Volume Groups  VolGroup01  VolGroup01 Logical View  lvol1. Click Edit Properties. Specify a new size and click OK.

Renaming a Logical Volume

Renaming a logical volume changes the name of the logical volume. The following example renames lvol0 to newlvol0. Use the lvrename command to perform this activity:

# lvrename /dev/VolGroup01/lvol0 /dev/VolGroup01/newlvol0
   Renamed "lvol0" to "newlvol0" in volume group "VolGroup01"

You can use the LVM Configurator to rename a logical volume. Follow the steps below to rename lvol1:

 Start LVM Configurator and go to Volume Groups  VolGroup01  VolGroup01 Logical View  lvol1. Click Edit Properties. Specify a new name and click OK.

Removing a Logical Volume

Removing a logical volume is a destructive task. Ensure that you perform a backup of any data in the target logical volume prior to deleting it. You will need to unmount the file system or disable swap in the logical volume. See Chapter 14 “File Systems and Swap” on how to unmount a file system and disable swap.

Use the lvremove command to remove lvol0:

# lvremove /dev/VolGroup01/lvol0
Do you really want to remove active logical volume “lvol0”? [y/n]: y
  Logical volume “lvol0” successfully removed

The command gives a warning message. Proceed with caution.

You can use the LVM Configurator to remove a logical volume. Follow the steps below to remove lvol1:

 Start LVM Configurator and go to Volume Groups  VolGroup01  VolGroup01 Logical View  lvol1. Click Remove Logical Volume and then Yes to remove it.

Moving Physical Extents

If a physical volume with some PEs being used needs to be removed from a volume group, the data that resides in those PEs will need to be moved out to an available physical volume(s) in the same volume group. The destination physical volume(s) should have at least the same number of unused PEs available. The pvmove command is used for this purpose. The following example moves PEs from /dev/sdc1 to unused PEs available on any other physical volume in the volume group. Specify the name of a destination physical volume if you wish the PEs to be moved there. This operation can safely be performed on an online, busy logical volume.

# pvmove /dev/sdc1
  /dev/sdc1: Moved: 8.0%
  /dev/sdc1: Moved: 17.9%
. . . . . . . .
  /dev/sdc1: Moved: 100.0%

Issue vgdisplay and pvdisplay commands to view the updated information.

You can use the LVM Configurator to move PEs. Follow the steps below to move /dev/sdc1 PEs:

 Start LVM Configurator and go to Volume Groups  VolGroup01  VolGroup01 Physical View  /dev/sdc  Partition 1. Highlight extents in the graphics and click Migrate Selected Extent(s) from Volume. Click Yes to confirm the move.

Reducing a Volume Group

Reducing a volume group removes one or more physical volumes from it. Currently, VolGroup01 includes three physical volumes: /devsdb, /dev/sdc1 and /dev/sdd. Use vgreduce to remove /dev/sdc1 from it. The physical volume must not be in use.

# vgreduce VolGroup01 /dev/sdc1
  Removed “/dev/sdc1” from volume group “VolGroup01”

Issue vgdisplay with –v option to see the updated information about VolGroup01.

You can use the LVM Configurator to reduce a volume group. Follow the steps below to remove /dev/sdc1 to reduce VolGroup01:

 Start LVM Configurator and go to Volume Groups  VolGroup01  VolGroup01 Physical View  /dev/sdc  Partition 1. Click Remove Volume from Volume Group and then Yes to remove it.

Removing a Volume Group

Removing a volume group removes all logical volumes within that volume group and then remove the volume group. Currently, you have lvol1 and oravol logical volumes in VolGroup01. Use the vgremove command with –f (--force) option to remove the logical volumes and then the volume group itself:

# vgremove –f VolGroup01
  Logical volume "lvol1" successfully removed
  Logical volume "oravol" successfully removed
  Volume group “VolGroup01” successfully removed

Remember to proceed with caution whenever you perform reduce and remove operations.

You can use the LVM Configurator to remove a volume group. Follow the steps below to remove VolGroup01.

 Start LVM Configurator and go to Volume Groups  VolGroup01  VolGroup01 Logical View. Remove both logical volumes. Go to VolGroup01 Physical View  VolGroup01 and Click Remove Volume Group. Click Yes to remove the volume group.

Uninitializing a Physical Volume

Uninitializing a physical volume removes all LVM metadata information from it. Use the pvremove command:

# pvremove /dev/sdb
  Labels on physical volume “/dev/sdb” successfully wiped

You can use the LVM Configurator to uninitialize a physical volume. Follow the steps below to uninitialize /dev/sdb:

 Start LVM Configurator and go Unallocated Volumes  /dev/sdb  Partition 1. Click Remove Volume from LVM and then Yes to uninitialize /dev/sdb.

HP-UNIX EXAM HPO-095
Google Book Search