使用 USB 上的 root 权限暂停

使用 USB 上的 root 权限暂停

我有一台笔记本电脑,它从 USB 存储上的根文件系统运行 Ubuntu 14.04。这不太好用,因为从挂起状态唤醒后,ext4 会在 USB 准备就绪之前频繁尝试写入根文件系统。

当这种情况发生时,我在内核日志中看到以下内容,请注意我如何收到一堆 I/O 错误sda1,然后一秒钟后内核终于检测到 USB 存储驱动器。

[ 2826.517419] wlan0: associated
[ 2826.517452] IPv6: ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready
[ 2827.575371] EXT4-fs warning (device sda1): ext4_end_bio:317: I/O error -5 writing to inode 1733735 (offset 0 size 0 starting block 12629950)
[ 2827.575380] Buffer I/O error on device sda1, logical block 12629694
[ 2827.575400] EXT4-fs warning (device sda1): ext4_end_bio:317: I/O error -5 writing to inode 3148603 (offset 0 size 8192 starting block 12844470)
[ 2827.575404] Buffer I/O error on device sda1, logical block 12844212
[ 2827.575411] Buffer I/O error on device sda1, logical block 12844213
[ 2827.575448] EXT4-fs warning (device sda1): ext4_end_bio:317: I/O error -5 writing to inode 3015015 (offset 0 size 90112 starting block 6588832)
[ 2827.575453] Buffer I/O error on device sda1, logical block 6588576
[ 2827.575461] Buffer I/O error on device sda1, logical block 6588577
[ 2827.575465] Buffer I/O error on device sda1, logical block 6588578
[ 2827.575469] Buffer I/O error on device sda1, logical block 6588579
[ 2827.575473] Buffer I/O error on device sda1, logical block 6588580
[ 2827.575477] Buffer I/O error on device sda1, logical block 6588581
[ 2827.575481] Buffer I/O error on device sda1, logical block 6588582
[ 2828.857284] sd 0:0:0:0: [sda] No Caching mode page found
[ 2828.857293] sd 0:0:0:0: [sda] Assuming drive cache: write through

起初,内核日志之外没有任何可见迹象表明问题已触发,但如果我让 Ubuntu 继续运行超过这个时间点,那么文件系统将出现错误并最终切换到只读模式。此时,我必须重新启动进入恢复模式并fsck.ext4从 root shell 手动运行才能修复文件系统。

我可以更改某些设置吗,以便从挂起状态唤醒后对根设备的访问可以延迟到 USB 驱动器准备就绪?

答案1

该问题仅出现在 USB 设备上而在其他设备上不出现的原因是以下两个因素的结合:

  • USB 存储与其他存储介质不同,它依靠内核线程进行操作。
  • 从挂起状态恢复时,内核会同时唤醒所有线程。

结果是,在恢复期间,USB 系统一方面试图检测媒体,另一方面试图将挂起和恢复过程中的日志消息写入磁盘,两者之间将发生竞争。

如果 syslog 在检测到 USB 设备之前尝试写入,ext4 就会出现错误,由于某种原因无法干净地处理,最终文件系统将需要手动运行 fsck。

我发现的解决方案是让内核线程在唤醒其他线程之前提前 12 秒启动。为了实现这个功能,我必须对内核进行以下更改:

--- linux-3.13.0/kernel/power/suspend.c.orig    2014-01-20 03:40:07.000000000 +0100
+++ linux-3.13.0/kernel/power/suspend.c 2014-08-04 00:57:43.847038640 +0200
@@ -299,6 +299,8 @@
        goto Resume_devices;
 }

+unsigned int resume_delay = 0;
+
 /**
  * suspend_finish - Clean up before finishing the suspend sequence.
  *
@@ -307,6 +309,15 @@
  */
 static void suspend_finish(void)
 {
+       if (resume_delay) {
+               /* Give kernel threads a head start, such that usb-storage
+                * can detect devices before syslog attempts to write log
+                * messages from the suspend code.
+                */
+               thaw_kernel_threads();
+               pr_debug("PM: Sleeping for %d milliseconds.\n", resume_delay);
+               msleep(resume_delay);
+       }
        suspend_thaw_processes();
        pm_notifier_call_chain(PM_POST_SUSPEND);
        pm_restore_console();
--- linux-3.13.0/kernel/sysctl.c.orig   2014-08-04 08:11:26.000000000 +0200
+++ linux-3.13.0/kernel/sysctl.c        2014-08-03 23:27:23.796278219 +0200
@@ -277,8 +277,17 @@
 static int max_extfrag_threshold = 1000;
 #endif

+extern unsigned int resume_delay;
+
 static struct ctl_table kern_table[] = {
        {
+               .procname       = "resume_delay",
+               .data           = &resume_delay,
+               .maxlen         = sizeof(unsigned int),
+               .mode           = 0644,
+               .proc_handler   = proc_dointvec,
+       },
+       {
                .procname       = "sched_child_runs_first",
                .data           = &sysctl_sched_child_runs_first,
                .maxlen         = sizeof(unsigned int),

相关内容