Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does linux kernel creates sysfs?

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?

like image 696
Rajesh Golani Avatar asked Oct 22 '13 21:10

Rajesh Golani


People also ask

What is the function of the sysfs virtual file?

The purpose of sysfs is to expose the readable and writable properties of what the kernel calls "kobjects" to userspace. The only purpose of kobjects is reference-counting: when the last reference to a kobject is deleted, the system will reclaim the resources associated with it.

Where is sysfs located?

Sysfs is always mounted on /sys . The directories in Sysfs contain the hierarchy of devices, as they are attached to the computer. Sysfs is the commonly used method to export system information from the kernel space to the user space for specific devices. The sysfs is tied to the device driver model of the kernel.

How sysfs is created?

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.

How does sysfs work in Linux?

The Linux kernel provides a virtual file system called sysfs. By providing virtual files, sysfs is able to export information about various kernel sub-systems, hardware devices and associated device drivers from the kernel's device model to user space.


1 Answers

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

like image 160
Federico Avatar answered Oct 01 '22 21:10

Federico