Knowledgebase

2 easy methods to extend/shrink resize primary partition in Linux Print

  • 0

Related Searches: How to resize primary partition in Linux. How to extend non lvm root partition. How to change size of partition in Linux using parted and fdisk without destroying data. Steps to expand partition in RHEL/CentOS 7 and 8 Linux. Perform Disk Management in CentOS. How to use unallocated space to change size of partition in a disk in Linux. How to resize root partition not on LVM in Linux. Step by step guide to resize primary partition. How to expand partition with examples in Linux. centos disk management. rhel 7 extend non lvm root partition. centos 7 resize root partition. Steps to resize primary partition to extend non lvm root partition in linux. how to add unallocated disk space to a partition in Linux. centos shrink or extend non lvm root partition. add space to partition. fdisk extend partition. resize boot partition. extend non lvm root partition in linux. change size of partition.how to expand partition. centos resize primary partition. rhel change size of partition.

 

In this article we will share the steps to resize primary partition, here we will extend non lvm root partition. With LVM it is far more easier and less riskier to change size of partition in an volume group in Linux.

WARNING:
  • This article covers steps to resize primary partition (non-lvm) which can be dangerous and can leave your Linux system in an unusable broken state. It is important that you backup your content before you attempt to change size of partition.
  • You can change size of partition (non-lvm) only on the last partition on the storage device with unallocated space. If the respective partition is not the last partition then the only way to expand partition is to backup your data, rebuild the disk and partitions, then restore the data. No tool such as gparted, parted or fdisk can help you change size of partition in such case.
  • You must have some unallocated space or free space available in the concerned device to be able to expand partition. In some of the virtual environment you have an option to change the storage device size but on physical node if there is no enough unallocated space then resize primary partition (extend non lvm root partition) is not possible
  • This article assumes you're using either a GPT partition table, or an msdos partition table using primary partition types to extend non lvm root partition.

 

NOTE:
We will only demonstrate the steps to extend non lvm root partition, but using the same methods you can also shrink and change size of partition to lower value in Linux. But be sure to backup your data.

 

Lab Environment to resize primary partition (RHEL/CentOS 7/8) in Linux

We have performed resize primary partition operation on Virtual Machine running on Proxmox installed on Linux server. Our VM is running with CentOS 8 but we have also verified these steps on RHEL/CentOS 7 and RHEL 8 Linux.

Here our VM is installed on /dev/sda device where /dev/sda1 is boot partition, /dev/sda2 is root while /dev/sda3 is for swap.. Additionally I have left some unallocated free space in /dev/sda for the demonstration of this article to extend non lvm root partition.

 

Method 1: Change size of partition using parted CLI utility

You can either use gparted (GUI utility) or parted (CLI utility) to change size of partition in Linux. Here we plan to resize primary partition which in our case is /dev/sda.

 

List available partitions

To list the available partitions in /dev/sda we will execute below command

[root@centos-8 ~]# parted -l /dev/sda
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags  <--- This is my partition layout
 1      1049kB  538MB   537MB   primary  ext4            boot
 2      538MB   11.3GB  10.7GB  primary  ext4
 3      11.3GB  12.3GB  1074MB  primary  linux-swap(v1)

As mentioned in the disclaimer section, you can change size of partition only on the last partition of the device but here root partition (/dev/sda2) is not the last one instead swap (/dev/sda3) is our last partition. So to expand root partition we must delete swap device to be able to to use unallocated space and extend non lvm root partition. After deleting swap, root partition will become the last partition on /dev/sda after which we can resize primary partition.

Disable swap partition

Currently we have around 1GB reserved for swap partition

