Skip to content

Commit

Permalink
disk_check: Script updated to run good in 201811 & 201911 (sonic-net#…
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
renukamanavalan authored Aug 24, 2021
1 parent 27502f0 commit e12c1c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
19 changes: 15 additions & 4 deletions scripts/disk_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,17 +69,17 @@ 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))
else:
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


Expand All @@ -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

Expand Down
8 changes: 6 additions & 2 deletions tests/disk_check_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import syslog
from unittest.mock import patch
import pytest
import subprocess

sys.path.append("scripts")
import disk_check
Expand All @@ -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",
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e12c1c8

Please sign in to comment.