CentOS 7 – resizing the root partition

Recently, I ran out of space on the root partition on one of my virtual CentOS 7 machines. So, I just had to extend it – which took me longer than expected, to figure out.
So, in case someone else is struggling to do this, I wan’t to make a quick guide. When having the right commands and tools, it’s not that big an issue, but if you’re used to CentOS 6 (and below), there’s some minor differences.

First of all, the disk needed to be resized in my virtualbox – I assume you’re able to do that, it’s quite easy.

But then the partition need to be resized to use the new available space, which was the start of a small journey for me, as it was the root partition and I wanted to keep all my data.

1. Find the names of the disks

We need to know what the disks are called:

# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 3,92g
swap centos -wi-ao---- 412,00m

In my case, I want to resize my root partition, that is logical volume “root”.

2. Creating a new logical volume

We need to have some logical volume, that we can extend from. So we’ll have to create a new one, using fdisk:

(my disk is called sda, replace with the name of your disk…)

# fdisk /dev/sda
Command (m for help): n
Command action
e   extended
p   primary partition (X)
p
Partition number (X-Y): 3
First sector (XXXX-XXXX, default XXXX):
Using default value XXXX
Last sector, +sectors or +size{K,M,G} (XXXX-XXXX, default XXXX):
Using default value XXXX
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

(I’ve replaced all values with X’s, to avoid confusion)

First, “n” for new
It will suggest some default values, which are fine if you want to extend to all the free space:
Partision: the first available partition number it prints. In my case it was “3″.
First sector: use the default value. It will suggest the first available sector, which is what we want.
Last sector: use the defualt value. It will suggest the last available sector, which is also what we want.

A new command prompt will be displayed, and you enter “w” to write the changes and quit fdisk.

3. Creating a new physical volume

You now have a new logical volume. That’s great, but you can’t use it yet – we need a physical volumen to extend from.

Use fdisk -l to see the new volume and its name. You should see something like:

/dev/sda3 8388608 51199999 21405696 83 Linux

(If there are multiple, you should be able to regognize it on the size – normally it’s the last on the list called “/dev/sdaX”, as the number is just incremented.)

In mys case, it’s called “/dev/sda3″.

Now, we can create the new physical volume, using pvcreate:

pvcreate /dev/sda3

4. Extending the root partition

Now we have a new, unused logical volume, that we can use for the root partition – and we’re ready to extend the root partition.

But first we need to get the name of the root partition. You can find it using the good ol’ dh. Find the filesystem mounted on “/”:

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 24G 2,9G 21G 13% /
devtmpfs 236M 0 236M 0% /dev
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 245M 4,3M 241M 2% /run
tmpfs 245M 0 245M 0% /sys/fs/cgroup
/dev/sda1 497M 158M 340M 32% /boot
tmpfs 49M 0 49M 0% /run/user/1000

So, the name of the root partition is “/dev/mapper/centos-root”. And the name of the new volume we created earlier is “/dev/sda3″.

That’s all we need to know. Let’s extend it:

vgextend /dev/mapper/centos-root /dev/sda3

5. Extending the filesystem

The last thing we need to do, is to extend the filesystem, so it’s actually using the new amount of space.

It’s very easy, using the command “xfs_growfs”:

xfs_growfs -d /

(the -d option tell the command to use the maximum amount of available space on the underlaying partition)

If you get and error, telling that you don’t have the xfs_growfs command, you’ll have to install it, but it’s very easy:

yum install xfsprogs

You should now be able to see the new, resize partition, using df -l.

6. Summary and conclusion

Resizing the root partition is not something you just do from the command line. Using gparted or similar makes it a bit easier, but the underlaying mecanism is the same.
But when you know the commands, it’s not that difficult.