devices - udev

Devices

Each hardware device is represented with a special file, stored under /dev/. Depending on the devices the type of the special file can be either: “character mode” or “block mode”. In character mode it is possible to do read/write operations, and in block mode beside read/write, it is  also possible to seek within the available data. Each special file is associated with two numbers  that identify the device to the kernel in a unique manner. These are called “major” and “minor” numbers.

pi@raspberrypi:~ $ ls -l /dev/mmcblk0p1
brw-rw---- 1 root disk 179, 1 Feb  2 21:17 /dev/mmcblk0p1          <--it is a block device (b)

The permissions of a special file map to the permissions necessary to access the device itself. If a device file has root access, only, then only the root can operate that device. To creeate a special file, the command "mknod" can be used.

Since Debian Squeeze, the naming scheme for hard drives has been unified by the Linux kernel, and all hard drives are now represented by /dev/sd*. Each partition is represented by its number on the disk on which it resides: for instance, /dev/sda1 is the first partition on the first disk, and /dev/sdb3 is the third partition on the second disk.

=====================

udev

In the past the /dev/ directory  used to contain all possible special files (for all possible devices) which could be used on the computer. This approach restricted the number of devices that one could use (due to the hardcoded list of names), and it was impossible to know which special files were actually useful.

Nowadays devices shoud be handled as hot-swappable computer devices. For example, the OS should manage to use a digital camera, and on the same slot later a USB key, both of which appear to the computer as disk drives. To solve this kind of problem and to implement the hot-pluggable function,  "udev" was created.

Device drivers are part of the Linux kernel (which make up more than 50% of its source code), and device discovery, state changes, etc. are handled by the Linux kernel. But after loading the driver into memory, the only action the kernel does, is to send out an event message. These events are captured by the device manager, udevd, and it decides what shall happen next.

When udev is notified by the kernel of the appearance of a new device, it collects various information on the given device, especially those that uniquely identify it (MAC address for network card, serial number for some USB devices, etc.). Using all these information, udev then consults all of the rules contained in /etc/udev/rules.d/ and /lib/udev/rules.d/ and decides how to name the device and then udev can create the special file under /dev.

=====================

fdisk -l            checking disks and its partitions
lsusb               lists usb devices

=====================

How to use USB storage device:
1. Connect the USB storage device
2. sudo fdisk -l                                       <--lists connected drives, find USB device by size (e.g /dev/sda1)
3. sudo mkdir /media/externaldrive.                    <--create a mount point for accessing the external device
4. sudo chgrp -R users /media/externaldrive            <--give permission to access mount point
5. sudo chmod -R g+w /media/externaldrive              <--give write permissions for the mount point
6. sudo mount /dev/sdXN /media/externaldrive -o=rw     <--mount the USB device and it is ready for use

No comments:

Post a Comment