diff --git a/scripts/disk_check.py b/scripts/disk_check.py index b13f1335f0..4fa8d69746 100644 --- a/scripts/disk_check.py +++ b/scripts/disk_check.py @@ -21,6 +21,11 @@ Monit may be used to invoke it periodically, to help scan & fix and report via syslog. +Tidbit: + If you would like to test this script, you could simulate a RO disk + with the following command. Reboot will revert the effect. + sudo bash -c "echo u > /proc/sysrq-trigger" + """ import argparse @@ -64,7 +69,7 @@ def test_writable(dirs): def run_cmd(cmd): - proc = subprocess.run(cmd, shell=True, text=True, capture_output=True) + proc = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE) ret = proc.returncode if ret: log_err("failed: ret={} cmd={}".format(ret, cmd)) @@ -72,9 +77,9 @@ def run_cmd(cmd): log_info("ret={} cmd: {}".format(ret, cmd)) if proc.stdout: - log_info("stdout: {}".format(str(proc.stdout))) + log_info("stdout: {}".format(proc.stdout.decode("utf-8"))) if proc.stderr: - log_info("stderr: {}".format(str(proc.stderr))) + log_info("stderr: {}".format(proc.stderr.decode("utf-8"))) return ret @@ -95,9 +100,15 @@ def do_mnt(dirs): return 1 for d in dirs: + d_name = get_dname(d) + d_upper = os.path.join(UPPER_DIR, d_name) + d_work = os.path.join(WORK_DIR, d_name) + os.mkdir(d_upper) + os.mkdir(d_work) + ret = run_cmd("mount -t overlay overlay_{} -o lowerdir={}," "upperdir={},workdir={} {}".format( - get_dname(d), d, UPPER_DIR, WORK_DIR, d)) + d_name, d, d_upper, d_work, d)) if ret: break diff --git a/tests/disk_check_test.py b/tests/disk_check_test.py index b5ad7a489c..ce4faad900 100644 --- a/tests/disk_check_test.py +++ b/tests/disk_check_test.py @@ -2,6 +2,7 @@ import syslog from unittest.mock import patch import pytest +import subprocess sys.path.append("scripts") import disk_check @@ -26,7 +27,7 @@ "workdir": "/tmp/tmpy", "mounts": "overlay_tmpx blahblah", "err": "/tmpx is not read-write|READ-ONLY: Mounted ['/tmpx'] to make Read-Write", - "cmds": ['mount -t overlay overlay_tmpx -o lowerdir=/tmpx,upperdir=/tmp/tmpx,workdir=/tmp/tmpy /tmpx'] + "cmds": ['mount -t overlay overlay_tmpx -o lowerdir=/tmpx,upperdir=/tmp/tmpx/tmpx,workdir=/tmp/tmpy/tmpx /tmpx'] }, "3": { "desc": "Not good as /tmpx is not read-write; mount fail as create of upper fails", @@ -90,9 +91,12 @@ def __init__(self, proc_upd = None): self.stderr = proc_upd.get("stderr", None) -def mock_subproc_run(cmd, shell, text, capture_output): +def mock_subproc_run(cmd, shell, stdout): global cmds + assert shell == True + assert stdout == subprocess.PIPE + upd = (current_tc["proc"][len(cmds)] if len(current_tc.get("proc", [])) > len(cmds) else None) cmds.append(cmd)