How to extend the filesystem on a Vmware ESX guest Red Hat Linux server

Being the administrator of a large Linux shop, I have had to extend the filesystems on the Red Hat Linux server a few times. Each time I had the opportunity to extend the filesystem, I found that I struggled to remember the commands and steps. This is because Linx is a very command line driven operating system. All right that is not completely true anymore, some variants of Linux have graphical interfaces. Such as Red Hat has the graphical Logical Volume Manager (LVM), but I find the LVM to be buggy and unreliable at times. Personally I find it more reliable and easier to extend the filesystem using the command line interface.

The sequence to extend the filesystem is as follow.
(1) Add the new disk. In my case I was running Red Hat Enterprise 5 Linux as a guest operating system in Vmware ESX server. I simply had to increase the disk size using the Vmware Vcenter interface, under edit setting, disk provisioning. You will need to reboot the guest OS after to recognize the new disk size.

(2) Partition the new disk space using fdisk.

(3) Initialize the new partition, using the command pvcreate. Run partprobe to update the kernel.

(4) Extend the existing volume group with the new partition using the command vgextend.

(5) Extend the existing logical volume group using lvextend.

(6) Grow the filesystem with the command resize2fs.

Take a snapshot of the existing disks and to understand how they are partitioned.
The fdisk command will display the configuration of the existing disks. In this example there are two disks, a 10.7 GB /dev/sda and /dev/sdb which is 21.4 GB. Disk sda has two partitions, sda1 and sda2. The second disk sdb has only one partition sdb1.

You also need to take a snapshot of how the partitions are allocated to the volume groups. In the below example, the result of pvscan indicates that sda2 is mapped to the volume group VolGroup00. But where is sda1? Well sda1 is mapped to the /boot filesystem, you will see it when you issue the mount command. You can also use the command pvs or pvdisplay instead of pvscan.

Add the new disk or extend the existing disk in Vmware.
Expand the disk sda using the VMware Vcenter management console. Under the summary tab of the guest server, go to edit settings – disk provisioning. Simply increase the disk size and then reboot the guest OS.

Partition the disk
After the guest server has been rebooted it should see the size increment in sda. You now need to partition the new disk space using the command fdisk.

[root@linux_server ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 2610.
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)

First cylinder (1306-2610, default 1306): (hit the enter key to accept the default)
Using default value 1306
Last cylinder or +size or +sizeM or +sizeK (1306-2610, default 2610): (press the enter key to accept the default. You can also select something lower than 2610, if you do not want to use all available free disk space)
Using default value 2610

Command (m for help): t (select t to change the system id)
Partition number (1-4): 3 (select the new partition number)
Hex code (type L to list codes): 8e (Enter 8e which is the code for Linux LVM)
Changed system type of partition 3 to 8e (Linux LVM)

Command (m for help): p (this will print the partition table, verify the changes you made)

Command (m for help): w (this will write the table tto disk committing it)
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16:ice or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

Note: (May have to hit enter to get command prompt back)

Initialize the physical volume
Initialize the new physical volume with the command pvcreate /dev/sda3. If you have not rebooted the operating system after creating the new partition, then you will need to run partprobe for the kernel to recognize the new partition. Or else you will get an error.

Add new physical volume to the volume group VolGroup00
The command vgdisplay will show you the specifics of the existing volume groups. Note the VG size is 9.88 GB. You can also use vgs and vgscan to display a different view of all volume groups.

Extend the Logical Volume Group /dev/VolGroup00/LogVol00 which is mounted as the root filesystem.
Display the specifics for all logical volume with lvdisplay. You can also use lvscan or lvs to display a summarized view.

Extend /dev/VolGroup00/LogVol00 using lvextend. The option -l +100%FREE will use up all available disk space. You can also specify the size with the option -L18G, which will extend the logical volume to 18 GB. The option -L+1G will add another gigabyte to the logical volume.

Expand the filesystem /dev/mapper/VolGroup00-LogVol00 which is mounted on /. The command resize2fs can be used to enlarge or shrink ext2 or ext3 filesystems that are unmounted. On Linux kernel version that is equal or greater than 2.6, resize2fs can expand a mounted ext3 filesystem only.

Post navigation

About Andrew Lin

Hi,
I have always wanted to creat a blog site but never had the time. I have been working in Information Technology for over 15 years. I specialize mainly in networks and server technologies and dabble a little with the programming aspects.
Andrew Lin