If you clone your VPS to a server with a larger disk, or something goes wrong when expanding your available disk space you may have a situation where your root partition on /dev/vda1 is not taking up all the space available.
For example, here we have a root partition of 10G on a 30G disk (commands entered are shown starting with #, notable parts in bold):
# df -Th / Filesystem Type Size Used Avail Use% Mounted on /dev/vda1 ext4 9.7G 2.6G 7.1G 27% / # fdisk -l /dev/vda | head -1 Disk /dev/vda: 30 GiB, 32212254720 bytes, 62914560 sectors
To expand /dev/vda1 so that it takes up the full 30G available, there are three steps to take:
- Take a backup. While always a good idea, editing the partition table incorrectly will mess up the disk.
- Modify the partition table so that /dev/vda1 is 30G in size
- Resize the ext3/ext4 partition so that it takes up the new, full size of /dev/vda1
Modify the Partition Table
# fdisk -l /dev/vda Disk /dev/vda: 30 GiB, 32212254720 bytes, 62914560 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 Disklabel type: dos Disk identifier: 0x3ac5a058 Device Boot Start End Sectors Size Id Type /dev/vda1 * 2048 20971519 20969472 10G 83 Linux
Confirm we have a single partition, and importantly it is marked as boot and starts at sector 2048. This information is crucial later, so copy it to your notepad.
Next we use the fdisk tool to temporarily ‘delete’ partition /dev/vda1, before immediately recreating it with the same size, type, and start location. This is safe when accomplished within a single use of fdisk tool, because it does not actually write changes until requested. When the changes take effect the partition is not deleted and recreated; the partition remains intact and the outer boundary is simply adjusted to accommodate the larger partition size. Even the file system remains intact (which is important). Here is the full process, with my fdisk input in bold (press enter after each input):
# fdisk /dev/vda Welcome to fdisk (util-linux 2.27.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): d Selected partition 1 Partition 1 has been deleted. Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-62914559, default 2048): 2048 Last sector, +sectors or +size{K,M,G,T,P} (2048-62914559, default 62914559): [ENTER for default] Created a new partition 1 of type 'Linux' and of size 30 GiB. Command (m for help): t Selected partition 1 Partition type (type L to list all types): 83 Changed type of partition 'Linux' to 'Linux'. Command (m for help): a Selected partition 1 The bootable flag on partition 1 is enabled now. Command (m for help): p Disk /dev/vda: 30 GiB, 32212254720 bytes, 62914560 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 Disklabel type: dos Disk identifier: 0x3ac5a058 Device Boot Start End Sectors Size Id Type /dev/vda1 * 2048 62914559 62912512 30G 83 Linux Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Re-reading the partition table failed.: 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).
There are a few steps here, so breaking it down:
- First we enter d to delete a partition. As there is only one partition (/dev/vda1) it is automatically selected.
- Next we create a new partition.
- n to start the new partition function
- p for a primary partition
- 1 to create /dev/vda1
- 2048 as our start sector (remember, we we pulled this value from “fdisk -l” earlier)
- Press Enter (blank input) to select the default last sector, which will be the end of the disk
- The type should be set to match what it was previously (which from fdisk -l, we can see was ID 83). Press t to set partition type, then enter 83 to set it to Linux.
- We can see in the initial “fdisk -l” output an asterisk under boot, meaning the partition was marked bootable. To set the bootable flag, press a for the boot function and enter 1 if prompted for partition number.
- Now check your work. Enter p command to print the new (in-memory) partition table, and compare the last line of output to what is in your notepad. Everything should be the same except for the “end”, “sectors”, and “size” .
- Finally, w to write new partition table to disk.
# partprobe /dev/vda
On success there is no output.
Resizing /dev/vda1 filesystem
# df -Th / Filesystem Type Size Used Avail Use% Mounted on /dev/vda1 ext4 9.7G 2.6G 7.1G 27% /
This is because we have expanded the partition (the area of disk that the filesystem is located in) but the filesystem has not been expanded to fll it. Correct this with the resize2fs tool:
# resize2fs /dev/vda1 resize2fs 1.42.13 (17-May-2015) Filesystem at /dev/vda1 is mounted on /; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/vda1 is now 7864064 (4k) blocks long.
And confirm that it worked:
# df -Th / Filesystem Type Size Used Avail Use% Mounted on /dev/vda1 ext4 30G 2.6G 27G 9% /
The root filesystem size has grown to 30G, using all the space available on /dev/vda.
!! If you use Koozali (smeserver) make sure to install using “sme noraid nolvm” when installing on a Virtual Private Server !!