How to resize a btrfs root partition on Oracle Linux

Resizing a root partition (/) of a running Oracle Linux isn’t hard but the information out there on the web is limited. In my situation, I was given access to an already installed Oracle Linux environment that wasn’t using all the available space of the disk. The root partition, formatted with the btrfs filesystem, only used 4GB when 32GB were available in total.

Resizing the partition is done in two steps:

  1. Resizing the actual partition itself
  2. Expanding the filesystem on top of the partition

Resizing the partition

Resizing the partition can be done via fdisk, which takes a device name as a parameter. The device name of the root partition can be quickly found out via df:

[root@rpi3 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        446M     0  446M   0% /dev
tmpfs           459M     0  459M   0% /dev/shm
tmpfs           459M   23M  436M   6% /run
tmpfs           459M     0  459M   0% /sys/fs/cgroup
/dev/mmcblk0p4  4.0G  1.8G  1.4G  56% /
/dev/mmcblk0p2  477M   55M  393M  13% /boot
tmpfs           128M     0  128M   0% /tmp
/dev/mmcblk0p1  256M   18M  239M   7% /boot/efi
tmpfs            92M     0   92M   0% /run/user/0

The partitions available on the system are /dev/mmcblk0 partition 1 – 4.  Attach to the disk via fdisk /dev/mmcblk0:

[root@rpi3 ~]# fdisk /dev/mmcblk0
Welcome to fdisk (util-linux 2.23.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 retrieves all existing partitions as well as the device size itself:

Command (m for help): p

Disk /dev/mmcblk0: 31.9 GB, 31914983424 bytes, 62333952 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
Disk label type: dos
Disk identifier: 0x000b164b

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            2048      526335      262144    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          526336     1550335      512000   83  Linux
/dev/mmcblk0p3         1550336     2074623      262144   82  Linux swap / Solaris
/dev/mmcblk0p4         2074624    10463231     4194304   83  Linux

Command (m for help):

The output at the very top shows that the device has 32GB available. df earlier showed that / has only a capacity of 4GB and the rest of the partitions have also only a couple of hundred MBs, meaning most of the disk space is unused.

The procedure is the following:

  1. Delete the existing partition
  2. Create a new partition starting at exactly the same sector as the old one and the end sector being the last sector of the disk
  3. Run partprobe or reboot the system to make the changes take effect

Deleting the existing partition

Before you go ahead and delete the existing partition, note down the starting sector of the partition. In my case, I’m going to delete and recreate partition 4 as that is where the / is on. The starting sector for that partition is 2074624.  The deletion is done via d and the partition number:

Command (m for help): d
Partition number (1-4, default 4): 4
Partition 4 is deleted

Command (m for help):

Creating a new partition

The new partition is created via the n command. Choose a primary partition p and select the first sector, which is the very same one of the old partition 2074624. Then choose the end sector, which is the last sector of the disk as per the fdisk​ output above -1 sector, i.e. Disk /dev/mmcblk0: 31.9 GB, 31914983424 bytes, 62333952 sectors = 62333952 sectors – 1 = 62333951. Last, write the changes to disk via w.

Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): p
Selected partition 4
First sector (2074624-62333951, default 2074624): 2074624
Last sector, +sectors or +size{K,M,G} (2074624-62333951, default 62333951): 62333951
Partition 4 of type Linux and of size 28.8 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 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)
Syncing disks.
[root@rpi3 ~]#

Reboot the system or run partprobe, if available, to avoid the reboot.

Expanding the btrfs filesystem

Resizing the partition itself isn’t enough to make the system use the available space. The filesystem on top also needs to be expanded to accommodate the available space. This can be done via btrfs filesystem resize max /, where filesystem resize is the resize filesystem option, max tells the program to expand the filesystem to its entirety (it could also be, for example, 2g or 100m instead) and / is the partition that should be expanded.

[root@rpi3 ~]# btrfs filesystem resize max /
Resize '/' of 'max'
[root@rpi3 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        446M     0  446M   0% /dev
tmpfs           459M     0  459M   0% /dev/shm
tmpfs           459M   23M  436M   6% /run
tmpfs           459M     0  459M   0% /sys/fs/cgroup
/dev/mmcblk0p4   29G  2.1G   26G   8% /
/dev/mmcblk0p2  477M   55M  393M  13% /boot
tmpfs           128M     0  128M   0% /tmp
/dev/mmcblk0p1  256M   18M  239M   7% /boot/efi
tmpfs            92M     0   92M   0% /run/user/0
[root@rpi3 ~]#

After the command is completed the filesystem has been expanded and df shows its new capacity.

Author: Gerald

Developer, Oracle expert, performance enthusiast and genuine technology geek.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.