Kahibaro
Discord Login Register

3.2.1 Devices and partitions

Understanding Devices and Partitions

In Linux system administration, it is essential to distinguish clearly between devices and the partitions that sit on top of them. This chapter focuses on how Linux represents storage hardware as devices, how partitions are defined on those devices, and how you can inspect and understand this layout from the command line.

Block Devices and Their Representation in Linux

Linux treats hardware storage such as hard disks, solid state drives, and some forms of removable media as block devices. A block device allows the kernel to read and write fixed sized chunks of data, usually called blocks or sectors, instead of individual bytes.

These block devices appear in the filesystem under /dev. The /dev directory does not contain regular files, but device nodes that act as special entry points into the kernel. When you open a device node, the kernel routes the access to the underlying hardware.

Typical disk devices look like /dev/sda, /dev/sdb, /dev/nvme0n1, or /dev/vda. The name gives a hint about the type of device and the order in which the kernel discovered it. For example, traditional SCSI or SATA disks often appear as sdX, where X starts at a for the first disk, b for the second, and so on. NVMe devices use a different naming scheme with numbers that indicate both the device and the namespace, such as /dev/nvme0n1.

Important rule: A device node such as /dev/sda represents an entire disk, not a filesystem. Never write directly to a whole disk device unless you are sure you are creating or overwriting partition tables or raw data.

The Role of the Partition Table

A raw disk is just a long sequence of sectors. To make this usable for operating systems that expect multiple separate areas on a disk, such as separate filesystems or reserved regions, a partition table is used. The partition table is a small structure located at the start of the disk that records where each partition begins and ends.

The partition table does not hold data itself beyond these definitions. It simply maps ranges of sectors to logical partitions. Linux reads this table at boot or when the disk is attached and then exposes each partition as a separate block device node under /dev.

Two partitioning schemes are common today. One is the older MBR, also known as DOS partitioning, and the other is the newer GPT that is usually used with UEFI systems. Both define how partitions are described and how many can exist, but they do not change the fundamental idea that partitions are just ranges of sectors on a single device.

Device Names for Partitions

Once the kernel reads the partition table, it exposes each partition as its own block device. These devices are derived from the parent disk name.

On classic SCSI or SATA style names, such as /dev/sda, partitions are numbered by appending a digit. So the first partition becomes /dev/sda1, the second /dev/sda2, and this pattern continues. If there are extended partitions under MBR, logical partitions typically start at number 5 such as /dev/sda5.

On NVMe devices, the pattern is slightly different to avoid ambiguity. The whole device might be /dev/nvme0n1. The first partition appears as /dev/nvme0n1p1, the second as /dev/nve0n1p2. The p separates the disk name from the partition number.

The operating system treats each partition device just like any other block device. You can create filesystems on them, mount them, or use them as physical volumes for advanced storage systems. The key point is that these partitions share the same underlying disk, so they use different non overlapping sections of the same storage space.

Important rule: Partition devices such as /dev/sda1 are the correct targets for creating filesystems. Commands like mkfs.ext4 should normally operate on a partition device, not on the whole disk device.

Viewing Devices and Partitions

Linux provides several tools to inspect disks and their partitions. A simple and safe way is to use listing commands that only read metadata and do not modify anything.

The lsblk command shows block devices in a tree like format. A typical invocation is:

lsblk

This displays devices, their sizes, types, and relationships. Disks appear as disk types, while partitions are listed as part types under their parent disks. Additional columns may reveal filesystem types or mount points if they exist.

Another common tool is fdisk in list mode. To see the partition table of a specific disk, you can run:

sudo fdisk -l /dev/sda

This does not change anything on the disk if you only list. The output shows sectors, sizes, and the partition type identifiers. For GPT disks, tools like parted can also show you partition information in a more flexible way.

The /sys and /proc virtual filesystems also expose device details. For example, /proc/partitions lists partitions and their sizes in blocks. The /sys/block directory contains subdirectories for each disk and their partitions, with attributes that the kernel uses internally.

Physical Devices vs Virtual Devices

