LinkedIn Sourceforge

Vincent's Blog

Pleasure in the job puts perfection in the work (Aristote)

How to Resize a Partition in OpenBSD

Posted on 2024-10-26 12:42:00 from Vincent in OpenBSD

Resizing partitions in OpenBSD can be a bit intimidating, but it's a straightforward process if you follow the right steps. This guide will walk you through expanding the /home partition. We'll start with the initial state, then move on to unmounting, resizing, and checking the filesystem before finalizing the changes. All the code snippets provided here are ready to use.


Introduction

I'll show you how I've increased the filesystem for /home on one of my small OpenBSD machine having a celeron CPU, 4GB of ram and a small disk of 128GB. This is typically a machine where "/" is monted read-only and /var and /tmp are memory filesystems.
You can find more details on this setup here

Initial state

Before resizing the partition, it's good practice to check the current disk usage. Here is what the initial state of our filesystems looks like:

obsd:~ # df -h
Filesystem     Size    Used   Avail Capacity  Mounted on
/dev/sd0a     19.4G    1.7G   16.7G    10%    /
/dev/sd0d     29.1G   26.0G    1.6G    95%    /home

As we can see, the /home partition (/dev/sd0d) is almost full, with only 1.6 GB of space left. To address this, we will resize the partition and increase its available space.

Umount the filesystem

The next step is to unmount the /home partition. This is crucial because we cannot resize a partition while it's mounted:

obsd:~ # umount /home
obsd:~ # mount
/dev/sd0a on / type ffs (local, wxallowed)

Now, only the root (/) partition remains mounted.

Resizing the partition

To resize the partition, we use disklabel. OpenBSD's disklabel tool allows us to adjust the size of the partition directly:

obsd:~ # disklabel -E sd0
Label editor (enter '?' for help at any prompt)
sd0> p g
OpenBSD area: 532544-500118159; size: 238.2G; free: 180.2G
                size           offset  fstype [fsize bsize   cpg]
  a:            20.0G           532544  4.2BSD   2048 16384 12960 # /
  b:             8.0G         42475840    swap                    # none
  c:           238.5G                0  unused
  d:            30.0G         59263808  4.2BSD   2048 16384 12960 # /home
  i:             0.3G               64   MSDOS
sd0> c d
Partition d is currently 62926560 sectors in size, and can have a maximum
size of 440854351 sectors.
size: [62926560] 60G
sd0*> p g
OpenBSD area: 532544-500118159; size: 238.2G; free: 150.2G
                size           offset  fstype [fsize bsize   cpg]
  a:            20.0G           532544  4.2BSD   2048 16384 12960 # /
  b:             8.0G         42475840    swap                    # none
  c:           238.5G                0  unused
  d:            60.0G         59263808  4.2BSD   2048 16384 12960 # /home
  i:             0.3G               64   MSDOS
sd0*> w
sd0> q
No label changes.

In the example above, we increased the size of partition d (which is /home) to 60 GB. After confirming the changes, disklabel updates the partition table.

Growing the Filesystem

Once the partition size has been adjusted, we need to expand the filesystem to utilize the newly allocated space:

obsd:~ # growfs /dev/sd0d
We strongly recommend you to make a backup before growing the Filesystem

 Did you backup your data (Yes/No) ? yes
new filesystem size is: 31459280 frags
Warning: 176960 sector(s) cannot be allocated.
growfs: 61357.5MB (125660160 sectors) block size 16384, fragment size 2048
    using 303 cylinder groups of 202.50MB, 12960 blks, 25920 inodes.
super-block backups (for fsck -b #) at:
 63037600, 63452320, 63867040, 64281760, 64696480, 65111200, 65525920, 65940640, 66355360, 66770080, 67184800, 67599520, 68014240, 68428960, 68843680, ...
obsd:~ #

Here, growfs expands the filesystem to fill the newly resized partition. Always remember to back up your data before proceeding with this step, as resizing a filesystem can sometimes be risky.

Filesystem Check

After resizing, it’s good practice to force a filesystem check to ensure integrity:

obsd:~ # fsck_ffs -f /dev/sd0d
** /dev/rsd0d
** Last Mounted on /home
** Phase 1 - Check Blocks and Sizes
** Phase 2 - Check Pathnames
** Phase 3 - Check Connectivity
** Phase 4 - Check Reference Counts
** Phase 5 - Check Cyl groups
58310 files, 13639390 used, 16789039 free (1583 frags, 2098432 blocks, 0.0% fragmentation)

MARK FILE SYSTEM CLEAN? [Fyn?] y


***** FILE SYSTEM WAS MODIFIED *****
obsd:~ #

This fsck_ffs command verifies the consistency of the filesystem and marks it as clean.

Final check

With the filesystem resized and checked, we can remount the /home partition and verify the changes:

obsd:~ # mount /home
obsd:~ # df -h
Filesystem     Size    Used   Avail Capacity  Mounted on
/dev/sd0a     19.4G    1.7G   16.7G    10%    /
/dev/sd0d     58.0G   26.0G   29.1G    48%    /home

As shown above, the /home partition now has 58 GB of total space, with 29.1 GB available. This gives you more room for your files and applications.

Conclusion

Resizing a partition in OpenBSD involves careful planning and execution, but following the steps outlined above can make the process smooth. Remember to always back up your data before making changes to partitions or filesystems to avoid potential data loss. Happy computing!



7, 6
displayed: 2545



What is the first vowel of the word Python?