1
$ stat  new
  File: ‘new’
  Size: 928         Blocks: 8          IO Block: 4096   regular file
Device: 804h/2052d  Inode: 28188755    Links: 1
Access: (0622/-rw--w--w-)  Uid: ( 1000/       t)   Gid: ( 1000/       t)
Access: 2015-12-19 06:33:07.842559147 -0500
Modify: 2013-06-11 12:54:44.944836000 -0400
Change: 2015-03-25 11:15:20.778708140 -0400
 Birth: -

the manpage of stat says Device is the "device number" in hex or decimal format.

Shadur says:

A Device id is the combination of major and minor number that identifies that particular block device. In your case, '804' identifies the fourth partition of the first SCSI-like drive located according to the BIOS. In traditional terms, this would be /dev/sda4.

It seems that Device is indeed the combination of major and minor numbers of the partition where the file new is in:

$ ls -l /dev/sda4
brw-rw---- 1 root disk 8, 4 Oct 12 15:44 /dev/sda4

If I am correct, the major number 8 identifies the driver of the partition, and the minor number 4 identifies the device which is the partition.

But how can I know Device: 804h identifies the fourth partition of the first scsi-like drive?

1

Well, hex 804 is actually two bytes, typically written as 0x0804. The first byte is 0x08 (the "major" number), the second is 0x04 (the "minor" number). Converting them to decimal, that's where 8, 4 comes from.

You can find out what the 8 means from /proc/devices, which gives block device 8 as sd, which is SCSI disk. It's the first one in there, which is how you know it's the first one. 8,0 is sda, 8,1 is sda1, 8,2 is sda2, etc.

If you look through the kernel documentation—Documentation/devices.txt—it turns out that the sd driver actually only originally allowed 15 partitions; 8,16 is /dev/sdb (and 8,17 is /dev/sdb1, 8, 32 is /dev/sdc, etc.). If you exceed that limit, additional numbers are allocated dynamically.

Actually, the /dev entry name is decided by udev, and is controlled by the udev configuration (though the kernel suggests a default, and almost everyone uses it.) The static allocation of device numbers is mainly a historical oddity—it's not really needed on systems running udev except for a few boot-critical devices.

  • Thanks.(1) Is there a bijective mapping between the devices files and the pairs of major and minor numbers, in the sense that we can infer either of a device file and a pair of major and minor numbers from the other. (2) What do you mean by " It's the first one in there, which is how you know it's the first one. "? where do you find "it's the first one", so you know it is the first one? – Tim Jan 21 '16 at 19:38
  • @Tim Every device file has a major and minor number. Typically there is only one device file with a particular pair of numbers. (The particular mapping though can vary from system to system and even between reboots). But you could, for example, run mknod /root/foo b 8 1 and then /root/foo would work just like /dev/sda1. You could also configure udev to make two devices files for one number (though typically symlinks are used instead). – derobert Jan 21 '16 at 19:43
  • (1) is it correct that a device (major and minor) number identify a device directly, and a device file represents a device indirectly via a device number? I get it from your /root/foo example. (2) for a fixed device, is it correct that its minor number can be changed when Linux reboots? Is its major number fixed? I heard it is, but I also heard that a device can be exposed as both a block device file and a character device file, so is the major number of a device not fixed? (3) (2) which I added in my last comment. – Tim Jan 21 '16 at 20:04
  • @Tim (1) yes, the kernel only cares about the numbers. So the numbers directly identify the device. (Note user-space software, though, generally only cares about the names.) (2) Linux uses the statically-assigned numbers first—the ones in devices.txt, and hardcoded in the kernel source code. If you exceed those (e.g., more than 15 partitions) then it dynamically picks more numbers. If the devices probe in a different order on different boots, they can get different numbers. Not so common with SATA (except multiple controllers), but some things (e.g., USB) have no inherent order. (cont.) – derobert Jan 21 '16 at 20:11
  • @Tim (3) sda, sdb, sdc were basically assigned in order. It's the lowest-numbered sd, that's why it's sda. [Though I guess if somehow they managed to assign <8 to another sd block, they'd not change the names.] That's what I meant by first in the file. The lines are in order (at least on my machine), sorted by the major number. – derobert Jan 21 '16 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.