Not every device in /dev corresponds to a physical piece of hardware. Some block devices are virtual layers built on top of others. Examples include logical volumes from LVM, device mapper devices such as encrypted volumes, and software RAID arrays.

These virtual devices often have names such as /dev/mapper/vg0-root or /dev/md0 for RAID. They can themselves contain filesystems or even be partitioned, depending on how they are configured. What matters is that Linux still treats them as block devices, just like disks and partitions.

The important distinction is that physical devices represent actual hardware or directly attached storage, while virtual devices are defined by software. Partitions are usually defined on physical disks, but some tools allow partition like constructs on virtual devices too.

Interpreting Sizes and Alignment

When you examine devices and partitions, you see sizes reported in bytes or in larger units such as MiB and GiB. Internally, disks use sectors, which are usually 512 bytes or 4096 bytes. The size of a partition is:

$$
\text{size in bytes} = (\text{end sector} - \text{start sector} + 1) \times \text{sector size}
$$

Many tools hide this detail, but understanding it can help when dealing with alignment. Modern disks and SSDs perform best if partitions start at boundaries that align with internal erase blocks. Most partitioning tools attempt to align automatically at 1 MiB boundaries. As a result, you often see starting points at sector values that correspond to 1 MiB or multiples of it, given the sector size.

Important statement: When creating new partitions, always let modern tools use their default alignment. Misaligned partitions can lead to lower performance and increased wear on SSDs.

Primary, Extended, and Logical Partitions

On disks that use the older MBR partition scheme, the table can record only four primary entries. To allow more, the concept of an extended partition was introduced. In this model, you can have up to four entries, where at most one can be an extended partition that in turn contains multiple logical partitions.

Linux makes this structure visible as partition devices just like any others. Primary partitions use numbers 1 to 4. If there is an extended partition, it is one of these numbers, and its contents become logical partitions, usually starting at number 5. For example, a disk might have /dev/sda1 and /dev/sda2 as primary partitions, /dev/sda3 as the extended container, and /dev/sda5, /dev/sda6 inside that extended area.

GPT removes the need for extended partitions by allowing many more partition entries in a uniform way. On GPT disks you simply have partitions numbered from 1 upward without any special extended construct.

Device Discovery and Hotplug

Modern systems often have disks attached or removed while the machine is running. This hotplug behavior is common for external USB drives, SD cards, and some storage arrays. The kernel detects these changes and creates or removes device nodes in /dev, and updates information in /sys.

If you plug in a USB drive, you might see a new device such as /dev/sdc appear along with partitions like /dev/sdc1. The dmesg command can show kernel messages announcing new devices. Alternatively, running lsblk again reveals the updated device set.

When partitions are created or modified, the kernel must be informed so that it can update its internal view. Many partitioning tools do this automatically when you write changes. If not, you can use partprobe to ask the kernel to re read the partition table, as long as the device is not in active use in a way that prevents changes.

Devices and Partitions in Practice

In daily administration, you usually do not interact directly with the raw disk except during initial setup. The normal flow is to identify the correct disk device, create a partition table, define partitions according to your layout, and then treat these partition devices as the building blocks for filesystems or higher level storage.

For example, during installation you might see that the installer creates /dev/sda1 for a small boot partition, /dev/sda2 for a root filesystem, and /dev/sda3 for a separate home partition. Each of these corresponds to a segment of the same physical disk. Later, you mount these partitions at different directories, work with files, and rarely need to think about where they sit inside the disk, unless you modify the storage layout.

Being able to read partition listings and to recognize which device corresponds to which part of your system is critical when performing operations that can alter or destroy data. Misidentifying /dev/sdb and /dev/sdc can lead to formatting the wrong disk, so cautious inspection with tools like lsblk and fdisk -l is always a necessary step before any destructive operation.

By understanding how Linux represents devices and partitions, you gain a reliable mental model that connects the hardware inside your machine with the block devices you see on the command line and the filesystems you manage at a higher level.

Views: 10

Comments

Please login to add a comment.

Don't have an account? Register now!