How-to: Extend LVM Logical Volume on Linux
So, first of all attach the new hard disk to the machine and if it supports hot swap-able disks you’ll not even have to shutdown the system first. If this the case, you can use the SysFS filesystem to trigger a SCSI bus re-scan using a command similar to the one below.
root ~:# echo "- - -" > /sys/class/scsi_host/host0/scan SCSI device sda: 41943040 512-byte hdwr sectors (21475 MB) sda: Write Protect is off SCSI device sda: drive cache: write back root ~:#
Now, let’s assume that the current system’s storage space is this:
root ~:# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-LogVol00 18G 4.0G 13G 24% / /dev/sda1 99M 40M 55M 42% /boot tmpfs 502M 0 502M 0% /dev/shm root ~:#
Next, we use the classic fdisk to create a partition on the newly added device (in this case /dev/sdb).
root ~:# 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 the 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-261, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-261, default 261): Using default value 261 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. SCSI device sdb: 4194303 512-byte hdwr sectors (2147 MB) sdb: Write Protect is off SCSI device sdb: drive cache: write back SCSI device sdb: 4194303 512-byte hdwr sectors (2147 MB) sdb: Write Protect is off SCSI device sdb: drive cache: write back Syncing disks. root ~:#
We then use the next utility to notify the operating system of the partition table changes on the new device…
root ~:# partprobe /dev/sdb root ~:#
And we finally create the physical volume for the new hard disk…
root ~:# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created root ~:#
And you can now extend the (logical) volume group that you want to have more space.
root ~:# vgentend VolGroup00 /dev/sdb1 Volume group "VolGroup00" successfully extended root ~:#
Remember, when extending the logical volume always leaving some space for LVM meta-data. So, we have…
root ~:# lvextend -L +1.8G /dev/VolGroup00/LogVol00 Rounding up size to full physical extend 1.81 GB Extending logical volume LogVol00 to 19.72 GB Logical volume LogVol00 successfully resized root ~:#
At last, we have to resize the extended logical volume to allocate the additional disk space.
root ~:# 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 5169152 (4k) blocks. The filesystem on /dev/VolGroup00/LogVol00 is now 5169152 blocks long. root ~:#
And obviously, the size was increased…
root ~:# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-LogVol00 20G 4.0G 15G 22% / /dev/sda1 99M 40M 55M 42% /boot tmpfs 502M 0 502M 0% /dev/shm root ~:#