Categories
Linux

Expanding / partition in Linux operating system live with no downtime

I had a disk that was not fully expanded. It only partitioned 4.8GB to the /. So I wanted to expand it live, with no downtime, or data loss. Either way, I would ALWAYS back up the data before doing this even you care about it at all. Either way you should have backups if you want to keep it regardless. I tested this just now with Almalinux 8, but I imagine it would work for a slew of other operating systems, as you only need fdisk and resize2fs, which all come with most operating systems.

I will say it one more time before we start, BACK UP YOUR DATA, always. And test those backups.

df -h to check current disk size or fdisk -l /dev/vda1

[root@72 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        3.8G     0  3.8G   0% /dev
tmpfs           3.8G     0  3.8G   0% /dev/shm
tmpfs           3.8G  395M  3.5G  11% /run
tmpfs           3.8G     0  3.8G   0% /sys/fs/cgroup
/dev/vda1       4.8G  3.8G  774M  84% /
tmpfs           777M     0  777M   0% /run/user/0

You will notice your mount pount/partition is tight or 100% used up. Expand that partition via KVM, VMWARE, Cockpit, Proxmox, etc. That is outside the scope of this article. It is technology dependent how you want to expand your disk. Once you’ve done that, you can reboot, or try running a rescan.

echo 1 > /sys/block/vda/device/rescan

Once that is done and you’ve expanded your disk, we can expand it. Drop into fdisk:

[root@72 ~]# fdisk /dev/vda

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Then we will run through deleting the primary partition, and recreating it with the entire disk you now have. Here is the combo of letters to get through the prompts. I will follow it up with text I ran, as I prefer to see it in console. p, d, 1, n, p, 1 and then hit enter for default first and last sector. Then n to NOT remove the signature, and w to write it out.

Command (m for help): p
Disk /dev/vda: 250 GiB, 268435456000 bytes, 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
Disklabel type: dos
Disk identifier: 0x318ba904

Device     Boot     Start       End   Sectors  Size Id Type
/dev/vda1  *         2048 507508351 507506304  242G 83 Linux
/dev/vda2       507508352 524285567  16777216    8G 82 Linux swap / Solaris

Command (m for help): d
Partition number (1,2, default 2): 1

Partition 1 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1,3,4, default 1): 1
First sector (2048-524287999, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-507508351, default 507508351):

Created a new partition 1 of type 'Linux' and of size 242 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help): w

The partition table has been altered.
Syncing disks.


Now that is done you can resize to expand it with resize2fs and run df -h once again to see the expansion complete.

[root@72 ~]# resize2fs /dev/vda1
resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 31
The filesystem on /dev/vda1 is now 63438288 (4k) blocks long.

[root@72 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        3.8G     0  3.8G   0% /dev
tmpfs           3.8G     0  3.8G   0% /dev/shm
tmpfs           3.8G  395M  3.5G  11% /run
tmpfs           3.8G     0  3.8G   0% /sys/fs/cgroup
/dev/vda1       239G  3.8G  225G   2% /
tmpfs           777M     0  777M   0% /run/user/0
[root@72 ~]#

Boom, enjoy.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.