How to Add a New Partition on OpenBSD

Following the same theme as my last post, instead of claiming space added to the end of a volume by extending an existing partition (technically, a "slice" in OpenBSD), I will show how to add a new partition using this space.

I find it convenient to create the new slice as the "d" slice and then put /home onto it.

So, let's get started.

  • Boot into the system's rescue disk:

    > boot bsd.rd
    

    Then select (S)hell at the prompt.

  • Claim extra space

    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.
    #
    
  • Add a new slice for /home (the "d" slice)

    This system has only the root slice and swap. We will add a new slice, using all the unused space at the end. In the following example, I have a 120GB volume, where 20GB is used by root and swap, and the rest unallocated.

    # disklabel -E wd0
    Label editor (enter '?' for help at any prompt)
    > p
    OpenBSD area: 64-251658225; size: 251658161; free: 209728575
    #                size           offset  fstype [fsize bsize  cpg]
      a:         38812976               64  4.2BSD   2048 16384 38128
      b:          3116610         38813040    swap
      c:        251658240                0  unused
    > a
    partition: [d]
    offset: [41929650]
    size: [209728575]
    FS type: [4.2BSD]
    Rounding offset to bsize (32 sectors): 41929664
    Rounding size to bsize (32 sectors): 209728544
    > p
    OpenBSD area: 64-251658225; size: 251658161; free: 31
    #                size           offset  fstype [fsize bsize  cpg]
      a:         38812976               64  4.2BSD   2048 16384 38128
      b:          3116610         38813040    swap
      c:        251658240                0  unused
      d:        209728544         41929664  4.2BSD   2048 16384    1
    > w
    > q
    No label changes.
    #
    
  • Create a filesystem on the new slice:

    # newfs /dev/rwd0d
    /dev/rwd0d: 102406.5MB in 209728544 sectors of 512 bytes
    506 cylinder groups of 202.47MB, 12958 blocks, 25984 inodes each
    super-block backups (for fsck -b #) at:
     32, 414688, 829344, 1244000, 1658656, 2073312, 2487968, 2902624, 3317280,
     3731936, 4146592, 4561248, 4975904, 5390560, 5805216, 6219872, 6634528,
     7049184, 7463840, 7878496, 8293152, 8707808, 9122464, 9537120, 9951776,
     ...
     [snip]
     ...
     193229728, 193644384, 194059040, 194473696, 194888352, 195303008, 195717664,
     196132320, 196546976, 196961632, 197376288, 197790944, 198205600, 198620256,
     199034912, 199449568, 199864224, 200278880, 200693536, 201108192, 201522848,
     201937504, 202352160, 202766816, 203181472, 203596128, 204010784, 204425440,
     204840096, 205254752, 205669408, 206084064, 206498720, 206913376, 207328032,
     207742688, 208157344, 208572000, 208986656, 209401312,
    #
    
  • Migrate /home

    Here, we mount the root filesystem (which contains the old /home), and also the new filesystem, and finally migrate the data from old /home to new space.

    # mount /dev/wd0a /mnt
    # mount /dev/wd0d /mnt2
    # (cd /mnt/home; tar cf - .) | (cd /mnt2; tar xpf -)
    # rm -rf /mnt/home
    # mkdir /mnt/home
    

    Note, the tar command above is a nice platform independent way of copying everything from one directory to another, preserving everything (permissions, etc...). The cp command is different on Linux and the *BSDs, yet tar, used in the above fasion, is identical on Linux, FreeBSD, NetBSD, and OpenBSD. I thank Todd Fries of Free Daemon Consulting for teaching me that one.

  • Mount on boot

    The following command will add the new /home to your /etc/fstab so it is automatically mounted upon boot.

    # echo "/dev/wd0d /home ffs rw,softdep 0 1" >> /mnt/etc/fstab
    
  • Clean up:

    # umount /mnt*
    # reboot
    

    Now enjoy the extra space!

Comments