May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

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:

fdisk to verify presence of new disk
pvcreate to initialize the new disk
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
vgextend to add the newly created physical volume to a volume group
lvextend to extend the size of the logical volume
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.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>