Add hard drive to extend existing LVM volume

por | 7 febrero, 2010

http://www.utahsysadmin.com/2009/12/08/add-hard-drive-to-extend-lvm/

Previously I posted how to add a hard drive and create a new volume in LVM. This time we’ll add a new hard drive and then increase or extend the size of an existing volume or partition. This is an example using an RHEL 5 derivative, OEL 5. The server is really a VM inside VMware vSphere 4, but that is of no consequence to what we are doing.

In this example, we have an existing /data partition of 350 GB. Well, it’s just not big enough, so we’ll add another 100 GB hard drive and give it to the /data partition.

After adding the 100 GB hard drive to the VM through vCenter, connect to the server through SSH or the console. Here’s the existing setup:

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
7.7G 2.2G 5.2G 30% /
/dev/sda1 99M 24M 71M 26% /boot
tmpfs 1.5G 0 1.5G 0% /dev/shm
/dev/mapper/VolGroup01-LogVol00
345G 15G 313G 5% /data

# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
Vendor: VMware Model: Virtual disk Rev: 1.0
Type: Direct-Access ANSI SCSI revision: 02
Host: scsi0 Channel: 00 Id: 01 Lun: 00
Vendor: VMware Model: Virtual disk Rev: 1.0
Type: Direct-Access ANSI SCSI revision: 02

First we tell Linux to find the new drive in the correct location:

# echo “scsi add-single-device 0 0 2 0″ > /proc/scsi/scsi

Now we see the new drive has been detected:

# cat /proc/scsi/scsi
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
Vendor: VMware Model: Virtual disk Rev: 1.0
Type: Direct-Access ANSI SCSI revision: 02
Host: scsi0 Channel: 00 Id: 01 Lun: 00
Vendor: VMware Model: Virtual disk Rev: 1.0
Type: Direct-Access ANSI SCSI revision: 02
Host: scsi0 Channel: 00 Id: 02 Lun: 00
Vendor: VMware Model: Virtual disk Rev: 1.0
Type: Direct-Access ANSI SCSI revision: 02

If we check fdisk, we see the new drive is blank like we expect:

# fdisk -l

Disk /dev/sda: 12.8 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 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 1566 12474472+ 8e Linux LVM

Disk /dev/sdb: 375.8 GB, 375809638400 bytes
255 heads, 63 sectors/track, 45689 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 45689 366996861 83 Linux

Disk /dev/sdc: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

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

So let’s give it a partition:

# fdisk /dev/sdc
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 13054.
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 0×0000 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-13054, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-13054, default 13054):
Using default value 13054

Command (m for help): p

Disk /dev/sdc: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 1 13054 104856223+ 83 Linux

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

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

Fdisk now confirms that everything looks good:

# fdisk -l

Disk /dev/sda: 12.8 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 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 1566 12474472+ 8e Linux LVM

Disk /dev/sdb: 375.8 GB, 375809638400 bytes
255 heads, 63 sectors/track, 45689 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 45689 366996861 83 Linux

Disk /dev/sdc: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 1 13054 104856223+ 83 Linux

Verify that LVM can see the drive:

# lvmdiskscan
…skip…
/dev/sdc1 [ 100.00 GB]
…skip…

Create a physical volume within LVM:

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

Add/Extend this physical volume to the volume group:

# vgextend VolGroup01 /dev/sdc1
Volume group “VolGroup01″ successfully extended

Extend the logical volume to use the full size of the volume group:

# lvm lvextend -l +100%FREE /dev/VolGroup01/LogVol00
Extending logical volume LogVol00 to 449.99 GB
Logical volume LogVol00 successfully resized

Actually resize the partition for the OS to recognize the new size:

# resize2fs -p /dev/mapper/VolGroup01-LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/mapper/VolGroup01-LogVol00 is mounted on /data; on-line resizing required
Performing an on-line resize of /dev/mapper/VolGroup01-LogVol00 to 117961728 (4k) blocks.
The filesystem on /dev/mapper/VolGroup01-LogVol00 is now 117961728 blocks long.

And the new drive space is now available to the user, who is already busy filling it:

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
7.7G 2.2G 5.2G 30% /
/dev/sda1 99M 24M 71M 26% /boot
tmpfs 1.5G 0 1.5G 0% /dev/shm
/dev/mapper/VolGroup01-LogVol00
443G 48G 373G 12% /data

/dev/mapper/VolGroup00-LogVol00

lvm lvextend -l +100%FREE /dev/VolGroup00/LogVol00

resize2fs -p /dev/mapper/VolGroup00-LogVol00