Skip to main content
Engineering LibreTexts

04-A.1: Storage Devices

  • Page ID
    26832

    EXAM OBJECTIVES COVERED
    1.4 Given a scenario, manage storage in a Linux environment.
    2.3 Given a scenario, create, modify, and redirect files.
    4.1 Given a scenario, analyze system properties and remediate accordingly.

    Objectives of this Module

    In this lesson, you will:

    • Learn how to partition a drive in Linux.
    • Learn to mount and manage Linux file systems.
    • Identify the command line tools that can be used to perform disk setup in Linux.
    • Differentiate between the purposes for journaling file systems and virtual file systems.

    Storage in Linux

    Like most other computing platforms, Linux is capable of working with most of the storage devices that are available on the market. The common types of storage include:

    • hard disk drive (HDD) - they continue to hold greater quantities of date and are dropping in price;
    • solid state devices (SSD) - a solid-state storage device that uses integrated circuit assemblies to store data persistently, typically using flash memory;
    • USB flash drive - a data storage device that includes flash memory with an integrated USB interface;
    • external storage devices - this includes the wide variety of optical devices - CD/DVD/Blu-ray as well as older magnetic tape;
    • Secure Digital (SD) cards are a tiny flash memory card designed for high-capacity memory and various portable devices.

    So, no worries about whether your Linux system can make use of any of the latest technology. In fact Linux is often quicker to pick up on newer technology than some of the commercial companies.

    Linux: Block vs Character Devices

    Character devices
    Character special files or character devices provide unbuffered, direct access to the hardware device. They do not necessarily allow programs to read or write single characters at a time; that is up to the device in question. The character device for a hard disk, for example, will normally require that all reads and writes are aligned to block boundaries and most certainly will not allow reading a single byte.

    Character devices are sometimes known as raw devices, to avoid the confusion surrounding the fact that a character device for a piece of block-based hardware will typically require programs to read and write aligned blocks.

    Block devices
    Block special files or block devices provide buffered access to hardware devices, and provide some abstraction from their specifics.[5] Unlike character devices, block devices will always allow the programmer to read or write a block of any size (including single characters/bytes) and any alignment. The downside is that because block devices are buffered, the programmer does not know how long it will take before written data is passed from the kernel's buffers to the actual device, or indeed in what order two separate writes will arrive at the physical device. Additionally, if the same hardware exposes both character and block devices, there is a risk of data corruption due to clients using the character device being unaware of changes made in the buffers of the block device.

    Device Files

    Device files are also known as device special files. Device files are employed to provide the operating system and users an interface to the devices that they represent. All Linux device files are located in the /dev directory, which is an integral part of the root (/) filesystem because these device files must be available to the operating system during the boot process.

    One of the most important things to remember about these device files is that they are most definitely not device drivers. They are more accurately described as portals to the device drivers. Data is passed from an application or the operating system to the device file which then passes it to the device driver which then sends it to the physical device. The reverse data path is also used, from the physical device through the device driver, the device file, and then to an application or another device.

    Let's look at the data flow of a typical command to visualize this.

    Issuing the cat /etc/resolv.conf command from a terminal causes the resolv.conf file to be read from the disk with the disk device driver handling the device specific functions such as locating the file on the hard drive and reading it. The data is passed through the device file and then from the command to the device file and device driver for pseudo-terminal 6 where it is displayed in the terminal session
    Figure \(\PageIndex{1}\): Simple data flow for a typical command. ("Simple data flow for a typical command." by David Both is licensed under CC BY-SA 4.0)

    In Figure 1, above, a simplified data flow is shown for a common command. Issuing the cat /etc/resolv.conf command from a GUI terminal emulator such as Konsole or xterm causes the resolv.conf file to be read from the disk with the disk device driver handling the device specific functions such as locating the file on the hard drive and reading it. The data is passed through the device file and then from the command to the device file and device driver for pseudo-terminal 6 where it is displayed in the terminal session.

    Of course, the output of the cat command could have been redirected to a file in the following manner, cat /etc/resolv.conf > /etc/resolv.bak in order to create a backup of the file. In that case, the data flow on the left side of Figure 1 would remain the same while the data flow on the right would be through the /dev/sda2 device file, the hard drive device driver and then onto the hard drive itself.

    These device files make it very easy to use standard streams (STD/IO) and redirection to access any and every device on a Linux or Unix computer. Simply directing a data stream to a device file sends the data to that device.

    Classification

    Device files can be classified in at least two ways. The first and most commonly used classification is that of the data stream commonly associated with the device. For example, tty (teletype) and serial devices are considered to be character based because the data stream is transferred and handled one character or byte at a time. Block type devices such as hard drives transfer data in blocks, typically a multiple of 256 bytes.

    If you have not already, go ahead and as a non-root user in a terminal session, change the present working directory (PWD) to /dev and display a long listing. This shows a list of device files with their file permissions and their major and minor identification numbers. For example, the following device files are just a few of the ones in the /dev/directory on my Fedora 24 workstation. They represent disk and tty type devices. Notice the leftmost character of each line in the output. The ones that have a "b" are block type devices and the ones that begin with "c" are character devices.

    brw-rw----   1 root disk        8,   0 Nov  7 07:06 sda
    brw-rw----   1 root disk        8,   1 Nov  7 07:06 sda1
    brw-rw----   1 root disk        8,  16 Nov  7 07:06 sdb
    brw-rw----   1 root disk        8,  17 Nov  7 07:06 sdb1
    brw-rw----   1 root disk        8,  18 Nov  7 07:06 sdb2
    crw--w----   1 root tty         4,   0 Nov  7 07:06 tty0
    crw--w----   1 root tty         4,   1 Nov  7 07:07 tty1
    crw--w----   1 root tty         4,  10 Nov  7 07:06 tty10
    crw--w----   1 root tty         4,  11 Nov  7 07:06 tty11
    

    The more detailed and explicit way to identify device files is using the device major and minor numbers. The disk devices have a major number of 8 which designates them as SCSI block devices. Note that all PATA and SATA hard drives have been managed by the SCSI subsystem because the old ATA subsystem was many years ago deemed as not maintainable due to the poor quality of its code. As a result, hard drives that would previously have been designated as "hd[a-z]" are now referred to as "sd[a-z]."

    You can probably infer the pattern of disk drive minor numbers in the small sample shown above. Minor numbers 0, 16, 32 and so on up through 240 are the whole disk numbers. So major/minor 8/16 represents the whole disk /dev/sdb and 8/17 is the device file for the first partition, /dev/sdb1. Numbers 8/34 would be /dev/sdc2.

    The tty device files in the list above are numbered a bit more simply from tty0 through tty63.

    Adapted from:
    "Managing devices in Linux" by David Both , Opensource.com is licensed under CC BY-SA 4.0

    • Was this article helpful?