How to resize a running partition and add a swap partition without rebooting

  • 46-Chaos-3185
  • 0 Comments

The manual is valid for most Linux distributions: Debian, Ubuntu, Arch, CentOs, Fedora, Gentoo etc.
All commands should be executed under the superuser. First, let’s look at the partitions detected:
fdisk -l
This will give you the list of the partitions that are available:
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 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: 0x648aed90
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 56915967 56913920 27,1G 83 Linux
/dev/sda3 56915968 83886079 26970112 12,9G 83 Linux

The 40GB hard disk is divided into two ext4 partitions, sda1 and sda3. This article will describe how to delete the second partition without rebooting it on a running system and create a 4GB swap partition. And expanding of the working first partition to the end of the available space.
Let’s mount the second sda3 disk:
umount /dev/sda3
Let’s run fdisk:
fdisk /dev/sda
In it, small Latin letters are commands. `m`, and then Enter will display a list of available commands.
We will enter the following commands one by one: `p`, `d`, `3`, `p` to delete the second partition:
Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 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: 0x648aed90
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 56915967 56913920 27,1G 83 Linux
/dev/sda3 56915968 83886079 26970112 12,9G 83 Linux
Command (m for help): d
Partition number (1,3, default 3): 3
Partition 3 has been deleted.
Command (m for help): p
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 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: 0x648aed90
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 56915967 56913920 27,1G 83 Linux
Command (m for help):

Next, create a new partition for swap.
Commands: `n`, `p`, `2`,
Here the utility asks for the first sector for the partition. If we make 4 GB swap, it is 4294967296 bytes. Divide into 512 – we will get 8388608 sectors – this is the size of the swap area. If the last sector is 83886079, the first sector is (83886079 – 8388608) = 75497471. It means that the first sector for 4 GB of the partition will have to be specified as 75497471 with the available parameters.
Next, just Enter to enter the default value (Last sector default 83886079)
And change the type of this section by commands: `t`, `2`, `82`, `p`.
To the question “Partition type (type L to list all types):” you can answer `L` and see the list of available options.
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 (2-4, default 2): 2
First sector (56915968-83886079, default 56915968): 75497471
Last sector, +sectors or +size{K,M,G,T,P} (75497471-83886079, default 83886079):
Created a new partition 2 of type 'Linux' and of size 4 GiB.
Command (m for help): t
Partition number (1,2, default 2): 2
Partition type (type L to list all types): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): p
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 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: 0x648aed90
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 56915967 56913920 27,1G 83 Linux
/dev/sda2 75497471 83886079 8388609 4G 82 Linux swap / Solaris
Command (m for help):

Now let’s increase the first partition without unmounting it (it is working system), for this purpose we will delete it and at once we will create new with the necessary parametres (last sector not 56915967, and 75497470), for this purpose we enter commands:
`d`, `1`, `b`, `p`, `1`, [Enter], [Enter], `N`
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-83886079, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-75497470, default 75497470):
Created a new partition 1 of type 'Linux' and of size 36 GiB.
Partition #1 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: N
Command (m for help):

If the first partition lost its boot tag after the operations (* in the boot column after the p command), you can activate it with commands a, 1:
Command (m for help): a
Partition number (1,2, default 2): 1
The bootable flag on partition 1 is enabled now.
Command (m for help):

Re-enter the `p` command and check to see if you are aprove of these changes. After all, everything you’ve done hasn’t come into effect yet. If that’s okay, you can enter the `w` command to accept the changes:
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).

After that, let’s run it:
partprobe /dev/sda
and then:
resize2fs /dev/sda1
It will be approximately the same conclusion if it is successful:
resize2fs 1.43.4 (31-Jan-2017)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/sda1 is now 9436927 (4k) blocks long.

Everything went well. The file systems are changed.

Let’s make swapspace from the second partition:

mkswap /dev/sda2
If successful:
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=0f0c8c2e-7f31-49de-b449-1a6efbd203ca

Let’s mount it as a swap:
swapon /dev/sda2

Now let’s change the autoload parameters in /etc/fstab

If there were some old records besides the main system partition, you should comment on them. And add one for the swap section:
UUID=0f0c8c2e-7f31-49de-b449-1a6efbd203ca none swap sw 0 0

(the identifier was set by the mkswap command)

After that swap should be connected automatically and everything will be ready for further work.

Leave a Comment

Your email address will not be published.