简体   繁体   English

如何在 Linux 中正常关机并防止 SDCARD 损坏

[英]How to gracefully shutdown in Linux and prevent corruption in SDCARD

I am working on Embedded Linux System which mounts a SDCARD ( formatted as FAT32) and writes about 500KB of Data per second continuously .我正在开发嵌入式 Linux 系统,它安装了一个 SDCARD(格式化为 FAT32)并每秒连续写入大约 500KB 的数据。

Now to handle Power failures, we designed a battery which gives a backup of about 3 seconds .现在为了处理电源故障,我们设计了一个电池,可以提供大约 3 秒的备份时间。

This enables us to complete graceful shutdown .这使我们能够完成正常关机。

We have only one process writing data to the SDCARD .So no other process will access the SDCARD in anyway.我们只有一个进程向 SDCARD 写入数据。所以无论如何其他进程都不会访问 SDCARD。

During Power failure and during shutdown process We use fflush and fsync and then finally fclose of the current file in which data is written to.在断电和关机过程中我们使用 fflush 和 fsync 然后最后 fclose 写入数据的当前文件。

We even have verified with oscilloscope by probing the hardware lines that data transaction becomes active with the SDCARD when we call fflush and fsync during shutdown process.我们甚至通过在关闭过程中调用 fflush 和 fsync 时,通过探测硬件线路,通过 SDCARD 激活数据事务,我们甚至用示波器进行了验证。 ie cache data is being tried to be written to SDCARD during this time .即在此期间正在尝试将缓存数据写入 SDCARD。 That makes me believe that fflush and fsync is implemented correctly .这让我相信 fflush 和 fsync 是正确实现的。

Only thing i didnot do is umount the SDCARD as it takes longer time.我唯一没有做的就是卸载 SDCARD,因为它需要更长的时间。

But still in about 10 tries , we can see the SDCARD root partition getting corrupted and files are missing .但是仍然在大约 10 次尝试中,我们可以看到 SDCARD 根分区被损坏并且文件丢失。

Is umount necessary for a proper graceful shutdown . umount 是否需要正常正常关闭。 What i understand is mount only creates a link of the inode of the root of this filesystem with rest of the file system .我的理解是 mount 只创建了这个文件系统根目录的 inode 与文件系统其余部分的链接。 and umount only removes the link .和 umount 只删除链接。

What is the perfect way or steps to ensure protection of the SDCARD while shutting down in Embedded Linux .在嵌入式 Linux 中关闭时确保 SDCARD 得到保护的完美方法或步骤是什么?

Below is the code which gets executed during the shutdown process currently.以下是当前在关闭过程中执行的代码。 is umount call necessary .是否需要 umount 调用。

    if(fflush(file)<0)
        printf("Failed  fflush errno=%d",errno);
    if(fsync(fileno(file))<0)
        printf ("Failed  fsync errno=%d",errno);
    if(fclose(file)<0)
        printf("Failed  fclose errno=%d",errno);
    file=NULL;

You are doing almost right, however, there is a catch: your SD device might require additional time to flush its own internal buffers.您做得几乎是正确的,但是,有一个问题:您的 SD 设备可能需要额外的时间来刷新自己的内部缓冲区。

The needed time is dependent on the precise chipset (and of course, brand, model, etc...).所需时间取决于精确的芯片组(当然还有品牌、型号等)。

In order to properly shutdown the system, you need to have a backup battery that allows you to be safe: consider that backup batteries on enterprise RAID cards ranges from 2 to 5 minutes, and the manufacturer knows exactly every hardware details.为了正确关闭系统,您需要有一个可以让您安全的备用电池:考虑到企业 RAID 卡上的备用电池范围从 2 到 5 分钟,制造商确切地知道每个硬件细节。

Unfortunately, after fsync() you have to wait for more time.不幸的是,在fsync()您必须等待更多时间。 In similar circumstances, our RasperryPi devices are safe towards corruption with a backup battery that allows the device to operate for at least 20 seconds (although 10-15 is safe in our case) just to play easy.在类似的情况下,我们的 RasperryPi 设备使用备用电池可以安全地防止损坏,该备用电池允许设备运行至少 20 秒(尽管在我们的情况下 10-15 秒是安全的)只是为了轻松玩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM