Skip to content

Commit 9d5894e

Browse files
committed
added better linux detection features: checks common linux boot paths to detect linux without mounting, seems to work but might need testing
1 parent 45d9018 commit 9d5894e

3 files changed

Lines changed: 49 additions & 22 deletions

File tree

H

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/lufus/writing/windows/detect.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,30 @@ def is_windows_iso(iso_path: str) -> bool:
8989
log.error("Windows detection: blkid error: %s: %s", type(e).__name__, e)
9090

9191
def is_linux_iso(iso_path: str) -> bool:
92-
"""Detect if an ISO is a Linux distribution."""
92+
"""Detect if an ISO is a Linux distribution by looking for bootloader and kernel markers."""
9393
log.info("Linux detection: checking %s", iso_path)
9494
try:
95-
# Check for common Linux markers in the ISO using 7z
95+
# Use 7z to list only top-level and boot-related directories/files, which is faster
9696
result = subprocess.run(["7z", "l", iso_path], capture_output=True, text=True, timeout=15)
9797
if result.returncode == 0:
98-
files = result.stdout.lower()
99-
linux_markers = [
100-
"/isolinux/",
101-
"/boot/grub/",
102-
"/arch/",
103-
"/ubuntu/",
104-
"/debian/",
105-
"/fedora/",
98+
content = result.stdout.lower()
99+
# Focus on definitive markers for Linux boot media
100+
markers = [
101+
"isolinux/",
102+
"boot/grub/",
103+
"boot/efi/",
104+
"efi/boot/",
105+
"arch/",
106+
"casper/", # Ubuntu/Debian live
107+
"live/", # Debian/Arch live
106108
"vmlinuz",
107109
"initrd",
108110
]
109-
for marker in linux_markers:
110-
if marker in files:
111+
for marker in markers:
112+
if marker in content:
111113
log.info("Linux detection: found marker %r -> Linux ISO confirmed", marker)
112114
return True
115+
log.info("Linux detection: no Linux markers found")
113116
except Exception as e:
114117
log.error("Linux detection error: %s", e)
115118

tests/test_linux_iso_detection.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from __future__ import annotations
2+
import sys
3+
import subprocess
4+
from pathlib import Path
5+
from unittest.mock import MagicMock
6+
7+
ROOT = Path(__file__).resolve().parents[1]
8+
SRC = ROOT / "src"
9+
if str(SRC) not in sys.path:
10+
sys.path.insert(0, str(SRC))
11+
12+
from lufus.writing.windows.detect import is_linux_iso
13+
14+
def test_is_linux_iso_detects_marker(monkeypatch):
15+
mock_run = MagicMock()
16+
mock_run.return_value = subprocess.CompletedProcess(
17+
args=["7z", "l", "test.iso"],
18+
returncode=0,
19+
stdout="isolinux/isolinux.cfg\nvmlinuz\ninitrd.img",
20+
stderr=""
21+
)
22+
monkeypatch.setattr(subprocess, "run", mock_run)
23+
assert is_linux_iso("test.iso") is True
24+
25+
def test_is_linux_iso_fails_without_marker(monkeypatch):
26+
mock_run = MagicMock()
27+
mock_run.return_value = subprocess.CompletedProcess(
28+
args=["7z", "l", "test.iso"],
29+
returncode=0,
30+
stdout="random/file/not/linux.txt",
31+
stderr=""
32+
)
33+
monkeypatch.setattr(subprocess, "run", mock_run)
34+
assert is_linux_iso("test.iso") is False

0 commit comments

Comments
 (0)