Debian 11 extend partition

Debian 11 extend partition

Suppose we have a virtual machine with the Debian operating system installed and we want to increase the size of a partition, for example a system partition. In this article, we will take a look as an example of at Debian 11 extend partition without LVM.

Introduction

Our Debian 11 is installed on Hyper-V. First, let’s check the version:

$ cat /etc/debian_version
11.1

In the future, we need to perform actions as the root user, connect under this user:

$ su -
Password:

Now let’s see the size of the filesystem by running df -h:

# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            944M     0  944M   0% /dev
tmpfs           192M  500K  192M   1% /run
/dev/sda1        19G  990M   17G   6% /
tmpfs           960M     0  960M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           192M     0  192M   0% /run/user/1000

Our system partition /dev/sda1, mounted on / and has a size of 19 GB .

Let’s see the output of fdisk -l:

# fdisk -l

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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: dos
Disk identifier: 0x73000288

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 39942143 39940096   19G 83 Linux
/dev/sda2       39944190 41940991  1996802  975M  5 Extended
/dev/sda5       39944192 41940991  1996800  975M 82 Linux swap / Solaris

In this example, we have 1 disk /dev/sda with a size of 20 gigabytes, which is divided into 3 partitions: /dev/sda1, /dev/sda2 and /dev/sda5. As you remember, we are interested in the /dev/sda1 with the Linux type.

Increasing disk size

In the virtualization environment we increase the size of the hard disk of our virtual machine. If your virtualization environment allows you to do this without shutting down the machine, then resize the disk and run:

# echo 1 > /sys/block/sda/device/rescan

If it is not possible to increase the disk without turning off the virtual machine, then turn off and resize. I increased the disk to 25 gigabytes, start the machine and check:

# 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: dos
Disk identifier: 0x73000288

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 39942143 39940096   19G 83 Linux
/dev/sda2       39944190 41940991  1996802  975M  5 Extended
/dev/sda5       39944192 41940991  1996800  975M 82 Linux swap / Solaris

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

After increasing the size of the disk, you must enlarge the partition itself. I specifically installed Debian partitioned as the installer suggested, meaning that many would do so. And what do we see? That we can’t just extend the /dev/sda1 partition, because immediately followed by sda5. Therefore, you first need to move it to the end of the disk (where the added sectors are located), and only then expand sda1 itself. But if you have only one partition in the fdisk output or it is at the end of the disk (look at the end sectors), you can go directly to the Debian 11 extend partition.

Move a swap partition to the end of the disk

Here it is immediately necessary to make a reservation that we will transfer the partition with swap, so you can simply delete it and create a new one. If you need to move partitions with data, then this is a different task. It is easier to solve using GParted on a Live CD and we will not consider it in this article.

Turning off the swap:

# swapoff -a

Run fdisk with an indication of our disk (/dev/sda):

# fdisk /dev/sda

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

Let’s make a note that all changes to fdisk must be done carefully. But knows that until you save the changes (w) you can always exit without any consequences.

We enter p to look at the list of partitions:

Command (m for help): p
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: dos
Disk identifier: 0x73000288

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 39942143 39940096   19G 83 Linux
/dev/sda2       39944190 41940991  1996802  975M  5 Extended
/dev/sda5       39944192 41940991  1996800  975M 82 Linux swap / Solaris

Partition 2 does not start on physical sector boundary.

Here we are interested in the total number of sectors on the disk (52428800) and the number of sectors for swap (1996800). Also note that the logical disk /dev/sda2 (which was created automatically) is not mounted anywhere and intersects with /dev/sda5. Perhaps this is a bug in the automatic creation of partitions and we will just remove it.

Remove swap partition 5 and logical partition 2 (command d):

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

Partition 5 has been deleted.

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

Partition 2 has been deleted.

And let’s see what we got:

Command (m for help): p
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: dos
Disk identifier: 0x73000288

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *     2048 39942143 39940096  19G 83 Linux

