问题描述
我已经开始在我的OS课程中查看linux内核代码.在那我对sys文件系统(sysfs)感兴趣.我有兴趣了解何时以及如何创建sysfs? linux内核代码中的哪些文件生成此文件系统?
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?
我在系统上设置了linux内核,并已开始通过代码进行调试.
I have setup linux kernel on my system and have started debugging through the code.
我已参考此文档来了解sysfs文件系统:[sysfs]: https://www.kernel.org/doc/Documentation/filesystems /sysfs.txt
I have referred to this document to understand sysfs file system :[sysfs] : https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt
但是该文档仅说明目录结构,目录创建和读取/写入属性.我对内核在引导过程中如何创建这些目录更感兴趣.我知道以下方法负责在sysfs中创建目录.
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);
此函数接受kboject结构,属性,并使用它们在sysfs中创建目录.
This function accepts kboject structure, attributes and using these it creates directory in sysfs.
我了解到,在引导时,内核会检测内存并在sys/devices/system/memory下创建目录.我计划在我的家庭作业中更改此目录结构.因此,能否请您指出负责创建此特定内存目录的文件和方法?
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?
推荐答案
您不应直接使用这种功能.您还应该避免使用kobject
(除非您接触内核内核).
You should not use that kind of functions directly. You should also avoid the use of kobject
(unless you are touching the kernel core).
通常,sysfs属性与device
结构相关联.因此,当您注册设备时,将创建sysfs属性.
Usually, sysfs attribute are associated to the device
structure. So, when you register a device the sysfs attribute are created.
在 device.h device结构/a>第689行.在该结构的末尾,您将找到以下字段
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;
在调用device_register()
如果遵循device_register()
函数,您将看到创建关联的sysfs属性的作用
If you follow the device_register()
function, you will see what it does to create the associated sysfs attribute
这篇关于Linux内核如何创建sysfs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!