Devices and Device Nodes

Devices and Device Nodes

Introduction

A device is a term used mostly for hardware-related activities in a system, including disks, printers, graphics cards, and keyboards. When MidnightBSD boots, the majority of the boot messages refer to devices being detected. A copy of the boot messages is saved to /var/run/dmesg.boot.

Each device has a device name and number. For example, ada0 is the first SATA hard drive, while kbd0 represents the keyboard. The number suffix starts at zero and increments for each additional device of the same type found in the system.

Most devices in MidnightBSD must be accessed through special files called device nodes, which are located in /dev. Device nodes are not regular files — they are interfaces into the kernel that let programs communicate with hardware. They are created and managed automatically by devfs, the device file system.

The /dev Directory

/dev is a special in-memory file system mounted by the kernel at boot. Its contents are populated dynamically as devices are detected or attached. When you plug in a USB drive, a new device node such as /dev/da0 appears automatically. When you remove it, the node disappears.

You can list the device nodes currently present on your system:

ls /dev

Device nodes have a type, owner, group, and permissions just like regular files. The type is shown by ls -l as either c (character device) or b (block device). Disk devices are character devices on MidnightBSD, as is common on BSD systems. Permissions control which users and programs may read from or write to a device.

To see the type and permissions of disk device nodes:

ls -l /dev/ada0 /dev/da0

Viewing Detected Devices

The kernel records messages about every device it detects during boot. These messages are saved to /var/run/dmesg.boot and can be read at any time:

cat /var/run/dmesg.boot

The dmesg command shows the current kernel message buffer, which includes the boot messages and any messages added since boot, such as devices attached after startup:

dmesg

To filter dmesg output for a specific device type, pipe it through grep:

dmesg | grep ada dmesg | grep usb

The pciconf command lists all PCI and PCI Express devices in the system, whether or not a driver has attached to them:

pciconf -lv

To list USB devices:

usbconfig list

Storage Devices

MidnightBSD uses different device name prefixes depending on the type of storage controller and connection interface.

SATA and PATA Disks (ada)

SATA hard drives and SSDs connected through the ATA driver appear as ada devices. The first drive is ada0, the second is ada1, and so on. PATA (IDE) drives also use the ada prefix on modern MidnightBSD.

SCSI, SAS, and USB Storage (da)

SCSI hard drives, SAS drives, and USB mass storage devices appear as da (Direct Access) devices. USB thumb drives and external hard drives attached via USB will appear as da0, da1, etc., alongside any SCSI or SAS disks.

NVMe Disks (nvme, nvd, nda)

NVMe solid state drives are exposed through two sets of nodes. The nvme node represents the controller itself, while nvd (or nda on newer MidnightBSD) represents the namespace (the disk you read and write to).

Optical Drives (cd)

CD-ROM, DVD, and Blu-ray drives appear as cd devices.

To mount a CD:

mount -t cd9660 /dev/cd0 /mnt
Memory Disks (md)

Memory disks are virtual disk devices backed by RAM or a file. They are useful for creating RAM disks, mounting disk images, or setting up temporary file systems.

Create a 64MB memory disk backed by RAM:

mdconfig -a -t malloc -s 64m -u 0

Attach a disk image file as a memory disk:

mdconfig -a -t vnode -f diskimage.img

Detach a memory disk:

mdconfig -d -u 0
GEOM Disk Labels (gpt, label)

The GEOM framework exposes stable disk labels that do not change when disks are reordered. GPT partition labels appear under /dev/gpt/ and disk labels created with glabel appear under /dev/label/. Using labels in /etc/fstab is more reliable than using raw device names.

Disk Partitions and Slices

MidnightBSD supports both GPT (GUID Partition Table) and MBR (Master Boot Record) partitioning. The partition naming scheme differs between the two.

GPT Partitions

On a GPT disk, partitions are named with a p suffix followed by the partition number:

MBR Slices and Partitions

On an MBR disk, the top level is divided into slices (named with s) which contain BSD partitions (named with a letter). This two-level scheme is the traditional BSD disk layout:

The traditional BSD partition letters and their conventional mount points are:

Network Devices

Network interfaces do not have device nodes in /dev. They are managed directly through the kernel's networking stack using commands such as ifconfig. However, they follow the same naming convention of a driver prefix and a unit number.

