From 04958e7adc4b6344d008bc692a56612a9a08d991 Mon Sep 17 00:00:00 2001 From: Lunfan Zhang Date: Mon, 11 Dec 2023 16:36:29 +0800 Subject: [PATCH] CP-46863 Dump Multipath Status from Storage Manager Signed-off-by: Lunfan Zhang --- drivers/mpathcount.py | 20 +++++++++++++++----- systemd/mpathcount.service | 1 + tests/test_mpathcount.py | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/drivers/mpathcount.py b/drivers/mpathcount.py index 601dde782..d91052860 100755 --- a/drivers/mpathcount.py +++ b/drivers/mpathcount.py @@ -21,6 +21,7 @@ import re import xs_errors import mpath_cli +import json supported = ['iscsi', 'lvmoiscsi', 'rawhba', 'lvmohba', 'ocfsohba', 'ocfsoiscsi', 'netapp', 'lvmofcoe', 'gfs2'] @@ -29,6 +30,8 @@ LOCK_NS2 = "mpathcount2" MAPPER_DIR = "/dev/mapper" +MPATHS_DIR = "/dev/shm" +MPATH_FILE_NAME = "/dev/shm/mpath_status" match_bySCSIid = False mpath_enabled = True SCSIid = 'NOTSUPPLIED' @@ -113,7 +116,8 @@ def get_root_dev_major(): # @entry: string representing previous value # @remove: callback to remove key # @add: callback to add key/value pair -def update_config(key, SCSIid, entry, remove, add): +# @mpath_status: map to record multipath status +def update_config(key, SCSIid, entry, remove, add, mpath_status=None): path = os.path.join(MAPPER_DIR, SCSIid) util.SMlog("MPATH: Updating entry for [%s], current: %s" % (SCSIid, entry)) if os.path.exists(path): @@ -136,6 +140,8 @@ def update_config(key, SCSIid, entry, remove, add): add('multipathed', 'true') add(key, str(newentry)) util.SMlog("MPATH: Set val: %s" % str(newentry)) + if mpath_status != None: + mpath_status.update({str(key): f"{count}/{max}"}) else: util.SMlog('MPATH: device %s gone' % (SCSIid)) remove('multipathed') @@ -175,7 +181,7 @@ def check_root_disk(config, maps, remove, add): update_config(key, i, config[key], remove, add) -def check_devconfig(devconfig, sm_config, config, remove, add): +def check_devconfig(devconfig, sm_config, config, remove, add, mpath_status=None): SCSIidlist = get_SCSIidlist(devconfig, sm_config) if not len(SCSIidlist): return @@ -189,9 +195,9 @@ def check_devconfig(devconfig, sm_config, config, remove, add): remove('multipathed') else: if key not in config: - update_config(key, i, "", remove, add) + update_config(key, i, "", remove, add, mpath_status) else: - update_config(key, i, config[key], remove, add) + update_config(key, i, config[key], remove, add, mpath_status) if __name__ == '__main__': try: @@ -232,6 +238,7 @@ def _add(key, val): mpc_exit(session, -1) try: + mpath_status = {} for pbd in pbds: def remove(key): session.xenapi.PBD.remove_from_other_config(pbd, key) @@ -246,7 +253,10 @@ def add(key, val): if srtype in supported: devconfig = record["device_config"] sm_config = session.xenapi.SR.get_sm_config(SR) - check_devconfig(devconfig, sm_config, config, remove, add) + check_devconfig(devconfig, sm_config, config, remove, add, mpath_status) + mpath_status = mpath_status if mpath_enabled else {} + util.atomicFileWrite(MPATH_FILE_NAME, MPATHS_DIR, json.dumps(mpath_status)) + os.chmod(MPATH_FILE_NAME, 0o0644) except: util.SMlog("MPATH: Failure updating db. %s" % sys.exc_info()) mpc_exit(session, -1) diff --git a/systemd/mpathcount.service b/systemd/mpathcount.service index 6a108dce7..1327cf1a8 100644 --- a/systemd/mpathcount.service +++ b/systemd/mpathcount.service @@ -7,5 +7,6 @@ After=xapi-init-complete.target StandardInput=socket StandardOutput=null StandardError=journal +ExecStartPre=/bin/bash -c 'if [ ! -f "/dev/shm/mpath_status" ]; then /opt/xensource/sm/mpathcount.py; fi' ExecStart=/usr/bin/sh -c '. /etc/xensource-inventory; while dd of=/dev/null bs=4096 count=1 status=none conv=noerror; do /opt/xensource/sm/mpathcount.py; done' Restart=always diff --git a/tests/test_mpathcount.py b/tests/test_mpathcount.py index 6c528085d..72bcae78b 100644 --- a/tests/test_mpathcount.py +++ b/tests/test_mpathcount.py @@ -112,7 +112,7 @@ def remove(key): print("del {}".format(key)) del store[key] - def fake_update_config(k, s, v, a, t): + def fake_update_config(k, s, v, a, t, p): store[k] = v update_config.side_effect = fake_update_config