Now let’s re-create the swap partition, but at the end of the disk. Create a new partition with n:

Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):

You will be asked to select the type of partition, for the created /dev/sda2. Select the logical e:

We indicate the serial number of partition 2:

Partition number (2-4, default 2): 2

Next, you will need to indicate the first sector. If we want to keep the swap size the same as it was, then from the last sector of the disk /dev/sda (52428800) we minus the number of sectors the partition /dev/sda2 (1996800) and we get 50432000:

First sector (39942144-52428799, default 39942144): 50432000

The last sector is the last sector of the disk:

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

Created a new partition 2 of type 'Extended' and of size 975 MiB.

Now let’s change its type by pressing t, specifying partition 2 and code 82 for the swap:

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code or alias (type L to list all): 82

Changed type of partition 'Extended' to 'Linux swap / Solaris'.

Let’s see result:

Command (m for help): p
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: dos
Disk identifier: 0x73000288

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 39942143 39940096   19G 83 Linux
/dev/sda2       50432000 52428799  1996800  975M 82 Linux swap / Solaris

Save the changes made w:

Command (m for help): w
The partition table has been altered.
Syncing disks.

After that fdisk will finish its work and we can create a swap (specify your new swap partition!):

# mkswap /dev/sda2

Setting up swapspace version 1, size = 975 MiB (1022357504 bytes)
no label, UUID=be9928b0-ce1b-44f1-b925-a94fffac5b2e

We copy the UUID, it will need to be specified in fstab so that it works after a reboot. Open with any text editor:

# nano /etc/fstab

We find an entry with the swap type (after the comment #swap was on) and there we replace the UUID with a new one. Save and exit.

It remains only to enable swap:

# swapon /dev/sda2

Debian 11 extend partition


Let’s run fdisk /dev/sda, where /dev/sda is the label of our disk):

# fdisk /dev/sda

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

We enter p to look at the list of partitions:

Command (m for help): p
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: dos
Disk identifier: 0x73000288

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 39942143 39940096   19G 83 Linux
/dev/sda2       50432000 52428799  1996800  975M 82 Linux swap / Solaris

You can have other partitions. The main thing is that there are free sectors after the partition that we want to expand. To do this in the previous section, we moved swap to the end of the disk. We got that sda1 ends at sector 39940096, and the next partition sda2 starts at 50432000 and there are free sectors between them. If you have one partition on the disk or it is the last one, then you need to look at the last sector of the partition(39940096) and disk (50432000).

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

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

Partition 1 has been deleted.

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

Enter n – create a new partition:

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)

We indicate the main type p:

Select (default p): p

The number of partition 1 (which was deleted):

Partition number (1,3,4, default 1): 1

Next the starting and ending sectors are indicated. This way we use all the unallocated/free space:

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

Created a new partition 1 of type 'Linux' and of size 24 GiB.

As you can see a 24 gigabytes partition was created with the Linux filesystem type.

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

Partition #1 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

We will see a message that the partition table has been changed but since the device is busy the changes will take effect only after a reboot.

The partition table has been altered.
Failed to update system information about partition 1: Device or resource busy

The kernel still uses the old partitions. The new table will be used at the next reboot.
Syncing disks.

Reboot the virtual machine:

# reboot

Resize ext4 file system

Now let’s use the resize2fs utility (for ext4) to increase the size of the filesystem:

# resize2fs /dev/sda1
resize2fs 1.46.2 (28-Feb-2021)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/sda1 is now 6303744 (4k) blocks long.

Checking the result:

# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            944M     0  944M   0% /dev
tmpfs           192M  496K  192M   1% /run
/dev/sda1        24G  1.1G   22G   5% /
tmpfs           960M     0  960M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           192M     0  192M   0% /run/user/1000

Conclusion

As you can see in debian 11 extend partition is not such a difficult task.

You can see similar articles on expanding partitions in various linux distributions:

4 thoughts on “Debian 11 extend partition”

Leave a Comment