简体   繁体   English

linux内核如何创建sysfs?

[英]How does linux kernel creates sysfs?

I have started looking at linux kernel code for my OS course. 我已经开始查看我的OS课程的linux内核代码了。 In that I'm interested in sys file system (sysfs). 因为我对sys文件系统(sysfs)感兴趣。 I'm interested in finding out when and how sysfs gets created? 我有兴趣了解sysfs何时以及如何创建? 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. 我在我的系统上安装了linux内核,并开始调试代码。

I have referred to this document to understand sysfs file system : [sysfs] : https://www.kernel.org/doc/Documentation/filesystems/sysfs.txt 我已经参考了这个文档来理解sysfs文件系统:[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. 我知道以下方法负责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. 此函数接受kboject结构,属性并使用它们在sysfs中创建目录。

I understood that at the time of boot, kernel detects the memory and creates the directories under sys/devices/system/memory. 我知道在启动时,内核会检测内存并在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? 那么,请您指出负责创建此特定内存目录的文件和方法吗?

You should not use that kind of functions directly. 您不应该直接使用这种功能。 You should also avoid the use of kobject (unless you are touching the kernel core). 您还应该避免使用kobject (除非您触摸内核核心)。

Usually, sysfs attribute are associated to the device structure. 通常,sysfs属性与device结构相关联。 So, when you register a device the sysfs attribute are created. 因此,当您注册设备时,将创建sysfs属性。

Take a look at the device structure in device.h line 689. At the end of the structure you will find the following field 请查看device.h第689行中的device结构。在结构的末尾,您将找到以下字段

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() 您必须创建自己的属性,将它们插入属性组并在调用device_register()之前将组分配给您的设备

If you follow the device_register() function, you will see what it does to create the associated sysfs attribute 如果您遵循device_register()函数,您将看到它创建关联的sysfs属性的作用

The code for sysfs resides in fs/sysfs/ . sysfs的代码驻留在fs/sysfs/ Kernel will create a sysfs in sysfs_init , and mount it by function sysfs_mount . 内核将在sysfs_init创建一个sysfs,并通过函数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. 您可能会发现文章Patrick Mochel 的sysfs文件系统非常有用,因为它介绍了sysfs如何工作以及内核中sysfs的代码组织

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM