fdisk

(redirected from Openbsd.Fdisk)

fdisk(8) is a disk partitioning utility. It is called when installing OpenBSD to change the default partition layout of the disk. It can also be called manually after installation when you want to add a new disk.

WARNING: BE CAREFUL! fdisk can cause irreversible 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 may want to read the section on Disk Setup.

Whole Disk for OpenBSD

If you want to set aside an entire disk for OpenBSD, it's relatively easy. First, find the correct disk using dmesg:

$ dmesg | grep -E '(wd|sd)[0-9]'
sd0 at scsibus1 targ 0 lun 0: <VirtIO, Block Device, >
sd0: 20480MB, 512 bytes/sector, 41943040 sectors
sd1 at scsibus2 targ 0 lun 0: <VirtIO, Block Device, >
sd1: 256000MB, 512 bytes/sector, 524288000 sectors
root on sd0a (5f6d0b5c9f65249d.a) swap on sd0b dump on sd0b

NOTE: 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). Here, I have two SCSI-like disks.

In this case, the disk I want to add is sd1, a [[Vmctl/Disk|new disk]] for additional storage. Here is the simple way to create fdisk partitions (Master Boot Record partitions):

$ doas fdisk -iy sd1

I can then proceed to use disklabel, followed by newfs.

Interactive fdisk

Instead of running the above command, it is possible to edit the fdisk partitions interactively:

$ doas fdisk -e sd1
Enter 'help' for information

Type help to get help, manual to read the fdisk man page, and print to view the partitions:

sd1: 1> help
        help            Command help list
        manual          Show entire OpenBSD man page for fdisk
        reinit          Re-initialize loaded MBR (to defaults)
        setpid          Set the identifier of a given table entry
        disk            Edit current drive stats
        edit            Edit given table entry
        flag            Flag given table entry as bootable
        update          Update machine code in loaded MBR
        select          Select extended partition table entry MBR
        swap            Swap two partition entries
        print           Print loaded MBR partition table
        write           Write loaded MBR to disk
        exit            Exit edit of current MBR, without saving changes
        quit            Quit edit of current MBR, saving current changes
        abort           Abort program without saving current changes
sd1: 1> print
Disk: sd1       geometry: 66837/255/63 [1073741824 Sectors]
Offset: 0       Signature: 0x0
            Starting         Ending         LBA Info:
 #: id      C   H   S -      C   H   S [       start:        size ]
-------------------------------------------------------------------------------
 0: 00      0   0   0 -      0   0   0 [           0:           0 ] unused      
 1: 00      0   0   0 -      0   0   0 [           0:           0 ] unused      
 2: 00      0   0   0 -      0   0   0 [           0:           0 ] unused      
 3: 00      0   0   0 -      0   0   0 [           0:           0 ] unused      

This disk sd1 is completely blank, so it is probably safe to edit the fdisk partitions and format the disk.

WARNING: If the disk already has existing partitions, double check to make sure you are not wiping out important data!

First, let's reinit. This should automatically create an fdisk partition on the last partition, partition 3, for OpenBSD and use all available space. It will also initialize this as the boot block.

sd1: 1> reinit
Disk: sd1       geometry: 66837/255/63 [1073741824 Sectors]
Offset: 0       Signature: 0xAA55
            Starting         Ending         LBA Info:
 #: id      C   H   S -      C   H   S [       start:        size ]
-------------------------------------------------------------------------------
 0: 00      0   0   0 -      0   0   0 [           0:           0 ] unused      
 1: 00      0   0   0 -      0   0   0 [           0:           0 ] unused      
 2: 00      0   0   0 -      0   0   0 [           0:           0 ] unused      
*3: A6      0   1   2 -  66836 254  63 [          64:  1073736341 ] OpenBSD     
Use 'write' to update disk.

Notice that the Signature changes from 0x0 to 0xAA55, and partition #3 is now set to id A6 (the OpenBSD partition type).

This can also be done manually by using the edit command on partition 3:

sd1: 1> edit 3
            Starting         Ending         LBA Info:
 #: id      C   H   S -      C   H   S [       start:        size ]
-------------------------------------------------------------------------------
 3: 00      0   0   0 -      0   0   0 [           0:           0 ] unused      
Partition id ('0' to disable) [01 - FF]: [0] (? for help) A6
Do you wish to edit in CHS mode? [n] y
BIOS Starting cylinder [0 - 66836]: [0] 
BIOS Starting head [0 - 254]: [1] 
BIOS Starting sector [1 - 63]: [2] 
BIOS Ending cylinder [0 - 66836]: [0] 66836
BIOS Ending head [0 - 254]: [0] 254
BIOS Ending sector [1 - 63]: [1] 63

We need to edit in Cylinder-Head-Sector (CHS) mode. To use the full space of the disk, you will want to choose the lowest numbers in the range for the starting cylinder, head, and sector; and the highest possible numbers for the ending cylinder, head, and sector.

We now write to disk:

sd1*: 1> write
Writing MBR at offset 0.

We'll print G to see everything in gigabytes:

sd1: 1> print G
Disk: sd1       geometry: 66837/255/63 [1073741824 Sectors]
Offset: 0       Signature: 0xAA55
            Starting         Ending         LBA Info:
 #: id      C   H   S -      C   H   S [       start:        size ]
-------------------------------------------------------------------------------
 0: 00      0   0   0 -      0   0   0 [           0:           0G] unused      
 1: 00      0   0   0 -      0   0   0 [           0:           0G] unused      
 2: 00      0   0   0 -      0   0   0 [           0:           0G] unused      
*3: A6      0   1   2 -  66836 254  63 [          64:         512G] OpenBSD     

If everything looks good,

sd1: 1> quit

We can now proceed to use disklabel, followed by newfs.