Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the block device in the Linux operating system

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of what the block device in the Linux operating system is, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on the block device in the Linux operating system. Let's take a look at it.

Under the Windows operating system, the disk device seems to be a real device, and we can manage the disk device through the graphical interface. For example, figure 1 is the disk management interface under Windows, through which you can clearly see the disk device and format it.

Figure 1 Windows disk device

The disk device of the Linux operating system is not intuitive. Under the concept of "everything is a file" in the LInux system, the disk device is actually a file, just a special file. Figure 2 shows the file path of some disks and partitions, where the yellow font is the path of the disk (similar to the file path), and the b in the previous red box indicates that the file is a disk device file, not a normal file.

Figure 2 disk devices under the Linux system

Disk device files are also located under VFS (virtual file system), similar to file systems such as Ext4 (see figure 3). The user level can use the interface to access ordinary files (API) to access the disk. The following code is a program implemented in Python to write strings to disk. The code is simple: open the path where the disk is located (path), and then call the write function to write the data.

The essence of disk in Linux system

From the above description, we know that for the Linux operating system, a disk is a file. The disk itself is a linear storage space (which can be understood as a large array), which is very similar to a file. Given the above similarities, there is nothing wrong with Linux abstracting the disk device into a file.

In essence, disk devices in the Linux operating system are managed based on a pseudo file system called bdev, which is a pseudo file system in memory (a file system in memory, no persistent data) in the same location as a file system such as Ext4. As shown in figure 3, the location of the bdev file system is the red area in the figure.

Figure 3 bdev file system location

Understand the management of block devices, combined with our previous introduction to the file system, so that it is easy to understand the following content. In the introduction to the file system-related articles, we know that the key to the data processing of different file systems is the set of functions they provide, which is determined when the file is opened. The same is true of disk devices. When we open a disk device, the operating system initializes the set of functions in inode according to the characteristics of the disk device. The subsequent read and write operations on the disk device can be completed through this set of functions. As shown in the following code, the block device, along with the character device and pipe, is processed as a special file and initializes the corresponding set of functions.

After the initialization of the function set, when the user calls the interface of the VFS layer, the VFS layer can find the specific processing function and then complete the user's operation. The function set here is no different from that of the local file system, except that the ordinary file system needs to manage directories and files, while the bdev pseudo-file system treats the disk as a large file, which is simpler.

Disk cache

Since the disk pseudo-file system bdev is itself a file system, it is naturally possible to have a cache. This cache is the cache system used to improve disk performance. The disk cache system is similar to the file system cache system, which is also implemented through page caching. Of course, the cache of the Linux disk can be turned off, and another set of functions will be called.

This may seem abstract, so let's take a look at the implementation of disk caching with a concrete example. The following is the set of functions of the disk pseudo-file system, which we introduce by taking writing data as an example.

Fig. 4 disk function set

The function that writes the data is blkdev_write_iter, which calls the generic_perform_write function. If you have read this file about the file system, it is clear that the latter is the function of writing data to the page cache in VFS. In other words, the logic of the block device pseudo file system is exactly the same as that of the local file system.

This is the end of the article on "what is the block device in the Linux operating system?" Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the block device in the Linux operating system". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report