3

Why does /cdrom has the same inode -number than /sys/devices/platform/power in Ubuntu?

The following have the same inode number in my Ubuntu

./media/BACKUP_1/MISC
./cdrom
./sys/devices/platform/power

I get them by running the following at root

find . -inum 12 2> /dev/null

Reply to Leffler's answer

I run

stat cdrom

I get

  File: `cdrom' -> `media/cdrom'
  Size: 11              Blocks: 0          IO Block: 4096   symbolic link
Device: 801h/2049d      Inode: 12          Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2009-08-03 04:25:35.000000000 +0300
Modify: 2009-08-03 04:19:05.000000000 +0300
Change: 2009-08-03 04:19:05.000000000 +0300

What does this info tell you?

Reply to Leffler's edit

Often, you can dissect the device number into a major and minor device number which is what 'ls -l' prints for a device.

This command ls -l cdrom gives me this

lrwxrwxrwx 1 root root 11 2009-08-03 04:19 cdrom -> media/cdrom 

How can you see the major and minor device number from this?

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

4 Answers4

6

The devices are probably on different file systems - and it is the combination of the file system and the inode number that is unique.

If you use the stat() system call, the relevant fields are the st_ino and st_dev (and st_rdev identifies special devices).


The question was extended - asking what information can be gleaned from:

  File: `cdrom' -> `media/cdrom'
  Size: 11              Blocks: 0          IO Block: 4096   symbolic link
Device: 801h/2049d      Inode: 12          Links: 1

There are many things that can be gleaned from this. The key ones are that this symbolic link is on the file system with device number (st_rdev) of 0x0801 (or 2049), and the inode number is 12. Often, you can dissect the device number into a major and minor device number which is what 'ls -l' prints for a device. There's a decent chance (but I have not formally verified this) that the major device number is 8 and the minor device is 1 (based on the hex representation 0x0801).


The question was extended a second time:

This command ls -l cdrom gives me this

lrwxrwxrwx 1 root root 11 2009-08-03 04:19 cdrom -> media/cdrom

How can you see the major and minor device number from this?

The short answer is "you can't". The output from one of these might be appropriately informative:

ls -l media/cdrom
ls -lL cdrom

The device shown in the previous question (the output from the stat command) has, I suggested, major device 8 and minor device 1. You'd find that by running 'ls -l' on the device that is mounted as the file system for '.'. You might use 'df .' to find the name of the mounted device - there are probably other mechanisms that would work too.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

An inode uniquely identifies a file on a filesystem. In your example, you are looking at three different filesystems: /, /media/BACKUP_1 (presumably an external VFAT32 drive or stick) and /sys.

These are three different filesystems; it is perfectly normal for the same inode number to be used on different filesystems. If inode numbers had to be unique across all filesystems, that would be a pretty harsh requirement, for several reasons:

  • there could only be 2sizeof(inode_t) × BITS_PER_BYTE files in the entire world and
  • every computer on the entire planet would need to be connected to every other computer all the time so that they don't accidentally hand out the same number twice.

Just imagine this: you create a file on your external device. Then you detach it and attach it to a different computer, which also creates a file. How would computer A know which inode numbers are already used by computer B?

Also, in your case there is a different peculiarity: /sys is not a "real" filesystem, it is a virtual filesystem. It only exposes internal kernel datastructures as files and directories. And it doesn't even do this all the time, it only does this when you actually look at it – then, and only then do the files magically spring into existence. Therefore, its inode numbers are synthesized and don't really have any use at all – indeed, IIRC some virtual filesystems just set the inode number to 0 for every file, or at least they tried to until they realized that this broke all sorts of tools.

In addition to that, /media/BACKUP_1 is presumably a VFAT32 filesystem, which is a DOS filesystem. Inodes are a Unix concept, so VFAT32 doesn't even have inodes, and again they are synthesized.

Indeed, many modern Unix filesystems don't have inodes either, they store files in B+ trees or some other highly optimized data structure and address them implicitly through their position in the tree. I know that the Reiser4 filesystem had some trouble because in some cases they didn't synthesize inode numbers at all or synthesized very big inode numbers that again broke some tools. (Some stupid implementations of tools which needed to do things similar to find -inode simply built an array from 0 to the highest inode number they could find, which would then consume all available memory on the machine if they were presented with an extremely large inode number.)

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • Great answer! - For reference, one byte is 8 bits. – Léo Léopold Hertz 준영 Aug 17 '09 at 22:35
  • 1
    One Byte is the smallest addressable unit of memory. On some computers it is 8 Bits, on some it is 6, 7, 9 or 12. Some even have a variable Byte size that can be selected during boot, on some it can even be selected per instruction and some don't even have Bytes at all. (I know that there is some ISO specification out there which defines a Byte to be 8 Bits, but that is only applicable *within* that specification.) – Jörg W Mittag Aug 18 '09 at 12:30
  • There was a Cray for which the C compiler defined `sizeof(char) == sizeof(int) == sizeof(long)` and `CHAR_BITS == 32`. On that machine, a byte was 32-bits. – Jonathan Leffler Aug 18 '09 at 17:00
  • Interesting. In that case I would actually argue that the machine doesn't have bytes at all, because to me, a byte implies a unit smaller than a word. But that depends on exactly how you define "byte" and "word", which I won't be attempting to do :-) – Jörg W Mittag Aug 19 '09 at 00:08
1

/sys/ is a separate filesystem, named sysfs. Inode numbers are only unique within a particular filesystem, not within the global file tree.

John Millikin
  • 197,344
  • 39
  • 212
  • 226
0

To verify the Hex and Dec numbers, use:

$ echo "obase=16; 2049"|bc 
801
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697