CN115878377A - Power failure protection method and device for embedded Linux system and computer equipment - Google Patents
Power failure protection method and device for embedded Linux system and computer equipment Download PDFInfo
- Publication number
- CN115878377A CN115878377A CN202211459228.8A CN202211459228A CN115878377A CN 115878377 A CN115878377 A CN 115878377A CN 202211459228 A CN202211459228 A CN 202211459228A CN 115878377 A CN115878377 A CN 115878377A
- Authority
- CN
- China
- Prior art keywords
- partition
- application
- data
- configuration
- file system
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 32
- 238000005192 partition Methods 0.000 claims abstract description 271
- 230000008439 repair process Effects 0.000 claims description 22
- 230000005856 abnormality Effects 0.000 claims description 11
- 238000004590 computer program Methods 0.000 claims description 5
- 230000007246 mechanism Effects 0.000 claims description 5
- 238000012795 verification Methods 0.000 abstract description 11
- 230000008569 process Effects 0.000 description 7
- 230000002159 abnormal effect Effects 0.000 description 4
- 238000013524 data verification Methods 0.000 description 4
- 230000006870 function Effects 0.000 description 4
- 238000007726 management method Methods 0.000 description 4
- 238000001514 detection method Methods 0.000 description 3
- 230000004048 modification Effects 0.000 description 3
- 238000012986 modification Methods 0.000 description 3
- 239000003990 capacitor Substances 0.000 description 2
- 230000004075 alteration Effects 0.000 description 1
- 238000013144 data compression Methods 0.000 description 1
- 238000004146 energy storage Methods 0.000 description 1
- 239000003292 glue Substances 0.000 description 1
- 230000006872 improvement Effects 0.000 description 1
- 238000007689 inspection Methods 0.000 description 1
- 238000006467 substitution reaction Methods 0.000 description 1
- 230000001360 synchronised effect Effects 0.000 description 1
Images
Classifications
-
- Y—GENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
- Y02—TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
- Y02D—CLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
- Y02D10/00—Energy efficient computing, e.g. low power processors, power management or thermal management
Landscapes
- Techniques For Improving Reliability Of Storages (AREA)
Abstract
本申请提供了一种嵌入式Linux系统掉电保护方法、装置与计算机设备,该方法包括以下步骤:将Linux系统的非易失性存储介质划分为系统分区、配置分区、第一应用分区、第二应用分区、数据分区;将系统分区使用overlayfs联合挂载到根路径,系统分区为只读层,内存文件系统为读写层,既实现了根路径可读写,又保护了系统分区;将配置分区使用掉电安全的文件系统挂载,应用冗余和校验措施,牺牲空间和效率,换取运行时配置文件写入的掉电安全,保证增删改查配置数据的原子性,使用用户空间文件系统提供友好的用户接口;两个应用分区互为冗余备份,备份应用分区可用于软件升级,即使升级意外中断也不影响设备运行。
The application provides a power-down protection method, device and computer equipment for an embedded Linux system. The method includes the following steps: dividing the non-volatile storage medium of the Linux system into a system partition, a configuration partition, a first application partition, a second application partition, and a second application partition. 2. Application partition and data partition; use overlayfs to jointly mount the system partition to the root path, the system partition is a read-only layer, and the memory file system is a read-write layer, which not only realizes the root path can be read and written, but also protects the system partition; The configuration partition is mounted with a power-failure safe file system, redundancy and verification measures are applied, and space and efficiency are sacrificed in exchange for the power-failure security of writing configuration files at runtime, ensuring the atomicity of adding, deleting, modifying and checking configuration data, and using user space The file system provides a friendly user interface; the two application partitions are redundant backups of each other, and the backup application partition can be used for software upgrades, even if the upgrade is interrupted unexpectedly, it will not affect the operation of the device.
Description
技术领域technical field
本申请涉及嵌入式计算机系统数据安全技术领域,尤其涉及一种嵌入式Linux系统掉电保护方法、装置与设备。The present application relates to the technical field of embedded computer system data security, in particular to a power-down protection method, device and equipment for an embedded Linux system.
背景技术Background technique
对于大部分工业应用计算机设备,在关闭设备时是直接切断设备电源,而不是像家用计算机一样先执行软件关机再切断电源。或者在运行过程中因为异常掉电、瞬时欠压等原因异常关闭设备。对于运行在工业计算机设备中的嵌入式Linux系统,直接切断设备电源时如果正在写数据,可能会损坏文件系统,如果关键文件损坏,设备将无法启动运行。目前大部分掉电保护机制都使用了备用电池、大电容、欠压检测电路等硬件手段,这些方法不仅需要额外硬件成本,同时使系统硬件更加复杂。For most industrial application computer equipment, when shutting down the equipment, the power supply of the equipment is directly cut off, instead of first executing the software shutdown and then cutting off the power supply like a home computer. Or shut down the device abnormally due to abnormal power failure, instantaneous undervoltage and other reasons during operation. For the embedded Linux system running in the industrial computer equipment, if the data is being written when the power of the equipment is directly cut off, the file system may be damaged, and if the key files are damaged, the equipment will not be able to start and run. At present, most power-down protection mechanisms use hardware means such as backup batteries, large capacitors, and undervoltage detection circuits. These methods not only require additional hardware costs, but also make the system hardware more complicated.
发明内容Contents of the invention
为了解决上述技术问题,本申请提供了一种嵌入式Linux系统掉电保护方法、装置与计算机设备,实现无需备用电池、无需欠压检测电路、无需大电容储能的情况下,通过纯软件手段来实现对嵌入式Linux系统的掉电保护。同时支持重要配置数据的运行时安全写入,即使配置文件写入过程中发生异常掉电也能保证完整性、安全性、原子性。本方法无需将根文件系统整体放入内存文件系统,因此适用于根文件系统比较大的Linux系统。In order to solve the above-mentioned technical problems, this application provides a power-down protection method, device and computer equipment for an embedded Linux system, which can be realized through pure software means without backup battery, under-voltage detection circuit, and large capacitor energy storage. To realize the power-down protection of the embedded Linux system. At the same time, it supports the safe writing of important configuration data during runtime. Even if an abnormal power failure occurs during the writing of configuration files, integrity, security, and atomicity can be guaranteed. This method does not need to put the root file system into the memory file system as a whole, so it is suitable for Linux systems with relatively large root file systems.
第一方面,本申请提供了一种嵌入式Linux系统掉电保护方法,所述方法包括以下步骤:In the first aspect, the application provides a power-down protection method for an embedded Linux system, the method comprising the following steps:
步骤S1:将非易失性存储介质划分为系统分区、配置分区、第一应用分区、第二应用分区、数据分区,并进行初始化;Step S1: Divide the non-volatile storage medium into a system partition, a configuration partition, a first application partition, a second application partition, and a data partition, and perform initialization;
步骤S2:将系统分区和内存文件系统联合挂载到根路径;Step S2: Jointly mount the system partition and the memory file system to the root path;
步骤S3:检查并修复数据分区、挂载数据分区;Step S3: Check and repair the data partition, mount the data partition;
步骤S4:建立配置分区索引表、修复配置分区、构建用户空间文件系统,挂载所述用户空间文件系统;Step S4: establishing a configuration partition index table, repairing the configuration partition, constructing a user space file system, and mounting the user space file system;
步骤S5:检查并修复第一应用分区或者第二应用分区,挂载第一应用分区和第二应用分区。Step S5: Check and repair the first application partition or the second application partition, and mount the first application partition and the second application partition.
在上述方案中,将Linux系统的非易失性存储介质划分为系统分区、配置分区、第一应用分区、第二应用分区、数据分区;将系统分区使用overlayfs联合挂载到根路径,系统分区为只读层,内存文件系统为读写层,既实现了根路径可读写,又保护了系统分区;将配置分区使用掉电安全的文件系统挂载,应用冗余和校验措施,牺牲空间和效率,换取运行时配置文件写入的掉电安全,保证增删改查配置数据的原子性,使用用户空间文件系统提供友好的用户接口;两个应用分区互为冗余备份,备份应用分区可用于软件升级,即使升级意外中断也不影响设备运行。In the above scheme, the non-volatile storage medium of the Linux system is divided into system partition, configuration partition, first application partition, second application partition, and data partition; the system partition is jointly mounted to the root path using overlayfs, and the system partition It is a read-only layer, and the memory file system is a read-write layer, which not only realizes the root path can be read and written, but also protects the system partition; mount the configuration partition with a power-fail safe file system, apply redundancy and verification measures, and sacrifice Space and efficiency, in exchange for the power-off security of writing configuration files at runtime, ensuring the atomicity of adding, deleting, modifying and querying configuration data, and using the user space file system to provide a friendly user interface; the two application partitions are redundant backups of each other, and the backup application partition It can be used for software upgrade, even if the upgrade is interrupted unexpectedly, it will not affect the operation of the device.
在一个具体的可实施方案中,所述步骤S2包括:In a specific implementation, the step S2 includes:
步骤S21:构建一个系统分区挂载程序,伪装成系统init程序,其中,所述init程序的路径至少包括/sbin/init;Step S21: Build a system partition mount program, disguised as a system init program, wherein the path of the init program includes at least /sbin/init;
步骤S22:在上述系统分区挂载程序中采用overlayfs实现系统分区和内存文件系统联合挂载到一个临时路径,其中,所述联合挂载的只读层为系统分区,读写层为内存文件系统;Step S22: Using overlayfs in the above-mentioned system partition mount program to jointly mount the system partition and the memory file system to a temporary path, wherein the read-only layer of the joint mount is the system partition, and the read-write layer is the memory file system ;
步骤S23:调用pivot_root指令将所述临时路径切换为根路径;Step S23: calling the pivot_root command to switch the temporary path to the root path;
步骤S24:调用mount指令使用remount,ro参数将系统分区切换为只读;Step S24: call the mount command and use the remount, ro parameters to switch the system partition to read-only;
步骤S25:执行真实的系统init程序。Step S25: Execute the real system init program.
在一个具体的可实施方案中,所述步骤S3包括:In a specific implementation, the step S3 includes:
步骤S31:调用fsck指令使用-y参数检查并尝试修复数据分区;Step S31: calling the fsck command to use the -y parameter to check and try to repair the data partition;
步骤S32:根据fsck返回值判断是否存在无法修复的异常,如有则格式化数据分区;Step S32: judging whether there is an abnormality that cannot be repaired according to the return value of fsck, and if so, formatting the data partition;
步骤S33:调用mount指令挂载数据分区。Step S33: call the mount command to mount the data partition.
在一个具体的可实施方案中,所述配置分区包括文件系统元数据、Page Bitmap、多个存储键值对的Page、冗余的Page Bitmap、冗余的文件系统元数据区域,其中,In a specific implementation, the configuration partition includes file system metadata, Page Bitmap, multiple pages storing key-value pairs, redundant Page Bitmap, and redundant file system metadata area, wherein,
所述Page Bitmap区域的每个扇区包括496字节的Bitmap和16字节校验值,校验值用于检查Bitmap的完整性,所述Bitmap用于标记Page是否被占用,每个Page对应一个Bit;Each sector of the Page Bitmap area includes a Bitmap of 496 bytes and a check value of 16 bytes, the check value is used to check the integrity of the Bitmap, and the Bitmap is used to mark whether the Page is occupied, and each Page corresponds to a Bit;
所述Page由头部、头部校验和数据区域构成,其中,所述头部区域由键、预留段、数据长度、数据校验构成,头部区域的键与Linux系统中的文件路径相对应,头部校验用于检查头部自身的完整性,数据校验用于检查数据的完整性。Described Page is made up of header, header verification and data area, and wherein, described header area is made of key, reserved segment, data length, data verification, and the key of header area and the file path in Linux system Correspondingly, the header verification is used to check the integrity of the header itself, and the data verification is used to check the integrity of the data.
在一个具体的可实施方案中,所述步骤S4包括:In a specific implementation, the step S4 includes:
步骤S41:以O_DIRECT和O_SYNC模式打开配置分区对应的块设备;Step S41: Open the block device corresponding to the configuration partition in O_DIRECT and O_SYNC modes;
步骤S42:快速扫描配置分区,建立文件索引,其中,所述快速扫描使用文件系统元数据定位Page位置,使用PageBitmap判断Page是否有效;Step S42: Quickly scan the configuration partition to establish a file index, wherein the quick scan uses file system metadata to locate the Page location, and uses PageBitmap to determine whether the Page is valid;
步骤S43:通过Page的校验机制检查数据的完整性,发现异常时使用冗余数据进行修复;Step S43: check the integrity of the data through the verification mechanism of the Page, and use redundant data to repair when abnormalities are found;
步骤S44:构造并挂载一个用户空间文件系统,其中,所述用户空间文件系统是用户空间访问配置分区的窗口。Step S44: Construct and mount a user space file system, wherein the user space file system is a window for the user space to access the configuration partition.
在一个具体的可实施方案中,所述步骤S5包括:In a specific implementation, the step S5 includes:
步骤S51:将第一应用分区和第二应用分区标记为默认应用分区或者备份应用分区,其中,所述标记操作的依据为配置分区的配置文件;Step S51: mark the first application partition and the second application partition as default application partitions or backup application partitions, wherein the basis of the marking operation is the configuration file of the configuration partition;
步骤S52:检查备份应用分区是否存在无法修复的异常,如有则使用默认应用分区的数据修复备份应用分区;Step S52: Check whether there is an abnormality in the backup application partition that cannot be repaired, and if so, use the data of the default application partition to repair the backup application partition;
步骤S53:以读写模式挂载备份应用分区;Step S53: mount the backup application partition in read-write mode;
步骤S54:以只读模式挂载默认应用分区。Step S54: Mount the default application partition in read-only mode.
第二方面,本申请实施例提供了一种嵌入式Linux系统掉电保护装置,所述装置包括:In a second aspect, the embodiment of the present application provides a power-down protection device for an embedded Linux system, the device comprising:
分区管理模块,用于将非易失性存储介质划分为系统分区、配置分区、第一应用分区、第二应用分区、数据分区,并进行初始化;The partition management module is used to divide the non-volatile storage medium into a system partition, a configuration partition, a first application partition, a second application partition, and a data partition, and perform initialization;
系统分区挂载模块,将系统分区和内存文件系统联合挂载到根路径;The system partition mount module, which jointly mounts the system partition and the memory file system to the root path;
数据分区挂载模块,用于检查并修复数据分区、挂载数据分区;Data partition mount module, used to check and repair data partition, mount data partition;
配置分区管理模块,用于建立配置分区索引表、修复配置分区、构建用户空间文件系统,挂载所述用户空间文件系统;The configuration partition management module is used to establish the configuration partition index table, repair the configuration partition, construct the user space file system, and mount the user space file system;
应用分区挂载模块,用于检查并修复第一应用分区或者第二应用分区,挂载第一应用分区和第二应用分区。The application partition mounting module is used to check and repair the first application partition or the second application partition, and mount the first application partition and the second application partition.
在上述方案中,将Linux系统的非易失性存储介质划分为系统分区、配置分区、第一应用分区、第二应用分区、数据分区;将系统分区使用overlayfs联合挂载到根路径,系统分区为只读层,内存文件系统为读写层,既实现了根路径可读写,又保护了系统分区;将配置分区使用掉电安全的文件系统挂载,应用冗余和校验措施,牺牲空间和效率,换取运行时配置文件写入的掉电安全,保证增删改查配置数据的原子性,使用用户空间文件系统提供友好的用户接口;两个应用分区互为冗余备份,备份应用分区可用于软件升级,即使升级意外中断也不影响设备运行。In the above scheme, the non-volatile storage medium of the Linux system is divided into system partition, configuration partition, first application partition, second application partition, and data partition; the system partition is jointly mounted to the root path using overlayfs, and the system partition It is a read-only layer, and the memory file system is a read-write layer, which not only realizes the root path can be read and written, but also protects the system partition; mount the configuration partition with a power-fail safe file system, apply redundancy and verification measures, and sacrifice Space and efficiency, in exchange for the power-off security of writing configuration files at runtime, ensuring the atomicity of adding, deleting, modifying and querying configuration data, and using the user space file system to provide a friendly user interface; the two application partitions are redundant backups of each other, and the backup application partition It can be used for software upgrade, even if the upgrade is interrupted unexpectedly, it will not affect the operation of the device.
第三方面,本申请实施例提供了一种计算机设备,所述计算机设备包括处理器和非易失性存储器,所述非易失性存储器存储有计算机程序,所述计算机程序在所述处理器上执行时实现第一方面任一项实施例所述的方法。In a third aspect, the embodiment of the present application provides a computer device, the computer device includes a processor and a non-volatile memory, the non-volatile memory stores a computer program, and the computer program runs on the processor The method described in any one of the embodiments of the first aspect is realized when executed.
附图说明Description of drawings
为了更清楚地说明本发明的技术方案,下面将对实施例中所需要使用的附图作简单地介绍,应当理解,以下附图仅示出了本发明的某些实施例,因此不应被看作是对本发明保护范围的限定。In order to illustrate the technical solution of the present invention more clearly, the following drawings will be briefly introduced in the embodiments. It should be understood that the following drawings only show some embodiments of the present invention, and therefore should not be regarded as It is regarded as limiting the protection scope of the present invention.
图1为本申请实施例提供的方法的整体流程;Fig. 1 is the overall flow of the method provided by the embodiment of the present application;
图2为本申请实施例的非易失性存储介质分区情况,以及各分区的文件系统类型、挂载方式、挂载路径;Fig. 2 is the partition situation of the non-volatile storage medium of the embodiment of the present application, and the file system type, mount mode, and mount path of each partition;
图3为本申请实施例的配置分区文件系统总体架构;Fig. 3 is the configuration partition file system general framework of the embodiment of the present application;
图4为本申请实施例的配置分区文件系统PageBitmap区域构成;Fig. 4 is the configuration partition file system PageBitmap area composition of the embodiment of the present application;
图5为本申请实施例的配置分区文件系统Page构成。FIG. 5 shows the Page composition of the configuration partition file system according to the embodiment of the present application.
具体实施方式Detailed ways
为方便理解本申请实施例提供的方法,首先介绍一下涉及到的名词:In order to facilitate the understanding of the methods provided in the embodiments of this application, first introduce the terms involved:
Linux:一种操作系统内核的名称,基于该内核的操作系统一般统称为Linux操作系统。Linux: The name of an operating system kernel. The operating system based on this kernel is generally referred to as the Linux operating system.
根路径:Linux操作系统下一切皆为文件,文件都对应一个路径,根路径是所有路径的根。Root path: Everything under the Linux operating system is a file, each file corresponds to a path, and the root path is the root of all paths.
aufs:全称是Advanced Multi-layered unification filesytem,主要功能是把多个目录结合成一个目录,对外使用。aufs: The full name is Advanced Multi-layered unification filesytem, the main function is to combine multiple directories into one directory for external use.
overlayfs:Linux内核自带的的一种文件系统,主要功能是把多个目录结合成一个目录,对外使用。overlayfs: A file system that comes with the Linux kernel. Its main function is to combine multiple directories into one directory for external use.
ext4:第四代扩展文件系统(英语:Fourth extended filesystem),是Linux系统下最常用的文件系统,是一个具备日志功能的文件系统。ext4: The fourth extended file system (English: Fourth extended file system), is the most commonly used file system under Linux system, and it is a file system with log function.
tmpfs:临时文件系统,是一种基于内存的文件系统,它和虚拟磁盘ramdisk比较类似像,但不完全相同。和ramdisk一样,tmpfs可以使用RAM,但它也可以使用swap分区来存储,而且传统的ramdisk是个块设备,要用mkfs来格式化它,才能真正地使用它。而tmpfs是一个文件系统,并不是块设备,只是安装它,就可以使用了。tmpfs是最好的基于内存的文件系统。tmpfs: Temporary file system is a memory-based file system, which is similar to the virtual disk ramdisk, but not exactly the same. Like ramdisk, tmpfs can use RAM, but it can also use swap partition for storage, and the traditional ramdisk is a block device, it must be formatted with mkfs before it can be really used. And tmpfs is a file system, not a block device, just install it, you can use it. tmpfs is the best memory based file system.
挂载:由操作系统使一个存储设备上的计算机文件和目录可供用户通过计算机的文件系统访问的一个过程。Mount: The process by which an operating system makes computer files and directories on a storage device accessible to users through the computer's file system.
SDK:软件开发工具包是为特定的软件包、软件框架、硬件平台、操作系统等建立应用软件时的开发工具的集合。SDK: Software development kit is a collection of development tools for building application software for specific software packages, software frameworks, hardware platforms, operating systems, etc.
bitmap:一种数据结构,代表了有限域中的稠集(dense set),每一个元素至少出现一次,没有其他的数据和元素相关联。在索引,数据压缩等方面有广泛应用。bitmap: A data structure that represents a dense set in a finite field, each element appears at least once, and no other data is associated with the element. It is widely used in indexing, data compression, etc.
init:Linux系统操作中不可缺少的程序之一。所谓的init进程,它是一个由内核启动的用户级进程。内核会在过去曾使用过init的几个地方查找它,它的正确位置(对Linux系统来说)是/sbin/init。init: One of the indispensable programs in Linux system operation. The so-called init process, which is a user-level process started by the kernel. The kernel looks for it in several places where init has been used in the past, and its correct location (for Linux systems) is /sbin/init.
VFS:(virtual File System)的作用就是采用标准的Unix系统调用读写位于不同物理介质上的不同文件系统,为各类文件系统提供了统一的应用编程接口。VFS是一个可以让open()、read()、write()等系统调用不用关心底层的存储介质和文件系统类型就可以工作的粘合层。VFS: (virtual File System) uses standard Unix system calls to read and write different file systems located on different physical media, and provides a unified application programming interface for various file systems. VFS is a glue layer that allows system calls such as open(), read(), and write() to work regardless of the underlying storage medium and file system type.
用户空间文件系统:(Filesystem in Userspace,简称FUSE)是操作系统中的概念,指完全在用户态实现的文件系统。目前Linux通过内核模块对此进行支持。User space file system: (Filesystem in Userspace, referred to as FUSE) is a concept in the operating system, which refers to a file system completely implemented in user mode. Currently Linux supports this through kernel modules.
eMMC:Embedded Multi Media Card,嵌入式多媒体卡。eMMC: Embedded Multi Media Card, embedded multimedia card.
fsck:file system check,Linux系统中的一个工具,用来检查和维护文件系统。若系统掉电或磁盘发生问题,可利用fsck命令对文件系统进行检查和尝试修复。fsck: file system check, a tool in the Linux system, used to check and maintain the file system. If the system is powered off or the disk has problems, you can use the fsck command to check and try to repair the file system.
MD5:(MD5 Message-Digest Algorithm),一种信息摘要算法,可以产生出一个128位(16字节)的散列值,用于确保信息的完整一致。MD5: (MD5 Message-Digest Algorithm), an information digest algorithm that can generate a 128-bit (16-byte) hash value to ensure the integrity and consistency of information.
DDR:Double Data Rate SDRAM,双倍速率同步动态刷新随机访问存储器,一般称为内存条。DDR: Double Data Rate SDRAM, double-rate synchronous dynamic refresh random access memory, generally called a memory stick.
为了对本申请的技术方案有更进一步的了解,下面结合附图详细说明本申请的技术方案的一种具体实施案例,该案例所涉及的硬件平台信息如下:In order to have a further understanding of the technical solution of the present application, a specific implementation case of the technical solution of the present application will be described in detail below in conjunction with the accompanying drawings. The hardware platform information involved in this case is as follows:
处理器:6核ARMv8.2Processor: 6-core ARMv8.2
存储:16GB eMMC、8GBDDRStorage: 16GB eMMC, 8GB DDR
操作系统:Ubuntu 18.04.5LTSOS: Ubuntu 18.04.5LTS
内核版本:Linux4.19.140Kernel version: Linux4.19.140
参考图1,该具体实施案例包括以下步骤:With reference to Fig. 1, this concrete implementation case comprises the following steps:
步骤S1:将非易失性存储介质划分为系统分区、配置分区、第一应用分区、第二应用分区、数据分区,并进行初始化。Step S1: Divide the non-volatile storage medium into a system partition, a configuration partition, a first application partition, a second application partition, and a data partition, and perform initialization.
如图2所示,将Linux系统非易失性存储介质eMMC划分为5个区域,分别是系统分区、配置分区、第一应用分区、第二应用分区、数据分区,对上述分区进行格式化操作,并填充初始文件。As shown in Figure 2, the non-volatile storage medium eMMC of the Linux system is divided into five areas, which are system partition, configuration partition, first application partition, second application partition, and data partition, and format the above partitions , and populate the initial file.
系统分区的文件系统类型为ext4,初始文件主要有内核镜像、设备树、根文件系统、SDK库文件等,上述文件关系到设备能否启动,因此需要重点保护。The file system type of the system partition is ext4. The initial files mainly include the kernel image, device tree, root file system, SDK library files, etc. The above files are related to whether the device can be started, so they need to be protected.
配置分区的文件系统类型为自定义的文件系统,初始文件主要有操作系统配置文件和应用软件配置文件,配置文件需要运行时更新,因此配置分区必须以可读写方式挂载,并做到写入时掉电安全。The file system type of the configuration partition is a custom file system. The initial files mainly include operating system configuration files and application software configuration files. The configuration files need to be updated during runtime, so the configuration partition must be mounted in a readable and writable manner It is safe to power off when entering.
第一应用分区和第二应用分区的文件系统类型为ext4,初始文件主要有业务软件可执行文件、算法软件可执行文件、动态库文件、算法模型数据文件。The file system type of the first application partition and the second application partition is ext4, and the initial files mainly include business software executable files, algorithm software executable files, dynamic library files, and algorithm model data files.
数据分区的文件系统类型为ext4,存储日志文件、临时文件等设备运行过程中产生的数据,无需初始文件。The file system type of the data partition is ext4, which stores data generated during the operation of the device, such as log files and temporary files, without initial files.
步骤S2:将系统分区和内存文件系统联合挂载到根路径;Step S2: Jointly mount the system partition and the memory file system to the root path;
具体的,该步骤包括以下步骤:Specifically, this step includes the following steps:
步骤S21:构建一个系统分区挂载程序伪装成系统的init程序,其中,所述init程序的路径至少包括/sbin/init。Step S21: Construct a system partition mount program disguised as a system init program, wherein the path of the init program includes at least /sbin/init.
步骤S22:在上述系统分区挂载程序中采用overlayfs实现系统分区和内存文件系统tmpfs联合挂载到一个临时路径,其中,系统分区作为overlayfs的只读层,内存文件系统tmpfs作为overlayfs的读写层,既实现了根路径可读写,又做到了系统分区只读保护。Step S22: Use overlayfs in the above system partition mounting procedure to realize the joint mounting of the system partition and the memory file system tmpfs to a temporary path, wherein the system partition is used as the read-only layer of overlayfs, and the memory file system tmpfs is used as the read-write layer of overlayfs , which not only realizes the root path can be read and written, but also achieves the read-only protection of the system partition.
步骤S23:调用pivot_root指令将所述临时路径切换为根路径;Step S23: calling the pivot_root command to switch the temporary path to the root path;
步骤S24:调用mount指令使用remount,ro参数将系统分区切换为只读,从而在操作系统层面禁止任何数据写入系统分区,杜绝掉电时损坏。Step S24: Call the mount command and use the remount, ro parameters to switch the system partition to read-only, thereby prohibiting any data from being written into the system partition at the operating system level, and preventing damage during power failure.
步骤S25:执行真实的系统init程。init程序是Linux系统操作中不可缺少的程序之一,它是由内核启动的第一个用户级进程,其路径一般为/sbin/init。Step S25: Execute the real system init process. The init program is one of the indispensable programs in the operation of the Linux system. It is the first user-level process started by the kernel, and its path is generally /sbin/init.
步骤S3:检查并修复数据分区、挂载数据分区。Step S3: Check and repair the data partition, mount the data partition.
具体的,包括以下步骤:Specifically, the following steps are included:
步骤S31:调用fsck指令使用-y参数检查并尝试修复数据分区。Step S31: Invoke the fsck command with the -y parameter to check and try to repair the data partition.
步骤S32:根据fsck返回值判断是否存在无法修复的异常,如有则格式化数据分区。Step S32: Determine whether there is an abnormality that cannot be repaired according to the return value of fsck, and if so, format the data partition.
步骤S33:步骤S33:调用mount指令挂载所述数据分区。Step S33: Step S33: calling the mount command to mount the data partition.
具体的,在步骤S32中检查和修复完成后,将数据分区挂载到用户目录的data子目录。Specifically, after the inspection and repair are completed in step S32, the data partition is mounted to the data subdirectory of the user directory.
数据分区存储设备运行过程中产生的数据,包括日志文件、临时文件等。这些文件即使丢失也不会影响设备正常运行,因此,当检查到数据分区存在不可修复的异常时,可以直接格式化数据分区。The data partition stores the data generated during the operation of the device, including log files and temporary files. Even if these files are lost, the normal operation of the device will not be affected. Therefore, when an unrepairable abnormality is detected in the data partition, the data partition can be formatted directly.
由于后续的步骤会产生操作日志,这些日志需要存储到数据分区,因此数据分区要先于配置分区和应用分区挂载。Since the subsequent steps will generate operation logs, these logs need to be stored in the data partition, so the data partition must be mounted before the configuration partition and application partition.
步骤S4:建立配置分区索引表、修复配置分区、构建用户空间文件系统,挂载所述用户空间文件系统。Step S4: establishing a configuration partition index table, repairing the configuration partition, constructing a user space file system, and mounting the user space file system.
具体的,在具体挂载时,将分区索引表、修复配置分区、构建用户空间文件系统挂载到用户目录的config子目录。Specifically, during specific mounting, the partition index table, repair configuration partition, and build user space file system are mounted to the config subdirectory of the user directory.
参考图3,所示配置分区包括为至少以下区域:文件系统元数据、PageBitmap、多个存储键值对的Page、冗余的Page Bitmap、冗余的文件系统元数据,为方便用户程序访问配置分区,与Linux用户空间文件系统结合,用户程序可以像读写普通文件一样读写配置分区的文件。Referring to Figure 3, the configuration partition shown includes at least the following areas: file system metadata, PageBitmap, multiple Pages for storing key-value pairs, redundant Page Bitmap, redundant file system metadata, configured for user program access Partition, combined with the Linux user space file system, the user program can read and write the files of the configuration partition just like reading and writing ordinary files.
参考图4,在PageBitmap区域,每个磁盘扇区的512字节包括496字节的Bitmap和16字节校验数据,使用MD5校验值检查Bitmap的完整性。所述Bitmap用于标记Page是否被占用,每个Page对应一个Bit。Referring to Figure 4, in the PageBitmap area, 512 bytes of each disk sector includes a 496-byte Bitmap and 16-byte checksum data, and the integrity of the Bitmap is checked using the MD5 checksum. The Bitmap is used to mark whether the Page is occupied, and each Page corresponds to a Bit.
参考图5,每个Page由133字节的头部、16字节的头部校验和不定长的数据区域构成,其中头部区域由键、预留段、数据长度、数据校验构成。头部区域的键与Linux系统中的文件路径相对应,数据长度的最大值在文件系统格式化时确定。头部校验用于检查头部自身的完整性,数据校验用于检查数据的完整性,均使用MD5值校验。Referring to Figure 5, each Page consists of a 133-byte header, a 16-byte header checksum, and a variable-length data area, where the header area consists of a key, reserved segment, data length, and data checksum. The key in the header area corresponds to the file path in the Linux system, and the maximum data length is determined when the file system is formatted. The header verification is used to check the integrity of the header itself, and the data verification is used to check the integrity of the data, both of which use MD5 value verification.
同一个的文件被重复保存在3个Page中,如果某个Page在写入时发生掉电,当该文件被访问时,检查到该文件的重复一致性存在异常,就会使用冗余的Page来恢复异常的Page。通过数据校验和冗余备份机制,牺牲空间和效率确保增、删、改、查操作的原子性,做到即使配置文件写入过程中发生掉电,再次上电后,配置文件也能自动回滚到修改前的完整状态。The same file is repeatedly saved in 3 pages. If a page loses power when it is written, when the file is accessed, it is checked that the duplicate consistency of the file is abnormal, and the redundant page will be used. To restore the abnormal Page. Through the data verification and redundancy backup mechanism, the atomicity of adding, deleting, modifying, and checking operations is ensured at the expense of space and efficiency, so that even if a power failure occurs during the configuration file writing process, the configuration file can be automatically restored after the power is turned on again. Roll back to the full state before the modification.
具体来说,包括以下步骤:Specifically, the following steps are included:
步骤S41:以O_DIRECT和O_SYNC模式打开配置分区对应的块设备;Step S41: Open the block device corresponding to the configuration partition in O_DIRECT and O_SYNC modes;
步骤S42:快速扫描配置分区,建立文件系统索引,其中,所述快速扫描使用文件系统元数据定位Page位置,使用Page Bitmap判断Page是否有效;Step S42: Quickly scan the configuration partition to establish a file system index, wherein the quick scan uses the file system metadata to locate the Page position, and uses the Page Bitmap to determine whether the Page is valid;
步骤S43:通过Page的校验机制检查数据的完整性,发现异常时使用冗余数据进行修复;Step S43: check the integrity of the data through the verification mechanism of the Page, and use redundant data to repair when abnormalities are found;
步骤S44:构造一个用户空间文件系统,挂载到用户目录的config子目录,其中,所述用户空间文件系统是用户空间访问配置分区的窗口。Step S44: Construct a user space file system and mount it to the config subdirectory of the user directory, wherein the user space file system is a window for the user space to access the configuration partition.
步骤S5:检查并修复第一应用分区或者第二应用分区,挂载第一应用分区和第二应用分区。Step S5: Check and repair the first application partition or the second application partition, and mount the first application partition and the second application partition.
具体的,包括以下步骤:Specifically, the following steps are included:
步骤S51:将第一应用分区和第二应用分区标记为默认应用分区或者备份应用分区,其中,所述标记操作的依据为配置分区的配置文件;Step S51: mark the first application partition and the second application partition as default application partitions or backup application partitions, wherein the basis of the marking operation is the configuration file of the configuration partition;
具体的,根据配置分区的配置文件app-block-device中的配置项,将第一应用分区和第二应用分区确定为默认应用分区或者备份应用分区。Specifically, the first application partition and the second application partition are determined as default application partitions or backup application partitions according to the configuration items in the configuration file app-block-device of the configuration partitions.
步骤S52:检查备份应用分区是否存在无法修复的异常,如有则使用默认应用分区的数据修复备份应用分区;Step S52: Check whether there is an abnormality in the backup application partition that cannot be repaired, and if so, use the data of the default application partition to repair the backup application partition;
步骤S53:以读写模式挂载备份应用分区;Step S53: mount the backup application partition in read-write mode;
步骤S54:以只读模式挂载默认应用分区。Step S54: mount the default application partition in read-only mode.
具体的,检查并修复备份应用分区,将默认应用分区挂载到用户目录的app子目录,将备份应用分区挂载到用户目录的app-backup子目录。Specifically, check and repair the backup application partition, mount the default application partition to the app subdirectory of the user directory, and mount the backup application partition to the app-backup subdirectory of the user directory.
默认应用分区以只读方式挂载,禁止任何数据写入,杜绝掉电时损坏,因此无需进行检测和修复;备份应用分区以读写方式挂载,用于应用软件升级,升级过程由于掉电或者网络原因异常中止时,在系统再次启动时,如果检测到备份应用分区存在不可修复的异常或者数据完整性存在问题,则使用默认应用分区恢复备份应用分区。The default application partition is mounted in read-only mode, and any data writing is prohibited to prevent damage during power failure, so there is no need for detection and repair; the backup application partition is mounted in read-write mode for application software upgrades. The upgrade process is due to power failure Or when the network is terminated abnormally, when the system restarts, if an unrepairable abnormality or data integrity problem is detected in the backup application partition, the default application partition will be used to restore the backup application partition.
通过上述描述可看出,本申请实施例提供的方法将Linux系统的非易失性存储介质划分为系统分区、配置分区、第一应用分区、第二应用分区、数据分区;将系统分区使用overlayfs联合挂载到根路径,系统分区为只读层,内存文件系统为读写层,既实现了根路径可读写,又保护了系统分区;将配置分区使用掉电安全的文件系统挂载,应用冗余和校验措施,牺牲空间和效率,换取运行时配置文件写入的掉电安全,保证增删改查配置数据的原子性,使用用户空间文件系统提供友好的用户接口;两个应用分区互为冗余备份,备份应用分区可用于软件升级,即使升级意外中断也不影响设备运行。It can be seen from the above description that the method provided by the embodiment of the present application divides the non-volatile storage medium of the Linux system into a system partition, a configuration partition, a first application partition, a second application partition, and a data partition; the system partition uses overlayfs Jointly mount to the root path, the system partition is a read-only layer, and the memory file system is a read-write layer, which not only realizes the root path can be read and written, but also protects the system partition; mount the configuration partition using a power-failure safe file system, Apply redundancy and verification measures, sacrifice space and efficiency, in exchange for power-off security when writing configuration files at runtime, ensure the atomicity of adding, deleting, modifying and checking configuration data, and use user space file systems to provide friendly user interfaces; two application partitions Mutual redundant backup, the backup application partition can be used for software upgrade, even if the upgrade is interrupted unexpectedly, it will not affect the operation of the device.
与上述方法实施例相对应,本申请还提供一种嵌入式Linux系统掉电保护装置,所述装置包括:Corresponding to the above method embodiment, the present application also provides a power-down protection device for an embedded Linux system, the device comprising:
分区管理模块,用于将非易失性存储介质划分为系统分区、配置分区、第一应用分区、第二应用分区、数据分区,并进行初始化;The partition management module is used to divide the non-volatile storage medium into a system partition, a configuration partition, a first application partition, a second application partition, and a data partition, and perform initialization;
系统分区挂载模块,将系统分区和内存文件系统联合挂载到根路径;The system partition mount module, which jointly mounts the system partition and the memory file system to the root path;
数据分区挂载模块,用于检查并修复数据分区、挂载数据分区;Data partition mount module, used to check and repair data partition, mount data partition;
配置分区管理模块,用于建立配置分区索引表、修复配置分区、构建用户空间文件系统,挂载所述用户空间文件系统;The configuration partition management module is used to establish the configuration partition index table, repair the configuration partition, construct the user space file system, and mount the user space file system;
应用分区挂载模块,用于检查并修复第一应用分区或者第二应用分区,挂载第一应用分区和第二应用分区。The application partition mounting module is used to check and repair the first application partition or the second application partition, and mount the first application partition and the second application partition.
上述各个模块的具体功能可参考方法中的相关描述,在此不再一一赘述。For the specific functions of the above modules, reference may be made to relevant descriptions in the method, and details will not be repeated here.
通过上述描述可看出,本申请提供的装置将Linux系统的非易失性存储介质划分为系统分区、配置分区、第一应用分区、第二应用分区、数据分区;将系统分区使用overlayfs联合挂载到根路径,系统分区为只读层,内存文件系统为读写层,既实现了根路径可读写,又保护了系统分区;将配置分区使用掉电安全的文件系统挂载,应用冗余和校验措施,牺牲空间和效率,换取运行时配置文件写入的掉电安全,保证增删改查配置数据的原子性,使用用户空间文件系统提供友好的用户接口;两个应用分区互为冗余备份,备份应用分区可用于软件升级,即使升级意外中断也不影响设备运行。It can be seen from the above description that the device provided by this application divides the non-volatile storage medium of the Linux system into a system partition, a configuration partition, a first application partition, a second application partition, and a data partition; The system partition is a read-only layer, and the memory file system is a read-write layer, which not only makes the root path readable and writable, but also protects the system partition. Residual and verification measures, sacrifice space and efficiency, in exchange for power-off security of runtime configuration file writing, ensure the atomicity of adding, deleting, modifying and checking configuration data, and use user space file system to provide friendly user interface; two application partitions are mutually Redundant backup, the backup application partition can be used for software upgrade, even if the upgrade is interrupted unexpectedly, it will not affect the operation of the device.
此外,还提供了一种计算机设备,所述计算机设备包括处理器和非易失性存储器,所述非易失性存储器存储有计算机程序,所述计算机程序在所述处理器上执行时实现上述嵌入式Linux系统掉电保护方法。In addition, a computer device is also provided, the computer device includes a processor and a non-volatile memory, the non-volatile memory stores a computer program, and when the computer program is executed on the processor, the above-mentioned A power-down protection method for an embedded Linux system.
本说明书的一个或多个实施例旨在涵盖落入所附权利要求的宽泛范围之内的所有这样的替换、修改和变型。因此,凡在本说明书一个或多个实施例的精神和原则之内,所做的任何省略、修改、等同替换、改进等,均应包含在本申请的保护范围之内。One or more embodiments of this description are intended to embrace all such alterations, modifications and variations that fall within the broad scope of the appended claims. Therefore, any omission, modification, equivalent replacement, improvement, etc. made within the spirit and principles of one or more embodiments of this specification shall be included within the protection scope of this application.
以上,仅为本申请的具体实施方式,但本申请的保护范围并不局限于此,任何熟悉本技术领域的技术人员在本申请揭露的技术范围内,可轻易想到的变化或替换,都应涵盖在本申请的保护范围之内。因此,本申请的保护范围应以权利要求的保护范围为准。The above is only the specific implementation of the application, but the scope of protection of the application is not limited thereto. Any changes or substitutions that can be easily imagined by those skilled in the art within the technical scope disclosed in the application should be covered within the scope of protection of this application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims (8)
Priority Applications (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202211459228.8A CN115878377A (en) | 2022-11-17 | 2022-11-17 | Power failure protection method and device for embedded Linux system and computer equipment |
Applications Claiming Priority (1)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202211459228.8A CN115878377A (en) | 2022-11-17 | 2022-11-17 | Power failure protection method and device for embedded Linux system and computer equipment |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| CN115878377A true CN115878377A (en) | 2023-03-31 |
Family
ID=85760410
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| CN202211459228.8A Pending CN115878377A (en) | 2022-11-17 | 2022-11-17 | Power failure protection method and device for embedded Linux system and computer equipment |
Country Status (1)
| Country | Link |
|---|---|
| CN (1) | CN115878377A (en) |
Cited By (2)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN119902809A (en) * | 2024-12-31 | 2025-04-29 | 南京北路智控科技股份有限公司 | A vehicle terminal startup method, device, vehicle terminal and storage medium |
| WO2025167211A1 (en) * | 2024-02-05 | 2025-08-14 | 广州鲁邦通物联网科技股份有限公司 | File system protection method and apparatus for internet of things device |
Citations (7)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN102262568A (en) * | 2010-05-25 | 2011-11-30 | 上海中标软件有限公司 | Power-down prevention Linux system startup method |
| CN105955842A (en) * | 2016-04-19 | 2016-09-21 | 上海斐讯数据通信技术有限公司 | System partition structure and communication apparatus of embedded system |
| CN106033362A (en) * | 2015-03-13 | 2016-10-19 | 北京视联动力国际信息技术有限公司 | Method and device for processing flash memory partition |
| WO2017049828A1 (en) * | 2015-09-25 | 2017-03-30 | 中兴通讯股份有限公司 | Method, device and system for data processing based on linux |
| CN107943414A (en) * | 2017-10-16 | 2018-04-20 | 积成电子股份有限公司 | The file partition of built-in Linux and data read-write method |
| CN113568787A (en) * | 2021-09-23 | 2021-10-29 | 统信软件技术有限公司 | Power failure protection method, computing device and storage medium |
| CN114138349A (en) * | 2021-11-17 | 2022-03-04 | 苏州浪潮智能科技有限公司 | Method, system, terminal and storage medium for mounting data partition |
-
2022
- 2022-11-17 CN CN202211459228.8A patent/CN115878377A/en active Pending
Patent Citations (7)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN102262568A (en) * | 2010-05-25 | 2011-11-30 | 上海中标软件有限公司 | Power-down prevention Linux system startup method |
| CN106033362A (en) * | 2015-03-13 | 2016-10-19 | 北京视联动力国际信息技术有限公司 | Method and device for processing flash memory partition |
| WO2017049828A1 (en) * | 2015-09-25 | 2017-03-30 | 中兴通讯股份有限公司 | Method, device and system for data processing based on linux |
| CN105955842A (en) * | 2016-04-19 | 2016-09-21 | 上海斐讯数据通信技术有限公司 | System partition structure and communication apparatus of embedded system |
| CN107943414A (en) * | 2017-10-16 | 2018-04-20 | 积成电子股份有限公司 | The file partition of built-in Linux and data read-write method |
| CN113568787A (en) * | 2021-09-23 | 2021-10-29 | 统信软件技术有限公司 | Power failure protection method, computing device and storage medium |
| CN114138349A (en) * | 2021-11-17 | 2022-03-04 | 苏州浪潮智能科技有限公司 | Method, system, terminal and storage medium for mounting data partition |
Cited By (2)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| WO2025167211A1 (en) * | 2024-02-05 | 2025-08-14 | 广州鲁邦通物联网科技股份有限公司 | File system protection method and apparatus for internet of things device |
| CN119902809A (en) * | 2024-12-31 | 2025-04-29 | 南京北路智控科技股份有限公司 | A vehicle terminal startup method, device, vehicle terminal and storage medium |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| CN115793985B (en) | Secure storage method, apparatus, device and storage medium | |
| US8484522B2 (en) | Apparatus, system, and method for bad block remapping | |
| JP5376983B2 (en) | Memory system | |
| US8046528B2 (en) | Data writing method for flash memory, and flash memory controller and storage device thereof | |
| EP2048579B1 (en) | System and method for managing memory errors in an information handling system | |
| CN103577574B (en) | High-reliability linear file system based on nand flash | |
| CN118656036B (en) | Mirror image processing method, computer program product, apparatus, and storage medium | |
| CN106776122A (en) | A kind of method of main-apparatus protection in start-up course based on Flash | |
| CN115878377A (en) | Power failure protection method and device for embedded Linux system and computer equipment | |
| CN111158599A (en) | Method, device and equipment for writing data and storage medium | |
| CN103530138A (en) | Method and device for protecting file system data based on embedded LINUX | |
| CN101582044A (en) | Key information area maintenance method of file system and device | |
| US20090013167A1 (en) | Computer device, method for booting the same, and booting module for the same | |
| TW201403323A (en) | Memory storage device and repairing method thereof | |
| CN110134545B (en) | Method and system for providing virtual NVRAM based on trusted execution environment | |
| CN120560672B (en) | A fault-tolerant method for onboard computer operating systems | |
| CN116501409A (en) | Dual-Flash-based server starting method, computer equipment and storage medium | |
| US20060294339A1 (en) | Abstracted dynamic addressing | |
| CN116027973A (en) | Data writing method and device based on flash memory | |
| WO2026001164A1 (en) | Stack file system, system management method, controller, chip device, and vehicle | |
| CN120492227A (en) | Method, device, equipment and medium for recovering distributed storage logical volume data | |
| CN118860740A (en) | A method and system for backing up server CPLD firmware | |
| CN110727545A (en) | Power failure protection method based on combined file system | |
| CN111142792B (en) | Power-down protection method of storage device | |
| CN114356653A (en) | Power-down protection method and device for industrial control firewall |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| PB01 | Publication | ||
| PB01 | Publication | ||
| SE01 | Entry into force of request for substantive examination | ||
| SE01 | Entry into force of request for substantive examination |
