Computerglitch

An ongoing adventure

RHEL5 LVM Document

This document picks up on my Linux iSCSI notes and explains how to add disk space to a server using the Logical Volume Manager (LVM) which makes this a pretty simple task.


This document assumes a volume group with the name of “rootvg” is already created, and a logical volume group with the name of “logvol” is already created.


Once the new disk has been added to the system (via. iscsi or physical) you will need to partition the disk. We will use /dev/sdb as our disk.


First run fdisk on the new disk:

#fdisk /dev/sdb

Inside the fdisk program you can create a new partition by typing the letter “n”. It will then ask if you want the partition to be a primary or extended partition, we’ll use primary. To do this type “p”. For start and end accept the defaults by pressing enter. If you want to view the details of the partition type “p”. Next change the type of partition this is by typing “t”. It will ask what type of partition we want, we want Linux LVM which is 8e so type in “8e”. Save the changes and exit by typing “w”


Here is example output from the entire procedure:

[root@box ~]# 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.

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-652, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-652, default 652):
Using default value 652

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 652 5237158+ 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): w
The partition table has been altered!

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

Next we need to use pvcreate to laydown physical extents and other things LVM needs on the physical partition. The command is:

#pvcreate /dev/sdb1

Now we need to extend the existing volume group onto the new partition. To view details about the existing volume groups you can use the “vgdisplay” command. To extend the rootvg onto the new partition use the following command:

#vgextend rootvg /dev/sdb1

Next we are going to extend our existing logical volume (logvol) onto the new space. To view information about logical volumes use the “lvdisplay” command. To extend the logical volume onto the new space use the following command:

#lvextend -L +4G /dev/rootvg/logvol /dev/sdb1

This command says we want to increase the /dev/rootvg/logvol logical volume by 4GB.


The last thing to do is increase the filesystem. We are assuming a filesystem of type ext3, so we use the following command to use all the available space.

#resize2fs /dev/rootvg/logvol

Here is a snapshot of the entire process, in this example we are adding a 5GB disk to the existing volume group:

[root@box ~]# 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.

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-652, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-652, default 652):
Using default value 652

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 652 5237158+ 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): w
The partition table has been altered!

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

[root@box ~]# pvcreate /dev/sdb1
Physical volume “/dev/sdb1” successfully created

[root@testbox ~]# vgdisplay
/dev/hdc: read failed after 0 of 2048 at 0: Input/output error
— Volume group —
VG Name rootvg
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 14.88 GB
PE Size 32.00 MB
Total PE 476
Alloc PE / Size 476 / 14.88 GB
Free PE / Size 0 / 0
VG UUID uwWUje-myY0-3LEi-3eGR-mzxS-8sOK-dULdlu

[root@box ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-logvol
14G 1.2G 12G 9% /
/dev/sda1 99M 12M 83M 13% /boot
tmpfs 252M 0 252M 0% /dev/shm

[root@box ~]# vgextend logvol /dev/sdb1
/dev/hdc: open failed: Read-only file system
/dev/cdrom: open failed: Read-only file system
Attempt to close device ‘/dev/cdrom’ which is not open.
Volume group “logvol” successfully extended

[root@box ~]# vgdisplay
— Volume group —
VG Name logvol
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 19.84 GB
PE Size 32.00 MB
Total PE 635
Alloc PE / Size 476 / 14.88 GB
Free PE / Size 159 / 4.97 GB
VG UUID uwWUje-myY0-3LEi-3eGR-mzxS-8sOK-dULdlu

[root@box ~]# lvdisplay
— Logical volume —
LV Name /dev/rootvg/logvol
VG Name logvol
LV UUID w64bkC-sDlI-FRk1-44Es-JfCV-IkhJ-D3ZN7q
LV Write Access read/write
LV Status available
# open 1
LV Size 13.88 GB
Current LE 444
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

[root@box ~]# lvextend -L +4G /dev/rootvg/logvol /dev/sdb1
Extending logical volume logvol to 17.88 GB
Logical volume logvol successfully resized

[root@box ~]# resize2fs /dev/rootvg/logvol
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/rootvg/logvol is mounted on /; on-line resizing required
Performing an on-line resize of /dev/rootvg/logvol to 4685824 (4k) blocks.
The filesystem on /dev/rootvg/logvol is now 4685824 blocks long.

[root@box ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rootvg-logvol
18G 1.2G 16G 7% /
/dev/sda1 99M 12M 83M 13% /boot
tmpfs 252M 0 252M 0% /dev/shm

[root@box ~]#

Comments