From 120f4b415120726d955d83561c18885db1639dc2 Mon Sep 17 00:00:00 2001 From: Mikhail Sandakov Date: Mon, 9 Dec 2024 11:01:34 +0200 Subject: [PATCH 1/2] Include a preliminary check to ensure no RAID devices are listed in the /etc/fstab file RAID devices might get renamed after the conversion, causing issues with mounting and potentially leading to boot problems for a converted host. --- pleskdistup/actions/__init__.py | 1 + pleskdistup/actions/common_checks.py | 24 +---------- pleskdistup/actions/mounts.py | 64 ++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 pleskdistup/actions/mounts.py diff --git a/pleskdistup/actions/__init__.py b/pleskdistup/actions/__init__.py index 0eeeb2c..d77f4a5 100644 --- a/pleskdistup/actions/__init__.py +++ b/pleskdistup/actions/__init__.py @@ -7,6 +7,7 @@ from .extensions import * from .grub import * from .mariadb import * +from .mounts import * from .packages import * from .plesk import * from .spamassassin import * diff --git a/pleskdistup/actions/common_checks.py b/pleskdistup/actions/common_checks.py index bf514b9..b023ffc 100644 --- a/pleskdistup/actions/common_checks.py +++ b/pleskdistup/actions/common_checks.py @@ -7,7 +7,7 @@ import typing from abc import abstractmethod -from pleskdistup.common import action, log, mounts, packages, php, plesk, version +from pleskdistup.common import action, log, packages, php, plesk, version # This action should be considered as deprecated @@ -677,25 +677,3 @@ def _do_check(self) -> bool: return True return False - - -class AssertFstabOrderingIsFine(action.CheckAction): - FSTAB_PATH: str = "/etc/fstab" - - def __init__(self): - self.name = "checking if /etc/fstab is ordered properly" - self.description = """The /etc/fstab file entries is not ordered properly. -\t- {}""" - - def _do_check(self) -> bool: - if not os.path.exists(self.FSTAB_PATH): - # Might be a problem, but it is not something we checking in scope of this check - return True - - misorderings = mounts.get_fstab_configuration_misorderings(self.FSTAB_PATH) - - if len(misorderings) == 0: - return True - - self.description = self.description.format("\n\t- ".join([f"Mount point {mount_point} should be placed after {parent_dir}" for parent_dir, mount_point in misorderings])) - return False diff --git a/pleskdistup/actions/mounts.py b/pleskdistup/actions/mounts.py new file mode 100644 index 0000000..4d4df1b --- /dev/null +++ b/pleskdistup/actions/mounts.py @@ -0,0 +1,64 @@ +# Copyright 2023-2024. WebPros International GmbH. All rights reserved. + +import os + +from pleskdistup.common import action, mounts + + +FSTAB_PATH: str = "/etc/fstab" + + +class AssertFstabOrderingIsFine(action.CheckAction): + def __init__(self): + self.name = "checking if /etc/fstab is ordered properly" + self.description = """The /etc/fstab file entries is not ordered properly. +\t- {}""" + + def _do_check(self) -> bool: + if not os.path.exists(FSTAB_PATH): + # Might be a problem, but it is not something we checking in scope of this check + return True + + misorderings = mounts.get_fstab_configuration_misorderings(FSTAB_PATH) + + if len(misorderings) == 0: + return True + + self.description = self.description.format("\n\t- ".join([f"Mount point {mount_point} should be placed after {parent_dir}" for parent_dir, mount_point in misorderings])) + return False + + +class AssertFstabHasDirectRaidDevices(action.CheckAction): + allow_raid_devices: bool + + def __init__(self, allow_raid_devices: bool = False): + self.allow_raid_devices = allow_raid_devices + self.name = "checking if /etc/fstab has direct RAID devices" + self.description = """The /etc/fstab file has direct RAID devices entries. +A RAID device could be renamed after the conversion which will lead to unable to mount the device. +Potentially it could make the system unbootable. +To fix the issue, replace following direct RAID devices with the UUID: +\t- {} + +Or you could skip the check by calling the tool with --allow-raid-devices option. +""" + + def _do_check(self) -> bool: + if self.allow_raid_devices: + return True + + if not os.path.exists(FSTAB_PATH): + # Might be a problem, but it is not something we checking in scope of this check + return True + + raid_devices_in_fstab = [] + with open(FSTAB_PATH, "r") as fstab_file: + for line in fstab_file.readlines(): + if line.startswith("/dev/md"): + raid_devices_in_fstab.append(line.rstrip()) + + if len(raid_devices_in_fstab) == 0: + return True + + self.description = self.description.format("\n\t- ".join(raid_devices_in_fstab)) + return False From 7153b92eed08774dd33b6ad7cf279df743680ef5 Mon Sep 17 00:00:00 2001 From: Mikhail Sandakov Date: Mon, 9 Dec 2024 13:56:37 +0200 Subject: [PATCH 2/2] Avoid infinite loop when mount point not started with "/" --- pleskdistup/common/src/mounts.py | 2 +- pleskdistup/common/tests/mountstests.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pleskdistup/common/src/mounts.py b/pleskdistup/common/src/mounts.py index 86b22f7..b4451b0 100644 --- a/pleskdistup/common/src/mounts.py +++ b/pleskdistup/common/src/mounts.py @@ -32,7 +32,7 @@ def get_fstab_configuration_misorderings(configpath: str) -> typing.List[typing. misorderings: typing.List[typing.Tuple[str, str]] = [] for mount_point in mount_points_order.keys(): - if mount_point == "/": + if mount_point == "/" or not mount_point.startswith("/"): continue parent_dir: str = mount_point diff --git a/pleskdistup/common/tests/mountstests.py b/pleskdistup/common/tests/mountstests.py index ea6aabc..a0d60ff 100644 --- a/pleskdistup/common/tests/mountstests.py +++ b/pleskdistup/common/tests/mountstests.py @@ -81,3 +81,12 @@ def test_file_without_root(self): f.write("devpts /dev/pts devpts gid=5,mode=620 0 0\n") f.write("tmpfs /dev/shm tmpfs defaults 0 0\n") self.assertEqual(mounts.get_fstab_configuration_misorderings(self.test_file_path), [("/home", "/home/test")]) + + def test_mount_point_is_swap(self): + with open(self.test_file_path, "w") as f: + f.write("# comment\n") + f.write("/dev/sda2 /var ext4 defaults 0 1\n") + f.write("/dev/sda5 swap swap defaults 0 1\n") + f.write("/dev/sda4 /home ext4 defaults 0 1\n") + + self.assertEqual(mounts.get_fstab_configuration_misorderings(self.test_file_path), [])