11.1. Identifying Storage Devices
Storage administration goes wrong most often at the very first step: acting on the wrong device. This section builds the habit of identifying a disk - its name, size, sector geometry, and any existing signatures - before anything destructive happens near it, and of preferring names that survive reboots over names that merely happen to be correct today.
11.1.1. Block Devices, Partitions, and Layered Storage
A block device is addressed in units the device itself defines -
traditionally 512-byte sectors - and the kernel satisfies each read or
write of that unit directly. /dev/vdb is one such device. The kernel
assigns each a major and minor number and a name; udev, the device
manager introduced in Chapter 7 (Section 7.5.1),
creates the /dev file, watches for device events, and maintains the
alternative names described in Section 11.1.3.
A partition is not a separate device at the hardware level. It is a
range of sectors that a partition table - a small data structure at a
fixed location on the disk - describes. The kernel reads that table and
exposes each range as its own block device: /dev/vdb1 is the first
described range of /dev/vdb, and reading and writing it is reading and
writing that sector range of the parent disk. A partition therefore needs
no format of its own; what matters is that a filesystem, a swap signature,
or another consumer is later written into the range.
Layers can be skipped and stacked. A filesystem can live directly on a
whole disk with no partition table at all - Chapter 9
did exactly that for its I/O exercises. Going the other way, the kernel's
device-mapper framework and the md RAID driver synthesize new block
devices out of real ones: LVM stripes chunks across disks, LUKS decrypts
one device into another, RAID mirrors several into one. Every layer is
still "a block device that something above can treat as a disk", which is
why lsblk shows the whole stack as a tree. Figure 11.1 shows the layers
this chapter builds on /dev/vdb, and where the layering of
Chapter 13 would sit.
Figure 11.1. Conceptual view of the storage stack built in this chapter: one whole-disk device, a GPT partition table, three ext4 filesystems, and their mount points, with 512 MiB deliberately left unallocated. LVM, LUKS, RAID, and loop devices insert additional synthetic block devices between the disk and the filesystem.
lsblk from util-linux is the first command to run on an unfamiliar
system, because it shows every layer at once. On storage, as john:
$ lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS
NAME SIZE TYPE FSTYPE MOUNTPOINTS
vda 100G disk
└─vda1 100G part ext4 /
vdb 2G disk
The tree reads bottom-up: vda is a whole disk (TYPE disk) whose single
partition vda1 holds an ext4 filesystem mounted at /. vdb is the
second disk - a whole disk with no partitions, no filesystem, and nothing
mounted, exactly the blank state a practice disk should be in. Nothing on
the system writes to it yet.
lsblk option |
Description |
|---|---|
| (none) | Print all block devices in a tree with default columns. |
-o COLUMNS |
Choose columns by name, as in -o NAME,SIZE,TYPE. |
-f |
Add filesystem columns: FSTYPE, FSVER, LABEL, UUID. |
-D |
Add discard columns: DISC-ALN, DISC-GRAN, DISC-MAX (Section 11.5.1). |
DEVICE |
Restrict the listing to one device and its children. |
Two habits prevent most "wrong disk" accidents with lsblk: read the
tree edges before acting (a partition shown with a MOUNTPOINTS entry is
in use), and re-run it after every change - Section 11.2.3's partition
creation and Section 11.3.1's filesystem creation each end with a fresh
lsblk that shows the new state.
11.1.2. Inspecting Topology, Capacity, Sector Sizes, and Signatures
lsblk answers "what exists". The next questions are physical: how large
is the device exactly, what sector geometry does it report, where does it
sit on its controller, and does it already carry data. Three tools answer
those in the root shell, all against the blank /dev/vdb.
fdisk -l prints the raw capacity and geometry:
# fdisk -l /dev/vdb
Disk /dev/vdb: 2 GiB, 2147483648 bytes, 4194304 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
The disk reports 2,147,483,648 bytes - 2 GiB exactly, composed of
4,194,304 sectors of 512 bytes. fdisk -l is a read-only view from any
shell; the same program's interactive mode can partition disks too, but
this chapter partitions with parted.
GNU Parted reads the same disk with more structure. Running parted with
a device and commands executes those commands and exits:
# parted /dev/vdb unit MiB print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 2048MiB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
Error: /dev/vdb: unrecognised disk label
The error is the finding: Partition Table: unknown and the accompanying
message mean the disk carries no partition table that parted recognizes -
blank media, not broken media. (parted prints a blank line, the device
summary, and the error on standard error; it exits 0 for a print on an
unlabeled disk, and the label gets created in Section 11.2.3.) Parted is
also this chapter's partitioning tool, so its forms are worth one table
up front:
parted form |
Description |
|---|---|
parted DEVICE |
Interactive editor on DEVICE; commands are typed at a prompt, quit leaves. |
parted DEVICE command … |
Run the given commands on DEVICE and exit - the form used here. |
-l / --list |
Print a summary of every device's table and exit. |
-s / --script |
Never prompt for confirmation; required in scripts and destructive batch steps (Section 11.2.3). |
unit UNIT |
Choose the unit for reported positions (MiB here). |
mklabel LABEL |
Write a new, empty partition table (gpt or msdos), replacing whatever is on the disk. |
mkpart [name] fs-type start end |
Create a partition spanning start to end; on GPT, name is the partition name, on msdos a type position (primary) is required instead. |
align-check optimal N |
Confirm partition N starts on the device's optimal boundary (Section 11.2.3). |
The kernel exposes the sector geometry directly through blockdev:
# blockdev --getss /dev/vdb
512
# blockdev --getpbsz /dev/vdb
512
# blockdev --getsize64 /dev/vdb
2147483648
--getss is the logical sector size the device presents; --getpbsz is
the physical sector size it actually writes; --getsize64 is the exact
byte capacity. The two sector sizes differ on most modern hard disks and
SATA SSDs, which present 512-byte logical sectors for compatibility while
writing 4096-byte physical sectors - the "512e" arrangement. The example
virtio disk reports 512 for both because the hypervisor presents a simple
device. The distinction matters when partitioning: a partition whose
boundaries fall on physical-sector boundaries performs better and, on
4Kn-native devices, avoids outright misalignment. Section 11.2.2 returns
to this when planning partition starts.
Where a device sits - and which stable name udev derived from that
position - is visible through udevadm:
# udevadm info --query=property --name=/dev/vdb | grep -E "^(DEVNAME|DEVTYPE|ID_PATH)="
DEVNAME=/dev/vdb
DEVTYPE=disk
ID_PATH=pci-0000:00:04.0
DEVTYPE=disk confirms a whole-disk device, and ID_PATH records the
hardware path: PCI device 0000:00:04.0, the virtio-blk controller. The
pipeline is one lookup operation - udevadm prints all properties,
grep -E keeps the three that matter. The same value appears as a stable
device name in Section 11.1.3.
The last property worth checking before writing anything is what the device already contains. Filesystems and other on-disk structures identify themselves with signatures: magic numbers at fixed offsets (an MBR partition table starts with boot code and a table at byte 0, a GPT header begins at the second 512-byte sector, an ext4 superblock begins 1024 bytes into the filesystem). Nothing about partitioning erases these - a disk repartitioned but not reformatted still carries the old filesystem signatures inside the new partition ranges. Two commands read them:
wipefs -n DEVICElists the signatures it finds without changing anything. On blank media it prints nothing at all.blkid DEVICEreports filesystem type, label, and UUID for each device with a recognized signature.
Both are used at the moment it matters - immediately before creating a
filesystem in Section 11.3.1, where wipefs -n proves the target
partitions are actually blank, and again after creation, where the new
ext4 signature appears. One more pairing is worth knowing now: mkfs
tools refuse to overwrite an existing signature without an explicit
confirmation, and never overwrite a mounted device (Section 11.3.2
demonstrates both refusals), so a stale signature is a warning, not a
silent data-loss path.
11.1.3. Persistent Device Names, UUIDs, and Labels
The kernel names disks in the order drivers find them: vdb is "the
second virtio disk", sdb is "the second SCSI-named disk", and on a
system with several similar controllers, two disks can trade names
between boots. A mount configuration keyed to such a name can attach the
wrong filesystem after a hardware change. udev therefore builds a tree of
stable names under /dev/disk/, each a symlink to the kernel device:
| Directory | Keyed to | Stable across |
|---|---|---|
/dev/disk/by-uuid/ |
Filesystem UUID, set at mkfs time |
Repartitioning of other disks, renaming, cable changes; not reformatting |
/dev/disk/by-label/ |
Filesystem label, set at mkfs time |
Same as UUID, but only as unique as your labels are |
/dev/disk/by-partuuid/ |
GPT partition GUID | Repartitioning of other disks; persists across reformatting |
/dev/disk/by-partlabel/ |
GPT partition name | Same as PARTUUID, but only as unique as your names are |
/dev/disk/by-path/ |
Hardware location (controller, port, slot) | Device-name reordering; changes if the device moves to another port or slot |
/dev/disk/by-id/ |
Drive serial number or WWN | Everything except replacing the drive itself |
/dev/disk/by-diskseq/ |
Kernel sequence number of the media | Nothing; identifies the current insertion of a device |
The by-path names already exist for the blank data disk, because the controller position exists before any content does:
# ls -l /dev/disk/by-path/ | grep vdb
lrwxrwxrwx 1 root root 9 Nov 20 02:22 pci-0000:00:04.0 -> ../../vdb
lrwxrwxrwx 1 root root 9 Nov 20 02:22 virtio-pci-0000:00:04.0 -> ../../vdb
pci-0000:00:04.0 is the ID_PATH value from Section 11.1.2 with the
symlink pointing at ../../vdb relative to the directory. By-path names
say where a disk sits, which makes them the right choice when the
administrator means a physical slot ("the disk in the third bay") and the
wrong choice for a configuration that must survive moving cables.
The content-keyed names - by-uuid, by-label, by-partuuid,
by-partlabel - appear when the content does: a filesystem's UUID and
label are written by mkfs (Section 11.3.1), and a GPT partition's GUID
and name are written by the partitioning step (Section 11.2.3). Both
listings are shown there, once the example filesystems exist to populate
them.
Two cautions complete the picture. First, the example disk has no
/dev/disk/by-id/ entry, because virtio disks report no serial number or
WWN; physical SATA, SAS, and NVMe drives normally do, and by-id is often
the most readable permanent name on such hardware. Second, all of these
names are conventions udev maintains, not magic: a site that needs its
own name can add a udev rule that creates another symlink, exactly as
Chapter 7 did for the root disk in Section 7.5.3.
For /etc/fstab, the recommendation this chapter follows: use
UUID=<filesystem uuid> for mounted filesystems (unique by construction,
survives every hardware change short of reformatting), use labels only
when a human is meant to read them, and use PARTUUID= when the
filesystem itself is expected to be recreated but the partition layout
is permanent.
11.1.4. Checking Disk Health and Device Errors
Before trusting a device - and before blaming one - two sources report health: the drive's own SMART data, and the kernel log.
SMART (Self-Monitoring, Analysis and Reporting Technology) is read with
smartctl from the smartmontools package, which needs root because it
sends commands through the device driver. On the example system:
$ sudo smartctl -i /dev/vdb
smartctl 7.4 2023-08-01 r5530 [x86_64-linux-6.12.86+deb13-amd64] (local build)
Copyright (C) 2002-23, Bruce Allen, Christian Franke, www.smartmontools.org
/dev/vdb: Unable to detect device type
Please specify device type with the -d option.
Use smartctl -h to get a usage summary
The failure is about the transport, not the disk: smartctl reaches a drive's SMART registers through controller-specific passthrough commands, and virtio disks expose none. A physical SATA, SAS, or NVMe disk answers the same command with its model, serial number, firmware version, and SMART capability - the identity record the rest of the toolset builds on. This virtual disk has no SMART data to read, so the health-checking demonstrations in this chapter use the kernel log; on real hardware the two are complementary.
On a physical disk, the commands worth knowing are:
sudo smartctl -H /dev/sdX- the overall health assessment (PASSEDor a failure verdict).sudo smartctl -A /dev/sdX- the attribute table: reallocated sectors, pending sectors, wear indicators, temperature. Rising reallocated or pending counts are the classic early warning of a failing drive.sudo smartctl -t short /dev/sdX- run the drive's short self-test (a couple of minutes);sudo smartctl -l selftest /dev/sdXprints the result log afterwards.- The
smartddaemon (thesmartmontoolspackage's service) polls drives periodically and can mail reports of degradation - its configuration lives in/etc/smartd.conf.
USB enclosures are the common gap in coverage: many do not pass SMART commands through at all, so an external disk may be as opaque to smartctl as this virtual one.
The kernel log is the universal channel. A failing device reports itself
there - I/O errors, medium errors, and (for ext4) the remount events of
Section 11.5.3 - and the log predates any tooling: dmesg shows the
current buffer, journalctl -k the persisted per-boot history
(Chapter 7, Section 7.6.1, covered both). The
practical order when a disk is suspect: read the log first, because it
shows what actually failed; then read SMART, which shows the drive's own
account of its condition.