To understand the same inode numbers for different objects in Ubuntu

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?

Upvotes: 3

Views: 1879

Answers (4)

Jonathan Leffler
Jonathan Leffler

Reputation: 755064

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.

Upvotes: 6

To verify the Hex and Dec numbers, use:

$ echo "obase=16; 2049"|bc 
801

Upvotes: 0

Jörg W Mittag
Jörg W Mittag

Reputation: 369614

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.)

Upvotes: 2

John Millikin
John Millikin

Reputation: 201016

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

Upvotes: 1

Related Questions