Reese Knowledgebase

Extending root partition using LVM

View Kristian Reese's profile on LinkedIn

If you like this article, please +1 or Recommend via FB with the provided buttons above:

Article ID: 145
by: Reese K.
Posted: 03 Jan, 2014
Last updated: 09 Jan, 2014
Views: 2303

Extending root partition using LVM

After deploying a VMware virtual machine via template, it may be necessary to extend the root file system based on use requirements as templates typically are minimal in their configuration.  In this example, I opted to add a virtual disk instead of increasing the size of the existing vmdk, and extend the LVM vg (volume group) and lv (logical volume), then the file system itself.

In this particular example, the above description will be performed on a RedHat 6.3 OS.  Therefore, there is no need to manually add the disk as the kernel now does so automatically.

The steps to be performed include:

  1. fdisk to verify presence of new disk
  2. pvcreate to initialize the new disk
    1. In this example, I do NOT create a partition on the new disk.  Instead, I'm just using the whole disk.  In the case of physical media (using a recycled disk from another system), and you opt to use the whole disk device only (instead of partitioning it first), the partition table must be erased, which will obviously destroy all data on that disk, so be sure this is what you want to do.  Refer to the pvcreate man page.  To erase the partition table on a previously used disk, zero out the first sector with: 
      dd if=/dev/zero of=PhysicalVolume bs=512 count=1
  3. vgextend to add the newly created physical volume to a volume group
  4. lvextend to extend the size of the logical volume
  5. resize2fs to resize the root partition, which of course is mounted.  Refer to the resize2fs man page, citing "If the filesystem is mounted, it can be used to expand the size of the mounted filesystem, assuming the kernel supports on-line resizing.  (As of this writing, the Linux 2.6 kernel supports on-line resize for filesystems mounted using ext3 and ext4.)."

Real life example:

QAMAIN ctbapp2a ~ # df -Ph /
Filesystem                       Size  Used Avail Use% Mounted on
/dev/mapper/vg_ctbapp2a-lv_root   16G  2.7G   12G  19% /
QAMAIN ctbapp2a ~ #
QAMAIN ctbapp2a ~ # fdisk -l /dev/sdb

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
64 heads, 32 sectors/track, 20480 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

QAMAIN ctbapp2a ~ # pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created
QAMAIN ctbapp2a ~ #
QAMAIN ctbapp2a ~ # vgextend vg_ctbapp2a /dev/sdb
  Volume group "vg_ctbapp2a" successfully extended
QAMAIN ctbapp2a ~ #
QAMAIN ctbapp2a ~ # lvextend /dev/vg_ctbapp2a/lv_root /dev/sdb
  Extending logical volume lv_root to 35.63 GiB
  Logical volume lv_root successfully resized
QAMAIN ctbapp2a ~ #
QAMAIN ctbapp2a ~ # resize2fs /dev/vg_ctbapp2a/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_ctbapp2a/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 3
Performing an on-line resize of /dev/vg_ctbapp2a/lv_root to 9339904 (4k) blocks.
The filesystem on /dev/vg_ctbapp2a/lv_root is now 9339904 blocks long.

QAMAIN ctbapp2a ~ # df -Ph /
Filesystem                       Size  Used Avail Use% Mounted on
/dev/mapper/vg_ctbapp2a-lv_root   36G  2.7G   31G   8% /

In the case of physical disk storage, the process would be nearly identical when adding another physical hard disk.  If you do not have the option to add physical media, and there exists ample storage on the disk containing the root file system, you can extend said disk by adding another partition to it.  This would require a reboot for the changes to be seen, otherwise, the overall process would be the same except you'd specify the newly created partition in the place of /dev/sdb above from the pvcreate command on down.

This article was:   Helpful | Not Helpful
Prev   Next
Couldn't find device with uuid     rsyslog

Showing: 1-2 of 2  
Kamlesh | 08 Sep, 2016 12:02 AM
After adding the second disk to rootvg system will fail to boot. Have you tried booting up machine after extending the / using two device.
ktreese | 08 Sep, 2016 12:32 PM
Hi Kamlesh - it's been a while since I've done this process. All I can say is that I don't recall any issues.
Prev   Next
Couldn't find device with uuid     rsyslog