How to Resize an OpenBSD Root Partition

At ARP Networks, sometimes we have to resize an OpenBSD root partition to satsify a customer requirement, template requirement, or something else along those lines.

I've sometimes seen customers struggle to do this themselves, so the following is an example of how I resized a root partition from 5 GB to 9 GB, leaving 1 GB for swap. The underlying volume was already expanded from 5 GB to 10 GB, to give us room to do this.

So, let's get to it.

  • Boot into the system's rescue disk:

    > boot bsd.rd
    

    Then select (S)hell at the prompt.

  • Resize:

    Grab the growfs(8) program from the root partition. This isn't included with the rescue disk, for some reason.

    # mount /dev/wd0a /mnt
    # cp /mnt/sbin/growfs .
    # umount /mnt
    

    Re-initialize the MBR, using the entire disk.

    # fdisk -i wd0
    Do you wish to write new MBR and partition table? [n] y
    Writing MBR at offset 0.
    #
    

    Change the size of the slices.

    This system has only the root partition and swap. The idea is to remove the swap partition, then extend root, and finally put the swap partition back.

    # disklabel -E wd0
    # Inside MBR partition 3: type A6 start 63 size 20964762
    Treating sectors 63-20964825 as the OpenBSD portion of the disk.
    You can use the 'b' command to change this.
    
    Initial label editor (enter '?' for help at any prompt)
    > p
    OpenBSD area: 63-20964825; size: 20964762; free: 9446220
    #                size           offset  fstype [fsize bsize  cpg]
      a:         10490382               63  4.2BSD   2048 16384    1
      b:          1028160         10490445    swap
      c:         20971520                0  unused
    > d
    partition to delete: [] b
    > p
    OpenBSD area: 63-20964825; size: 20964762; free: 10474380
    #                size           offset  fstype [fsize bsize  cpg]
      a:         10490382               63  4.2BSD   2048 16384    1
      c:         20971520                0  unused
    > c
    partition to change size: [] a
    Partition a is currently 10490382 sectors in size, and can have a maximum
    size of 20964762 sectors.
    size: [10490382] 9G
    Rounding to cylinder: 18876312
    > a b
    offset: [18876375]
    size: [2088450]
    FS type: [swap]
    > p
    OpenBSD area: 63-20964825; size: 20964762; free: 0
    #                size           offset  fstype [fsize bsize  cpg]
      a:         18876312               63  4.2BSD   2048 16384    1
      b:          2088450         18876375    swap
      c:         20971520                0  unused
    > w
    > q
    No label changes.
    #
    

    Grow the filesystem using growfs(8)

    # growfs /dev/wd0a
    We strongly recommend you to make a backup before growing the Filesystem
    
     Did you backup your data (Yes/No) ? Yes
    new file systemsize is: 4719078 frags
    Warning: 216792 sector(s) cannot be allocated.
    growfs: 9111.1MB (18659520 sectors) block size 16384, fragment size 2048
            using 45 cylinder groups of 202.47MB, 12958 blks, 25984 inodes.
    super-block backups (for fsck -b #) at:
     10781088, 11195744, 11610400, 12025056, 12439712, 12854368, 13269024, 13683680, 14098336, 14512992, 14927648, 15342304, 15756960, 16171616, 16586272, 17000928, 17415584, 17830240,
     18244896
    #
    

    Give it a rinse.

    # fsck /dev/wd0a
    ** /dev/rwd0a
    ** Last Mounted on /
    ** Phase 1 - Check Blocks and Sizes
    ** Phase 2 - Check Pathnames
    ** Phase 3 - Check Connectivity
    ** Phase 4 - Check Reference Counts
    ** Phase 5 - Check Cyl groups
    100205 files, 418854 used, 4172217 free (97 frags, 521515 blocks, 0.0% fragmentation)
    
    MARK FILE SYSTEM CLEAN? [Fyn?] y
    
    
    ***** FILE SYSTEM WAS MODIFIED *****
    #
    

    And that's pretty much it. Everything is done within OpenBSD's rescue disk kernel bsd.rd, no need to have a separate CD-ROM / USB rescue disk utility.

Comments