Common network interface names:

To list all network interfaces and their current configuration:

ifconfig

To show dmesg output for a specific network driver:

dmesg | grep em0

Terminal and Serial Devices

Virtual Consoles (ttyv)

MidnightBSD provides several virtual consoles accessible from the physical keyboard and display. They appear as ttyv devices and can be switched between with Alt+F1 through Alt+F8 (or as configured in /etc/ttys).

Serial Ports (ttyu, cuau)

Physical serial ports and USB-to-serial adapters appear as ttyu and cuau devices. The ttyu name is used for incoming connections (dial-in), while cuau is used for outgoing connections (dial-out) such as connecting to a console server or modem.

USB-to-serial adapters (such as FTDI or Prolific chips) appear as ttyU (capital U) devices:

To connect to a serial device at 9600 baud using cu:

cu -l /dev/cuau0 -s 9600
Pseudo-Terminals (pts)

Pseudo-terminals are pairs of virtual devices used by SSH, terminal emulators, and other programs that need a terminal interface. They appear as /dev/pts/ entries and are created on demand.

Input Devices

Keyboards (kbd)

Keyboard devices are represented by kbd nodes. The console keyboard is typically kbd0. USB keyboards may appear as additional units.

PS/2 Mouse (psm)

PS/2 mice are accessed through the psm device.

USB Mouse and HID Devices (ums, uhid)

USB mice appear as ums devices. Generic USB HID (Human Interface Device) devices that do not match a more specific driver appear as uhid.

Synaptics and evdev Input (input)

On systems using the evdev input layer, input devices such as touchpads, keyboards, and mice may also appear as numbered nodes under /dev/input/.

Pseudo-Devices

Pseudo-devices are special device nodes that do not correspond to any physical hardware. They provide useful kernel-level interfaces for general programming and system use.

/dev/null

Reading from /dev/null always returns end-of-file immediately. Writing to it discards all data silently. It is commonly used to suppress unwanted output:

command > /dev/null 2>&1
/dev/zero

Reading from /dev/zero produces an endless stream of zero bytes. It is used to create blank files of a specific size or to zero out memory regions:

dd if=/dev/zero of=blankfile bs=1m count=100
/dev/random and /dev/urandom

/dev/random provides cryptographically secure random bytes from the kernel's entropy pool. /dev/urandom also provides random bytes and will not block even when the entropy pool is low (it uses a CSPRNG seeded from the pool instead). Both are suitable for generating keys, tokens, and other security-sensitive values.

dd if=/dev/random bs=32 count=1 | hexdump -C
/dev/full

/dev/full always returns a "disk full" error when written to. It is useful for testing how a program behaves when it cannot write to a file.

/dev/stdin, /dev/stdout, /dev/stderr

These nodes are symbolic references to the standard input, output, and error streams of the current process. They allow programs that expect a file path to read from or write to the terminal or a pipe.

cat /dev/stdin
/dev/fd/

The /dev/fd/ directory provides nodes for each open file descriptor of the current process. /dev/fd/0, /dev/fd/1, and /dev/fd/2 correspond to stdin, stdout, and stderr respectively.

/dev/mem and /dev/kmem

/dev/mem provides access to physical memory and /dev/kmem provides access to kernel virtual memory. Access is restricted to root. These devices are used by low-level diagnostic tools.

Device Management Tools

MidnightBSD provides several tools for inspecting and managing devices.

dmesg

Displays the kernel message buffer, including device detection messages from boot and any messages generated since:

dmesg | less
devinfo

devinfo prints the device tree as the kernel sees it, showing parent/child relationships between buses and devices:

devinfo -r
pciconf

Lists all PCI and PCIe devices with their driver bindings and hardware identifiers. Useful for identifying hardware that has no driver attached (shown with a none driver):

pciconf -lv
usbconfig

Lists connected USB devices and their configuration:

usbconfig list usbconfig dump_info
camcontrol

camcontrol manages devices attached through the CAM (Common Access Method) storage layer, including SCSI, SAS, SATA, and NVMe disks. To list all CAM devices:

camcontrol devlist

To see detailed information about a specific disk, including its model and serial number:

camcontrol inquiry da0 camcontrol identify ada0
diskinfo

diskinfo reports the geometry and size of a disk device:

diskinfo -v ada0