【问题标题】:Correct auto unmount of pen drive正确自动卸载笔式驱动器
【发布时间】:2021-01-27 17:06:21
【问题描述】:

我搜索了类似的帖子,但没有找到。我正在寻找一些建议或正确方向的观点,因为我找不到关于这个主题的太多信息。

我正在尝试在运行来自 buildroot 的自定义 linux 构建的树莓派 4 上编写一个守护进程。守护进程使用 udev (libudev.h) 和 epoll (sys/epoll.h) 检测新插入的笔式驱动器,创建目录并安装设备。它还检测所述设备的删除,卸载然后删除目录。

在移除笔式驱动器之前,它运行良好,尽管卸载正在执行(没有任何错误返回),但当我重新插入笔式驱动器时,我总是收到此消息“FAT-fs (sda1) Volume was not正确卸载。某些数据可能已损坏。请运行 fsck"。 我究竟做错了什么?如何正确卸载它?

 //pen removed
 if(!action.compare("remove") && !partition.compare("partition")){
    //if directory exists
    dir = opendir(path.c_str());
    if(dir){
       //close directory to be able to unmount
       close(dir);
       //unmount
       status = umount(path.c_str());
       if(status != 0)
           syslog(LOG_ERR, "%m\n");
       //remove the directory
       status = rmdir(path.c_str());
       if(status != 0)
           syslog(LOG_ERR, "%m\n");
       }
}
//pen inserted
else if(!action.compare("add") && !partition.compare("partition")){
    //if directory doesn't exist
    dir = opendir(path.c_str());
    if(!dir){
        //create the directory 
        status = mkdir(path.c_str(), 777); 
        if(status != 0)
           syslog(LOG_ERR, "%m\n");
        }
        //mount 
        status = mount(devicenode.c_str(), path.c_str(), "vfat", MS_NOATIME, NULL);
        if(status != 0)
           syslog(LOG_ERR, "%m\n");
        }
}

【问题讨论】:

    标签: linux daemon buildroot raspberry-pi4


    【解决方案1】:

    在移除笔式驱动器之前必须卸载驱动器。这显然是不可能自动完成的。

    一个选项是使用-o sync 挂载选项挂载;这将确保立即刷新文件,从而减少文件系统损坏的机会。但是,AFAIK,它不会删除警告消息。

    警告消息是由于在安装笔式驱动器时设置了一个脏位,并且在卸载时清除了该位。当它被挂载并且已经设置了脏位时,将打印警告。

    Windows 通过在完成对笔式驱动器的写入时清除脏位并在开始新的写入时重新设置来解决此问题。据我所知,Linux 没有类似的功能。您可以尝试在内核中实现它。

    【讨论】:

      猜你喜欢
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 2016-04-20
      相关资源
      最近更新 更多