-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from plesk/check-fstab
Add a preliminary checker to ensure the mount points in /etc/fstab are in the correct order
- Loading branch information
Showing
4 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright 2023-2024. WebPros International GmbH. All rights reserved. | ||
import os | ||
import typing | ||
|
||
|
||
def get_fstab_configuration_misorderings(configpath: str) -> typing.List[typing.Tuple[str, str]]: | ||
""" | ||
Analyzes the fstab configuration file to find misorderings in mount points. | ||
This function reads the fstab configuration file specified by `configpath` and checks for any misorderings | ||
in the mount points. A misordering is defined as a mount point that appears before its parent directory | ||
in the fstab file. | ||
Args: | ||
configpath (str): The path to the fstab configuration file. | ||
Returns: | ||
List[Tuple[str, str]]: A list of tuples where each tuple contains a misordered parent directory and | ||
its corresponding mount point. | ||
Example: | ||
>>> get_fstab_configuration_misorderings('/etc/fstab') | ||
[('/home', '/home/user'), ('/var', '/var/log')] | ||
""" | ||
|
||
if not os.path.exists(configpath): | ||
return [] | ||
|
||
mount_points_order: typing.Dict[str, int] = {} | ||
with open(configpath, "r") as f: | ||
for iter, line in enumerate(f.readlines()): | ||
if line.startswith("#") or line == "\n" or line == "": | ||
continue | ||
mount_point = line.split()[1] | ||
mount_points_order[mount_point] = iter | ||
|
||
misorderings: typing.List[typing.Tuple[str, str]] = [] | ||
for mount_point in mount_points_order.keys(): | ||
if mount_point == "/": | ||
continue | ||
|
||
parent_dir: str = mount_point | ||
root_found: bool = False | ||
|
||
while not root_found: | ||
parent_dir = os.path.dirname(parent_dir) | ||
if parent_dir in mount_points_order and mount_points_order[parent_dir] > mount_points_order[mount_point]: | ||
misorderings.append((parent_dir, mount_point)) | ||
if parent_dir == "/": | ||
root_found = True | ||
|
||
return misorderings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Copyright 2023-2024. WebPros International GmbH. All rights reserved. | ||
import unittest | ||
import tempfile | ||
|
||
import src.mounts as mounts | ||
|
||
|
||
class FstabMisorderingTests(unittest.TestCase): | ||
def setUp(self): | ||
self.test_file_path = tempfile.mktemp() | ||
|
||
def test_no_file(self): | ||
self.assertEqual(mounts.get_fstab_configuration_misorderings("noexist.txt"), []) | ||
|
||
def test_empty_file(self): | ||
with open(self.test_file_path, "w") as _: | ||
pass | ||
self.assertEqual(mounts.get_fstab_configuration_misorderings(self.test_file_path), []) | ||
|
||
def test_empty_string(self): | ||
with open(self.test_file_path, "w") as f: | ||
f.write("") | ||
self.assertEqual(mounts.get_fstab_configuration_misorderings(self.test_file_path), []) | ||
|
||
def test_one_mount_point(self): | ||
with open(self.test_file_path, "w") as f: | ||
f.write("# comment\n") | ||
f.write("/dev/sda1 / ext4 defaults 0 1\n") | ||
self.assertEqual(mounts.get_fstab_configuration_misorderings(self.test_file_path), []) | ||
|
||
def test_no_misorderings(self): | ||
with open(self.test_file_path, "w") as f: | ||
f.write("# comment\n") | ||
f.write("/dev/sda1 / ext4 defaults 0 1\n") | ||
f.write("/dev/sda2 /var ext4 defaults 0 1\n") | ||
f.write("proc /proc proc defaults 0 0\n") | ||
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), []) | ||
|
||
def test_one_misordering(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/sda1 / ext4 defaults 0 1\n") | ||
f.write("proc /proc proc defaults 0 0\n") | ||
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), [("/", "/var")]) | ||
|
||
def test_two_misorderings_for_one_parent(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/sda3 /var/log ext4 defaults 0 1\n") | ||
f.write("/dev/sda1 / ext4 defaults 0 1\n") | ||
f.write("proc /proc proc defaults 0 0\n") | ||
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), [("/", "/var"), ("/", "/var/log")]) | ||
|
||
def test_several_different_misorderings(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/sda1 / ext4 defaults 0 1\n") | ||
f.write("/dev/sda5 /home/test ext4 defaults 0 1\n") | ||
f.write("/dev/sda4 /home ext4 defaults 0 1\n") | ||
f.write("proc /proc proc defaults 0 0\n") | ||
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), [("/", "/var"), ("/home", "/home/test")]) | ||
|
||
def test_file_without_root(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 /home/test ext4 defaults 0 1\n") | ||
f.write("/dev/sda4 /home ext4 defaults 0 1\n") | ||
f.write("proc /proc proc defaults 0 0\n") | ||
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")]) |