- How to to resize an ext4 partition from the command line?
- 8 Answers 8
- First: Increase the disk size.
- Second: Increase the partition size.
- Third: Enlargen the filesystem.
- Resizing a Partition + Filesystem on Linux from the CLI
- Looking to buy a Virtual or Dedicated server? Do you like privacy and low prices? Try Privex!
- Order a server TODAY! Privacy is affordable™
- 1. Installing GNU parted
- 2. Check fdisk to find the right disk / partition + disk/partition info
- 3. Resize the disk partition («physical» partition) on your partition table using parted
- 4. Resize the actual filesystem to fit the grown partition
- Looking to buy a Virtual or Dedicated server? Do you like privacy and low prices? Try Privex!
How to to resize an ext4 partition from the command line?
What’s the easiest way to resize an ext4 partition (or any type partition depending on the method) from the command line (potentially with the fewest commands, but also the easiest to understand)? Using a tool like Gparted is obviously easy in a GUI, but what about in the command line? I guess text-based GUIs can count for the answer too since it’s technically still in the command line. It just needs to be easy. By partition I mean a simple partition on a single disk of a personal computer (e.g. on a laptop). For example, I want to resize /dev/sda4 . There’s no RAIDs, there’s not more than one disk drive, there’s not anything complicated here. Just a simple partition on a single disk (/dev/sdaX on /dev/sda).
You probably know this, but Gparted is a GUI front-end to GNU Parted. Previous to Parted 2.4, you could resize, but this has since been removed. I suspect the only way is do it manually, resizing partition table, creating device maps, then resizing the file system.
I haven’t seen that specific one, but the gist of it is that you delete the partition then make a new one of the size you want. But what I’m inherently doing is not «deleting» anything, just resizing. The best answer to this question would likely be a way to «resize» a partition, not delete one and create a new one, and the resize2fs steps would be entirely unnecessary and happen in the background (because naturally and obviously the user also wants to use all the space in the partition so it shouldn’t be an extra step).
8 Answers 8
You can use fdisk to change your partition table while running. Refer to Live resizing of an ext4 filesytem on Linux (on The silence of the code blog):
Disclaimer: The following instructions can easily screw your data if you make a mistake. I was doing this on a VM which I backed up before performing the following actions. If you lose your data because you didn’t perform a backup don’t come and complain.
.
First: Increase the disk size.
In ESXi this is simple, just increase the size of the virtual disk. Now you have a bigger hard drive but you still need to a) increase the partition size and b) resize the filesystem.
Second: Increase the partition size.
- Delete the primary partition
- Delete the extended partition
- Create a new primary partition starting at the same sector as the original one just with a bigger size (leave some for swap)
- Create a new extended partition with a logical partition in it to hold the swap space
me@ubuntu:~$ sudo fdisk /dev/sda Command (m for help): p Disk /dev/sda: 268.4 GB, 268435456000 bytes 255 heads, 63 sectors/track, 32635 cylinders, total 524288000 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: 0x000e49fa Device Boot Start End Blocks Id System /dev/sda1 * 2048 192940031 96468992 83 Linux /dev/sda2 192942078 209713151 8385537 5 Extended /dev/sda5 192942080 209713151 8385536 82 Linux swap / Solaris Command (m for help): d Partition number (1-5): 1 Command (m for help): d Partition number (1-5): 2 Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): Using default value 1 First sector (2048-524287999, default 2048): Using default value 2048 Last sector, +sectors or +size (2048-524287999, default 524287999): 507516925 Command (m for help): p Disk /dev/sda: 268.4 GB, 268435456000 bytes 255 heads, 63 sectors/track, 32635 cylinders, total 524288000 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: 0x000e49fa Device Boot Start End Blocks Id System /dev/sda1 2048 507516925 253757439 83 Linux Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): e Partition number (1-4, default 2): 2 First sector (507516926-524287999, default 507516926): Using default value 507516926 Last sector, +sectors or +size (507516926-524287999, default 524287999): Using default value 524287999 Command (m for help): p Disk /dev/sda: 268.4 GB, 268435456000 bytes 255 heads, 63 sectors/track, 32635 cylinders, total 524288000 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: 0x000e49fa Device Boot Start End Blocks Id System /dev/sda1 2048 507516925 253757439 83 Linux /dev/sda2 507516926 524287999 8385537 5 Extended Command (m for help): n Partition type: p primary (1 primary, 1 extended, 2 free) l logical (numbered from 5) Select (default p): l Adding logical partition 5 First sector (507518974-524287999, default 507518974): Using default value 507518974 Last sector, +sectors or +size (507518974-524287999, default 524287999): Using default value 524287999 Command (m for help): p Disk /dev/sda: 268.4 GB, 268435456000 bytes 255 heads, 63 sectors/track, 32635 cylinders, total 524288000 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: 0x000e49fa Device Boot Start End Blocks Id System /dev/sda1 2048 507516925 253757439 83 Linux /dev/sda2 507516926 524287999 8385537 5 Extended /dev/sda5 507518974 524287999 8384513 83 Linux Command (m for help): t Partition number (1-5): 5 Hex code (type L to list codes): 82 Changed system type of partition 5 to 82 (Linux swap / Solaris) Command (m for help): p Disk /dev/sda: 268.4 GB, 268435456000 bytes 255 heads, 63 sectors/track, 32635 cylinders, total 524288000 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: 0x000e49fa Device Boot Start End Blocks Id System /dev/sda1 2048 507516925 253757439 83 Linux /dev/sda2 507516926 524287999 8385537 5 Extended /dev/sda5 507518974 524287999 8384513 82 Linux swap / Solaris 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. me@ubuntu:~$ sudo reboot
Third: Enlargen the filesystem.
You can do this with resize2fs online on a mounted partition.
me@ubuntu:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 91G 86G 12M 100% / udev 3.9G 4.0K 3.9G 1% /dev tmpfs 1.6G 696K 1.6G 1% /run none 5.0M 0 5.0M 0% /run/lock none 3.9G 144K 3.9G 1% /run/shm none 100M 16K 100M 1% /run/user me@ubuntu:~$ sudo resize2fs /dev/sda1 resize2fs 1.42.5 (29-Jul-2012) Filesystem at /dev/sda1 is mounted on /; on-line resizing required old_desc_blocks = 6, new_desc_blocks = 16 The filesystem on /dev/sda1 is now 63439359 blocks long. me@ubuntu:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 239G 86G 142G 38% / udev 3.9G 12K 3.9G 1% /dev tmpfs 1.6G 696K 1.6G 1% /run none 5.0M 0 5.0M 0% /run/lock none 3.9G 152K 3.9G 1% /run/shm none 100M 36K 100M 1% /run/user
Slight catch: After rebooting the swap space wasn’t active. Turned out you need to run mkswap , adjust /etc/fstab to the new UUID and turn the swap on
me@ubuntu:~$ sudo mkswap /dev/sda5 Setting up swapspace version 1, size = 8384508 KiB no label, UUID=141d401a-b49d-4a96-9b85-c130cb0de40a me@ubuntu:~$ sudo swapon --all --verbose swapon on /dev/sda5 swapon: /dev/sda5: found swap signature: version 1, page-size 4, same byte order swapon: /dev/sda5: pagesize=4096, swapsize=8585740288, devsize=8585741312
Edit /etc/fstab to replace the UUID for the old swap partition with the new one from mkswap .
Resizing a Partition + Filesystem on Linux from the CLI
Note that we’ll only be covering EXPANSION / GROWING, and not shrinking, since it’s much more difficult to shrink a partition than to grow it.
- A system running Linux (Ubuntu, Debian, Linux Mint, Fedora, CentOS, RedHat or anything else)
- Free space in your partition table — AFTER the partition you want to resize — to extend the partition into.
- The third-party program parted (available in most package managers)
Looking to buy a Virtual or Dedicated server? Do you like privacy and low prices? Try Privex!
We have virtual servers starting from just US$0.99/mo, and dedicated servers starting from as low as US$50/mo
Unlike other hosts, we don’t ask for any personal details — only a name (can be an alias / username), and an e-mail address so we can send you your server details and renewal invoices.
We also accept several different cryptocurrencies with our own in-house payment processor — no third parties involved in processing your payments.
At the time of writing, we currently accept: Bitcoin (BTC), Litecoin (LTC), Monero (XMR), Dogecoin (DOGE), HIVE, and HBD
Order a server TODAY! Privacy is affordable™
1. Installing GNU parted
For Debian-based distros such as Ubuntu, Debian, Linux Mint, etc. — you can install parted from apt:
sudo apt update sudo apt install -y parted
For Redhat-based distros (Fedora, Redhat, CentOS, Oracle, Amazon Linux, etc.), you can use either dnf (for modern distros) or yum (for older distros):
# for modern distros sudo dnf install -y parted # for older distros, or if you get an error saying dnf isn't installed sudo yum install -y parted
For Arch Linux, you can use pacman :
2. Check fdisk to find the right disk / partition + disk/partition info
Run the following command to get information about all disks and partitions on your system:
Look through the output to find the disk (e.g. /dev/sda ) that you want to resize, as well as the partition you want to resize.
You might also be able to find the disk /dev/ path by running df -h / (if you want to resize a different partition instead of the rootfs / — then replace / with the mountpoint of the partition you want to resize)
3. Resize the disk partition («physical» partition) on your partition table using parted
Once you’ve located the disk you want to resize a partition on (e.g. /dev/sda ), open it using parted:
Now use the print command within parted, which will print information about that disk, and the partitions on the disk, so you can figure out how much space you have available to expand the partition into, and the partition number of the partition you want to resize:
Finally, use resizepart [partition_number] with the number of the partition you want to resize, to actually resize the partition:
You may be warned that the disk is in use, this is generally okay, so enter yes
When asked to enter the End: — if the partition is at the end of the disk, you can enter the size of the disk outputted by print , but take off 0.2GB for safety, e.g. if it was 48.5GB — you’d enter 48.3G.
If the partition is between two partitions, then you’ll want to enter an end location at least 100mb before the «Start» of the following partition. For example, if you’re resizing partition 1 (start: 1020MB, end: 20GB), and partition 2 starts at 41.2GB — then you could set the End of part 1 to 41GB (~200mb before part 2).
4. Resize the actual filesystem to fit the grown partition
The command you’d run to grow a filesystem, depends on what filesystem the partition is actually formatted with.
Most Linux systems use the filesystem ext4 (or an older version such as ext3 — which uses the same command to resize anyway), though either yourself or the installer for your distro may have formatted some/all of the partitions with an alternative filesystem such as XFS, or Btrfs.
To discover the filesystem used by each partition on your system, use the following command:
To grow an ext2 , ext3 or ext4 filesystem, use the command (where /dev/sda1 is the partition device to resize):
To grow an xfs filesystem, use the command (where / is the MOUNTPOINT of the XFS formatted device):
To grow a btrfs filesystem, use the command (where / is the MOUNTPOINT of the Btrfs formatted device):
sudo btrfs filesystem resize max /
Finally, check the filesystem was resized by running df -h to list the size, used space, and avail space on all mounts:
Looking to buy a Virtual or Dedicated server? Do you like privacy and low prices? Try Privex!
We have virtual servers starting from just US$0.99/mo, and dedicated servers starting from as low as US$50/mo
Unlike other hosts, we don’t ask for any personal details — only a name (can be an alias / username), and an e-mail address so we can send you your server details and renewal invoices.
We also accept several different cryptocurrencies with our own in-house payment processor — no third parties involved in processing your payments.
At the time of writing, we currently accept: Bitcoin (BTC), Litecoin (LTC), Monero (XMR), Dogecoin (DOGE), HIVE, and HBD