Linux › Section C · Devices › Topic C.2

The sysfs device path and dd

A name such as /dev/sda says little about the hardware behind it, so the kernel also publishes a detailed map of every device under /sys. And when you need to copy raw data to or from a device, dd does the job byte for byte, which makes it both indispensable and dangerous.

Why /dev is not enough

Device files in /dev are for using devices: a program opens /dev/sda to read the disk. But the name reveals little about the hardware, such as which controller the disk hangs off, its model, or whether it is removable. Names are also handed out in the order the kernel finds devices, so the disk called /dev/sdb today may be /dev/sdc after you add another drive. The kernel solves the first problem with sysfs, a virtual filesystem mounted at /sys that describes devices by their real position in the hardware. The naming problem is handled with udev's stable names, as the topic udev explains.

The layout of /sys

The real device tree lives under /sys/devices, arranged by how the hardware is connected: a bus, then a controller, then the devices on it. A SATA disk's directory might be the illustrative /sys/devices/pci0000:00/0000:00:1f.2/ata2/host1/target1:0:0/1:0:0:0/block/sda, which traces the path from the PCI bus through the SATA controller to the disk. Because such paths are long, sysfs also offers shortcuts made of symbolic links: /sys/block lists block devices, /sys/class groups devices by kind, such as net, tty and block, and /sys/bus groups them by bus, such as pci and usb. ls -l /sys/block shows each link and where it points.

Attribute files

Each device directory holds small attribute files, usually one value each, meant mainly for programs. In a disk's directory, dev holds the major and minor numbers, such as 8:0; size holds the capacity counted in 512-byte units; removable is 1 for removable media; and queue/rotational is 1 for a spinning disk and 0 for a solid-state drive. Network interfaces have files such as /sys/class/net/eth0/address, the hardware address. You read them with cat. A few attributes are writable by root to change kernel settings, which is how tools adjust things such as screen brightness. Commands such as lsblk gather much of their information from sysfs.

From a device file to its sysfs path

Finding a device's sysfs directory by hand can be tedious, so ask udev, which keeps this information. udevadm info --query=all --name=/dev/sda prints everything udev knows about the disk: the line starting P: is its path under /sys, N: is the device node name, S: lines are extra symbolic links udev created for it, and E: lines are properties gathered by udev's rules, such as the model and serial number. udevadm info --query=path --name=/dev/sda prints just the path. The output on a real Linux system is long; skim the P: and S: lines first, and see the topic udev for the rest.

Worked example: a disk's size from sysfs

You want the size of the disk /dev/sda without any special tools. 1. Read the attribute: cat /sys/block/sda/size. Suppose it prints 976773168. 2. This counts 512-byte units, whatever the disk's own sector size, so multiply: 976,773,168 × 512 = 500,107,862,016 bytes. 3. In powers of ten, that is about 500.1 GB, the figure a manufacturer would print on the box. 4. In powers of two, divide by 1,073,741,824 to get about 465.8 GiB, the figure many Linux tools report. 5. Check: the same disk's dev file reads 8:0, matching the numbers ls -l shows for /dev/sda, so you were looking at the right device.

dd: copying raw data

dd reads from one file and writes to another in fixed-size blocks, which makes it the tool for raw copies of devices and for cutting chunks out of files. Its syntax is unusual, inherited from an old IBM job-control language: options are written as name=value with no dashes. if= names the input file and of= the output file, defaulting to standard input and output. bs= sets the block size, the number of bytes read and written at a time, and count= limits how many blocks are copied. The simplest example, dd if=/dev/zero of=zeros.bin bs=1K count=4, writes a file of 4,096 zero bytes.

More dd options

skip=n skips n input blocks before copying, and seek=n skips n blocks of the output before writing, so together they let you work on a region in the middle of a file or device. ibs= and obs= set different input and output block sizes when bs= is not suitable. Size suffixes follow GNU rules: K is 1,024 bytes and M is 1,048,576, while kB and MB mean 1,000 and 1,000,000. status=progress prints running statistics during a long copy. conv=fsync makes dd flush the data to the device before it exits. conv=notrunc stops dd from truncating an existing output file, which matters whenever you overwrite only part of one.

Worked example: cutting and patching a file

These commands were run with GNU dd on a 16-byte file blocks.bin holding AAAABBBBCCCCDDDD, using 4-byte blocks. 1. Extract the third block: dd if=blocks.bin of=part.bin bs=4 skip=2 count=1 skips two blocks and copies one, so part.bin holds CCCC, and dd reports 4 bytes copied. 2. Patch the second block of a copy: printf 'XXXX' | dd of=copy.bin bs=4 seek=1 conv=notrunc gives AAAAXXXXCCCCDDDD. 3. Repeat without conv=notrunc and the result is just AAAAXXXX, because dd cut the file off after its write. 4. The lesson: when changing part of a file, always add conv=notrunc and keep a backup first.

Using dd on real devices safely

dd does exactly what you type, with no confirmation, and on a device a single wrong letter can erase a disk. Reading is safe: sudo dd if=/dev/sda of=mbr.bin bs=512 count=1 saves a copy of the disk's first sector. Writing is not. Before writing to a device, run lsblk and check the size and model so you know which name is the target, unmount its partitions, and back up anything you need. A typical write puts an installer image on a USB stick: sudo dd if=distro.iso of=/dev/sdX bs=4M status=progress conv=fsync, where sdX is the stick, never the system disk. Read the command twice before pressing Enter, and never swap if= and of=.

Common mistakes to avoid

First, swapping if= and of=, which copies in the wrong direction and can overwrite the source. Second, writing to the wrong device because names changed after plugging in a stick; confirm with lsblk every time. Third, reading /dev/zero or /dev/urandom with no count, which fills the output until the disk is full. Fourth, forgetting conv=notrunc when patching part of a file. Fifth, confusing K (1,024) with kB (1,000), or GB with GiB when reading sizes. Sixth, writing to a partition such as /dev/sdb1 when an image is meant for the whole device, /dev/sdb.

Key takeaways

/dev is for using devices; /sys, the sysfs virtual filesystem, describes them. /sys/devices holds the real hardware tree, and /sys/block, /sys/class and /sys/bus are shortcuts of symbolic links. Attribute files such as dev, size (in 512-byte units) and removable are read with cat. udevadm info --query=all --name=/dev/sda shows a device's sysfs path (P:) and udev data. dd copies raw blocks: if=, of=, bs=, count=, skip= and seek=, with conv=notrunc, conv=fsync and status=progress. Check the target with lsblk, back up, and read every dd command twice.

Flashcards

Flashcard

sysfs

Virtual filesystem at /sys describing devices and drivers

For information and settings, not for I/O.

Test your understanding

Question 1 of 5

cat /sys/block/sdb/size prints 62521344. About how big is the device?

Question 2 of 5

What does dd if=disk.img of=chunk.bin bs=1M skip=10 count=2 do?

Question 3 of 5

You overwrite bytes in the middle of config.bin with dd ... seek=4 bs=1, but afterwards the rest of the file is gone. What was missing?

Question 4 of 5

Before running sudo dd if=distro.iso of=/dev/sdc bs=4M status=progress conv=fsync, what is the most important check?

Question 5 of 5

Which statement about /dev and /sys is correct?

← Previous topicNext topic →