Команда dev в linux

How are «/dev» Linux files created?

First of all, what is the name of these types of «files» that are really some sort of script or binary in disguise?

Second, how are they created? Are these files built into the system at a kernel level, or is there a way to create a «magic file» yourself (how about a /dev/rickroll )?

I had no clue how to tag this question, especially since I don’t know the name of what I’m looking for. Feel free to edit in any relevant tags.

BTW, this is a fundamental part of the design of unix and unix-like operating systems: (almost) everything is a file, or can be made to look like a file.

These are «device nodes». However the ones you mentioned — unlike those associated with disks, keyboard, mice, audio-cards, and other devices — are so-called «pseudo-devices», as they’re not «real» devices and only exists in the kernel. It’s possible to create new ones, by writing a suitable device-driver and add it to the kernel (eg. a pseudo-device for monitoring some activity on the computer). Before the /dev-directory existed on disk — these days it’s a virtual file-system (of type devfs) created by the kernel.

All files, even «real» files, are software artifacts. The software behind every device, file, socket, special file, or something yet to be invented provides a table of functions to handle open() , read() , close() , etc. After that, it’s up to the software

7 Answers 7

/dev/zero is an example of a «special file» — particularly, a «device node». Normally these get created by the distro installation process, but you can totally create them yourself if you want to.

If you ask ls about /dev/zero :

# ls -l /dev/zero crw-rw-rw- 1 root root 1, 5 Nov 5 09:34 /dev/zero 

The «c» at the start tells you that this is a «character device»; the other type is «block device» (printed by ls as «b»). Very roughly, random-access devices like harddisks tend to be block devices, while sequential things like tape drives or your sound card tend to be character devices.

The «1, 5» part is the «major device number» and the «minor device number».

Читайте также:  Change users name linux

With this information, we can use the mknod command to make our very own device node:

This creates a new file named foobar , in the current folder, which does exactly the same thing as /dev/zero . (You can of course set different permissions on it if you want.) All this «file» really contains is the three items above — device type, major number, minor number. You can use ls to look up the codes for other devices and recreate those too. When you get bored, just use rm to remove the device nodes you just created.

Basically the major number tells the Linux kernel which device driver to talk to, and the minor number tells the device driver which device you’re talking about. (E.g., you probably have one SATA controller, but maybe multiple harddisks plugged into it.)

If you want to invent new devices that do something new. well, you’ll need to edit the source code for the Linux kernel and compile your own custom kernel. So let’s not do that! 🙂 But you can add device files that duplicate the ones you’ve already got just fine. An automated system like udev is basically just watching for device events and calling mknod / rm for you automatically. Nothing more magic than that.

There are still other kinds of special files:

  • Linux considers a directory to be a special kind of file. (Usually you can’t directly open a directory, but if you could, you’d find it’s a normal file that contains data in a special format, and tells the kernel where to find all the files in that directory.)
  • A symlink is a special file. (But a hard link isn’t.) You can create symlinks using the ln -s command. (Look up the manpage for it.)
  • There’s also a thing called a «named pipe» or «FIFO» (first-in, first-out queue). You can create one with mkfifo . A FIFO is a magical file that can be opened by two programs at once — one reading, one writing. When this happens, it works like a normal shell pipe. But you can start each program separately.
Читайте также:  How to scan ports linux

A file that isn’t «special» in any way is called a «regular file». You will occasionally see mention of this in Unix documentation. That’s what it means; a file that isn’t a device node or a symlink or whatever. Just a normal, every day file with no magical properties.

Источник

Understanding /dev and its subdirs and files

Almost all the files under /dev are device files. Whereas reading and writing to a regular file stores data on a disk or other filesystem, accessing a device file communicates with a driver in the kernel, which generally in turn communicates with a piece of hardware (a hardware device, hence the name).

There are two types of device files: block devices (indicated by b as the first character in the output of ls -l ), and character devices (indicated by c ). The distinction between block and character devices is not completely universal. Block devices are things like disks, which behave like large, fixed-size files: if you write a byte at a certain offset, and later read from the device at that offset, you get that byte back. Character devices are just about anything else, where writing a byte has some immediate effect (e.g. it’s emitted on a serial line) and reading a byte also has some immediate effect (e.g. it’s read from the serial port).

The meaning of a device file is determined by its number, not by its name (the name matters to applications, but not to the kernel). The number is actually two numbers: the major number indicates which driver is responsible for this device, and the minor number allows a driver to drive several devices¹. These numbers appear in the ls -l listing, where you would normally find the file size. E.g. brw-rw—- 1 root disk 8, 0 Jul 12 15:54 /dev/sda → this device is major 8, minor 0.

Some device files under /dev don’t correspond to hardware devices. One that exists on every unix system is /dev/null ; writing to it has no effect, and reading from it never returns any data. It’s often convenient in shell scripts, when you want to ignore the output from a command ( >/dev/null ) or run a command with no input ( ad infinitum) /dev/urandom (which returns random bytes ad infinitum).

Читайте также:  Linux аналог bat файла

A few device files have a meaning that depends on the process that accesses it. For example, /dev/stdin designates the standard input of the current process; opening from has approximately the same effect as opening the original file that was opened as the process’s standard input. Somewhat similarly, /dev/tty designates the terminal to which the process is connected. Under Linux, nowadays, /dev/stdin and friends are not implemented as character devices, but instead as symbolic links to a more general mechanism that allows every file descriptor to be referenced (as opposed to only 0, 1 and 2 under the traditional method); for example /dev/stdin is a symbolic link to /proc/self/fd/0 . See How does /dev/fd relate to /proc/self/fd/?.

You’ll find a number of symbolic links under /dev . This can occur for historical reasons: a device file was moved from one name to another, but some applications still use the old name. For example, /dev/scd0 is a symbolic link to /dev/sr0 under Linux; both designate the first CD device. Another reason for symbolic links is organization: under Linux, you’ll find your hard disks and partitions in several places: /dev/sda and /dev/sda1 and friends (each disk designated by an arbitrary letter, and partitions according to the partition layout), /dev/disk/by-id/* (disks designated by a unique serial number), /dev/disk/by-label/* (partitions with a filesystem, designated by a human-chosen label); and more. Symbolic links are also used when a generic device name could be one of several; for example /dev/dvd might be a symbolic link to /dev/sr0 , or it might be a link to /dev/sr1 if you have two CD readers and the second one is to be the default DVD reader.

Finally, there are a few other files that you might find under /dev , for traditional reasons. You won’t find the same on every system. On most unices, /dev/log is a socket that programs use to emit log messages. /dev/MAKEDEV is a script that creates entries in /dev . On modern Linux systems, entries in /dev/ are created automatically by udev, obsoleting MAKEDEV .

¹ This is actually no longer true under Linux, but this detail only matters to device driver writers.

Источник

Оцените статью
Adblock
detector