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:
Command (m for help): n <- hit n <enter> for a new partition
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.
Make sure the kernel sees the new partition:
Now we get to the LVM stuff, like a cake, from the bottom up, first we build a new physical volume:
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:
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.
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.