Ubuntu 21 extend partition

ubuntu 21

Suppose we have a virtual machine with the Ubuntu operating system installed and we want to increase the size of a partition. In this article, we will take an example of Ubuntu 21 extend partition without LVM.

Inputs

Our Ubuntu 21 is installed on Hyper-V. First, let’s check the size of the file system by running df -h (all commands are executed as root):

# df -h
Filesystem           Size  Used Avail Use% Mounted on
tmpfs            394M         1,4M  392M            1% /run
/dev/sda3         24G         7,4G   16G           33% /
tmpfs            2,0G            0  2,0G            0% /dev/shm
tmpfs            5,0M            0  5,0M            0% /run/lock
tmpfs            4,0M            0  4,0M            0% /sys/fs/cgroup
/dev/sda2        512M         7,8M  505M            2% /boot/efi
tmpfs            394M         144K  394M            1% /run/user/1000

Our system partition, mounted in /, is 24 GB in size.

Let’s see the output fdisk -l:

# fdisk -l
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: F5F02D9D-060D-422F-BA27-1981A6CA23F4

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  1054719  1050624  513M EFI System
/dev/sda3  1054720 52426751 51372032 24.5G Linux filesystem

In this example, we have 1 disk /dev/sda with a size of 25 gigabytes, which is divided into 3 logical ones: /dev/sda1, /dev/sda2 and /dev/sda3 with the Linux filesystem type – we are interested in it.

Increasing disk size

In a virtualization environment, we increase the size of the hard disk of our virtual machine. Most likely, your virtualization system will ask you to turn off the VM first. I increased the disk to 30 gigabytes, start the machine:

#fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: F5F02D9D-060D-422F-BA27-1981A6CA23F4

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  1054719  1050624  513M EFI System
/dev/sda3  1054720 52426751 51372032 24.5G Linux filesystem

Ubuntu 21 extend partition

Attention! Before starting work on expanding the system partition, be sure to make a backup copy of your data!

After increasing the size of the disk, you need to increase the system partition itself. Let’s execute fdisk /dev/sda, where /dev/sda is the label of our disk (Disk /dev/sda):

# fdisk /dev/sda

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.

Command (m for help):

We enter p to look at the list of partitions:

Command (m for help): p

Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: F5F02D9D-060D-422F-BA27-1981A6CA23F4

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  1054719  1050624  513M EFI System
/dev/sda3  1054720 52426751 51372032 24.5G Linux filesystem

To expand a partition, you must first delete information about it. To do this, enter d and specify the partition (3 for /dev/sda3):

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

Partition 3 has been deleted.

In this case, only the record about the partition is deleted, the data itself remains on the disk!

Enter n – creating a new partition

Command (m for help): n

Next, we indicate the number of the partition:

Partition number (3-128, default 3):3

Next, the starting and ending sectors are indicated. Be sure to check that they match the hyphenated values. This way we use all the unallocated space:

First sector (1054720-62914526, default 1054720): 1054720
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1054720-62914526, default 62914526): 62914526

Created a new partition 3 of type 'Linux filesystem' and of size 29.5 GiB.

As you can see, a 29.5 gigabyte partition was created with the Linux filesystem type.

It will also ask if we want to delete the current filesystem type. We refuse:

Partition #3 contains a ext4 signature.

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

It remains only to save the partition table:

Command (m for help): w

Reboot the virtual machine:

# reboot

Now we will use the resize2fs utility (for ext4) to increase the size of the filesystem:

# resize2fs /dev/sda3

resize2fs 1.45.6 (20-Mar-2020)
open: Permission denied while opening /dev/sda3
n0mit@n0mit-vm:~$ sudo resize2fs /dev/sda3
resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/sda3 is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 4
The filesystem on /dev/sda3 is now 7732475 (4k) blocks long.

Checking the result:

# df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           394M  1.4M  392M   1% /run
/dev/sda3        29G  7.4G   21G  27% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           4.0M     0  4.0M   0% /sys/fs/cgroup
/dev/sda2       512M  7.8M  505M   2% /boot/efi
tmpfs           394M   84K  394M   1% /run/user/126
tmpfs           394M   72K  394M   1% /run/user/1000

As you can see in Ubuntu 21 extend artition is not such a difficult task.

Leave a Comment