How to: Extend a partition
  • 02 Mar 2023
  • 3 Minutes to read
  • Dark
    Light

How to: Extend a partition

  • Dark
    Light

Article summary

Below is a guide written to assist in extending a cloud drive after an upgrade has been processed:
In the below example we have upgraded a cloud server from 150GB of storage to 250GB of storage

Check the current configuration

We can see the old space is still currently reflected by running lsblk:

[root@server ~]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   150G  0 disk
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0   149G  0 part
  ├─centos-root 253:0    0 145.8G  0 lvm  /
  └─centos-swap 253:1    0   3.2G  0 lvm  [SWAP]
sr0              11:0    1  1024M  0 rom
loop0             7:0    0     4G  0 loop /var/tmp

 Rescan the disk that has been resized

We can rescan sda to get the system to recognize the new storage space using the following command:

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

Please note that in the above rescan you need to have the correct block device e.g. sda, sdb, etc, depending on which drive you’ve expanded.

After running the above command we can see the new drive space is reflected correctly when running lsblk:

[root@server ~]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   250G  0 disk
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0   149G  0 part
  ├─centos-root 253:0    0 145.8G  0 lvm  /
  └─centos-swap 253:1    0   3.2G  0 lvm  [SWAP]
sr0              11:0    1  1024M  0 rom
loop0             7:0    0     4G  0 loop /var/tmp

Once the O.S. can see the correct disk size, we can resize the root partition using parted


Use parted to resize the required partition

Run the parted program and then use the print command to see the current state of the partitions.

[root@server ~]# parted
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 268GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  161GB   160GB   primary               lvm

You can see under the ‘flags’ column that it has the lvm tag which tells us that it’s not a swap or boot partition. This is important to help identify the correct partition to resize. You’ll also want to make sure that it’s the last partition in the list as you cannot resize any partition that has another partition after it.

In this example we will need to resize partition number 2 using resizepart.

(parted) resizepart
Partition number? 2
End?  [161GB]? 268GB

(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 268GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  268GB   267GB   primary               lvm

In this example, we have used resizepart and then entered the maximum size of the disk shown in the print command we used previously. (268GB). You can see that the ‘size’ column now says 267GB.
Now type quit to exit parted. 

Check the results

Once that is done you can double check that the partition has resized correctly using lsblk:

[root@server ~]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   250G  0 disk
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0 248.6G  0 part
  ├─centos-root 253:0    0 145.8G  0 lvm  /
  └─centos-swap 253:1    0   3.2G  0 lvm  [SWAP]
sr0              11:0    1  1024M  0 rom
loop0             7:0    0     4G  0 loop /var/tmp

You can now see sda2 is showing the correct size of 248.6G and we have successfully expanded this partition.

Simply expanding a physical partition is usually not the end goal. You’ll generally want to expand the LVM (pv, and lv of the primary volume group), and then expand the filesystem to make the space usable. These steps are covered in the following guide:

How to expand an LVM (Logical Volume Manager)


Was this article helpful?

Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.
ESC

Eddy AI, facilitating knowledge discovery through conversational intelligence