The proc and sysfs filesystems

proc and sysfs are two pseudo filesystems that give a window onto the inner workings of the kernel. They both represent kernel data as files in a hierarchy of directories: when you read one of the files, the contents you see do not come from disk storage, it has been formatted on-the-fly by a function in the kernel. Some files are also writable, meaning that a kernel function is called with the new data you have written and, if it is of the correct format and you have sufficient permissions, it will modify the value stored in the kernel's memory. In other works, proc and sysfs provide another way to interact with device drivers and other kernel code.

proc and sysfs should be mounted on the directories /proc and /sys:

mount -t proc proc /proc
mount -t sysfs sysfs /sys

Although they are very similar in concept, they perform different functions. proc has been part of Linux since the early days. Its original purpose was to expose information about processes to user space, hence the name. To this end, there is a directory for each process named /proc/<PID> which contains information about its state. The process list command, ps, reads these files to generate its output. In addition, there are files that give information about other parts of the kernel, for example, /proc/cpuinfo tells you about the CPU, /proc/interrupts has information about interrupts, and so on. Finally, in /proc/sys, there are files that display and control the state and behavior of kernel sub-systems, especially scheduling, memory management, and networking. The best reference for the files you will find in proc is man page proc(5).

In fact, over time, the number of files in proc and their layout has become rather chaotic. In Linux 2.6, sysfs was introduced to export a subset of the data in an ordered way.

In contrast, sysfs exports a very ordered hierarchy of files relating to devices and the way they are connected to each other.

Mounting filesystems

The mount command allows us to attach one filesystem to a directory within another, forming a hierarchy of filesystems. The one at the top, which was mounted by the kernel when it booted, is called the root filesystem. The format of the mount command is as follows:

mount [-t vfstype] [-o options] device directory

You need to specify the type of the filesystem, vfstype, the block device node it resides on, and the directory you want to mount it to. There are various options you can give after the -o, have a look at the manual for more information. As an example, if you want to mount an SD card containing an ext4 filesystem in the first partition onto directory /mnt, you would type the following:

mount -t ext4 /dev/mmcblk0p1 /mnt

Assuming the mount succeeds, you would be able to see the files stored on the SD card in the directory /mnt. In some cases, you can leave out the filesystem type and let the kernel probe the device to find out what is stored there.

Looking at the example of mounting the proc filesystem, there is something odd: there is no device node, /dev/proc, since it is a pseudo filesystem, not a real one. But the mount command requires a device as a parameter. Consequently we have to give a string where the device should go, but it does not matter much what that string is. These two commands achieve exactly the same result:

mount -t proc proc /proc
mount -t proc nodevice /proc

It is fairly common to use the filesystem type in the place of the device when mounting pseudo filesystems.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.199.184