The Linux Concept Journey — Block Devices

Shlomi Boutnaru, Ph.D.
2 min readJul 1, 2024

--

Block devices provide the ability to randomly access data which is organized in fixed-size blocks. Examples of such devices are: RAM disks, CD-ROM drives and hard drives. The speed of block devices is in general higher than those of character devices (https://medium.com/@boutnaru/the-linux-concept-journey-character-devices-0c75aa70ceb2). Another difference is that a character device has a single current position. However, in the case of a block device we need to be able to move to any random position for accessing/writing data (https://linux-kernel-labs.github.io/refs/heads/master/labs/block_device_drivers.html). We can see a comparison (with examples) between character devices and block devices in the diagram below (https://www.csltraining.com/block-device-vs-character-devices-in-linux-os/).

Moreover, as with every device, block devices have a major number and a minor number (https://medium.com/@boutnaru/the-linux-concept-journey-major-minor-numbers-56abe372482e ). We can think of the major number identifying the driver and the minor number identifying each physical device handled by the driver. Although there is a difference between block devices they have some common abstractions like: data is typically buffered/cached on reads (an even writes if supported) and data if mostly organized as files and directories for ease of use by the user (https://www.codingame.com/playgrounds/2135/linux-filesystems-101---block-devices/about-block-devices).

Lastly, in order to add a character device driver we need to register it with the kernel. This can be done by leveraging the “register_blkdev” macro which is part of the “include/linux/fs.h” header file (https://elixir.bootlin.com/linux/v6.9.7/source/include/linux/blkdev.h#L809). In the case of kernel version “6.9.7” there are 33 files which reference that macro (https://elixir.bootlin.com/linux/v6.9.7/A/ident/register_blkdev).

See you in my next writeup ;-) You can follow me on twitter — @boutnaru (https://twitter.com/boutnaru). Also, you can read my other writeups on medium — https://medium.com/@boutnaru. You can find my free eBooks at https://TheLearningJourneyEbooks.com.

https://www.csltraining.com/block-device-vs-character-devices-in-linux-os/

--

--