Growing a partition

WARNING: BE CAREFUL! Some commands in this tutorial can cause permanent data loss. We recommend you backup your files before attempting to run these commands. You should also double check every command to make sure you have made no typos or mistakes. A single typo could destroy your data forever!

NOTE: This guide is no substitute for reading the OpenBSD FAQ. In particular, you should read the section on Disk Setup and Growing a Partition.

NOTE: Before performing the upgrades, you will want to announce the changes to your users and teammates.

On OpenBSD, there are two disk drivers: wd and sd. wd is an IDE-like disk, and sd is a SCSI-like disk (including USB disks). To list all your available disks:

# dmesg | grep -E '(sd[0-9]|wd[0-9])'
sd0 at scsibus1 targ 0 lun 0: <ATA, WDC WD10EZEX-21M, 01.0> naa.50014ee25f5d927d
sd0: 953869MB, 512 bytes/sector, 1953525168 sectors

We see here there is one hard disk with over 900GB of disk space. Let's see how much we are using:

# df -h
Filesystem     Size    Used   Avail Capacity  Mounted on
/dev/sd0a      986M    103M    833M    11%    /
/dev/sd0k      295G    151G    130G    54%    /home
/dev/sd0d      3.9G   16.0K    3.7G     0%    /tmp
/dev/sd0f      5.8G    1.6G    3.9G    29%    /usr
/dev/sd0g      986M    234M    702M    25%    /usr/X11R6
/dev/sd0h     19.4G    5.3G   13.1G    29%    /usr/local
/dev/sd0j      5.8G    2.0K    5.5G     0%    /usr/obj
/dev/sd0i      1.9G    2.0K    1.8G     0%    /usr/src
/dev/sd0e     11.3G   30.9M   10.7G     0%    /var

Only around 300G of the total 900GB is actually formatted and available for use. If you check disklabel, you will see that 500GB of disk space is free:

# disklabel -E sd0
Label editor (enter '?' for help at any prompt)
sd0> p G
OpenBSD area: 64-1953520065; size: 931.5G; free: 575.7G
#                size           offset  fstype [fsize bsize   cpg]
  a:             1.0G               64  4.2BSD   2048 16384 12960 # /
  b:             4.1G          2097216    swap                    # none
  c:           931.5G                0  unused                    
  d:             4.0G         10690752  4.2BSD   2048 16384 12960 # /tmp
  e:            11.7G         19079328  4.2BSD   2048 16384 12960 # /var
  f:             6.0G         43606400  4.2BSD   2048 16384 12960 # /usr
  g:             1.0G         56189312  4.2BSD   2048 16384 12960 # /usr/X11R6
  h:            20.0G         58286464  4.2BSD   2048 16384 12960 # /usr/local
  i:             2.0G        100229504  4.2BSD   2048 16384 12960 # /usr/src
  j:             6.0G        104423808  4.2BSD   2048 16384 12960 # /usr/obj
  k:           300.0G        117006720  4.2BSD   4096 32768 26062 # /home

p G means to print in Gigabytes, to make it more human readable.

The disk partition labeled c represents the entire disk; the total disk size is 931.5G. We can also see that 575.7G disk space is free, which means we can use it to grow the last partition on our disk (partition k).

NOTE: The free space must follow the partition you want to grow. Otherwise, you will be unable to grow the partition.

WARNING: The partition you want to grow must be unmounted before you grow it. Otherwise, your partition may become corrupt and you will lose all your data.

First, we need to unmount the partition:

# unmount /dev/sd0k

NOTE: In order to unmount the /home partition like we did above, you may need to log out of all users except the root user, then log in as root. If any user is in their /home folder, the filesystem will be busy and you will be unable to unmount.

Afterwards, we will edit the disklabel and enlarge the size of sd0k:

# disklabel -E sd0  
Label editor (enter '?' for help at any prompt)
sd0> ?  
Available commands:
 ? | h    - show help                 n [part] - set mount point
 A        - auto partition all space  p [unit] - print partitions
 a [part] - add partition             q        - quit & save changes
 b        - set OpenBSD boundaries    R [part] - resize auto allocated partition
 c [part] - change partition size     r        - display free space
 D        - reset label to default    s [path] - save label to file
 d [part] - delete partition          U        - undo all changes
 e        - edit drive parameters     u        - undo last change
 g [d|u]  - [d]isk or [u]ser geometry w        - write label to disk
 i        - modify disklabel UID      X        - toggle expert mode
 l [unit] - print disk label header   x        - exit & lose changes
 M        - disklabel(8) man page     z        - delete all partitions
 m [part] - modify partition

If disklabel is not showing all the free space available, you may first need to run b to set the OpenBSD boundary. This can occur if your disk is greater than 2TB or if you are in a virtual machine and enlarged the size of your disk space after installation.

sd0> b
Starting sector: [64] 
Size ('*' for entire disk): [1953520065] *
sd0> 

We want to c k change the size of partition k:

sd0> c k
Partition k is currently 629145600 sectors in size, and can have a maximum
size of 1836513345 sectors.
size: [629145600] *
sd0*> p G
OpenBSD area: 64-1953520065; size: 931.5G; free: 0.0G
#                size           offset  fstype [fsize bsize   cpg]
  a:             1.0G               64  4.2BSD   2048 16384 12960 # /
  b:             4.1G          2097216    swap                    # none
  c:           931.5G                0  unused                    
  d:             4.0G         10690752  4.2BSD   2048 16384 12960 # /tmp
  e:            11.7G         19079328  4.2BSD   2048 16384 12960 # /var
  f:             6.0G         43606400  4.2BSD   2048 16384 12960 # /usr
  g:             1.0G         56189312  4.2BSD   2048 16384 12960 # /usr/X11R6
  h:            20.0G         58286464  4.2BSD   2048 16384 12960 # /usr/local
  i:             2.0G        100229504  4.2BSD   2048 16384 12960 # /usr/src
  j:             6.0G        104423808  4.2BSD   2048 16384 12960 # /usr/obj
  k:           875.7G        117006720  4.2BSD   4096 32768 26062 # /home
sd0*> w
sd0> q

Finally, we grow the disk:

# growfs /dev/sd0k
We strongly recommend you to make a backup before growing the Filesystem

 Did you backup your data (Yes/No) ? yes
new filesystem size is: 229564168 frags
Warning: 80576 sector(s) cannot be allocated.
growfs: 896695.7MB (1836432768 sectors) block size 32768, fragment size 4096
        using 1101 cylinder groups of 814.44MB, 26062 blks, 52224 inodes.
super-block backups (for fsck -b #) at:
 630492096, 632160064, 633828032, 635496000, 637163968, 638831936, 640499904,
 642167872, 643835840, 645503808, 647171776, 648839744, 650507712, 652175680,
 653843648, 655511616, 657179584, 658847552, 660515520, 662183488, 663851456,

We perform a fsck? to ensure consistency:

# fsck /dev/sd0k
** /dev/rsd0k
** 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
1929 files, 39512255 used, 186430532 free (28 frags, 23303813 blocks, 0.0% fragmentation)

MARK FILE SYSTEM CLEAN? [Fyn?] F

***** FILE SYSTEM WAS MODIFIED *****

To finish up, we mount the partition again, then check the files to ensure that everything is there:

# mount /dev/sd0k /home
# ls /home/
...
# df -h
Filesystem     Size    Used   Avail Capacity  Mounted on
...
/dev/sd0k      862G    155M    819G     0%    /home
...

See Also: