5

Is there a way to resize a UFS /root partition while running OS which resides on it? I am running FreeBSD.

Mateusz Piotrowski
  • 4,623
  • 5
  • 36
  • 70

3 Answers3

4

There's a fairly comprehensive guide to growing a disk under FreeBSD in the handbook. For UFS (the default for FreeBSD), you can grow online from kernel version 10.0 onward.

However, changing the partitions around mounted filesystems might lead to data loss or inconsistencies which might be first discovered when the system tries to boot the next time. Also remember to check up on your bootloader.


Growing a filesytem basically boils down to this:

  1. Adjust your partition table. Be very careful with this step, as messing up here can lead to corrupting your entire disk. You'd probably want to unmount all partitions that are not actively needed to run a minimal system, most notably the swap partition.

    From your question, I assume you know how to use gpart to accomplish this.

    After having made the adjustments, you can safely remount all partitions and re-enable swap.

  2. Actually grow the FS. For FreeBSD, this is as easy as issuing growfs <blockdevice> with <blockdevice> being the partition that you resized.

For shrinking, you have to do these steps in reverse:

  1. Shrink the filesystem to make sure you won't overwrite anything important: growfs -s <new_size> <blockdevice>. Make sure that you choose a size less than your new target filesystem size, i.e. if you want to go down from a 100GB partition to a 70GB one, resize the FS to about 60-65GB at this step. Overshooting more means more headroom, but usually also means more relocations and thus longer wait time.
  2. Adjust the partition table. Unlike before, at this step you choose your exact target size. You'd probably want to unmount additional partitions at this step too, especially if you move other partitions around (in fact, in this case you have to unmount these).
  3. Grow the filesystem. This gets rid of the headroom you left at the end of the (new) partition: growfs <blockdevice>. Omitting the size parameter tells it to use the entire partition.
Wüstengecko
  • 671
  • 4
  • 9
  • Actually, growing a mounted partition under FreeBSD 10 or later should be perfectly safe. Resizing a partition should be safe as well - as long as you don't use the "debugflags" hack, which disables the safety mechanisms. – Edward Tomasz Napierala Sep 29 '17 at 18:45
2

There is a much nicer way to do this:

service growfs onestart

It will invoke /etc/rc.d/growfs once. That script basically calls all the same commands that are in the FreeBSD Handbook. This script is meant to be called on first boot, so that you can distribute a very small virtual hard disk, and then when it boots the first time on a large virtual hard disk, the VM expands to use the whole disk provided. If you later expand your virtual disk (or if you dd your filesystem onto a larger physical disk) you can invoke the script manually once to do all the right things.

Paco Hope
  • 193
  • 5
-1

You have to mount rootfs as readonly, and do instant reboot after resizing finished.

Ipor Sircer
  • 14,376
  • 1
  • 27
  • 34
  • 3
    Note that what's true for one operating system is not universally true. For a **correct** answer for FreeBSD, I suggest researching `growfs` and (for recent versions of FreeBSD) ZFS. – JdeBP Nov 18 '16 at 20:07