From e12c1c82c26bc67f887c4324066af002b30119ad Mon Sep 17 00:00:00 2001 From: Renuka Manavalan <47282725+renukamanavalan@users.noreply.github.com> Date: Mon, 23 Aug 2021 20:50:21 -0700 Subject: [PATCH] disk_check: Script updated to run good in 201811 & 201911 (#1747) What I did Have independent subdirs for each mounted dir to avoid any collisions of files/dirs by same name. Adopt for older version of python3 How I did it Changes: Individual subdirs for each dir to be mounted subprocess args made compatible with older version of python3 (tested in version 3.5.3) How to verify it Simulate read-only state Run this script Test ssh via new tacacs user (who had not logged in earlier) --- scripts/disk_check.py | 19 +++++++++++++++---- tests/disk_check_test.py | 8 ++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) 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)