diff --git a/scripts/doctor-k3s-storage-host.py b/scripts/doctor-k3s-storage-host.py index f13a77e..e26dd9f 100644 --- a/scripts/doctor-k3s-storage-host.py +++ b/scripts/doctor-k3s-storage-host.py @@ -30,20 +30,23 @@ def mount(target): def disks(): - data = json.loads(run(["lsblk", "-J", "-o", "NAME,TYPE,MAJ:MIN,FSTYPE,UUID,MOUNTPOINTS,TRAN"])) + # Bullseye's lsblk lacks MOUNTPOINTS. findmnt below checks every mount, + # since the older singular MOUNTPOINT column may show only one of them. + data = json.loads(run(["lsblk", "-J", "-o", "NAME,TYPE,MAJ:MIN,FSTYPE,UUID,MOUNTPOINT,TRAN"])) return data.get("blockdevices") or [] def inspect(): data, runtime = mount(DATA_MOUNT), mount("/var/lib/rancher/k3s") devices = disks() + mounted_devices = set(run(["findmnt", "-rn", "-o", "MAJ:MIN"]).split()) present = set() candidates = [] def visit(device, transport=None): transport = device.get("tran") or transport present.add(device.get("maj:min")) if (device.get("type") == "part" and transport == "usb" and device.get("fstype") == "ext4" - and device.get("uuid") and not any(device.get("mountpoints") or [])): + and device.get("uuid") and not device.get("mountpoint") and device.get("maj:min") not in mounted_devices): candidates.append({"name": device.get("name"), "uuid": device["uuid"], "maj:min": device.get("maj:min")}) for child in device.get("children") or []: visit(child, transport) diff --git a/tests/jeannie/test_doctor_k3s_storage.py b/tests/jeannie/test_doctor_k3s_storage.py index 8a5969a..5467a1e 100644 --- a/tests/jeannie/test_doctor_k3s_storage.py +++ b/tests/jeannie/test_doctor_k3s_storage.py @@ -23,14 +23,24 @@ class StorageDoctorTests(unittest.TestCase): stale = {"target": "/data", "source": "/dev/sda1", "maj:min": "8:1", "options": "ro,relatime"} runtime = {"target": "/var/lib/rancher/k3s", "maj:min": "8:1", "options": "ro,relatime"} devices = [{"name": "sdb", "type": "disk", "maj:min": "8:16", "tran": "usb", "children": [ - {"name": "sdb1", "type": "part", "maj:min": "8:17", "fstype": "ext4", "uuid": "uuid", "mountpoints": [None]}]}] + {"name": "sdb1", "type": "part", "maj:min": "8:17", "fstype": "ext4", "uuid": "uuid", "mountpoint": None}]}] with mock.patch.object(host, "mount", side_effect=[stale, runtime]), \ mock.patch.object(host, "disks", return_value=devices), \ - mock.patch.object(host, "run", side_effect=["failed", "EXT4-fs error on sda1"]): + mock.patch.object(host, "run", side_effect=["8:1\n8:2", "failed", "EXT4-fs error on sda1"]): result = host.inspect() self.assertTrue(result["stale_readonly_mount"]) self.assertEqual(result["reappeared_usb_ext4"][0]["maj:min"], "8:17") + def test_candidate_mounted_elsewhere_is_not_selected(self): + stale = {"maj:min": "8:1", "options": "ro,relatime"} + devices = [{"name": "sdb", "type": "disk", "maj:min": "8:16", "tran": "usb", "children": [ + {"name": "sdb1", "type": "part", "maj:min": "8:17", "fstype": "ext4", "uuid": "uuid", "mountpoint": None}]}] + with mock.patch.object(host, "mount", side_effect=[stale, stale]), \ + mock.patch.object(host, "disks", return_value=devices), \ + mock.patch.object(host, "run", side_effect=["8:1\n8:17", "failed", ""]): + result = host.inspect() + self.assertEqual(result["reappeared_usb_ext4"], []) + def test_ambiguous_or_running_service_never_changes_mounts(self): for evidence in ({"stale_readonly_mount": True, "service": "active", "reappeared_usb_ext4": [{"uuid": UUID}]}, {"stale_readonly_mount": True, "service": "failed", "reappeared_usb_ext4": [{"uuid": UUID}, {"uuid": "y"}]},