Wednesday, April 25, 2007

Where my Linux device files went and how sysfs helped me find them

I shouldn't have been, but the other I was surprised to see (unlike back in the day) the /dev directory isn't completely populated with every device know to man. They are gone, replaced by something called udev which creates the files depending which hardware is available. So unlike in the old Trinux, I can't simply copy into the target filesystem. There are no hd[abcd] devices on my development box. Although there is a script (/usr/share/initramfs-tools/hooks/udev) that is used by the initial ramdisk do to this, but it looked like overkill, particularly once I start digging into /etc/udev/rules.d. Sure I could have got it working, but how long would it take? What other tools would it depend on? That may or may not be in Busybox. All I wanted to do was create basic ide and scsi devices and the /dev/cciss/ devices used by the Compaq RAID adapters in the DL380s we have at work.

What I needed was the major and minor numbers for all devices are available in the system. Once I figured that out I could use mknod to create them myself. Quick and dirty, like everything else Trinux. Of course the downside is /sbin/udevd is a daemon that is running and I assume helps. I will have to manually run the scripts that created the devices as new kernel modules are installed and new classes of devices. Of course, with ubuntutrinux I'm building as large as kernels as possible with almost everything in it as possible.

So how do I find out what devices are there and learn the major/minor numbers?

That is where sysfs comes in, allowing you to get detailed information about hardware devices. A funny side note. I wouldn't recommend browsing this hierarchy with the busybox ls because I was getting segfaults.

So let's look at the SCSI (actually SATA) devices present on my Dell server and where they show up with sysfs.


mdfranz@gx620:/sys$ ls -al /dev/sd*
brw-rw---- 1 root disk 8, 0 2007-04-25 16:20 /dev/sda
brw-rw---- 1 root disk 8, 1 2007-04-25 16:20 /dev/sda1
brw-rw---- 1 root disk 8, 2 2007-04-25 16:20 /dev/sda2
brw-rw---- 1 root disk 8, 3 2007-04-25 16:20 /dev/sda3
brw-rw---- 1 root disk 8, 16 2007-04-25 16:20 /dev/sdb
brw-rw---- 1 root disk 8, 17 2007-04-25 16:20 /dev/sdb1
brw-rw---- 1 root disk 8, 18 2007-04-25 16:20 /dev/sdb2
mdfranz@gx620:/sys$ find /sys -name "sd*"
/sys/module/sd_mod
/sys/block/sdb
/sys/block/sdb/sdb2
/sys/block/sdb/sdb1
/sys/block/sda
/sys/block/sda/sda3
/sys/block/sda/sda2
/sys/block/sda/sda1
/sys/bus/scsi/drivers/sd
mdfranz@gx620:/sys$ cd /sys/block/sdb/
mdfranz@gx620:/sys/block/sdb$ cat dev
8:16

No comments: