This article introduces the device er ing mechanism in the Linux kernel based on the specific code. Device er is a ing framework from a logical Device to a physical Device provided in the Linux 2.6 kernel. Under this mechanism, users can easily develop storage resource management policies based on their own needs. Currently, logical Volume Manager in Linux is popular, such as LVM2Linux Volume Manager 2 version) and EVMS (Enterprise Volume Management System) and dmraid (Device Mapper Raid Tool) are all implemented based on this mechanism. Understanding this mechanism is the basis for further analysis and understanding of the implementation and design of these volume managers. Through this article, we can further understand the design and implementation of Linux system block I/O.
Device Mapper is a general Device ing mechanism that supports logical volume Management in Linux2.6 kernel. It provides a highly modular kernel architecture for block Device drivers used for storage resource management.
In the kernel, it uses a modular target driver plug-in to filter IO requests or redirect requests, currently, the target driver plug-in includes soft raid, soft encryption, logical volume strip, multi-path, image, and snapshot, in the figure, linear, mirror, snapshot, and multipath represent these target drivers. Device er further embodies the principle of policy and mechanism separation in the Linux kernel design, and puts all policy-related work into the user space, the kernel mainly provides the mechanisms required to complete these policies. The Device mapper user space is mainly responsible for configuring specific policies and control logic, such as establishing mappings between logical devices and physical devices and how to establish these mappings, the specific filtering and redirection of IO requests are completed by the relevant code in the kernel. Therefore, the entire device mapper mechanism consists of two parts: the device mapper driver of the kernel space, the device mapper library of the user space, and the dmsetup tool provided by it. In the following section, we will introduce the kernel and user space.
Kernel
The kernel-related code of Device mapper has been integrated into the kernel source code as part of the Linux 2.6 kernel release version. The code is in the driver/md/directory of the kernel source code, the code file can be divided into two parts: files that implement the basic architecture in the device mapper kernel and target driver plug-in files that implement specific ing. The analysis results below are mainly obtained based on the above source code file.
User space
Device er is relatively simple in user space, mainly including the device mapper library and dmsetup tool. The Device mapper library encapsulates the necessary operations required to create and delete device mapper logical devices in ioctl and user space. dmsetup is a command line tool that allows you to directly create and delete device mapper devices. Because their functions and procedures are relatively simple, they are not described in detail in this article. The user space is mainly responsible for the following work:
1. Find the target device related to each mapped device;
2. Create a ing table based on the configuration information;
3. input the ing table built in the user space to the kernel to build the dm_table structure corresponding to the mapped device;
4. Save the current ing information for future reconstruction.
The following examples are used to describe the usage of dmsetup and the device ing mechanism of device mapper. The most important task in user space is to build and save the ing table. The following are some examples of ing tables:
1)
0 1024 linear/dev/sda 204
1024 512 linear/dev/sdb 766
1536 128 linear/dev/sdc 0
2) 0 2048 striped 2 64/dev/sda 1024/dev/sdb 0
3) 0 4711 mirror core 2 64 nosync 2/dev/sda 2048/dev/sdb 1024
In example 1, the logical device is 0 ~ 1023 sectors, 1024 ~ 1535 sectors and 1536 ~ 1663 the three address ranges are mapped to the/dev/sda device sector 204th,/dev/sdb device sector 766th, And/dev/sdc device sector 0th by line ing. start area.
In Example 2, the logical device starts from sector 0, the segment with a length of 2048 sectors is mapped to the region starting with sector 1,024th of the/dev/sda device and sector 0th of the/dev/sdb device in a strip manner. It also tells the kernel that the target driver of the strip type has two strip devices mapped to the logic device, and the strip size is 64 slices, this allows the driver to split IO requests across devices.
In Example 3, the logical device starts from sector 0, segments with a length of 4711 sectors are mapped to the first sector of the/dev/sda device and the region starting with sector 2048th of the/dev/sdb device as images.
After the ing table is determined, it is relatively easy to create and delete logical devices. You can use the following dmsetup command to complete the corresponding operations.
Dmsetup create device name ing table file/* create a logical device based on the specified ing table */
Dmsetup reload device name ing table file/* reads the ing file from the disk for the specified device and reconstructs the ing relationship */
Dmsetup remove device name/* Delete specified logical device */
After the user space issues the logic device creation command according to the ing table, the device mapper establishes the logical address-to-physical address ing relationship in the kernel based on the input parameters and ing relationships. Device 4 created based on the ing relationship in Example 1 of the ing table, in the lower part of the figure, the logical address-to-physical address ing relationships established in the kernel according to the ing table are abstracted.