[root@centos-8 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3781         164        3245           8         371        3389
Swap:          1023           0        1023

We will turn off my swap (disable swap partition) and use this space to extend non lvm root partition in Linux

[root@centos-8 ~]# swapoff -a

Verify the swap partition space, as you see not it is 0

[root@centos-8 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3781         164        3241           8         375        3389
Swap:             0           0           0

Update /etc/fstab to make sure swap partition is not mounted at boot up stage.

[root@centos-8 ~]# cat /etc/fstab
UUID=b3ba77fc-86a7-40dc-a93e-402d8e3987ab /                       ext4    defaults        1 1
UUID=621d26e4-4bcc-441d-b336-a9bae280343d /boot                   ext4    defaults        1 2
#UUID=3816d191-31fd-4e20-874d-b18360133aca swap                    swap    defaults        0 0

 

Delete swap and expand partition

Now to resize primary partition /dev/sda2 and expand partition to a new value we must first delete swap partition using parted utility. As we need root partition to be the last partition of /dev/sda before we expand partition.

[root@centos-8 ~]# parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  538MB   537MB   primary  ext4            boot
 2      538MB   11.3GB  10.7GB  primary  ext4
 3      11.3GB  12.3GB  1074MB  primary  linux-swap(v1)

(parted) rm 3  <-- First we need to delete swap partition
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  538MB   537MB   primary  ext4         boot
 2      538MB   11.3GB  10.7GB  primary  ext4

(parted) u  <-- Change the unit type to sector to avoid any risk of loosing data when you resize root partition
Unit?  [compact]? s
(parted) p <-- Print the available partition table
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 31457280s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start     End        Size       Type     File system  Flags
 1      2048s     1050623s   1048576s   primary  ext4         boot
 2      1050624s  22022143s  20971520s  primary  ext4			<--- Note down the start sector number, this will be used in next step

(parted) rm 2 <-- We will delete the root partition's entry. This will not impact the content of root partition and only partition table is modified. 
Warning: Partition /dev/sda2 is being used. Are you sure you want to continue?
Yes/No? Yes
Error: Partition(s) 2 on /dev/sda have been written, but we have been unable to inform the kernel of the change, probably because
it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.
Ignore/Cancel? Ignore  <-- If you reboot your server at this stage then you may end up with a broken node so don't reboot your node at this stage.
(parted) p <-- Print the current partition table
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 31457280s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start  End       Size      Type     File system  Flags
 1      2048s  1050623s  1048576s  primary  ext4         boot

(parted) mkpart <-- Now we will create root partition with new size
Partition type?  primary/extended? primary
File system type?  [ext2]? ext4
Start? 1050624s  <-- Here give the start sector as it was earlier for root partition
End? 24022143s  <-- Give the new end sector higher than the earlier value to resize root partition
(parted) p  <-- Print the new partition table after you extend non lvm root partition
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 31457280s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start     End        Size       Type     File system  Flags
 1      2048s     1050623s   1048576s   primary  ext4         boot
 2      1050624s  24022143s  22971520s  primary  ext4         lba

(parted) quit  <-- We are all done here
Information: You may need to update /etc/fstab.

But our root partition is showing the same size as earlier i.e. ~10GB

[root@centos-8 ~]# df -h /dev/sda2
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       9.8G  1.2G  8.1G  13% /

To complete the steps to resize non lvm root partition, execute resizefs for ext3 and ext4 partitions or xfs_growfs for xfs partition to expand partition and refresh the changes

[root@centos-8 ~]# resize2fs /dev/sda2
resize2fs 1.44.3 (10-July-2018)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/sda2 is now 2871440 (4k) blocks long.

Re-verify the new size of root partition to make sure our steps to extend non lvm root partition was successful.

[root@centos-8 ~]# df -h /dev/sda2
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        11G  1.3G  9.0G  12% /

 

Re-create swap partition

Now we must create Swap partition which we deleted earlier in this article. We will again use parted utility to create swap partition

[root@centos-8 ~]# parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? linux-swap  <-- To create swap partition
Start?
Start? 24022144s
End? 98%
Warning: The resulting partition is not properly aligned for best performance: 24022144s % 2048s != 0s
Ignore/Cancel? Ignore
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 16.1GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  538MB   537MB   primary  ext4            boot
 2      538MB   12.3GB  11.8GB  primary  ext4
 3      12.3GB  12.8GB  512MB   primary  linux-swap(v1)  lba  <-- Now we have a swap partition

(parted) quit
Information: You may need to update /etc/fstab.

Now with parted we only created a partition with file system type as swap. Use mkswap to turn this partition into swap.

[root@centos-8 ~]# mkswap /dev/sda3
Setting up swapspace version 1, size = 488.3 MiB (511995904 bytes)
no label, UUID=decb7df5-8d7a-41b4-bfd7-bee076006ec9

Next we must update /etc/fstab with the UUID of our new swap partition. To get the new UUID of swap partition

[root@centos-8 ~]# blkid /dev/sda3
/dev/sda3: UUID="decb7df5-8d7a-41b4-bfd7-bee076006ec9" TYPE="swap" PARTUUID="5290bf38-03"

Update this UUID in /etc/fstab as shown below

[root@centos-8 ~]# cat /etc/fstab
UUID=b3ba77fc-86a7-40dc-a93e-402d8e3987ab /                       ext4    defaults        1 1
UUID=621d26e4-4bcc-441d-b336-a9bae280343d /boot                   ext4    defaults        1 2
UUID=3816d191-31fd-4e20-874d-b18360133aca swap                    swap    defaults        0 0

Finally turn on the new swap partition

[root@centos-8 ~]# swapon /dev/sda3

Verify the new swap partition

[root@centos-8 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3781         155        3390           8         235        3407
Swap:           488           0         488

Now you can reboot your Linux server to make sure everything is OK and resize primary partition was successful.

 

Method 2: Change size of partition using fdisk utility

Similar to parted command, you can also use fdisk utility to resize primary partition and extend non lvm root partition.

 

List available partitions

Before we resize primary partition, let us list the available partitions

[root@centos-8 ~]# fdisk -l /dev/sda
Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 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: 0x5290bf38

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048  1050623  1048576  512M 83 Linux
/dev/sda2        1050624 22022143 20971520   10G 83 Linux  <-- This is our root partition
/dev/sda3       22022144 24119295  2097152    1G 82 Linux swap / Solaris

Now as you see our existing root partition /dev/sda2 size is ~10G. Here we will expand partition with +1GB using unallocated disk space from /dev/sda.

[root@centos-8 ~]# df -h /dev/sda2
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       9.8G  1.3G  8.1G  14% /

 

Delete swap partition

Currently our swap partition is enabled and is the last partition of /dev/sda. So we must first delete swap partition here before we change size of partition as we need root to be the last partition in /dev/sda

[root@centos-8 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3781         165        3243           8         372        3388
Swap:          1023           0        1023

So we will turn off the swap partition before we disable it

[root@centos-8 ~]# swapoff -a

Now it is time to use fdisk utility to resize partition

[root@centos-8 ~]# 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): p  <-- Print the partition table
Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 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: 0x5290bf38

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048  1050623  1048576  512M 83 Linux
/dev/sda2        1050624 22022143 20971520   10G 83 Linux
/dev/sda3       22022144 24119295  2097152    1G 82 Linux swap / Solaris

Command (m for help): d  <-- To delete a partition ude 'd'
Partition number (1-3, default 3): 3  <-- Provide the partition number which you wish to delete. Here our swap partition number is 3

Partition 3 has been deleted.

Command (m for help): p  <-- Next we will print the new partition table again
Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 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: 0x5290bf38

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048  1050623  1048576  512M 83 Linux
/dev/sda2       1050624 22022143 20971520   10G 83 Linux

 

Part 1 - Resize root partition

We will continue with the steps to extend non lvm root partition in the same fdisk session. Note down the start sector of the root partition before you expand partition.

Command (m for help): d  <-- Now we need to delete root partition before we extend non lvm root partition. This will not impact the content stored in root partition and will only clear the partition table. 
Partition number (1,2, default 2): 2  <-- provide the root's partition number

Partition 2 has been deleted.

Command (m for help): p <-- Print the partition table with the new changes
Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 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 identifie
        

Was this answer helpful?
Back