In this article, we will overview how to resize a partition in a Linux. We will use only built-in tools and commands.
Before you begin with partitions, make sure you have solid backup. Once data is deleted it cannot be undeleted.
Primary way of resizing drive partitions in Linux is to delete the old one and create a new partition, using the previous starting sector (you can think about it like “left bound of the new partition”). Then you need to simply modify the file system properties to fit the new bounds. If done carefully, you will not lose your data on the modified partition, despite the frightening phrase "delete the partition".
Linux has built-in tools (commands) to work with partitions:
- df / lsblk - allows you to list available drives, including block devices.
- mount / umount - simple tools allowing you to detach a partition to change its properties.
- fdisk - partitions management tool. It will recreate partitions with newly specified bounds.
- e2fsck - allows checking of modified file system for errors.
- resize2fs - modifies existing file system to fit new partition size bounds.
In this example we are going to use Ubuntu Server 14 as an instance in Amazon EC2 cloud. So, the disk we are working with is a block EBS device called /dev/xvdb.
Note: depending on your Linux distribution and available storage devices, volume and drive names can be different. You have also use the sudo command if your current account doesn’t have “root” privileges.
Shrinking or Extending Linux Partition
First of all, let’s find the partition we want to resize. Use df -h command to list available partitions:
The last line contains the device /dev/xvdb1 - it is our working partition. Note that there are actually 2 names:
- /dev/xvdb - it is whole device.
- /dev/xvdb1 - one partition allocated on /dev/xvdb device.
You can check it using, for example, the lsblk tool (for block-level devices only):
I have created the file file_should_remain.test to check whether the resized partition did not lose its data
We need to unmount the volume before modifications - just run the umount <mounting point> command, where mounting point is a file system’s representation of the device. And don’t forget sudo to get appropriate permissions:
Now run fdisk command with our test disk name as an argument and then type “p” to print its partitions list:
The last line contains important info about the current partition size and its bounds: “Start” and “End”. Remember “Start” value (2048 in this example). Now delete this partition by typing “d” and then type “w” to save these modifications:
After the old partition is removed we need to create the new one, either a bigger or smaller size. The following procedure is the same for both actions, but keep in mind that:
- You can extend the partition only to the space available at the “right” side. Simply saying, free space should be at the end of resizing partition.
- You cannot shrink the current partition to a size smaller than the actual data stored.
- It is a must to specify the same starting point as the previous partition while creating the new one. Otherwise you may lose your data!
In our example, the new partition will be smaller size so the Shrink operation will be performed. Let’s run fdisk again and type “n” - creating the new partition:
You may use default values in most fdisk questions, but carefully check these highlighted on the picture above: first sector (starting point) and last sector. You can use the +<size>G format to specify a desired partition size in Gigabytes (use “M” for Megabytes). Finish by typing “w” to save changes.
Now we need to verify partition consistency by running e2fsck:
Note the highlighted line - the tool shows that there is a mismatch in the file system configuration and current partition size. This is due to shrinking the partition with no changes made to the file system metadata. Just answer “no” to continue checking.
Let’s fix the error using resize2fs and then check the filesystem again:
Now the file system block size is the same as for partition configuration. Finally, let’s mount the resized volume and check that our data is still here:
As you can see, our test file is still here and the partition has a new size.
That's it. Now you have fresh-new partitions. If you have any questions or comments regarding the article, let us know.