问题
I have started looking at linux kernel code for my OS course. In that I'm interested in sys file system (sysfs). I'm interested in finding out when and how sysfs gets created? Which files in linux kernel code generate this file system?
I have setup linux kernel on my system and have started debugging through the code.
I have referred to this document to understand sysfs file system : [sysfs] : https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt
But this document explains only about directory structure, creation of directories and reading/writing attributes. I'm more interested in how kernel creates these directories during booting . I understood that following method is responsible for directory creation in sysfs.
int sysfs_create_file(struct kobject *kobj, struct attribute *attr);
This function accepts kboject structure, attributes and using these it creates directory in sysfs.
I understood that at the time of boot, kernel detects the memory and creates the directories under sys/devices/system/memory. I'm planning to change this directory structure as part of my homework. So, could you please point me to files and methods which are responsible for creation of this specific memory directories?
回答1:
You should not use that kind of functions directly. You should also avoid the use of kobject
(unless you are touching the kernel core).
Usually, sysfs attribute are associated to the device
structure. So, when you register a device the sysfs attribute are created.
Take a look at the device
structure in device.h line 689. At the end of the structure you will find the following field
const struct attribute_group **groups;
You have to create your own attribute, insert them in an attribute group and assigns your groups to your device before invoking device_register()
If you follow the device_register()
function, you will see what it does to create the associated sysfs attribute
回答2:
The code for sysfs resides in fs/sysfs/
. Kernel will create a sysfs in sysfs_init
, and mount it by function sysfs_mount
.
You may find the paper The sysfs Filesystem by Patrick Mochel useful, as it introduces how sysfs works and code organization of sysfs in the kernel.
来源:https://stackoverflow.com/questions/19529018/how-does-linux-kernel-creates-sysfs