简体   繁体   English

Ubuntu:如果由于错误= remount-ro而挂载/只读,则重启

[英]Ubuntu: reboot if / mounted readonly due to errors=remount-ro

Problem: We generally face a problem where ubuntu os gets mounted readonly. 问题:我们通常会遇到ubuntu操作系统以只读方式挂载的问题。 Reason is clear as mentioned in fstab on errors=remount-ro. 如fstab中关于errors = remount-ro所述,原因很明显。

Question: Is there any mechanism to reboot the appliance if it comes to readonly mounted state. 问:如果处于只读挂载状态,是否有任何机制可以重新启动设备。

Tried: I tried to write a script as below which will get monitored by watchdog. 尝试:我尝试编写如下脚本,该脚本将由看门狗监控。 This works but it continuously reboot if script return exit 1 due to any mount point is still readonly. 这可以工作,但是如果由于任何安装点导致脚本返回出口1仍然是只读的,它将连续重新启动。 What i expect is to check if uptime if less then a day then it should not reboot even though any mount point is readonly? 我期望的是检查是否正常运行时间是否少于一天,然后即使任何安装点都是只读的也不应重新启动?

root@ubuntu1404:/home/ubuntu# cat /rofscheck.sh

#!/bin/bash

now=`date`
echo "------------"
echo "start : ${now}"
up_time=`awk '{print int($1)}' /proc/uptime`

#if uptime is less than 1 day then skip test
if [ "$up_time" -lt 86400 ]; then
    echo "uptime is less ${now}, exit due to uptime"
    exit 0
fi
grep -q ' ro' /proc/mounts > /dev/null 2>&1 || exit 0

# alert watchdog that fs is readonly.
exit 1

Now in /etc/watchdog.conf below config is done. 现在在/etc/watchdog.conf中完成配置。

test-binary = /rofscheck.sh

To reproduce the problem to mount readonly all mounted fs, ran this: 要重现该问题以只读方式装入所有已装入的fs,请运行以下命令:

$ echo u > /proc/sysrq-trigger $ echo u > /proc/sysrq-trigger

which does emergency remount readonly. 紧急重新挂载为只读。

this script worked for me, even if it is much similar to yours. 即使它与您的脚本非常相似,该脚本也对我有用。 I am running it on Ubuntu 14.04 64bit with latest updates. 我正在最新更新的Ubuntu 14.04 64bit上运行它。

#!/bin/bash

now=$(date)
up_time=$(awk '{print int($1)}' /proc/uptime)
min_time=7200

#if uptime is less than 2 hours then skip test
if [ ${up_time} -lt ${min_time} ]; then
    echo "uptime is ${up_time} secs, less than ${min_time} secs (now is ${now}): exit 0 due to uptime"
    exit 0
fi

exit=$(grep ' ro,' /proc/mounts | wc -l)
if [ ${exit} -gt 0 ]; then
        exit 1
else
        exit 0
fi

Please note that I have setup the minimum time as a variable, adjust it at your convenience. 请注意,我已将最短时间设置为变量,请在方便时进行调整。

A small important notice: I am using this solution on a very cheap couple of cloud servers I own that I use for testing purposes only. 一条重要的重要通知:我正在自己拥有的两台非常便宜的云服务器上使用此解决方案,仅用于测试目的。 I have also setup filesystem check at every reboot with the following command: 我还在每次重新启动时使用以下命令设置了文件系统检查:

tune2fs -c 1 /dev/sda1

I would never use this kind of watchdog usage in a production environment. 我永远不会在生产环境中使用这种看门狗用法。

Hope this helps. 希望这可以帮助。 Best regards. 最好的祝福。

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

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