Package Managements

This article explains how to add or remove space from LVM partition in detail with examples.

LVM is a disk management solution that allow administrators to manage space allocated to appropriate directories. LVM allow you to manage disk space more effectively. LVM allow you to add , remove space from existing volume. In previous article we look around at the LVM structure.

What is LVM

In this article

  • Create LVM partition at command line
  • Create LVM partition with fdisk
  • Create LVM partition using parted

Before you start creating LVM partition, make sure you understand physical volume(PV), volume group (VG), and logical volume (LV).

lvm

Physical volume (PV) :- PV are the partitions on hard disk, or hard disk itself. PV are the base of LVM structure and referred as physical volumes.

Volume Group (VG) :- VG are the combined physical volume into a single pool of storage. Think it is as a group of PV.

Logical volume (LV) :- LV are the actual partitions on system created from VG.

LVM package is the part of core installation, so there is rare chance of missing it. Still its good habit to verify the installation

rpm-qa-lvm

If it is not available, install it first.

yum-install-lvm2

By default, during the installation, Red Hat Linux use LVM to manage the disks. If you choose to create the default layout, LVM is used to divide the hard drive, and then the necessary Linux mount points are created. To demonstrate this process from scratch I used custom layout to skip LVM layout during the installation. Use pvs command to list the current physical volume.

pvs-blank

As you can see we have no active physical volume so far lets create our first PV. You can create PV from hard disk or from hard disk partition. If you have additional hard disk and want to make it the part of LVM, use following command to create PV from hard disk.

#pvcreate [disk]

Same command is used to create physical volume from hard disk partition.

#pvcreate [disk partition]

If you have more than one partition to be configured as PV, you can list them as well

#pvcreate  [disk partition]  [disk partition] [disk partition]
  • If you have single hard disk, you should create one PV in single largest partition.
  • If you have multiple disks, you should create partitions on each disk and create PV from partitions.
  • You need to set partition type to " Linux LVM [8e if you use MBR, ee for GPT]" in order to create PV.
  • GRUB Legacy does not support LVM. So if you need to use GRUB legacy, you must have to create separate /boot partition and format it.
  • Whenever you use #pvcreate command make sure, you target the correct device, or this commands will result in data loss.

fdisk -l command will list the current partitions, and their mount point, and type.

fdisik-l-default

Now you know that PV can be created from disk or disk partitions. Let's make a new partition of 100 MB using fdisk utility.

fdisk /dev/sda   [Replace /dev/sda with your hard disk mount point]
n                     [command to create new partition in fdisk prompt]
press enter           [ accept default first cylinder ]
+100M                 [ Size of LVM, specify it at as per your need and availability of space ]
t                     [command to set the partition type]
6                     [Number or our created partition, make sure here you specify current number. Wrong number here will result data loss]
8e                    [Code to set partition type LVM]
w                     [command to save the change ]

fdisk-dev-sda

Kernel will not reflect this change until reboot. You can force kernel to reread the partition table by using partprobe command

fdisk-l-linux-lvm

To create physical volume [PV] you only need single partition marked as LVM partition.

Next two sections are added to enhances your skills, for RHCE exam stick with fdisk method. During the practice, I suggest you to go through the following section as well.

parted is the advance disk management tool which support lager disks. With fdisk you cannot create a partition lager then 2TB in addition it does not support GPT, parted solve this glitch. Let's make one more partition, this time use parted command.

Use print command at parted command prompt to list current partitions.

parted-print

By default partition size is displayed in GB, change it to in MB and note down the last partition end point.

parted-last-partition-end

Make 100MB partition. mkpart command is used to create new partition. You need to pass following information.

partition type, file system type, partition starting point, partition ending point

parted-mkpart

Verify new partition and note down its partition number

parted-new-partition

set [partition number] lvm on command is used to set partition type. From above output we know that our partition number is 7  lets update its partition type to LVM and exit from parted tool.

parted-quite

So far in this tutorial we have created two partitions from two different utilities.

Lets make our practice little bit more interesting. We will add new hard disk to system and use entire disk as LVM partition.

For this practice I hosted my system on Vmware workstation software, if you are on same software follow this.

If you have set up system on other virtual software for example virtual box, virtual pc, check their documentations to learn, how to add new hard disk to system.

If you are doing this practice on physical system, skip this step.

How to add new hard disk in system on Vmware workstation

Turn off the system. You cannot modify the hardware in running state.

#init 0

Click Edit virtual machine settings

edit-virtual-machine

from open dialog box select Hard disk and click Add

add-harddisk

Add Hardware Wizard will be pop up, Select Hard Disk and click Next

add-hardware-wizard-next

Select Create new virtual disk and click Next

create-new-virtual-disk

Select SCSI disk an click Next

select-scsi

Specify the disk size, select Store virtual disk as single file and click Next

specify-disk-capacity

Specify the location for this hard disk and click Finish

specify-disk-location

We are back on virtual setting box, click ok to close.

virtual-machine-setting

Turn on system and login from root account to complete the remaining practice.

power-on

Newly added hard disk have no partition table yet, we need to create partition table before we can use it. We will use parted utility to create new partition.

parted-sdb

Make a single partition and mark it as LVM.

single-lvm-partition

Exit from parted

parted-quite-command

Force kernel to reread the new partition table

#partprobe

Finally our partitions are ready to be part of physical volume.

fdisk-l-all-partition

If it feels over burden in first instances, you can skip parted partitions and complete the practice with single partition created from fdisk.

pvcreate command is used to create physical volume, it creates a header on each supplied partitions. Make sure you point the correct partition, this command will delete all data before creating header.

pvcreate

You can verify created physical volumes with following command.

pvdisplay

Volume group is collection of physical volumes. We need to create volume group from create physical volumes. You can create volume group from single physical volume and add more physical volume in it with vgextend command, or you can use single command to combine the creation of a volume group.

For example, to create the group VolGroup00 with the three physical volume mentioned above, we can run:

# vgcreate VolGroup00 /dev/sda6 /dev/sda7 /dev/sdb1

The command may warn you if it detects an existing file system on any of the partitions. You can track created volume group with vgdisplay command

vgcreate

Our volume group is ready. Now we will create logical volumes on this. We can create logical volume with lvcreate command by giving the name of a new logical volume, its size, and the volume group it will live on.

# lvcreate -L [size] [volume_group] -n [logical_volume]

Lets create two logical volume lv00 and lv01. You can choose any descriptive name which you want for your logical volume when creating it.

lvcreate

You can Verify created logical volume with following command

lvdisplay

Your logical volumes are ready to use. To use them create file system and mount as normal partitions.

Create ext4 partition on lv00

format-lv00

Create ext4 partition on lv01

format-lv01

We have created file system on logical volumes by formatting them. Now we need to create mount point for them. Following command will create new mount points and Mount them as normal partitions. Make sure you select newly created logical volumes. Do not select the actual partitions on which logical volumes were created

mount-lv

These partitions will not be mount automatically upon restart. To mount them permanently open /etc/fstab file

vi-fstab

At the end of file, add entry for our logical volumes and save the file

fstab-entry

Reboot the system

reboot

Track our logical volumes, they would be automatically mounted along with other regular partitions.

mount