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


Storage, How To, Linux, Tips and Tricks

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.

 

Earlier I had shared steps to create a file system and check file system type in Linux. Now in this article I 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:
I 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

I have performed resize primary partition operation on Virtual Machine running on Oracle VirtualBox installed on Linux server. My VM is running with CentOS 8 but I have also verified these steps on RHEL/CentOS 7 and RHEL 8 Linux.

Here my 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 I 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 my last partition. So to expand root partition I 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 I 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

I 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 my 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 my 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 identifier: 0x5290bf38

Device     Boot Start     End Sectors  Size Id Type
/dev/sda1  *     2048 1050623 1048576  512M 83 Linux  <-- Now we only have a boot partition

Command (m for help): n  <-- Next we will create and expand partition for root
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p  <-- This will be a primary partition
Partition number (2-4, default 2): 2  <-- For root partition we know the partition number was 2 so we will use the same as it is important to use the same start sector
First sector (1050624-31457279, default 1050624): 1050624  <-- Use the same first sector as it was earlier to resize root partition
Last sector, +sectors or +size{K,M,G,T,P} (1050624-31457279, default 31457279): +11G  <-- Here you can define the new size to resize root partition and expand partition

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

Do you want to remove the signature? [Y]es/[N]o: N  <-- Since we want to retain ext4 signature on our root partition we will use "N"

Command (m for help): p  <-- Now print the new partition table once resize root partition is successful

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 24119295 23068672   11G 83 Linux  <-- We have our new root partition with 11G as new size

 

Create swap partition

After we resize primary partition, in the same fdisk session we will also create a new swap partition which we had deleted initially with new start and end sector

Command (m for help): n  <-- Now we will also create a new swap partition
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p  <-- This also will be a primary partition
Partition number (3,4, default 3): 3  <-- We can give any partition number here but we will use 3 as earlier
First sector (24119296-31457279, default 24119296):  <-- This can be the same as default option
Last sector, +sectors or +size{K,M,G,T,P} (24119296-31457279, default 31457279): +512M   <--- My new swap partition size will be 512MB

Created a new partition 3 of type 'Linux' and of size 512 MiB.

Command (m for help): p  <-- Print the new 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 24119295 23068672   11G 83 Linux
/dev/sda3       24119296 25167871  1048576  512M 83 Linux  <-- Now resize root partition was successful and we have all the partitions with us

Command (m for help): t   <-- We must also change the filesystem type of our swap partition
Partition number (1-3, default 3): 3  <-- This is our swap partition
Hex code (type L to list all codes): 82  <-- 82 is the code for Linux swap filesystem type

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

Command (m for help): p  <-- Print the partition table to verify 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 identifier: 0x5290bf38

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048  1050623  1048576  512M 83 Linux
/dev/sda2        1050624 24119295 23068672   11G 83 Linux
/dev/sda3       24119296 25167871  1048576  512M 82 Linux swap / Solaris

Command (m for help): w <-- Save the new partition table with the changes to expand partition
The partition table has been altered.
Syncing disks.

Update the kernel regarding the recent changes we did to change size of partition

[root@centos-8 ~]# partprobe /dev/sda

But our swap partition is still shown as 0

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

Now let us complete our steps to create swap partition using mkswap

[root@centos-8 ~]# mkswap /dev/sda3
Setting up swapspace version 1, size = 512 MiB (536866816 bytes)
no label, UUID=f34150e6-6d37-448c-b81f-69ecdcb13ed5

Next update /etc/fstab with the UUID for your new swap partition. You can use blkid to get the UUID

[root@centos-8 ~]# blkid /dev/sda3
/dev/sda3: UUID="f34150e6-6d37-448c-b81f-69ecdcb13ed5" TYPE="swap" PARTUUID="5290bf38-03"

Update the same in /etc/fstab as shown below. Alternatively you can also mount filesystems using systemd instead of /etc/fstab file.

UUID=f34150e6-6d37-448c-b81f-69ecdcb13ed5 swap                    swap    defaults        0 0

Now you can turn on the swap partition

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

Verify the same using free command

[root@centos-8 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3781         164        3237           8         378        3388
Swap:           511           0         511

 

Part 2 - Resize root partition

If not done already in above steps, once you exit fdisk utility, update the kernel regarding the recent changes we did to change size of partition

[root@centos-8 ~]# partprobe /dev/sda

We are not done with resize primary partition, as df command still shows old partition size for root partition

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

Execute resize2fs for ext3 and ext4 partitions or xfs_growfs for xfs partition to expand partition on /dev/sda2 with the new 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 2883584 (4k) blocks long.

Now you can verify the new size of root partition

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

Lastly I hope the steps from the article to resize primary partition and extend non lvm root partition on RHEL/CentOS 7/8 Linux was helpful. So, let me know your suggestions and feedback using the comment section.

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

29 thoughts on “2 easy methods to extend/shrink resize primary partition in Linux”

  1. really helped after hours of digging in internet finally found this wonderful article with clean instructions. i don’t usually comment but i’m really thankful for this one.

    Reply
  2. This was very well put together. I achieved what I desired.
    Thank you very much for your efforts
    Regards from Finland 🙂

    Reply
  3. Hey, very detailed instruction, thank you for that! I was able to do the same with fdisk on RHEL 8 but that didn’t work on my RHEL 7 machine. Your way with parted was nearly the same but helped me still a lot! Just one thing to add: If you have a xfs Filesystem you can extend it later with xfs_growfs instead of resize2fs …

    Cheers!

    Reply
  4. Cant see who wrote this article but wow, great article. I read a dozen other web pages trying to describe how to achieve this and NONE of them mentioned the most obvious issue: The SWAP partition is in the way!!!!
    Thanks for the article! Well done and keep posting.

    Reply

Leave a Comment