Adding support for mounting NTFS drives in the Controller. (#440)

This commit is contained in:
lrnselfreliance 2026-05-15 19:25:43 -06:00 • committed by GitHub
parent 09e9af531a
commit 8f0e4cd93f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 164 additions and 13 deletions

View file

@ -18,7 +18,7 @@ from controller.lib.config import is_docker_mode
logger = logging.getLogger(__name__)
SUPPORTED_FILESYSTEMS = {"ext4", "btrfs", "vfat", "exfat"}
SUPPORTED_FILESYSTEMS = {"ext4", "btrfs", "vfat", "exfat", "ntfs", "ntfs3"}
def get_wrolpi_uid_gid() -> tuple[int, int]:
@ -171,9 +171,10 @@ def mount_drive(
# Create mount point if needed
Path(mount_point).mkdir(parents=True, exist_ok=True)
# For exfat/vfat, add uid/gid options to set ownership to wrolpi user
# These filesystems don't support POSIX permissions natively
if fstype in ("exfat", "vfat"):
# For filesystems without native POSIX permissions, set ownership to
# the wrolpi user via uid/gid mount options. Covers FAT family and
# both NTFS drivers (ntfs-3g FUSE and the in-kernel ntfs3).
if fstype in ("exfat", "vfat", "ntfs", "ntfs3"):
uid, gid = get_wrolpi_uid_gid()
options = f"{options},uid={uid},gid={gid}"
@ -281,6 +282,13 @@ def check_shadowed_data(target: str) -> Optional[dict]:
for entry in entries:
entry_path = target_path / entry
try:
# Skip symlinks: a symlink-to-directory at the mount target would
# otherwise be walked, inflating size_bytes with content from
# outside the would-be-shadowed area (e.g. /etc, /). The symlink
# itself is still listed in `entries` so the user is warned about
# it; it just contributes 0 bytes.
if entry_path.is_symlink():
continue
if entry_path.is_dir():
for path in entry_path.rglob("*"):
try:
@ -289,7 +297,7 @@ def check_shadowed_data(target: str) -> Optional[dict]:
except OSError:
# File disappeared mid-walk or unreadable; skip.
continue
elif entry_path.is_file() and not entry_path.is_symlink():
elif entry_path.is_file():
size_bytes += entry_path.stat().st_size
except OSError:
continue

View file

@ -107,7 +107,7 @@ class TestGetBlockDevices:
"type": "disk",
"children": [
{"name": "sda1", "path": "/dev/sda1", "size": "100G", "type": "part", "fstype": "ext4"},
{"name": "sda2", "path": "/dev/sda2", "size": "100G", "type": "part", "fstype": "ntfs"},
{"name": "sda2", "path": "/dev/sda2", "size": "100G", "type": "part", "fstype": "xfs"},
]
}
]
@ -318,6 +318,36 @@ class TestMountDrive:
assert "uid=1001" in options
assert "gid=1001" in options
def test_ntfs_mount_includes_uid_gid(self):
"""Should add uid/gid options when mounting ntfs (Windows-formatted drive)."""
with mock.patch("controller.lib.disks.is_docker_mode", return_value=False):
with mock.patch("controller.lib.disks.get_wrolpi_uid_gid", return_value=(1001, 1001)):
with mock.patch("pathlib.Path.mkdir"):
with mock.patch("subprocess.run") as mock_run:
mock_run.return_value = mock.Mock(returncode=0, stdout="", stderr="")
result = mount_drive("/dev/sda1", "/media/test", fstype="ntfs")
assert result["success"] is True
call_args = mock_run.call_args[0][0]
options_idx = call_args.index("-o") + 1
options = call_args[options_idx]
assert "uid=1001" in options
assert "gid=1001" in options
def test_ntfs3_mount_includes_uid_gid(self):
"""Same handling for the in-kernel ntfs3 driver (kernel 5.15+)."""
with mock.patch("controller.lib.disks.is_docker_mode", return_value=False):
with mock.patch("controller.lib.disks.get_wrolpi_uid_gid", return_value=(1001, 1001)):
with mock.patch("pathlib.Path.mkdir"):
with mock.patch("subprocess.run") as mock_run:
mock_run.return_value = mock.Mock(returncode=0, stdout="", stderr="")
result = mount_drive("/dev/sda1", "/media/test", fstype="ntfs3")
assert result["success"] is True
call_args = mock_run.call_args[0][0]
options_idx = call_args.index("-o") + 1
options = call_args[options_idx]
assert "uid=1001" in options
assert "gid=1001" in options
class TestGetWrolpiUidGid:
"""Tests for get_wrolpi_uid_gid function."""
@ -457,4 +487,43 @@ class TestCheckShadowedData:
assert result["size_bytes"] == 1000
assert result["entries"] == ["videos", "zims"]
def test_does_not_follow_symlinks_to_directories(self, tmp_path):
"""A symlink at the mount target must not have its target walked.
Regression: previously a symlink-to-dir was detected by is_dir()
(which follows symlinks), then rglob walked the target — so
`ln -s /etc /media/wrolpi/x` would sum the size of /etc.
"""
outside = tmp_path / "outside"
outside.mkdir()
# A few large files outside the mount target.
(outside / "a").write_bytes(b"x" * 10_000)
(outside / "b").write_bytes(b"x" * 20_000)
target = tmp_path / "wrolpi"
target.mkdir()
(target / "videos").mkdir()
(target / "videos" / "movie.mp4").write_bytes(b"x" * 100)
# Symlink pointing outside the mount target.
(target / "external").symlink_to(outside)
result = check_shadowed_data(str(target))
# `external` is listed (the user should know it's there) but it
# contributes 0 to the size — only the real `videos` data counts.
assert result["entries"] == ["external", "videos"]
assert result["size_bytes"] == 100
def test_does_not_follow_symlinks_to_files(self, tmp_path):
"""A symlink-to-file is listed but contributes 0 bytes."""
outside_file = tmp_path / "big.bin"
outside_file.write_bytes(b"x" * 50_000)
target = tmp_path / "wrolpi"
target.mkdir()
(target / "link.bin").symlink_to(outside_file)
result = check_shadowed_data(str(target))
assert result["entries"] == ["link.bin"]
assert result["size_bytes"] == 0