26.06.2024

How to manage devices and disks on Linux?

Why when I plug in a disc it doesn't work in the OS, a flash drive is not read by the system? And the network interface refuses to appear in the device list? How to connect discs and configure their work or allow to use a certain list of devices? To all these and other questions we will consider the answer, having analysed the architecture and the principle of working with devices in Linux. After this tutorial you will be able to configure, manage the state and permission of devices on your system by yourself.

How does disc mapping work in Linux?

Let's break down the full cycle of mounting a disc in Linux, in order to understand how it works on the system. And what can be done to solve problems that arise?

Screenshot №1 — Schema

We physically or virtually connect the disc to our system, and then information about the new device comes to the kernel. The kernel selects a driver for its operation and collects information about the device. In Linux, any device is a "file" and this means that the kernel, after collecting the information, writes the data to a file in the /dev folder. But this is not an ordinary file description of the connected device, but a point of access to the disc!

Programs can open, read, write and manipulate these device files using standard system calls such as open(), read(), write() and ioctl(). These calls are passed through the device file to the device driver, which then communicates with the hardware through the kernel.
The device driver converts the system calls into hardware commands, which the kernel passes to the device. Feedback from the device also passes through the driver back to the software.

After that the kernel creates the uevents event control passes to Udev, which is a device manager with a prepared set of rules. These are used to change the device name, user access rights, blocking, and scripts when connecting or disconnecting devices. If there are criteria that require changes to be made, Udev will go to the /dev directory and perform the necessary actions.

How do I connect a drive to Linux?

All steps in the tutorial can be performed on powerful cloud servers. Serverspace provides isolated VPS / VDS servers for common and virtualize usage.

Screenshot №2 — Create Server

It will take some time to deploy server capacity. After that you can connect in any of the convenient ways!

Now let's look at the situation in practice, we need to mount a disc and give it a special name, prohibit connection of devices with a certain UUID, and execute a script to automate our tasks. This can be anything: partitioning a new disc, rotating old files or notifying the administrator.

Let's connect the disc and do some basic configuration: partition and set the file system. Use the lsblk command to examine the mounted discs:

lsblk

Screenshot №3 — New device

The new disc appears as vdb with no logical volumes or partitioning. To start using it you need to create a table and volumes, and then select a file system for the logical partition that will be supported by the OS. After that mount the disc in the OS. To do this, go to the fdisk utility and type the command:

fdisk /dev/vdb

Screenshot №4 — Disk partitioning

By default, an MBR table is created for the disc, which can be overwritten and it is preferable to use GPT partitioning instead. To do this, specify the letter g, after that the table will be recreated and the disc will be repartitioned. To create a new volume in the current table, press n to define its parameters:

Screenshot №5 — Make logical part

Select the type for the partition and specify the first and the last sector to create one volume. After that we will see the result that a 10 GB volume has been created, it remains to write the changes and save them with the w command. In order for the disc to be processed by the OS it is necessary to initialise the file system for the volume. To do this, use the mkfs.ext4 utility:

mkfs.ext4 /dev/vdb1

Screenshot №6 — Make file system

To mount a disc temporarily, we can use the mount command, where we must first specify the disc itself and the mount point. But this solution will be valid until reboot, in order to mount a new disc automatically each time - go to the /etc/fstab folder and make changes:

nano /etc/fstab
/dev/vdb1 /home ext4 defaults,noexec 0 2

Screenshot №7 — Make mount record

To initialise a drive, you need to specify its UUID or direct path in our case /dev/vdb1. To view the current discs, type lsblk.

Screenshot №8 — Result of mount

Great, the drive is mounted and ready to use! Now, it will go through the mount step on every restart, however, what if you need to rename it? Or allow a limited number of people to use it?

How to manage the drive and use udev?

To manage block, symbolic devices use udev utility, which communicates with the kernel through uevent events. All settings for devices are made in configuration files for each device separately. In order to create a symbolic link or restrict access you need to know the unique parameters of the disc to be searched:

udevadm info -a -p /sys/block/vdb

Screenshot №9 — Attributes

In order to write a rule it is necessary to find a unique value for a disc, in this case it is ATTR{serial} with a certain number. To write a rule, let's go to the rules.d folder, where all previously written algorithms for working with each device are stored. Let's create a rule called:

nano 98-local.rules

Let's write the following syntax, which will first find the correct device and then apply the settings from udev rules:

SUBSYSTEM=="block", ID_SERIAL="BHYVE-2C94-9746-08F9", SYMLINK+="NewDisk"

Then save the file and restart the udev service:

systemctl daemon-restart && systemctl restart systemd-udevd

And check the relevant parameters through the command:

udevadm test /dev/vdb1

Screenshot №10 — Test rules

In the udev rule file, you can use different validation conditions such as:

All of the above attributes can be used to filter the device. Also for each of them logical elements are used, for example == or != in the condition. The action parameters are then specified: