How to extend your disk by adding another without downtime using LVM

What is Logical volume management (LVM)?

As the name implies, LVM is a way of providing management of physical disks in logical manner. Any modern linux system has LVM support and many use LVM by default for disk management and setup during install.

Why use LVM?

LVM allows a logical structure over a physical disk or number of physical disks. As an example one could combine two 10GB disks and have a 20GB space from that from which a 20GB ext3 file system can be made.

The great advantage of LVM is that manipulations can be done without the need for downtime in most cases.

What you need to know about LVM?

The basic concepts one needs to understand about LVM are the Physical Volume (PV), the Volume Group (VG) and the Logical Volume (LV). The Physical Volume basically maps to physical partitions or drives. On top of the PV is the VG which can be composed of many PVs. This is where the power of LVM comes from; since one can have many PVs within a VG, when one runs out of disk space, one can always add more disk and just extend this (provided one has space to add disk). There is no downtime needed for this extension to happen. LVs are division that sit ontop of VGs and it on the LVs that filesystems are created (eg. Ext3) A great place to take advantage of LVM is when one is using virtualization and one runs out of disk. One does not need to overprovision one’s allocated disk space when doing capacity management.

Below I show the necessary commands and steps necessary to extend the root partition of a Redhat system by 10GB.

Check the disk size of the system. It can be noted that the root partition (/) is 3.9GB in size.

[root@test01 ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/VolGroup00-LogVol00

3.9G 1.1G 2.6G 30% /

/dev/sda1 99M 13M 82M 14% /boot

tmpfs 2.0G 0 2.0G 0% /dev/shm

It can be noted that at this time there is only one physical disk (sda), with two partitions

[root@test01 ~]# fdisk -l

Disk /dev/sda: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sda1 * 1 13 104391 83 Linux

/dev/sda2 14 1044 8281507+ 8e Linux LVM

The vgdispay command shows the VGs that are present. These VG names are the default names that are used during the installation of the OS. The VG we are interested in the VolGroup00. That is where the root partition is. The df -h command above confirms that in the path of the dev device

[root@test01 ~]# vgdisplay

/dev/hdc: open failed: No medium found

— Volume group —

VG Name VolGroup00

System ID

Format lvm2

Metadata Areas 1

Metadata Sequence No 3

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 2

Open LV 2

Max PV 0

Cur PV 1

Act PV 1

VG Size 7.88 GB

PE Size 32.00 MB

Total PE 252

Alloc PE / Size 252 / 7.88 GB

Free PE / Size 0 / 0

VG UUID 1lMZKO-EXYR-C62d-0PwD-9Bbk-mmZx-f6SsTm

At this time a 10GB disk is added, notice that this is not picked up automatically. It will show up after a restart but we do not want to do a restart so we will have to trigger a scan

[root@test01 ~]# fdisk -l

Disk /dev/sda: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sda1 * 1 13 104391 83 Linux

/dev/sda2 14 1044 8281507+ 8e Linux LVM

The command below triggers a scan of the scsi module, an fdisk after that shows a new disk (sdb) which has no file system yet

[root@test01 ~]# echo “- – -” > /sys/class/scsi_host/host0/scan

[root@test01 ~]# fdisk -l

Disk /dev/sda: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sda1 * 1 13 104391 83 Linux

/dev/sda2 14 1044 8281507+ 8e Linux LVM

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn’t contain a valid partition table

Run the fdisk and write an LVM file (option 8e) system on the new disk. One can use the option L to display a list of available file systems.

 

[root@test01 ~]# fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won’t be recoverable.

The number of cylinders for this disk is set to 1305. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with:

1) software that runs at boot time (e.g., old versions of LILO)

2) booting and partitioning software from other OSs

(e.g., DOS FDISK, OS/2 FDISK)

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

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 or +size or +sizeM or +sizeK (1-1305, default 1305):

Using default value 1305

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): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

[root@test01 ~]# fdisk -l

Disk /dev/sda: 8589 MB, 8589934592 bytes

255 heads, 63 sectors/track, 1044 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sda1 * 1 13 104391 83 Linux

/dev/sda2 14 1044 8281507+ 8e Linux LVM

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sdb1 1 1305 10482381 8e Linux LVM

The first lvm command necessary to start the extension is to first add the new disk to the PVs on the system. The pvcreate command does this. Below that the pvdisplay command confirms the successful implementation of that.

[root@test01 ~]# pvcreate /dev/sdb1

Physical volume “/dev/sdb1” successfully created

[root@test01 ~]# pvdisplay

/dev/hdc: open failed: No medium found

— Physical volume —

PV Name /dev/sda2

