- sysfs — _The_ filesystem for exporting kernel objectsВ¶
- What it is:В¶
- Using sysfsВ¶
- Directory CreationВ¶
- AttributesВ¶
- Subsystem-Specific CallbacksВ¶
- Reading/Writing Attribute DataВ¶
- Top Level Directory LayoutВ¶
- Current InterfacesВ¶
- Linux Kernel Tutorial
- Linux Kernel Tutorial — Table of Content
- 1. What is an Operating system?
- 2. What is Kernel
- 3. Operations of a Kernel
- 4. Types of kernels
- 5. Kernel space and Userspace?
- 6. What is Linux ABI
- 7. Major Subsystems of the Linux Kernel
- 8. Linux Kernel system (file system)
- If you want to enrich your career and become a professional in Linux, then visit Mindmajix — a global online training platform: » Linux Online Certification » This course will help you to achieve excellence in this domain.
- Linux Kernel Tutorial for Beginners
- What is an Operating system?
- Let’s have a look at the components which are required for the functioning of an operating system.
- What is Kernel?
- The kernel is responsible for:
- Linux operating system
- The Linux Kernel
- Operations of a Kernel
- Types of kernels
- 1. Monolithic kernel
- 2. MicroKernel
- 3. Hybrid kernel
- Kernel space and Userspace?
- 1. Kernel space
- 2. User Space
- What is Linux ABI?
- The Linux Loadable Kernel Module
- The benefits of LKMs (Linux Loadable Kernel Module)
- Linux kernel interfaces:
- Major Subsystems of the Linux Kernel
- System call interface:
- Process management:
- 1. Memory management:
- 2. Virtual file system:
- 3. Device Drivers:
- 4. Architecture-dependent code:
- 5. Upgrading the Kernel:
- Linux Kernel system (file system)
- The file system layout
- Manipulating files
- Managing files
- File permissions
- Moving files
- Copying files
- Removing or deleting files
- Conclusion
- List of Other Linux Blogs:
sysfs — _The_ filesystem for exporting kernel objectsВ¶
10 January 2003
What it is:В¶
sysfs is a ram-based filesystem initially based on ramfs. It provides a means to export kernel data structures, their attributes, and the linkages between them to userspace.
sysfs is tied inherently to the kobject infrastructure. Please read Everything you never wanted to know about kobjects, ksets, and ktypes for more information concerning the kobject interface.
Using sysfsВ¶
sysfs is always compiled in if CONFIG_SYSFS is defined. You can access it by doing:
Directory CreationВ¶
For every kobject that is registered with the system, a directory is created for it in sysfs. That directory is created as a subdirectory of the kobject’s parent, expressing internal object hierarchies to userspace. Top-level directories in sysfs represent the common ancestors of object hierarchies; i.e. the subsystems the objects belong to.
Sysfs internally stores a pointer to the kobject that implements a directory in the kernfs_node object associated with the directory. In the past this kobject pointer has been used by sysfs to do reference counting directly on the kobject whenever the file is opened or closed. With the current sysfs implementation the kobject reference count is only modified directly by the function sysfs_schedule_callback().
AttributesВ¶
Attributes can be exported for kobjects in the form of regular files in the filesystem. Sysfs forwards file I/O operations to methods defined for the attributes, providing a means to read and write kernel attributes.
Attributes should be ASCII text files, preferably with only one value per file. It is noted that it may not be efficient to contain only one value per file, so it is socially acceptable to express an array of values of the same type.
Mixing types, expressing multiple lines of data, and doing fancy formatting of data is heavily frowned upon. Doing these things may get you publicly humiliated and your code rewritten without notice.
An attribute definition is simply:
A bare attribute contains no means to read or write the value of the attribute. Subsystems are encouraged to define their own attribute structure and wrapper functions for adding and removing attributes for a specific object type.
For example, the driver model defines struct device_attribute like:
It also defines this helper for defining device attributes:
For example, declaring:
is equivalent to doing:
Note as stated in include/linux/kernel.h “OTHER_WRITABLE? Generally considered a bad idea.” so trying to set a sysfs file writable for everyone will fail reverting to RO mode for “Others”.
For the common cases sysfs.h provides convenience macros to make defining attributes easier as well as making code more concise and readable. The above case could be shortened to:
static struct device_attribute dev_attr_foo = __ATTR_RW(foo);
the list of helpers available to define your wrapper function is:
assumes default name_show and mode 0444
assumes a name_store only and is restricted to mode 0200 that is root write access only.
fore more restrictive RO access currently only use case is the EFI System Resource Table (see drivers/firmware/efi/esrt.c)
assumes default name_show, name_store and setting mode to 0644.
which sets the name to NULL and is used as end of list indicator (see: kernel/workqueue.c)
Subsystem-Specific CallbacksВ¶
When a subsystem defines a new attribute type, it must implement a set of sysfs operations for forwarding read and write calls to the show and store methods of the attribute owners:
[ Subsystems should have already defined a struct kobj_type as a descriptor for this type, which is where the sysfs_ops pointer is stored. See the kobject documentation for more information. ]
When a file is read or written, sysfs calls the appropriate method for the type. The method then translates the generic struct kobject and struct attribute pointers to the appropriate pointer types, and calls the associated methods.
Reading/Writing Attribute DataВ¶
To read or write attributes, show() or store() methods must be specified when declaring the attribute. The method types should be as simple as those defined for device attributes:
IOW, they should take only an object, an attribute, and a buffer as parameters.
sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the method. Sysfs will call the method exactly once for each read or write. This forces the following behavior on the method implementations:
On read(2), the show() method should fill the entire buffer. Recall that an attribute should only be exporting one value, or an array of similar values, so this shouldn’t be that expensive.
This allows userspace to do partial reads and forward seeks arbitrarily over the entire file at will. If userspace seeks back to zero or does a pread(2) with an offset of вЂ0’ the show() method will be called again, rearmed, to fill the buffer.
On write(2), sysfs expects the entire buffer to be passed during the first write. Sysfs then passes the entire buffer to the store() method. A terminating null is added after the data on stores. This makes functions like sysfs_streq() safe to use.
When writing sysfs files, userspace processes should first read the entire file, modify the values it wishes to change, then write the entire buffer back.
Attribute method implementations should operate on an identical buffer when reading and writing values.
Writing causes the show() method to be rearmed regardless of current file position.
The buffer will always be PAGE_SIZE bytes in length. On i386, this is 4096.
show() methods should return the number of bytes printed into the buffer.
show() should only use sysfs_emit() or sysfs_emit_at() when formatting the value to be returned to user space.
store() should return the number of bytes used from the buffer. If the entire buffer has been used, just return the count argument.
show() or store() can always return errors. If a bad value comes through, be sure to return an error.
The object passed to the methods will be pinned in memory via sysfs referencing counting its embedded object. However, the physical entity (e.g. device) the object represents may not be present. Be sure to have a way to check this, if necessary.
A very simple (and naive) implementation of a device attribute is:
(Note that the real implementation doesn’t allow userspace to set the name for a device.)
Top Level Directory LayoutВ¶
The sysfs directory arrangement exposes the relationship of kernel data structures.
The top level sysfs directory looks like:
devices/ contains a filesystem representation of the device tree. It maps directly to the internal kernel device tree, which is a hierarchy of struct device .
bus/ contains flat directory layout of the various bus types in the kernel. Each bus’s directory contains two subdirectories:
devices/ contains symlinks for each device discovered in the system that point to the device’s directory under root/.
drivers/ contains a directory for each device driver that is loaded for devices on that particular bus (this assumes that drivers do not span multiple bus types).
fs/ contains a directory for some filesystems. Currently each filesystem wanting to export attributes must create its own hierarchy below fs/ (see ./fuse.txt for an example).
dev/ contains two directories char/ and block/. Inside these two directories there are symlinks named : . These symlinks point to the sysfs directory for the given device. /sys/dev provides a quick way to lookup the sysfs interface for a device from the result of a stat(2) operation.
More information can driver-model specific features can be found in Documentation/driver-api/driver-model/.
TODO: Finish this section.
Current InterfacesВ¶
The following interface layers currently exist in sysfs:
Источник
Linux Kernel Tutorial
Related articles
Latest Published Articles
Angular JS community
Active Free Webinars
If you are looking to gain knowledge on the Linux kernel and the different aspects associated with it, then you have landed at the right place. In this tutorial, we are going to look into the details of various topics that are associated with the Linux kernel.
Linux Kernel Tutorial — Table of Content
1. What is an Operating system?
2. What is Kernel
3. Operations of a Kernel
4. Types of kernels
5. Kernel space and Userspace?
6. What is Linux ABI
7. Major Subsystems of the Linux Kernel
8. Linux Kernel system (file system)
If you want to enrich your career and become a professional in Linux, then visit Mindmajix — a global online training platform: » Linux Online Certification » This course will help you to achieve excellence in this domain.
Linux Kernel Tutorial for Beginners
Before going into the details of the kernel tutorial, let’s have a glance over the basics which would help us in acquiring a better understanding of the entire topic.
What is an Operating system?
An operating system is a software platform that creates an environment where a user can run different applications on a computing device. The operating system acts as a bridge between the software programs and the hardware components of a system.
It is utilized by different devices such as Mobiles, Tabs, desktops, web servers, video game consoles, etc. There are various Operating Systems available in the market, and Windows, Linux, Unix, and Mac OS X are some of the examples.
Let’s have a look at the components which are required for the functioning of an operating system.
- The Bootloader: It takes care of the boot process of your device.
- The Shell: A shell is a programming language that controls other files, processes, and controls all other programs as well.
- The kernel: It is the main component of the OS and manages memory, CPU, and other related components.
- Desktop Environment: Where a user interacts with.
- Graphical server: It is a subsystem of OS and used for showing graphics on your screen
- Applications: These are the set of programs that perform different user tasks such as word, excel, etc.
- Daemons: Provider of background services.
What is Kernel?
A kernel is the critical component of an operating system. It works as a bridge between the applications and data processing at the hardware level with the help of its interprocess communication and system calls.
Whenever an operating system is loaded into memory, firstly, the kernel is loaded and stays there till the operating system gets shut down. The kernel is responsible for taking care of low-level tasks such as task management, memory management, risk management, etc.
The kernel is responsible for:
- Process management for application execution.
- Memory and I/O (input/output) management.
- System call control (core act of Kernel).
- Device management with the help of device drivers.
Linux operating system
Linux is an open-source platform developed
in 1991 by Linus Torvalds. It is used in a wide variety of devices as an operating system, like in computers, servers, mobiles, mainframes, and other embedded devices. As it is open-source software, users can customize this operating system according to their needs.
It supports almost every major computer platform such as ARM, x86, SPARK, etc. The most common usage of Linux is for Server, but it is also used in Desktop computers, ebook readers, smartphones, etc.
The Linux Kernel
A Kernel is the core component of any Linux based operating system. It represents the core aspect of the Linux distributions for Desktop computers and servers. It has a monolithic architecture, and the operating system operates entirely in the kernel space.
The monolithic kernel not only encircles the Central Processing Unit, IPC, and memory, but also has system server calls, device drivers, and file system management. Linux kernel works as a layer between the software and hardware of a device.
Operations of a Kernel
The kernel is termed as the heart of any operating system as it controls all other programs in a system. When a device starts, the kernel goes through a process called the initialization function, such as checking memory. It takes care of the memory allocation part and creates an environment for running the applications without any disturbances.
Kernel works as a service provider, so the programs can request the kernel for accomplishing multiple tasks such as requesting the use of the disk, network card, or other pieces of hardware, and also, kernel sets interrupt for the CPU to enable multitasking.
It protects the computational environment by not letting the faulty programs enter into the operational functions of others. It stops the unauthorized programs at the entrance by not allowing the memory space and limits the CPU time that they consume.
Types of kernels
In general, we have three types of kernels, and they are,
- Monolithic kernel: It contains many device drivers that create a communication interface between the hardware and software of a device.
- Microkernel: It could only execute basic functionality.
- Hybrid kernel: it combines the aspects of Monolithic kernel and Microkernel.
1. Monolithic kernel
It is a widely used kernel by operating systems. In a Monolithic architecture, the kernel consists of various modules that can dynamically be loaded and unloaded. This kind of architecture would extend the capabilities of the OS and allows easy extensions to the kernel.
Maintenance of a kernel becomes easy with monolithic architecture because it allows a concerned module to load and unload when there is a need to fix a bug in a particular module.
So, it eliminates the tedious task of bringing down and recompiling the whole kernel for little changes. It is easier in the monolithic kernel to unload the module that is no more in usage.
2. MicroKernel
Microkernel has evolved as an alternative to the monolithic kernel to address the issue of the ever-growing size of kernel code which the monolithic kernel failed to do. This architecture allows some basic services like protocol stack, device driver management, file system, etc., to run in userspace. This could enhance the capability of OS with minimum code, improved security, and ensures stability.
It restricts the damages to the impacted areas by leaving the rest of the system to function correctly without any interruptions. In Microkernel architecture, all the basic OS services are available to programs via interprocess communication (IPC). Microkernel allows direct interaction between the device drivers and hardware.
3. Hybrid kernel
The hybrid kernel can decide what it wants to run in user mode, and in supervisor mode. In the hybrid kernel environment, usually, things like device drivers, file system I/O would run in user mode, whereas server calls and IPC are kept in supervisor mode. It gives the best experience of both worlds.
Kernel space and Userspace?
In the Linux operating system, the system memory is divided into two different regions: kernel space and userspace. Let’s look into each region of memory and know the functionalities of both.
1. Kernel space
Kernel space is found in an elevated state which provides full access to the hardware devices and protects the memory space. This memory space and user space together called Kernel-space. In a kernel space environment, core access to the system services and hardware are maintained and provided as a service to the rest of the system.
2. User Space
The userspace or userland is a code that runs outside the operating system kernel environment.
Userspace is defined as various applications or programs or libraries that an operating system uses to connect with the kernel. Because of the complicated process to access the memory, malicious functions can be restricted only to the user system.
What is Linux ABI?
It is nothing but kernel userspace ABI (application binary user interface). It exists between program modules. ABIs are used to access the codes that are compiled and ready for usage.
ABI is an interface between two binary program modules: one of these modules is an operating system facility or library, and the second one is a program run by a user.
The Linux Loadable Kernel Module
If you want to add code to the Linux kernel, the first thing you need to do is to add some source files to the kernel source tree. There may be situations where you are required to add code to the kernels while it is running, this process is called a loadable kernel module.
The benefits of LKMs (Linux Loadable Kernel Module)
- LKM saves time and avoids errors.
- It helps in finding the bugs quickly.
- LKMs save the memory because they are loaded into memory only when required.
- It offers faster maintenance and debugging time.
Linux kernel interfaces:
The Linux Kernel provides different interfaces to the user-space applications that execute different tasks and have different properties. It consists of two separate Application Programming Interfaces (APIs): one is kernel userspace, and the other is kernel internal.
Kernel user space is the Linux API userspace and allows the programs in the user space into system services and resources of the kernel.
Major Subsystems of the Linux Kernel
Below mentioned are some of the subsystems of the Linux kernel. Let’s discuss them one by one in detail.
System call interface:
A system call is a programmatic process in which a program requests a service from the kernel of an operating system. It includes various hardware services such as connecting with hardware devices and creating a communication interface among the integral parts of the Kernel. System call creates an efficient interface between an operating system and a process.
Process management:
The Kernel takes care of creating and destroying the different processes and monitors their connection to the outside world such as input and output. It handles the communication between different methods via signals, interprocess communication primitive, or pipes. In addition to all these, it also has a scheduler that controls the processes in sharing the CPU.
1. Memory management:
Memory is a vital component of an Operating system and the kernel takes care of it. Linux manages the available memory and hardware mechanisms for virtual and physical mappings.
Memory management isn’t just managing 4KB buffers, and it is much more than that. Linux also provides abstractions other than 4kb buffers, known as a slab allocator.
Slab allocator uses the 4kb buffer as its base but then allocates structures from inside by monitoring things like, which pages are full, empty, and partially used.
This allows the scheme to grow dynamically and in supporting the more significant needs of the system.
2. Virtual file system:
Virtual file system (VFS) is an important integral part of the kernel and facilitates common interface abstraction for the file system. The VFS creates a switching layer between the file system supported by the kernel and SCI (System Call Interface).
In addition to the above things, Linux supports various types of file systems that require different ways of organizing data to store in physical format. For instance, a disk can be formatted with the commonly used FAT file system, or Linux standard ext3 file system, or several others.
3. Device Drivers:
A vast part of the source code of the kernel is stored in the device drivers, and that makes a specific hardware device usable. The Linux provides a driver subdirectory that is further divided into various devices that are supported, such as I2C, Bluetooth, serial, etc.
4. Architecture-dependent code:
Even though much of the Linux runs on its independent architecture, some elements should be considered for the architecture efficiency and normal operation.
Linux has many subdirectories, and each architecture subdirectory has many numbers of other subsidiaries. And, these subdirectories focus on the specific tasks of the kernel such as memory management, boot, kernel, etc.
5. Upgrading the Kernel:
As we are aware of the concept called an update, we do have that option in the kernel to update it from the older version to a newer one.
The retention of old configurations is significant, and to achieve this, one has to back up the configuration file in the kernel source directory. If anything goes wrong while updating the kernel, follow the below steps.
- Download the latest source code from the kernel.org main page.
- Apply variations to the old version tree to make it a new one.
- Reconfigure the kernel on the basis of the older kernel configuration file that you had backed up.
- Develop the new kernel.
- Now, you can install the latest kernel in your system.
Linux Kernel system (file system)
The files and systems in the Linux kernel system are where most of the users find difficulties, majorly because it is hard to tell which files are in which directories if you don’t have the knowledge. For this reason, we shall try to look at the organization of the file systems.
We shall also learn how to create, delete, move, and rename directories. Additionally, we shall learn how to edit files and change permissions.
The file system layout
The UNIX file system can aptly fit into a one-line description; “Everything on a UNIX system that is not a process is a file”. This statement holds true for files that are a little bit more than just files. Therefore, a Linux system does not differentiate between a file and directory mainly because the directory, in essence, is a file containing names of other files, services, texts, images, and programs.
Additionally, a Linux system also treats input and output devices as files. The general understanding is that the files are a sort of in a tree structure on the main hard drive; this is for easy management and order. Most of the files on a Linux system are regular files, regardless of the data they hold, be it programs, executable files, or normal data.
While we have already said that everything in a Linux system is a file, there is a general understanding that there are some exceptions. For instance:
(a) Directories: A file list of other files
(b) Special files: These are the mechanisms used for input and output. Special files are in /dev.
(c) Links: This is a system to make a file, including a directory visible in various parts of the “system tree”.
(d) Domain (sockets): These are special types of files similar to the IP/TCP sockets. These files are protected by the file system access control and they provide inter-process networking.
Named pipes: These types of files are the bridge between processes. They are more or less the same as sockets and enhance communication between processes without the use of networks or sockets semantics.
Remember that I had indicated that most computer users generalize that the file system is more or less like a tree, here is a good example of a Linux file system tree.
It is important to note that depending on the UNIX system in use, the file system tree may change; some files and directories may change.
The file system tree starts at the slash or the trunk, which, if you look at our table is the (/) forward slash. This is what we call the root directory; it is the underlying directory for all files.
Directories are one level below the slash or root directory often have the slash in their proceeding names to indicate their position and to prevent confusion with other files or directories with similar names.
A question that plagues most Linux users is where programs and program files are stored when they are installed on the system. Let us examine this for a minute.
Linux uses two partitions: The data partition where the system data, including the root directories and all system resources required to start the system, are located, and the swap partition, which is an expansion of the physical memory on the computer.
All files (including programs) are stored in this root directory in accordance with the Linux tree file system we have already looked at.
Manipulating files
To show file names, properties, date of creation, permission, type, size, link files, and owners, the Is command is the easiest way.
Creating and deleting files and directories on your system is very important when you want to create new files or delete redundant directories to free up space. Because the graphical interface is much or less than MS-DOS, creating files is not that difficult. Deleting files, on the other hand, is moderately difficult.
There are some popular file managers for the GNU/Linux, with most of them being executable files that are accessible from the desktop manager, home directory icon, or the command line using the following commands.
Managing files
Nautilus: This is the default file manager in the Gnome GNU desktop. There are very useful resources on how to use this tool online.
Konqueror: This file manager is typical in KDE desktops.
MC: Code named Midnight Commander is fashioned from the Norton Commander.
For easier file management, the above applications are worth the time of reading through the documentation and the effort.
It is also important to note that there are many more file management applications, but these are the most popular and have a moderate difficulty level. Additionally, these tools optimize the UNIX commands in a specific manner.
To keep files and things in one place, you must allocate specific file default locations by creating directories and subdirectories for them. You can do this by using the mkdir command. For instance:
Additionally, you can create subdirectories easily in one-step by using the – p option. For instance:
No such file or directory
File permissions
In some instances, you will find that the file needs more or other permission not included in the file creation permission; this is called an access right.
Access rights are set using the same mkdir command. It is important to note that there are rules on how to name a directory. For instance, in one directory, you cannot have two files with the same name.
However, it is important to note that Linux, as well as UNIX, are case sensitive systems (you can have two file names with you and YOU in the same directory).
Additionally, there are no limits to the length of a filename, so naming files should be a breeze. You can also use special characters in the file names as long as those characters do not hold a special meaning to the shell.
Moving files
Moving unclassified files uses the mv command.
The same command is also in use when we are renaming files
The command can also come in handy if you want to rename files:
In the above example, we can see that only the file name changes, and all the other properties don’t change.
Copying files
The cp command is used to copy directories and files. There is also a very useful option of copying all underlying subdirectories and files (recursive copy) which uses the –R. Here is a look at the general syntax.
Removing or deleting files
The command rm comes into play when you want to remove single files, while the command rmdir plays its role in removing empty directories. It is important to note that some directories are undeletable (.dot and ..dot) because they are necessary for the proper ranking of a directory in the tree hierarchy.
Like UNIX, Linux does not have a garbage can (recycle bin) and once you remove a file, that is it, it is gone and you cannot get it back unless you have a backup. To protect against this, sometimes “mistake delete”, you can activate the interactive behavior of the cp, mv, and rm commands by using the -i option.
When the –i option is active, the system does not execute a command such as delete immediately; instead, it prompts for confirmation, which needs that stroke of a key or an additional click to execute the command fully.
Conclusion
The Linux Kernel plays an important role in resource allocation to different applications. The kernel acts as a centralized place to connect the hardware and software and runs the applications in a system. Linux kernel has got more popularity due to its open-source nature.
Users can customize this OS according to their requirements. Therefore, it’s been used by a wide variety of devices.
The modular characteristic of the Linux kernel allows a wide range of modifications without rebooting the system. The Flexibility of the kernel enables its users to perform their level best. Moreover, the monolithic nature of this kernel has greater computational power than the microkernel.
If you are interested to learn Linux and become a certified developer in it. Then check out our certified Linux training courses near your cities.
These courses are incorporated with live instructor-led training, industry use cases, and hands-on live projects. This training program will make you an expert in Microsoft Azure and help help you to achieve your dream job
List of Other Linux Blogs:
Linux File Permissions | Linux Advanced Functions And Commands |
Introduction to Linux Operating System | Linux Networking Commands with Examples |
What is Linux? | Top 10 Reasons Why You Should Learn Linux |
What is Linux Operating System? | Linux Commands for Beginners |
Vinod M is a Big data expert writer at Mindmajix and contributes in-depth articles on various Big Data Technologies. He also has experience in writing for Docker, Hadoop, Microservices, Commvault, and few BI tools. You can be in touch with him via LinkedIn and Twitter.
Источник