Anyone who runs Linux servers for an extended time is bound to run into the need to expand disk space once in a while. LVM is a great tool to help with this, but, it’s the new kid on the block for partition management. If you’re dealing with a legacy system, chances are, you’re not going to have LVM available and instead will need to rely on standard file system tools to work this out.
First, a couple of notes about these commands and instructions…
- THIS CAN BREAK YOUR STUFF, this worked for me, it might not work for you, use at your own risk
- This is for Debian and derivatives, mileage will vary on other distros
- This is for a non-root partition, maybe it will also work there but I would strongly recommend using an offline tool like booting to a gparted instance if you’re going to resize root
- This is not for LVM based partitions, if you are using LVM, you should look at that documentation instead
- I’m also assuming you’re using a virtual environment which allows disk expansion on-the-fly or that you haven’t partitioned your whole disk
So, first things first, expand your disk in your virtual environment. This is typically done by simply going into the settings for the virtual machine and increasing the disk size by some amount. If this option isn’t available then it’s likely that your VM software will not allow an online expansion, check the documentation on your software.
Next you’ll need to get the virtual machine to pick up the newly allocated space. On Debian, you need to identify the disk object by looking in the sys area as follows:
ls /sys/class/scsi_disk/
This will list all of the physical disks (or virtually physical in this case…) allocated to the system. Next you need to initiate a rescan:
echo '1' > /sys/class/scsi_disk/0\:0\:0\:0/device/rescan
Make sure to modify the “/0\:0\:0\:0/” portion to match the disk that you want to rescan. It won’t hurt to do this across any additional disks but it must be done on the one you’re looking to expand.
You should now be able to see the expanded disk by issuing the fdisk command:
fdisk -l /dev/sda
Where sda is your subject disk, if you don’t already know this, you might want to just do an fdisk -l which will show all of your drives and partitions. The output should have revealed the additional space allocation, if it does not, something went wrong and you will need to troubleshoot further.
Now comes the dangerous part. You’re going to need to remove and recreate the partition table followed by expanding the use. You may be able to use another program such as cfdisk but for this we’ll be using trusty old fdisk.
Start by getting into the disk you want to expand:
fdisk /dev/sda
Next print the partition to verify you have the right info; you should see something like below:
Command (m for help): p Disk /dev/sda: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000062b4 Device Boot Start End Blocks Id System /dev/sda1 * 2048 20969471 10483712 83 Linux
Next we’re going to delete it, yep, that’s right, delete it:
Command (m for help): d Selected partition 1
Now that the original partition is gone we need to recreate it:
Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-956, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-956, default 956): Using default value 956
This will make a new partition using all available space on the partition. You can issue a “p” command again to make sure it happened or just “w” to write the changes followed by “q” to quit:
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Command (m for help): q
Now that the partition is available to the system, the table has to be resized so the OS can use the new space. This is accomplished by using the resizefs utility, notice this is for the partition so make sure to use the corresponding partition number, sda1 vs. sda:
# resize2fs /dev/sda1
Finally, to validate, you can issue various commands to validate the new space. I recommend the df command as follows which will show you all the partitions, sizing, and utilization statistics:
# df -h
You should see all you expect to at this point and be off and running with your shiny new disk space, no reboot required!