Resize a root filesystem on the fly

Resize the root filesystem on a Red Hat Virtual machine without rebooting

Most sys-admins have been there, a full file system on a critical production server that you cannot afford to reboot.. If you are using VMWare, and linux LVM, You are in luck. As always, Keep good backups, and use at your own risk, mistakes can render your system completely unusable.

This guide can be used  for any filesystem that has been mounted on a Linux logical volume.

This article makes a few assumptions:

1. You are using VMWare or another hypervisor that allows online resizing of disk files

2. You are using Red Hat, CentOS or any other Linux distro with the root (/) file system mounted on a Linux logical volume.

3. you are using the first scsi hard disk in the virtual machine for your root file system, with one or two partitions

The first step is to resize the disk file.. for vmware you can use the GUI and edit the VM settings, and increase the size of the hard disk.

- Important - You must first remove all snapshots for the disk file you want to resize

Next we switch to the Virtual machine and rescan the disk:

echo 1 > /sys/class/scsi_device/0\:0\:0\:0/device/rescan

where 0:0:0:0 is disk 0 on scsi bus 0. You do need the backslash to escape the colon.

If you check the output of dmesg, you will see something like this:

sda: detected capacity change from 2684354560 to 10737418240

The remaining steps are where we use LVM to resize the logical volume on which the root filesystem resides. LVM is like a layer cake, you have to start at the bottom with the physical disk and work your way up. I have been unable to get the Linux kernel to re-read the partition table for a mounted filesystem, but with LVM we don't need to resize any partitions, we can just add another one:

fdisk /dev/sda

Command (m for help): n   <- hit n <enter> for a new partition

Command action
   e   extended
   p   primary partition (1-4)

hit p <enter> for a primary partition

Partition number (1-4): 2 <- choose the next available partition

First cylinder (523-1305, default 523): <- hit enter to choose the first free cylinder
Using default value 523
Last cylinder or +size or +sizeM or +sizeK (523-1305, default 1305): <- hit enter to use all free space
Using default value 1305

Command (m for help): t <- Change the partition type, hit t <enter>

Partition number (1-4): 2 <- choose the partition you created

Hex code (type L to list codes): 8e <- use 8e for Linux LVM

Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): w <- write the new partition table
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

Make sure the kernel sees the new partition:

partprobe

Now we get to the LVM stuff, like a cake, from the bottom up, first we build a new physical volume:

pvcreate /dev/<disk><partition_number>

example: pvcreate /dev/sda2

Physical volume "/dev/sda2" successfully created

Next we add the newly created physical volume to the Logical volume that contains the root file system. I'll use the red hat default that gets created during the install:

vgextend <volume_group> /dev/<disk><partition_number>

example: vgextend VolGroup00 /dev/sda2

 Volume group "VolGroup00" successfully extended

Next is to resize the logical volume on which the root (/) file system is created:

lvextend /dev/<volume_group_name>/<logical_volume_name> /dev/<disk><partition> 

example: lvextend /dev/VolGroup00/LogVol01 /dev/sda2

  Extending logical volume disk1 to 9.41 GB
  Logical volume disk1 successfully resized

Nearly there! all we need to do now is to resize the filesystem:

resie2fs /dev/<volume_group_name>/<logical_volume_name>  

example: resize2fs /dev/VolGroup00/LogVol01

resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol01 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol01 to 2465792 (4k) blocks.
The filesystem on /dev/VolGroup00/LofVol01 is now 2465792 blocks long.

Check for free space.

df -h

The out put will show the new size of the file system, all without a reboot of the virtual machine. Very handy when your mail server root file system fills up.

Rob Sunday 16 January 2011 - 4:39 pm | | Tech Stuff

three comments

Dave

Nice write-up. It’s nice to have this all in one place. I appreciate your taking the time to post it. Dave

Dave, - 21-09-’12 09:32
Jason

Thanks for this! It worked great. We only had a couple of edits. We had to restart the virtual machine for the pvresize command to work. And we also declared the number of free extents that we were adding to the volume, in the lvextend command.

Jason, - 26-02-’13 23:19
Fenris

Thanks for a very helpful walkthrough. Too many other guides, even those talking about “on the fly” or “online” resizing just casually slip a reboot in there.

Just wanted to add also, that RHEL6 doesn’t have partprobe anymore, so there you’ll need something like

partx -a /dev/sda

in order to be able to use the newly created partition in LVM.

Fenris, - 14-05-’14 15:11
(optional field)
(optional field)
To prevent automated comment spam, I require you to answer this silly question

Comment moderation is enabled on this site. This means that your comment will not be visible until it has been approved by an editor.

Remember personal info?
Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.