Extending a root filesystem in Linux without LVM
January 9, 2013 21 Comments
Overview
One of the great things about working with virtual environments is that if you start to run out of space in a virtual machine, it is usually trivial to extend your disk. I’m going to take the example of a Linux guest under VMware Workstation though this should apply to most hypervisors and flavors of Linux.
If your VM is not using LVM, the steps you’ll perform are:
- Extend the size of the physical disk
- Extend the size of the partition on the physical disk
- Do an online resize of the filesystem to use the new space
There is a caveat that your root filesystem (or the one you’re extending) must be the last one on disk so it can grow contiguously into the free space. If your filesystem is very old, created with an older build of mkfs.ext2, it may not be configured for online resize (missing -O resize_inode flag – more recent versions of mkfs.ext2 automatically reserve space for block group descriptor tables, older versions ~2007/8 may not) and thus these steps won’t work.
If your guest VM is critical in any way, shape or form, I strongly recommend cloning it to keep a backup in case something goes wrong during the filesystem resize process.
Online resize of a root filesystem without LVM
Since you don’t have LVM, your only option is to increase the size of the hard disk and then expand the partition containing the filesystem you wish to expand. The contraints mentioned above apply. The filesystem to resize MUST be the last one on the disk and you can not have more than 3 partitions on the disk already. Confirm that you meet these requirements:
[root@temeria ~]# parted GNU Parted 2.1 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sda: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 316MB 315MB primary ext4 boot 2 316MB 17.2GB 16.9GB primary ext4
As you can see here, partition 2 is the last one on disk which meets our criteria.
If you find that you have a swap partition (#3) after the root filesystem, there is a workaround.
Exit parted and run “swapoff -a” to turn off all swap. Then go back into parted and delete the swap partition, so if it was number 3, you’d run parted and type “rm 3”. After this, edit /etc/fstab and remove the line pointing to the swap partition so your system doesn’t try to mount it at the next boot. Once you’ve done this, you should be able to continue with the rest of this process.
You will then end up without a swap partition at the end of this process.
We can confirm that this corresponds with the root filesystem by taking a look at our mounted filesystems:
(type “q” to exit out of parted first)
[root@temeria ~]# df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 16274852 2196088 13252040 15% / tmpfs 1025580 88 1025492 1% /dev/shm /dev/sda1 297485 32659 249466 12% /boot
Increase disk allocation in the hypervisor
The first thing you need to do is increase the size of your existing disk.
In VMware Workstation or the vSphere client, navigate to the virtual machine you’re working with, edit it’s settings and under the hardware tab, click on the Hard Disk. On the right side, you’ll see a button labelled Utilities. Click that and click on expand. You can now enter a new size for the disk. Once this is done, you’ll get a popup notifying you of the success.
The next step is to now consume the extra space in the virtual machine by increasing the partition size and then the filesystem size.
Power on the VM and log in to the shell. While is it possible to do this via GUI, I prefer to use the shell for its more universal accessibility.
[root@temeria ~]# parted
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 25.8GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 316MB 315MB primary ext4 boot
2 316MB 17.2GB 16.9GB primary ext4
You can see the disk size has increased now.
Increase the partition size
You can’t resize a mounted filesystem with parted and resize2fs won’t resize the underlying partition. The workaround is a bit tricky and you have to be careful to keep the start cylinder the same when doing this. What you do is, in fdisk you delete the partition and recreate it with a larger size making sure you keep the start location (cylinder) the same. The example below illustrates this:
[root@temeria ~]# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to switch off the mode (command ‘c’) and change display units to sectors (command ‘u’).
Command (m for help): p
Disk /dev/sda: 25.8 GB, 25769803776 bytes
255 heads, 63 sectors/track, 3133 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00073409
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 2097 16534528 83 Linux
Command (m for help): d
Partition number (1-4): 2
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (39-3133, default 39):
Using default value 39
Last cylinder, +cylinders or +size{K,M,G} (39-3133, default 3133):
Using default value 3133
Command (m for help): p
Disk /dev/sda: 25.8 GB, 25769803776 bytes
255 heads, 63 sectors/track, 3133 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00073409
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 3133 24857598+ 83 Linux
Command (m for help): w
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.
Reboot the system to ensure the partition table is reread.
Resize the filesystem
This is perhaps the simplest step.
Simply execute the resize2fs command with your partition as an argument.
[root@temeria ~]# resize2fs /dev/sda2 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/sda2 is mounted on /; on-line resizing required old desc_blocks = 1, new_desc_blocks = 2 Performing an on-line resize of /dev/sda2 to 6214399 (4k) blocks. The filesystem on /dev/sda2 is now 6214399 blocks long.
Check to see confirm the resize operation. You can use df -k or df -h (human friendly output)
[root@temeria ~]# df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 24469104 2196340 21035568 10% / tmpfs 1025580 88 1025492 1% /dev/shm /dev/sda1 297485 32659 249466 12% /boot [root@temeria ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 24G 2.1G 21G 10% / tmpfs 1002M 88K 1002M 1% /dev/shm /dev/sda1 291M 32M 244M 12% /boot
And that is it, you’re done!

Excellent Post. Works Smoothly.
Thank you!
Excellent documentation.
Thanks a lot! You saved my day.
thanks this realy helped me…….By T
but question do i have to turn swap partion on
Very good description, Thank you very much for posting this.
Pingback: Resizing linux filesystem without lvm | yet another draggy weblog
You shouldn’t Use Partition type as Extended after deleting the existing Partition. Your existing Partition was a Primary Linux Partition . When you create the partition as an Extended one the Print command should show it as Extended , Not as Linux . And you forgot to mention one thing , When creating the new partition the starting cylinder should be same as the Old one
excellent, it helped me to expand the root partition with ease…highly appreciate your help and knowledge
Very easy to follow. Well done. Works like a champ.
Thanks!
How do we re-enable swap? or is it necessary?
How do we re-enable swap? or is it not necessary?
very helpful, saves the day for a windows admin. much appreciated
excellent,excellent,excellent
I can’t believe how simple it was! Worked fine on VirtualBox 5.0 hosting a VM CentOS 5.7. Thank you very much!
Thank you, worked perfect
worked. But, when I rebooted it said no file system and couldn’t start, so booted of a disk, and it was purely because the partition wasn’t active. So when doing fdisk after re-creating the partition ensure if it’s the boot partition that you mark it active. So ensure it’s the bootable partition I just went back into fdisk and did a then the relevant partition in my case the 2nd partition with the 1st being swap.
Yo!! Thanks for the post!!! Very easy. took me less than ten minutes.
A few people asked about recreating swap after you delete the swap partition to extend your root partition. I had the same question. I did not find a guide for a partition, but I did find one to create a swap file instead, and it has worked fine:
https://askubuntu.com/questions/390769/how-do-i-resize-partitions-using-command-line-without-using-a-gui-on-a-server
Create a 512 MB file called /swapfile. This will be our swap file.
fallocate -l 512M /swapfile
Set the right permissions (because a world-readable swap file is a huge local vulnerability):
chmod 600 /swapfile
After creating the correctly sized file, format it to swap:
mkswap /swapfile
Activate the swap file:
swapon /swapfile
Edit /etc/fstab and add an entry for the swap file:
/swapfile none swap defaults 0 0
credit to tottie on askubuntu.com