2

I want to create a file under /sys/devices directory in Linux. What is the best way to do this?

vard
  • 4,057
  • 2
  • 26
  • 46
hussain
  • 51
  • 1
  • 6
  • Did you read [Linux/Documentation/filesystems/sysfs.txt](http://lxr.free-electrons.com/source/Documentation/filesystems/sysfs.txt)? – sawdust Jan 19 '16 at 06:29

1 Answers1

2

These answer and explanation came up after a quick google-search:

Why cant I create a directory in /sys - Link removed because of limitation

Wikipedia: Sysfs - Link removed because of limitation

If you absolutely have to modify/create anything there, you should first understand how /sys works. And why you want to change it.

EDIT: Petesh pointed out that you where indeed referring to drivers.

As I understand it, /sys/devices is simply a place for devices to dump information about themselves. You don't insert drivers here.

The drivers, or modules, are either implemented into the kernel before compiling it. Or you can add the module to /usr/lib/modules/uname -r/extramodules/, or overwrite it in /usr/lib/modules/uname -r/kernel/fs/btrfs/

You may also want to look at these:

Arch: Manual module handling

The Linux Kernel Module Programming Guide

Payne
  • 344
  • 3
  • 8
  • He's asking in the context of a device driver, so I don't believe your references will help him here. – Anya Shenanigans Jan 18 '16 at 14:33
  • Hi Payne,Thanks for you quick answer. From ldd book, i understood like we can create file or folder and grouping them in /sys using kobject APIs. We also have global kobjects like kernel_kobj, mm_kobj etc. But from some other reference i understood like for creating a file in /sys/devices directory we have to use device_register kind of APIs. But i am not getting the proper reference on how to use device APIs to create a file in /sys/devices. – hussain Jan 19 '16 at 05:35
  • As I have no experience with kobjects, so I won't pretend that I know how to use them. But from what I understand after a quick skimming of this article, the folders under a sysfs folder are generally initialized kobjects registered in sysfs by the kobject_add() function: https://lwn.net/Articles/263200/ I'm not sure if this applies to /sys/devises though. Again, this is a *virtual* directory. The files and folders here, are not 'real' files and folders on your drive. So if you want to manipulate this area of your system, I recommend reading that article in depth. – Payne Jan 20 '16 at 13:25