VG Name VolGroup00

PV Size 7.90 GB / not usable 23.41 MB

Allocatable yes (but full)

PE Size (KByte) 32768

Total PE 252

Free PE 0

Allocated PE 252

PV UUID LbNBJr-Ffex-Clgz-b8ql-rmaT-sFIB-ew2XO1

“/dev/sdb1” is a new physical volume of “10.00 GB”

— NEW Physical volume —

PV Name /dev/sdb1

VG Name

PV Size 10.00 GB

Allocatable NO

PE Size (KByte) 0

Total PE 0

Free PE 0

Allocated PE 0

PV UUID YrMw1e-EwKH-BOsD-VYv9-Y1Kb-15ZF-UVyZCz

After the creation of the PV, one needs to extend the VG to include the newly created PV, the vgextend command does this. The vgdispay command confirms the successful implementation of this. On can observe that there are now free physical extents

 

[root@test01 ~]# vgextend VolGroup00 /dev/sdb1

Volume group “VolGroup00” successfully extended

[root@test01 ~]# vgdisplay

— Volume group —

VG Name VolGroup00

System ID

Format lvm2

Metadata Areas 2

Metadata Sequence No 4

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 2

Open LV 2

Max PV 0

Cur PV 2

Act PV 2

VG Size 17.84 GB

PE Size 32.00 MB

Total PE 571

Alloc PE / Size 252 / 7.88 GB

Free PE / Size 319 / 9.97 GB

VG UUID 1lMZKO-EXYR-C62d-0PwD-9Bbk-mmZx-f6SsTm

The next step after extending the VG is to extend the LV. The command to do this is the lvextend command. Usually one when specifies the exact disk space in GB, there is a complaint about insufficient extents. That is fine. If one wants to use all the space, one can extend with the extents mentioned as available.

[root@test01 ~]# lvdisplay

— Logical volume —

LV Name /dev/VolGroup00/LogVol00

VG Name VolGroup00

LV UUID nWhxRl-nKfh-tdfv-0gb8-kWOG-Mqft-GEfFIM

LV Write Access read/write

LV Status available

# open 1

LV Size 4.00 GB

Current LE 128

Segments 1

Allocation inherit

Read ahead sectors auto

– currently set to 256

Block device 253:0

— Logical volume —

LV Name /dev/VolGroup00/LogVol01

VG Name VolGroup00

LV UUID xMWZPG-AdxX-jHBK-FRrJ-kbbp-OH3h-5NOYFj

LV Write Access read/write

LV Status available

# open 1

LV Size 3.88 GB

Current LE 124

Segments 1

Allocation inherit

Read ahead sectors auto

– currently set to 256

Block device 253:1

[root@test01 ~]# lvextend -L +10G /dev/VolGroup00/LogVol00

/dev/hdc: open failed: No medium found

Extending logical volume LogVol00 to 14.00 GB

Insufficient free space: 320 extents needed, but only 319 available

[root@test01 ~]# lvextend -l +319 /dev/VolGroup00/LogVol00

/dev/hdc: open failed: No medium found

Extending logical volume LogVol00 to 13.97 GB

Logical volume LogVol00 successfully resized

Nothing changes on the file system after the LV has been extended

[root@test01 ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/VolGroup00-LogVol00

3.9G 1.1G 2.6G 30% /

/dev/sda1 99M 13M 82M 14% /boot

tmpfs 2.0G 0 2.0G 0% /dev/shm

In order to have the disk space available to the filesystem one must run the resize2fs command. All the disk space available through the LV is used if one does not specify any size as an argument.

[root@test01 ~]# resize2fs /dev/VolGroup00/LogVol00

resize2fs 1.39 (29-May-2006)

Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required

Performing an on-line resize of /dev/VolGroup00/LogVol00 to 3661824 (4k) blocks.

The filesystem on /dev/VolGroup00/LogVol00 is now 3661824 blocks long.

The space is now seen by the file system and can be used by the OS

[root@test01 ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/VolGroup00-LogVol00

14G 1.1G 12G 9% /

/dev/sda1 99M 13M 82M 14% /boot

tmpfs 2.0G 0 2.0G 0% /dev/shm

The path specified in the df command maps to the same device path specified in the resize2fs command and others

[root@test01 ~]# ls -l /dev/VolGroup00/

total 0

lrwxrwxrwx 1 root root 31 Oct 21 17:22 LogVol00 -> /dev/mapper/VolGroup00-LogVol00

lrwxrwxrwx 1 root root 31 Oct 21 17:10 LogVol01 -> /dev/mapper/VolGroup00-LogVol01

Author: Nii Apleh Lartey

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.