A method for implementing write filtering of root file system of Shenwei platform
By redirecting the write operation of the root file system to the tmpfs file system on the Shenwei platform, and using overlayfs technology to merge and overlay mount, the aging problem caused by frequent writing of storage media in embedded systems and IoT devices is solved. , improves the reliability and availability of equipment and simplifies the maintenance process.
Patent Information
- Application Number
- CN202411162273.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-23
- Publication Date
- 2025-01-14
- Estimated Expiration
- 2044-08-23
AI Technical Summary
Embedded systems and IoT devices are susceptible to error changes, unexpected power outages, etc. in an unattended environment, and the frequent writes of storage media such as NAND Flash will accelerate aging, resulting in device failures and high and complex system maintenance costs.
On the Shenwei platform, the write operation of the root file system is redirected to a memory-based tmpfs file system, and mounted in a merged and overlayed manner through overlays technology to ensure that the write operation is automatically cleared after the system restarts, thereby protecting Underlying storage media.
Extends the service life of storage media, improves the reliability and availability of equipment, simplifies system maintenance, avoids system failures caused by accidental power outages, and reduces maintenance costs.
Smart Images

Figure CN118689840B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of Linux operating system, in particular to a file system part in Linux operating system, and more particularly to a method for implementing write screening of a root file system of a Shenwei platform. Background Art
[0002] Embedded systems, IoT devices, and public servers usually run in unattended, frequently accessed environments, where system stability is critical. Any erroneous changes to the system, improper operations, malicious attacks, and unexpected power outages on the device will cause device failures. In addition, embedded systems usually use NAND Flash such as MLC, TLC, and 3D NAND as storage media. These storage media have a limited number of writes. Frequent write operations will accelerate the aging of the storage media, which will eventually affect the service life of the device. At the same time, in application scenarios such as public terminals, IoT devices, and self-service terminals, there are also problems with high and complex system maintenance costs. Summary of the invention
[0003] The purpose of the present invention is to provide a method for implementing write filtering of the root file system of the Shenwei platform. The present invention is mainly aimed at application scenarios of the embedded system, Internet of Things equipment and other application scenarios of the Shenwei platform that need to protect the storage medium. All write operations on the root file system are redirected to a temporary memory-based tmpfs file system. The data of the tmpfs file system can be automatically cleared when the system is restarted, thereby protecting the underlying physical storage medium from being changed and improving the reliability and availability of the device.
[0004] In order to solve the above technical problems, the present invention provides a method for implementing write filtering of the root file system of the Shenwei platform, comprising the following steps:
[0005] Step 1: Determine whether the Linux kernel parameters contain the overlayroot field. If so, enable the root file system write filtering function and continue to execute the / sbin / init file. If not, disable the root file system write filtering function, and the / sbin / init file calls the / lib / systemd / systemd executable file to execute the subsequent Linux operating system startup process.
[0006] Step 2: Mount a temporary file system of tmpfs on / mnt, and create the directories lower, overlay, and merged on / mnt; mount a temporary file system of tmpfs on / mnt / overlay, name it root-rw, and create the directories upper and work on / mnt / overlay;
[0007] Step 3: Get the UUID and root file system type of the root file system from the / etc / fstab file, get the device file name of the root file system according to the UUID, and use the device file name and root file system type to mount the root file system on / mnt / lower in read-only mode. Then, mount / mnt / lower, / mnt / overlay / upper, and / mnt / overlay / work to / mnt / merged through overlayfs, and create the directories lower and overlay on / mnt / merged.
[0008] Step 4: Write the contents of the / etc / fstab file except the root file system to the fstab file of the merged and stacked mounted root file system, that is, the / mnt / merged / etc / fstab file;
[0009] Step 5: Switch the root file system to the combined overlay mounted root file system. Mount the original root file system to / mnt. Then transfer and mount / mnt / mnt / lower to / lower, / mnt / mnt / overlay to / overlay, / mnt / sys to / sys, / mnt / proc to / proc, / mnt / dev to / dev, and unmount / mnt. Finally, execute the / lib / systemd / systemd executable file to complete the subsequent Linux operating system startup process.
[0010] Preferably, the / sbin / init file is a script file that is fully customized and implemented on the Shenwei platform to implement the root file system write filtering function.
[0011] Preferably, after completing step 3, the overlayfs organizational structure includes:
[0012] The lower layer, / mnt / lower, is used as the lower layer of overlayfs and is used for subsequent read-only mounting of the root file system;
[0013] The upper layer, / mnt / overlay / upper, is the upper layer of overlayfs and is used to store data written to the root file system;
[0014] Work layer, / mnt / overlay / work is the work layer of overlayfs, which is used for temporary data storage of overlayfs merging and stacking operations;
[0015] The merge layer, / mnt / merged, is the merge layer of overlayfs, which is used to merge the lower layer and the upper layer into a stacked mounted root file system, so that the Linux operating system can finally access it after the write filtering function is enabled.
[0016] Preferably, in step 4, the / mnt / merged / etc / fstab file is used to mount file systems other than the root file system in the subsequent Linux startup process to ensure that the mount nodes other than the root file system are not protected by the root file system write filtering function and do not affect the preservation of their data.
[0017] Preferably, after completing step 5, the overlayfs organizational structure includes:
[0018] The lower layer, / lower, is the lower layer of overlayfs and is used to mount the root file system in read-only mode.
[0019] The upper layer, / overlay / upper, is the upper layer of overlayfs and is used to store data written to the root file system;
[0020] Work layer, / overlay / work is the work layer of overlayfs, which is used for temporary data storage of overlayfs merging and stacking operations;
[0021] The merge layer, / as the merge layer of overlayfs, is used for the root file system that the Linux operating system can finally access after the write filtering function is enabled.
[0022] Compared with the prior art, the present invention has the following beneficial effects:
[0023] The present invention uses overlayfs technology to stack and mount the read-only mounted root file system and the readable and writable tmpfs file system in a merged and superimposed manner on the Shenwei platform during the init phase of the Linux operating system startup process, and then switches the root file system to the stacked mounted root file system to continue the startup process of the Linux operating system. After the Linux operating system is started, the root file system remains read-only mounted, and the modification of the root file system is saved in the readable and writable tmpfs file system. The tmpfs is a temporary file system based on memory. When the system is restarted, the modification of the root file system is automatically cleared, and the system is restored to a predefined initial state. Other mounting nodes on the root file system do not affect the preservation of their data, and the write filtering of the root file system is realized. Compared with the traditional root file system, after the write filtering function is enabled, the data writing of the root file system is concentrated in the tmpfs file system based on memory, and will not be directly written to the root file system based on the storage medium, reducing the frequent writing of the storage medium, extending the service life of the storage medium in the device, and also avoiding the device from damaging the root file system due to accidental power failure, resulting in system failure, thereby improving the reliability of the device. When an error occurs in a device, the device can be quickly restored to a predefined initial state by clearing the data in the tmpfs file system or restarting the device, which greatly simplifies system maintenance and improves device availability. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 It is a flow chart of a method for implementing write screening of the root file system of the Shenwei platform provided by the present invention.
[0025] Figure 2 It is a schematic diagram of the overlayfs organizational structure after completing step three provided by the present invention.
[0026] Figure 3 It is a schematic diagram of the overlayfs organizational structure after completing step five provided by the present invention. DETAILED DESCRIPTION
[0027] The present invention is further described in detail below in conjunction with the accompanying drawings and specific embodiments. The advantages and features of the present invention will become more apparent from the following description. It should be noted that the accompanying drawings are in very simplified form and in non-precise proportions, and are only used to conveniently and clearly assist in explaining the purpose of the embodiments of the present invention.
[0028] like Figure 1As shown, / sbin / init is the main executable file for system initialization in the Linux system. It is the earliest running process during the Linux system startup process and is responsible for starting and managing the services and processes of the entire system. The present invention implements the root file system write filtering function by customizing the / sbin / init file on the Shenwei platform. At the same time, the present invention implements the root file system write filtering function switch. During the Linux system startup process, if the Linux kernel parameters contain the overlayroot field, the root file system write filtering function is enabled. If the overlayroot field is not included, the root file system write filtering function is disabled. The / sbin / init file is a fully customized script file. Please refer to the process. Figure 1 The steps included in the script file are as follows:
[0029] Step 1: Determine whether the Linux kernel parameters contain the overlayroot field. If so, the root file system write filter function is enabled and / sbin / init is executed. If not, the root file system write filter function is disabled, and / sbin / init calls / lib / systemd / systemd to execute the subsequent Linux operating system startup process. The command to determine whether the Linux kernel parameters contain the overlayroot field is as follows:
[0030] enable_overlayroot="no"
[0031] for x in $(cat / proc / cmdline); do
[0032] if [ "${x}" = "overlayroot" ]; then enable_overlayroot="yes"; fi
[0033] done
[0034] The function of the above enable_overlayroot="no" is to set the root file system write filter flag enable_overlayroot to the string "no".
[0035] for x in $(cat / proc / cmdline); The function of do is: use the cat command to obtain the Linux kernel parameters from the / proc / cmdline file, split each parameter into a separate string, assign it to the variable x (expression x in$(cat / proc / cmdline)), start a for loop to traverse the Linux kernel parameters, and do indicates the start of the for loop.
[0036] if [ "${x}" = "overlayroot" ]; then enable_overlayroot="yes"; The function of fi is: start an if conditional judgment. If the value of the variable "${x}" is equal to the string "overlayroot" (expression [ "${x}" = "overlayroot" ]), then execute the statement after then, write the root file system filter flag enable_overlayroot to the string "yes", and fi indicates the end of the if judgment.
[0037] The function of done is to end the for loop.
[0038] Step 2: Mount a temporary file system of tmpfs on / mnt, and create the directories lower, overlay, and merged on / mnt. Mount a temporary file system of tmpfs on / mnt / overlay, named root-rw, and create the directories upper and work on / mnt / overlay. Among them, / mnt / lower is the lower layer of overlayfs, used for subsequent read-only mounting of the root file system, / mnt / overlay / upper is the upper layer of overlayfs, used to save write data to the root file system, / mnt / overlay / work is the work layer of overlayfs, used for temporary data storage of overlayfs merge operations, and / mnt / merged is the root file system that overlayfs mounts by merging the lower layer and the upper layer. It is the root file system that the Linux operating system finally accesses after the write filtering function is enabled. The command is as follows:
[0039] mount -t tmpfs none / mnt
[0040] mkdir -p / mnt / lower / mnt / overlay / mnt / merged
[0041] mount -t tmpfs root-rw / mnt / overlay
[0042] mkdir -p / mnt / overlay / upper / mnt / overlay / work
[0043] The function of the mount -t tmpfs none / mnt command is to use the mount command to mount a temporary file system of the tmpfs file system type on / mnt. -t specifies that the file system type to be mounted is tmpfs, and none indicates that there is no specific device name.
[0044] The function of mkdir -p / mnt / lower / mnt / overlay / mnt / merged is to use the mkdir command to create the directories lower, overlay, and merged on / mnt. -p means that multi-level directories can be created.
[0045] The function of mount -t tmpfs root-rw / mnt / overlay is to use the mount command to mount a temporary file system of the tmpfs file system type on / mnt / overlay. -t specifies that the file system type to be mounted is tmpfs, and the device name of / mnt / overlay is root-rw.
[0046] The function of mkdir -p / mnt / overlay / upper / mnt / overlay / work is to use the mkdir command to create the directories upper and work on / mnt / overlay. -p means that multi-level directories can be created.
[0047] Step 3: Get the UUID and root file system type of the root file system from the / etc / fstab file, and get the device file name of the root file system based on the UUID. Use the device file name and root file system type to mount the root file system in read-only mode on / mnt / lower, then merge and overlay / mnt / lower, / mnt / overlay / upper, and / mnt / overlay / work and mount them on / mnt / merged, and create the directories lower and overlay on / mnt / merged. The command to merge and overlay / mnt / lower, / mnt / overlay / upper, and / mnt / overlay / work and mount them on / mnt / merged is as follows:
[0048] mount -t overlay -o lowerdir= / mnt / lower,upperdir= / mnt / overlay / upper,workdir= / mnt / overlay / work overlayfs-root / mnt / merged
[0049] The above command functions as follows: Use the mount command to merge and overlay / mnt / lower, / mnt / overlay / upper, and / mnt / overlay / work and mount them to / mnt / merged. -t specifies the file system type to be mounted as overlay. -o specifies lowerdir as / mnt / lower, upperdir as / mnt / overlay / upper, and workdir as / mnt / overlay / work. The device name of / mnt / merged is overlayfs-root.
[0050] like Figure 2 As shown, after completing step three, the overlayfs organizational structure includes: a lower layer, / mnt / lower as the lower layer of overlayfs, used for subsequent read-only mounting of the root file system; an upper layer, / mnt / overlay / upper as the upper layer of overlayfs, used to save write data to the root file system; a work layer, / mnt / overlay / work as the work layer of overlayfs, used for temporary data storage of overlayfs merging and stacking operations; a merge layer, / mnt / merged as the merge layer of overlayfs, used to merge the lower layer and the upper layer to stack and mount the root file system, so that the Linux operating system can perform final access after the write filtering function is enabled.
[0051] Step 4: Write the contents of the / etc / fstab file except the root file system to the fstab file in the merged stacked mounted root file system, that is, the / mnt / merged / etc / fstab file, which is used to mount file systems other than the root file system in the subsequent Linux startup process. Mount nodes other than the root file system will not be protected by the root file system write filter and will not affect the preservation of their data.
[0052] The command is as follows:
[0053] grep -v -x -E '^(#.*)|([[:space:]]*)$' / mnt / lower / etc / fstab | awk$2 != " / " {print}' > / mnt / merged / etc / fstab
[0054] The functions of the above commands are: use the text search grep command to enable extended regular expressions (parameter -E) to reversely match (parameter -v) comment lines and blank lines starting with # in the file / mnt / lower / etc / fstab in the form of a whole line (parameter -x) (expression '^(#.*)|([[:space:]]*)$'), pass the output of the grep command to the awk command through the pipe symbol |, print all lines whose contents in the second column are not root file system nodes (expression '$2 != " / " {print}'), and output to the / mnt / merged / etc / fstab file through the redirection symbol >.
[0055] Step 5: Switch the root file system to the combined stacked root file system. Mount the original root file system to / mnt, then transfer and mount / mnt / mnt / lower to / lower, transfer and mount / mnt / mnt / overlay to / overlay, transfer and mount / mnt / sys to / sys, transfer and mount / mnt / proc to / proc, transfer and mount / mnt / dev to / dev, and unmount / mnt. Finally, execute / lib / systemd / systemd to complete the subsequent Linux operating system startup process.
[0056] And because the above / sys, / proc and / dev are virtual file systems in the Linux system, they provide access to devices, system information and processes. By transfer-mounting / mnt / sys to / sys, transfer-mounting / mnt / proc to / proc, and transfer-mounting / mnt / dev to / dev, the Linux operating system can access the / sys, / proc and / dev directories after enabling the write filtering function.
[0057] The commands for switching the root file system to the combined stacked mount root file system, transferring and mounting / mnt / mnt / lower to / lower, transferring and mounting / mnt / mnt / overlay to / overlay, transferring and mounting / mnt / sys to / sys, transferring and mounting / mnt / proc to / proc, and transferring and mounting / mnt / dev to / dev are as follows:
[0058] cd / mnt / merged
[0059] pivot_root . mnt
[0060] mount --move / mnt / mnt / lower / lower
[0061] mount --move / mnt / mnt / overlay / overlay
[0062] mount --move / mnt / sys / sys
[0063] mount --move / mnt / proc / proc
[0064] mount --move / mnt / dev / dev
[0065] The function of the above cd / mnt / merged is to use the cd command to switch the current directory to / mnt / merged.
[0066] The function of pivot_root . mnt is: use the pivot_root command to switch the root file system to the current directory (expression .), and the original root file system will be moved to the mnt directory.
[0067] mount --move / mnt / mnt / lower The function of / lower is: use the mount command with the option --move to transfer the mount from / mnt / mnt / lower to / lower.
[0068] mount --move / mnt / mnt / overlay The function of / overlay is: use the mount command with the option --move to transfer the mount from / mnt / mnt / overlay to / overlay.
[0069] mount --move / mnt / sys / sys's function is: use the mount command with option --move to transfer the mount from / mnt / sys to / sys.
[0070] mount --move / mnt / proc The function of / proc is: use the mount command with the option --move to transfer the mount from / mnt / proc to / proc.
[0071] mount --move / mnt / dev The function of / dev is: use the mount command with the option --move to transfer the mount from / mnt / dev to / dev.
[0072] like Figure 3 As shown, after completing step five, the overlayfs organizational structure includes: a lower layer, / lower as the lower layer of overlayfs, used for read-only mounting the root file system; an upper layer, / overlay / upper as the upper layer of overlayfs, used to save write data to the root file system; a work layer, / overlay / work as the work layer of overlayfs, used for temporary data storage of overlayfs merge stacking operations; a merge layer, / as the merge layer of overlayfs, used for the root file system that the Linux operating system can finally access after the write filtering function is enabled.
[0073] In summary, the present invention provides a mechanism through root file system write filtering to prevent any erroneous changes or improper operation modifications to the system from affecting the underlying file system, thereby ensuring stable operation of the system; through root file system write filtering, the system is allowed to quickly restore to a predefined initial state by clearing data in the tmpfs file system or restarting the device, thereby greatly simplifying system maintenance work and reducing maintenance costs; through root file system write filtering, the write operation is redirected to a temporary memory-based tmpfs file system, thereby reducing direct writing to the physical storage medium, thereby extending the service life of the device.
[0074] The above description is only a description of the preferred embodiments of the present invention, and is not intended to limit the scope of the present invention. Any changes or modifications made by a person skilled in the art in the field of the present invention based on the above disclosure shall fall within the scope of protection of the claims.
Claims
1. A method for implementing write screening of the root file system of the Shenwei platform, characterized in that: The steps include: Step 1: Determine whether the Linux kernel parameters contain the overlayroot field. If so, enable the root file system write filtering function and continue to execute the / sbin / init file. If not included, the root file system write filtering function is disabled, and the / sbin / init file calls the / lib / systemd / systemd executable file to execute the subsequent Linux operating system startup process; Step 2: Mount a temporary file system of tmpfs on / mnt, and create the directories lower, overlay, and merged on / mnt; mount a temporary file system of tmpfs on / mnt / overlay, name it root-rw, and create the directories upper and work on / mnt / overlay; Step 3: Get the UUID and root file system type of the root file system from the / etc / fstab file, get the device file name of the root file system according to the UUID, and use the device file name and root file system type to mount the root file system on / mnt / lower in read-only mode. Then, mount / mnt / lower, / mnt / overlay / upper, and / mnt / overlay / work to / mnt / merged through overlayfs, and create the directories lower and overlay on / mnt / merged. Step 4: Write the contents of the / etc / fstab file except the root file system to the fstab file of the merged and stacked mounted root file system, that is, the / mnt / merged / etc / fstab file; Step 5: Switch the root file system to the combined overlay mounted root file system. Mount the original root file system to / mnt. Then transfer and mount / mnt / mnt / lower to / lower, / mnt / mnt / overlay to / overlay, / mnt / sys to / sys, / mnt / proc to / proc, / mnt / dev to / dev, and unmount / mnt. Finally, execute the / lib / systemd / systemd executable file to complete the subsequent Linux operating system startup process. The / sbin / init file is a script file that is fully customized and implemented on the Shenwei platform to implement the root file system write filtering function; In step 4, the / mnt / merged / etc / fstab file is used to mount file systems other than the root file system in the subsequent Linux startup process to ensure that the mount nodes other than the root file system are not protected by the root file system write filtering function.
2. A method for implementing write filtering of the root file system of the Shenwei platform as claimed in claim 1, characterized in that: After completing step 3, the overlayfs organizational structure includes: The lower layer, / mnt / lower, is used as the lower layer of overlayfs and is used for subsequent read-only mounting of the root file system; The upper layer, / mnt / overlay / upper, is the upper layer of overlayfs and is used to store data written to the root file system; Work layer, / mnt / overlay / work is the work layer of overlayfs, which is used for temporary data storage of overlayfs merging and stacking operations; The merge layer, / mnt / merged, is the merge layer of overlayfs, which is used to merge the lower layer and the upper layer into a stacked mounted root file system, so that the Linux operating system can finally access it after the write filtering function is enabled.
3. A method for implementing write filtering of the root file system of the Shenwei platform as claimed in claim 1, characterized in that: After completing step 5, the overlayfs organizational structure includes: The lower layer, / lower, is the lower layer of overlayfs and is used to mount the root file system in read-only mode. The upper layer, / overlay / upper, is the upper layer of overlayfs and is used to store data written to the root file system; Work layer, / overlay / work is the work layer of overlayfs, which is used for temporary data storage of overlayfs merging and stacking operations; The merge layer, / as the merge layer of overlayfs, is used for the root file system that the Linux operating system can finally access after the write filtering function is enabled.
Citation Information
Patent Citations
Power failure protection method, computing equipment and storage medium
CN113568787A
Read-write mount starting method and device, storage medium and electronic equipment
CN114756296A