Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: browse temporary Linux mounts under /media and /mnt (#10048)

Sourav Rajvi42b7de3fb9ea
diff --git a/studio/backend/hub/services/models/folder_browser.py b/studio/backend/hub/services/models/folder_browser.py
index 2e17a6659ad..4c34f6bb17b 100644
--- a/studio/backend/hub/services/models/folder_browser.py
+++ b/studio/backend/hub/services/models/folder_browser.py
@@ -24,7 +24,7 @@ from hub.utils.paths import (
well_known_model_dirs,
)
from utils.paths.external_media import (
- linux_run_media_mount_roots,
+ linux_external_mount_roots,
macos_volume_roots,
windows_drive_roots,
)
@@ -61,7 +61,7 @@ def _build_browse_allowlist(
_add(Path.home())
if media_roots is None:
- media_roots = [*linux_run_media_mount_roots(), *macos_volume_roots()]
+ media_roots = [*linux_external_mount_roots(), *macos_volume_roots()]
if drive_roots is None:
drive_roots = windows_drive_roots()
for p in media_roots:
diff --git a/studio/backend/hub/tests/test_model_services.py b/studio/backend/hub/tests/test_model_services.py
index 53e52090433..c9e925d0f4e 100644
--- a/studio/backend/hub/tests/test_model_services.py
+++ b/studio/backend/hub/tests/test_model_services.py
@@ -1942,7 +1942,7 @@ def test_browse_allowlist_includes_linux_run_media_mounts(monkeypatch, tmp_path)
home.mkdir()
model_dir.mkdir(parents = True)
monkeypatch.setattr(folder_browser.Path, "home", lambda: home)
- monkeypatch.setattr(folder_browser, "linux_run_media_mount_roots", lambda: [media_root])
+ monkeypatch.setattr(folder_browser, "linux_external_mount_roots", lambda: [media_root])
monkeypatch.setattr(folder_browser, "_resolve_hf_cache_dir", lambda: tmp_path / "missing-hf")
monkeypatch.setattr(scan_folders, "list_scan_folders", lambda: [])
monkeypatch.setattr(folder_browser, "well_known_model_dirs", lambda: [])
diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.py
index 51eed6ad180..cbc30a6dd5b 100644
--- a/studio/backend/routes/models.py
+++ b/studio/backend/routes/models.py
@@ -1553,7 +1553,7 @@ def _build_browse_allowlist(
_add(Path.home())
if media_roots is None:
media_roots = [
- *external_media.linux_run_media_mount_roots(),
+ *external_media.linux_external_mount_roots(),
*external_media.macos_volume_roots(),
]
if drive_roots is None:
@@ -1839,7 +1839,7 @@ def browse_folders(
[]
if managed
else [
- *external_media.linux_run_media_mount_roots(),
+ *external_media.linux_external_mount_roots(),
*external_media.macos_volume_roots(),
]
)
diff --git a/studio/backend/storage/studio_db.py b/studio/backend/storage/studio_db.py
index 0a46d7b49e1..1e0df810c53 100644
--- a/studio/backend/storage/studio_db.py
+++ b/studio/backend/storage/studio_db.py
@@ -36,6 +36,7 @@ from utils.paths import (
studio_db_path,
)
from utils.paths.external_media import is_linux_run_media_path, is_local_filesystem_root
+from utils.paths import path_utils as _path_utils
from utils.paths.path_utils import macos_volume_ignores_case
from utils.paths.scan_folder_health import is_readable_dir
from utils.paths.sensitive import (
@@ -78,6 +79,23 @@ def _denied_path_prefixes() -> list[str]:
return []
+_WSL_WINDOWS_SYSTEM_DIRS = ("windows", "program files", "program files (x86)")
+
+
+def _is_wsl_windows_system_path(path: str) -> bool:
+ """``/mnt/<drive>/Windows`` and ``Program Files`` under WSL: the native-Windows denylist, case-folded like DrvFs."""
+ root = _path_utils._WSL_AUTOMOUNT_ROOT
+ if not _path_utils._IS_WSL or not path.startswith(root):
+ return False
+ parts = path[len(root) :].split("/")
+ return (
+ len(parts) >= 2
+ and len(parts[0]) == 1
+ and parts[0].isalpha()
+ and parts[1].casefold() in _WSL_WINDOWS_SYSTEM_DIRS
+ )
+
+
def is_denied_system_path(path: str) -> bool:
"""True if *path* is, or descends from, a denied system directory.
@@ -108,7 +126,7 @@ def is_denied_system_path(path: str) -> bool:
if prefix == "/run" and is_linux_run_media_path(check):
continue
return True
- return False
+ return system == "Linux" and _is_wsl_windows_system_path(path)
def _contains_sensitive_path_component(path: str) -> bool:
diff --git a/studio/backend/tests/test_linux_external_media_paths.py b/studio/backend/tests/test_linux_external_media_paths.py
index 5fec6e1118a..a9fa9683e60 100644
--- a/studio/backend/tests/test_linux_external_media_paths.py
+++ b/studio/backend/tests/test_linux_external_media_paths.py
@@ -141,6 +141,74 @@ def test_linux_run_media_mount_roots_skips_sensitive_resolved_descendant(monkeyp
assert roots == [normal_mount.resolve()]
+def test_linux_media_mount_roots_lists_user_and_legacy_volumes(monkeypatch, tmp_path):
+ base = tmp_path / "media"
+ user_mount = base / "dspofu" / "USB"
+ legacy_mount = base / "Seagate"
+ other_user_dir = base / "other"
+ other_user_mount = other_user_dir / "backup"
+ sensitive_mount = base / "dspofu" / ".ssh"
+ user_mount.mkdir(parents = True)
+ legacy_mount.mkdir()
+ other_user_mount.mkdir(parents = True)
+ sensitive_mount.mkdir()
+ monkeypatch.setattr(external_media.platform, "system", lambda: "Linux")
+
+ roots = external_media.linux_media_mount_roots(base, user = "dspofu")
+
+ assert user_mount.resolve() in roots
+ assert legacy_mount.resolve() in roots
+ assert other_user_dir.resolve() in roots
+ assert other_user_mount.resolve() not in roots
+ assert sensitive_mount.resolve() not in roots
+ assert (base / "dspofu").resolve() not in roots
+
+
+def test_linux_media_mount_roots_follows_symlink_into_run_media(monkeypatch, tmp_path):
+ run_media = tmp_path / "run" / "media" / "dspofu" / "nvmeB"
+ run_media.mkdir(parents = True)
+ base = tmp_path / "media"
+ user_dir = base / "dspofu"
+ user_dir.mkdir(parents = True)
+ alias = user_dir / "nvmeB"
+ alias.symlink_to(run_media, target_is_directory = True)
+ monkeypatch.setattr(external_media.platform, "system", lambda: "Linux")
+
+ roots = external_media.linux_media_mount_roots(
+ base, user = "dspofu", run_media = tmp_path / "run" / "media"
+ )
+
+ assert roots == [run_media.resolve()]
+
+
+def test_linux_mnt_mount_roots_lists_named_volumes(monkeypatch, tmp_path):
+ base = tmp_path / "mnt"
+ ssd = base / "ssd"
+ usb = base / "usb"
+ secret = base / ".ssh"
+ ssd.mkdir(parents = True)
+ usb.mkdir()
+ secret.mkdir()
+ monkeypatch.setattr(external_media.platform, "system", lambda: "Linux")
+
+ roots = external_media.linux_mnt_mount_roots(base)
+
+ assert {p.resolve() for p in roots} == {ssd.resolve(), usb.resolve()}
+
+
+def test_linux_external_mount_roots_dedupes_aliased_volumes(monkeypatch, tmp_path):
+ shared = tmp_path / "vol"
+ shared.mkdir()
+ extra = tmp_path / "mnt-vol"
+ extra.mkdir()
+ monkeypatch.setattr(external_media.platform, "system", lambda: "Linux")
+ monkeypatch.setattr(external_media, "linux_run_media_mount_roots", lambda: [shared])
+ monkeypatch.setattr(external_media, "linux_media_mount_roots", lambda: [shared])
+ monkeypatch.setattr(external_media, "linux_mnt_mount_roots", lambda: [extra])
+
+ assert external_media.linux_external_mount_roots() == [shared, extra]
+
+
def test_hub_scan_folder_accepts_linux_run_media_mount(monkeypatch):
_stub_linux_path_checks(monkeypatch, scan_folders)
monkeypatch.setattr(external_media.platform, "system", lambda: "Linux")
@@ -256,6 +324,7 @@ def test_legacy_browse_allowlist_includes_linux_run_media_mounts(monkeypatch, tm
)
fake_external_media = SimpleNamespace(
linux_run_media_mount_roots = lambda: [media_root],
+ linux_external_mount_roots = lambda: [media_root],
macos_volume_roots = lambda: [],
windows_drive_roots = lambda: [],
)
@@ -296,3 +365,49 @@ def test_legacy_browse_allowlist_includes_linux_run_media_mounts(monkeypatch, tm
with pytest.raises(_HTTPException) as exc_root:
ns["_resolve_browse_target"](str(ssh_root), [ssh_root])
assert exc_root.value.status_code == 403
+
+
+def test_readable_dirs_within_does_not_restart_a_stuck_probe(monkeypatch):
+ import threading
+
+ release = threading.Event()
+ calls: list[str] = []
+
+ def _blocking_isdir(path):
+ calls.append(path)
+ release.wait(5)
+ return True
+
+ monkeypatch.setattr(external_media.os.path, "isdir", _blocking_isdir)
+ try:
+ assert external_media._readable_dirs_within(["/mnt/stale"], 0.05) == set()
+ assert external_media._readable_dirs_within(["/mnt/stale"], 0.05) == set()
+ assert calls == ["/mnt/stale"]
+ finally:
+ release.set()
+
+
+ "path, denied",
+ [
+ ("/mnt/c/Windows/System32", True),
+ ("/mnt/c/program files", True),
+ ("/mnt/d/Program Files (x86)/x", True),
+ ("/mnt/c", False),
+ ("/mnt/d/models", False),
+ ("/mnt/ssd/Windows", False),
+ ],
+)
+def test_wsl_drive_mount_denies_windows_system_dirs(monkeypatch, path, denied):
+ monkeypatch.setattr(studio_db.platform, "system", lambda: "Linux")
+ monkeypatch.setattr(studio_db._path_utils, "_IS_WSL", True)
+ monkeypatch.setattr(studio_db._path_utils, "_WSL_AUTOMOUNT_ROOT", "/mnt/")
+
+ assert studio_db.is_denied_system_path(path) is denied
+
+
+def test_windows_system_dirs_under_mnt_allowed_outside_wsl(monkeypatch):
+ monkeypatch.setattr(studio_db.platform, "system", lambda: "Linux")
+ monkeypatch.setattr(studio_db._path_utils, "_IS_WSL", False)
+
+ assert studio_db.is_denied_system_path("/mnt/c/Windows") is False
diff --git a/studio/backend/tests/test_windows_external_drive_paths.py b/studio/backend/tests/test_windows_external_drive_paths.py
index 5687612916e..d0bc160cf3d 100644
--- a/studio/backend/tests/test_windows_external_drive_paths.py
+++ b/studio/backend/tests/test_windows_external_drive_paths.py
@@ -216,6 +216,7 @@ def test_browse_allowlist_includes_windows_drive_roots(monkeypatch, tmp_path):
)
fake_external_media = SimpleNamespace(
linux_run_media_mount_roots = lambda: [],
+ linux_external_mount_roots = lambda: [],
macos_volume_roots = lambda: [],
windows_drive_roots = lambda: [drive_root],
)
@@ -284,6 +285,7 @@ def test_build_browse_allowlist_reuses_passed_roots(monkeypatch, tmp_path):
)
fake_external_media = SimpleNamespace(
linux_run_media_mount_roots = _media_roots,
+ linux_external_mount_roots = _media_roots,
macos_volume_roots = lambda: [],
windows_drive_roots = _drive_roots,
)
diff --git a/studio/backend/utils/paths/external_media.py b/studio/backend/utils/paths/external_media.py
index a78c9670ce2..30d4c79a4eb 100644
--- a/studio/backend/utils/paths/external_media.py
+++ b/studio/backend/utils/paths/external_media.py
@@ -44,7 +44,12 @@ def is_local_filesystem_root(path: str, *, _pathmod = os.path) -> bool:
return drive[:2] not in ("\\\\", "//")
-def _is_linux_media_mount_path(path: str, media_root: Path | str) -> bool:
+def _is_linux_media_mount_path(
+ path: str,
+ media_root: Path | str,
+ *,
+ min_parts: int = 2,
+) -> bool:
normalized = os.path.normpath(os.path.realpath(os.path.expanduser(path)))
root = os.path.normpath(os.path.realpath(os.path.expanduser(str(media_root))))
try:
@@ -54,7 +59,7 @@ def _is_linux_media_mount_path(path: str, media_root: Path | str) -> bool:
if rel == "." or rel == ".." or rel.startswith(f"..{os.sep}"):
return False
parts = [part for part in rel.split(os.sep) if part]
- return len(parts) >= 2 and all(part not in (".", "..") for part in parts[:2])
+ return len(parts) >= min_parts and all(part not in (".", "..") for part in parts[:min_parts])
def is_linux_run_media_path(path: str) -> bool:
@@ -80,6 +85,36 @@ def _contains_sensitive_media_component(path: Path, media_root: Path) -> bool:
return contains_sensitive_path_component(str(rel))
+def _accept_linux_volume(
+ volume_dir: Path,
+ scanned_base: Path,
+ *,
+ min_parts: int,
+ seen: set[str],
+ alias_root: Path | str | None = None,
+) -> Path | None:
+ """*volume_dir*'s real path when it is a readable, non-sensitive mount root under *scanned_base* (or *alias_root*)."""
+ if is_sensitive_path_component(volume_dir.name):
+ return None
+ try:
+ resolved = volume_dir.resolve()
+ if not (resolved.is_dir() and os.access(resolved, os.R_OK | os.X_OK)):
+ return None
+ except (OSError, RuntimeError, ValueError):
+ return None
+ if not _is_linux_media_mount_path(str(resolved), scanned_base, min_parts = min_parts) and not (
+ alias_root is not None and _is_linux_media_mount_path(str(resolved), alias_root)
+ ):
+ return None
+ if _contains_sensitive_media_component(resolved, scanned_base):
+ return None
+ key = os.path.normcase(os.path.realpath(str(resolved)))
+ if key in seen:
+ return None
+ seen.add(key)
+ return resolved
+
+
def linux_run_media_mount_roots(
base: Path | str = "/run/media", *, user: str | None = None
) -> list[Path]:
@@ -105,26 +140,88 @@ def linux_run_media_mount_roots(
except (OSError, RuntimeError, ValueError):
return []
for volume_dir in volume_dirs:
- if is_sensitive_path_component(volume_dir.name):
+ accepted = _accept_linux_volume(volume_dir, resolved_base, min_parts = 2, seen = seen)
+ if accepted is not None:
+ roots.append(accepted)
+ return roots
+
+
+def linux_media_mount_roots(
+ base: Path | str = "/media",
+ *,
+ user: str | None = None,
+ run_media: Path | str = "/run/media",
+) -> list[Path]:
+ """Readable ``/media/<user>/<volume>`` and legacy ``/media/<volume>`` roots; entries may symlink into *run_media*."""
+ if platform.system() != "Linux":
+ return []
+ user = user or _current_username()
+ base_path = Path(base)
+ try:
+ resolved_base = base_path.resolve()
+ children = list(base_path.iterdir())
+ except (OSError, RuntimeError, ValueError):
+ return []
+
+ roots: list[Path] = []
+ seen: set[str] = set()
+ for child in children:
+ if is_sensitive_path_component(child.name):
continue
try:
- resolved = volume_dir.resolve()
+ if not child.is_dir():
+ continue
+ # The current user's folder is the parent of their mounts, not a volume.
+ volume_dirs = list(child.iterdir()) if user and child.name == user else [child]
except (OSError, RuntimeError, ValueError):
continue
- if not _is_linux_media_mount_path(str(resolved), resolved_base):
- continue
- if _contains_sensitive_media_component(resolved, resolved_base):
- continue
- key = os.path.normcase(os.path.realpath(str(resolved)))
- if key in seen:
- continue
- try:
- is_dir = resolved.is_dir()
- except OSError:
+ for volume_dir in volume_dirs:
+ accepted = _accept_linux_volume(
+ volume_dir, resolved_base, min_parts = 1, seen = seen, alias_root = run_media
+ )
+ if accepted is not None:
+ roots.append(accepted)
+ return roots
+
+
+def linux_mnt_mount_roots(base: Path | str = "/mnt") -> list[Path]:
+ """Readable ``/mnt/<name>`` roots; probed with a timeout so a stale network mount cannot stall the browser."""
+ if platform.system() != "Linux":
+ return []
+ base_path = Path(base)
+ try:
+ resolved_base = base_path.resolve()
+ children = list(base_path.iterdir())
+ except (OSError, RuntimeError, ValueError):
+ return []
+ readable = _readable_dirs_within(
+ [str(c) for c in children if not is_sensitive_path_component(c.name)],
+ _DRIVE_PROBE_TIMEOUT_S,
+ )
+ roots: list[Path] = []
+ seen: set[str] = set()
+ for child in children:
+ if str(child) not in readable:
continue
- if is_dir and os.access(resolved, os.R_OK | os.X_OK):
+ accepted = _accept_linux_volume(child, resolved_base, min_parts = 1, seen = seen)
+ if accepted is not None:
+ roots.append(accepted)
+ return roots
+
+
+def linux_external_mount_roots() -> list[Path]:
+ """``/run/media``, ``/media`` and ``/mnt`` roots, deduped by real path."""
+ roots: list[Path] = []
+ seen: set[str] = set()
+ for root in (
+ *linux_run_media_mount_roots(),
+ *linux_media_mount_roots(),
+ *linux_mnt_mount_roots(),
+ ):
+ key = os.path.normcase(os.path.realpath(str(root)))
+ if key not in seen:
seen.add(key)
- roots.append(resolved)
+ roots.append(root)
return roots
@@ -162,6 +259,8 @@ def _active_windows_drive_bitmask() -> int:
# A disconnected mapped drive stays set in the GetLogicalDrives bitmask, so ``os.path.isdir`` on it can block for tens of seconds. Bound each drive probe so one stale mapping cannot stall a whole folder-browser request.
_DRIVE_PROBE_TIMEOUT_S = 2.0
+_probes_lock = threading.Lock()
+_probes_started: dict[str, float] = {}
def _readable_dirs_within(paths: Iterable[str], timeout: float) -> set[str]:
@@ -174,9 +273,19 @@ def _readable_dirs_within(paths: Iterable[str], timeout: float) -> set[str]:
results[path] = os.path.isdir(path) and os.access(path, os.R_OK)
except OSError:
results[path] = False
+ finally:
+ with _probes_lock:
+ _probes_started.pop(path, None)
threads: list[threading.Thread] = []
+ now = time.monotonic()
for path in paths:
+ # Probe stuck past the timeout (hard NFS mount): unreadable, never restarted.
+ with _probes_lock:
+ started = _probes_started.get(path)
+ if started is not None and now - started >= timeout:
+ continue
+ _probes_started.setdefault(path, now)
thread = threading.Thread(target = _probe, args = (path,), daemon = True)
thread.start()
threads.append(thread)