Device file
This article or section needs expansion.
In Unix-like operating systems, a device file or special file is an interface to a device driver that appears in a file system as if it were an ordinary file.
On Linux they are in the /dev directory, according to the Filesystem Hierarchy Standard.
On Arch Linux the device nodes are managed by udev.
Contents
Block devices
A block device is a special file that provides buffered access to a hardware device. For a detailed description and comparison of virtual file system devices, see Wikipedia:Device file#Block devices.
Block device names
The beginning of the device name specifies the kernel’s used driver subsystem to operate the block device.
Storage devices, like hard disks, SSDs and flash drives, that support the SCSI command (SCSI, SAS, UASP), ATA (PATA, SATA) or USB mass storage connection are handled by the kernel’s SCSI driver subsystem. They all share the same naming scheme.
The name of these devices starts with sd . It is then followed by a lower-case letter starting from a for the first discovered device ( sda ), b for the second discovered device ( sdb ), and so on.
- /dev/sda — device a , the first discovered device.
- /dev/sde — device e , the fifth discovered device.
The name of storage devices, like SSDs, that are attached via NVM Express (NVMe) starts with nvme . It is then followed by a number starting from 0 for the device controller, nvme0 for the first discovered NVMe controller, nvme1 for the second, and so on. Next is the letter «n» and a number starting from 1 expressing the device on a controller, i.e. nvme0n1 for first discovered device on first discovered controller, nvme0n2 for second discovered device on first discovered controller, and so on.
- /dev/nvme0n1 — device 1 on controller 0 , the first discovered device on the first discovered controller.
- /dev/nvme2n5 — device 5 on controller 2 , the fifth discovered device on the third discovered controller.
This article or section needs expansion.
SD cards, MMC cards and eMMC storage devices are handled by the kernel’s mmc driver and name of those devices start with mmcblk . It is then followed by a number starting from 0 for the device, i.e. mmcblk0 for first discovered device, mmcblk1 for second discovered device and so on.
- /dev/mmcblk0 — device 0 , the first discovered device.
- /dev/mmcblk4 — device 4 , the fifth discovered device.
SCSI optical disc drive
The name of optical disc drives (ODDs), that are attached using one of the interfaces supported by the SCSI driver subsystem, start with sr . The name is then followed by a number starting from 0 for the device, ie. sr0 for the first discovered device, sr1 for the second discovered device, and so on.
Udev also provides /dev/cdrom that is a symbolic link to /dev/sr0 . The name will always be cdrom regardless of the drive’s supported disc types or the inserted media.
- /dev/sr0 — optical disc drive 0 , the first discovered optical disc drive.
- /dev/sr4 — optical disc drive 4 , the fifth discovered optical disc drive.
- /dev/cdrom — a symbolic link to /dev/sr0 .
virtio-blk
The name of drives attached to a virtio block device (virtio-blk) interface start with vd . It is then followed by a lower-case letter starting from a for the first discovered device ( vda ), b for the second discovered device ( vdb ), and so on.
- /dev/vda — device a , the first discovered device.
- /dev/vde — device e , the fifth discovered device.
Partition
Partition device names are a combination of the drive’s device name and the partition number assigned to them in the partition table, i.e. /dev/drivepartition . For drives whose device name ends with a number, the drive name and partition number is separated with the letter «p», i.e. /dev/driveppartition .
- /dev/sda1 — partition 1 on /dev/sda .
- /dev/nvme2n5p3 — partition 3 on /dev/nvme2n5 .
- /dev/mmcblk3p4 — partition 4 on /dev/mmcblk3 .
- /dev/vda1 — partition 1 on /dev/vda .
- /dev/loop0p2 — partition 2 on /dev/loop0 .
Utilities
lsblk
The util-linux package provides the lsblk(8) utility which lists block devices, for example:
In the example above, only one device is available ( sda ), and that device has three partitions ( sda1 to sda3 ), each with a different file system.
wipefs
This article or section needs expansion.
wipefs can list or erase file system, RAID or partition-table signatures (magic strings) from the specified device to make the signatures invisible for libblkid(3) . It does not erase the file systems themselves nor any other data from the device.
See wipefs(8) for more information.
For example, to erase all signatures from the device /dev/sdb and create a signature backup
/wipefs-sdb-offset.bak file for each signature:
Pseudo-devices
Device nodes that do not have a physical device.
Источник
What do the device files in /dev actually do?
I was browsing around my filesystem and for the first time I took a second to analyze my /dev directory. I was surprised by the amount of devices files in it, and couldn’t make sense of all of them:
I know that the sd* ‘s are my disks and filesystems, but what are loopx , fd0uxxxx , sgx , vcsxxx and all the other single files. Most of them are device files according to ls .
I understand that fully answering this question would require a ton of information, so unless someone can provide a link with a great rundown of the different devices files in dev and what they are all used for, I propose we make a CW answer.
1 Answer 1
Some of these have man pages (in section 4; leave out the final digit(s) and in a few cases such as sda the final letter).
For a more definitive, but usually less easy to read answer, look in the kernel documentation. First determine whether the device is a block device or a character device, and its major and minor number. For example
so sda is a block device ( b ) with major:minor = 8:0. Now look it up in devices.txt : 8 block is SCSI disk devices, which are most disks nowadays (most IDE and SATA disks are seen through the SCSI interface too, though this depends on kernel compilation options). Block device 8:1 ( /dev/sda1 ) is the first partition of /dev/sda .
There may be a few oddball devices that aren’t documented. You can look on your system at e.g. /sys/dev/block/8:0 : this is a symbolic link to a directory under /sys that provides various pieces of information about the device. You can follow more links, and in particular (in this case) /sys/dev/block/8:0/device/driver lands you to a directory that corresponds with the driver. Another source of information is /proc/devices , which indicates what driver claims each major device number.
Most Linux systems use udev to populate /dev based on available drivers and hardware. You can browse the rules to create device files, typically in /lib/udev/rules.d and /etc/udev/rules.d (the exact locations are distribution-dependant).
A few of the files aren’t devices. lsof /dev/NAME (as root) will tell you to what process is using them and thus give you a hint at what they are for. For the directories, look at the files in them.
This answer is specific to Linux, but other unices follow the same principles. Check the man pages (in section 4 on most variants, but a few use section 7), or other system or kernel documentation. The relevant information is the block/character bit and the major:minor numbers.
Источник
How are «/dev» Linux files created?
There are special files in Linux that are not really files.
The most notable and clear examples of these are in the dev folder, «files» like:
- /dev/null — Ignores anything you write to the file
- /dev/random — Outputs random data instead of the contents of a file
- /dev/tcp — Sends any data you write to this file over the network
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 )?
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 :
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».
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.
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.
Источник
Linux do all devices have device files
There is a special nomenclature that linux uses to refer to hard drive partitions that must be understood in order to follow the discussion on the following pages.
In Linux, partitions are represented by device files. These are phoney files located in /dev . Here are a few entries:
By convention, IDE drives will be given device names /dev/hda to /dev/hdd . H ard D rive A ( /dev/hda ) is the first drive and H ard D rive C ( /dev/hdc ) is the third.
Table 2. IDE controller naming convention
drive name | drive controller | drive number |
/dev/hda | 1 | 1 |
/dev/hdb | 1 | 2 |
/dev/hdc | 2 | 1 |
/dev/hdd | 2 | 2 |
A typical PC has two IDE controllers, each of which can have two drives connected to it. For example, /dev/hda is the first drive (master) on the first IDE controller and /dev/hdd is the second (slave) drive on the second controller (the fourth IDE drive in the computer).
You can write to these devices directly (using cat or dd ). However, since these devices represent the entire disk, starting at the first block, you can mistakenly overwrite the master boot record and the partition table, which will render the drive unusable.
Table 3. partition names
drive name | drive controller | drive number | partition type | partition number |
/dev/hda1 | 1 | 1 | primary | 1 |
/dev/hda2 | 1 | 1 | primary | 2 |
/dev/hda3 | 1 | 1 | primary | 3 |
/dev/hda4 | 1 | 1 | swap | NA |
/dev/hdb1 | 1 | 2 | primary | 1 |
/dev/hdb2 | 1 | 2 | primary | 2 |
/dev/hdb3 | 1 | 2 | primary | 3 |
/dev/hdb4 | 1 | 2 | primary | 4 |
Once a drive has been partitioned, the partitions will represented as numbers on the end of the names. For example, the second partition on the second drive will be /dev/hdb2 . The partition type (primary) is listed in the table above for clarity, although the concept is not explained until Section 3.3 .
Table 4. SCSI Drives
drive name | drive controller | drive number | partition type | partition number |
/dev/sda1 | 1 | 6 | primary | 1 |
/dev/sda2 | 1 | 6 | primary | 2 |
/dev/sda3 | 1 | 6 | primary | 3 |
SCSI drives follow a similar pattern; They are represented by ‘sd’ instead of ‘hd’. The first partition of the second SCSI drive would therefore be /dev/sdb1 . In the table above, the drive number is arbitraily chosen to be 6 to introduce the idea that SCSI ID numbers do not map onto device names under linux.
Under (Sun) Solaris and (SGI) IRIX, the device name given to a SCSI drive has some relationship to where you plug it in. Under linux, there is only wailing and gnashing of teeth.
SCSI ID #2 SCSI ID #5 SCSI ID #7 SCSI ID #8 /dev/sda /dev/sdb /dev/sdc /dev/sdd
SCSI ID #2 SCSI ID #7 SCSI ID #8 /dev/sda /dev/sdb /dev/sdc
SCSI drives have ID numbers which go from 1 through 15. Lower SCSI ID numbers are assigned lower-order letters. For example, if you have two drives numbered 2 and 5, then #2 will be /dev/sda and #5 will be /dev/sdb . If you remove either, all the higher numbered drives will be renamed the next time you boot up.
If you have two SCSI controllers in your linux box, you will need to examine the output of /bin/dmesg in order to see what name each drive was assigned. If you remove one of two controllers, the remaining controller might have all its drives renamed. Grrr.
Table 5. Logical Partitions
drive name | drive controller | drive number | partition type | partition number |
/dev/hdb1 | 1 | 2 | primary | 1 |
/dev/hdb2 | 1 | 2 | extended | NA |
/dev/hda5 | 1 | 2 | logical | 2 |
/dev/hdb6 | 1 | 2 | logical | 3 |
The table above illustrates a mysterious jump in the name assignments. This is due to the use of logical partitions (see Section 3.4 , which always start with 5, for reasons explained later.
This is all you have to know to deal with linux disk devices. For the sake of completeness, see Kristian’s discussion of device numbers below.
The only important thing with a device file are its major and minor device numbers, which are shown instead of the file size: