NetBSD Partitioning

MBR Partitions

Coming from Linux, setting up partitions in NetBSD is quite different. The two operating systems can co-exist happily, though, because of how the two share partitions. The short version of this document is this: Linux uses an MBR entry for each of it's partitions, while NetBSD uses one MBR entry for all of its partitions, and then uses a subset to divide that entry as well into smaller entries.

For example, in a Linux setup with MBR, you may have something that looks like this:

Device Type Mount System
/dev/sda1 ext2 /boot Linux
/dev/sda2 swap Linux swap
/dev/sda3 ext4 / Linux

In this example, /dev/sda is the actual device (presumably a hard drive) that is divided into MBR partitions.

Using fdisk -l /dev/sda in Linux would display the MBR table.

Adding a NetBSD partition to MBR

If you wanted to add a NetBSD partition to this table, you would create a new entry with fdisk, and end up with a table similar to this:

Device Type Mount System
/dev/sda1 ext2 /boot Linux
/dev/sda2 swap Linux swap
/dev/sda3 ext4 / Linux
/dev/sda4 NetBSD

The partition solely for NetBSD resides alongside the other MBR entries.

NetBSD and disklabel

NetBSD occupies an entry in the MBR, but also creates its own partition table under that, using disklabel. These partitions are specific to NetBSD only – they will not be displayed in the MBR partition list.

NetBSD uses DOS partitioning naming schemes, with devices a:, b:, c: and d:.

Using this naming scheme, a: and b: would refer to the first two disklabels. However, c: would refer to the entire MBR partition that NetBSD is using. Using the example above, that would be the same device as /dev/sda4.

NetBSD also reserves the d: partition to represent the entire device. Comparatively, /dev/sda for Linux.

Linux Device NetBSD Device NetBSD disklabel Disk
/dev/sda /dev/sd0d d: Entire disk
/dev/sda4 /dev/sd0c c: Fourth MBR entry

For the record, NetBSD also uses /dev/sd0 to represent /dev/sd0d as well; that is, the whole disk. So if we modified our table to include all the entries, here is how it would look:

Linux Device NetBSD Device NetBSD disklabel Disk
/dev/sda /dev/sd0 d: Entire disk
/dev/sda /dev/sd0d d: Entire disk
/dev/sda4 /dev/sd0c c: Fourth MBR entry

This graphic from the NetBSD guide is also helpful:

See this document from the NetBSD guide.

Partitioning an MBR in NetBSD

If, for any reason, you want to clear out the MBR completely of an existing device, you can use dd to write over it:

dd if=/dev/zero bs=512 count=1 of=/dev/sd0
Using fdisk

When you have a new device that needs to create an MBR table, use fdisk in interactive mode.

Just running fdisk on a device will print out it's partition table. For example: fdisk sd0.

Run fdisk -u sd0 to run fdisk in interactive mode. Please note: that the syntax is sd0 and not /dev/sd0.

The first question it will ask you is if you want the change the disk geometry. The defaults are fine, so answer no.

Do you want to change our idea of what BIOS thinks? [n]

Next, fdisk will display the current MBR table layout. If the MBR is empty, then they will all be marked as unused:

Partition table:
0: <UNUSED>
1: <UNUSED>
2: <UNUSED>
3: <UNUSED>

It's worth pointing out here that the MBR table lists four devices. This is consistent with the original MBR limitations, where only four partitions could be created. Extended partitions works around this limitation, which we aren't going to cover here.

Creating a partition

Now, create a partition. Since this one is empty, start with the first, numbered zero.

Which partition do you want to change?: [none] 0

The first value, the sysid is the type of partition. The equivalent in fdisk for Linux would be the hex code, such as 82 for Linux and 83 for Linux swap. Use the default value of 169 to create a NetBSD partition.

The three following defaults are fine as well:

sysid: [0..255 default: 169] 
start: [0..1cyl default: 63, 0cyl, 0MB] 
size: [0..1cyl default: 16321, 1cyl, 8MB] 
bootmenu: []

fdisk will then print out the new table layout with the propsed changes.

Partition table:
0: NetBSD (sysid 169)
    start 63, size 16321 (8 MB, Cyls 0-1/5/4)
        PBR is not bootable: All bytes are identical (0x00)
1: <UNUSED>
2: <UNUSED>
3: <UNUSED>

If you have no more partitions to create, then select 'none' and write out the new partition table.

Which partition do you want to change?: [none]
Should we write new partition table? [n] y

Create NetBSD slices

Short version: run disklabel sd0 (same syntax, sd0, sd1, sd2, etc.) to see stuff, disklabel -i -I sd0 to create slices. Use a: b: and e:, skipping c: and d:.

To remove a slice, set the size to 0 on both ends. To create one to fill the remaining space, use $.

Then use newfs to create new filesystems. :)