mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-01 04:16:24 -05:00
Warning users when files exist in mount location. Users may have accidentally downloaded files into the media directory, we want to warn them.
This commit is contained in:
parent
da340a00b3
commit
cd8792f697
12 changed files with 482 additions and 34 deletions
|
|
@ -254,6 +254,49 @@ def is_mount_busy(mount_point: str) -> bool:
|
|||
return False
|
||||
|
||||
|
||||
def check_shadowed_data(target: str) -> Optional[dict]:
|
||||
"""Look for user data at `target` that would be hidden by a mount over it.
|
||||
|
||||
Returns a dict with `size_bytes` and `entries` when non-config content is
|
||||
present, or None when the target is empty (or contains only `config/`).
|
||||
|
||||
`config/` is whitelisted because it is created transiently by
|
||||
`generate_certificates.sh` before the primary drive is mounted; that is
|
||||
expected, not user data.
|
||||
|
||||
Returns None for non-existent targets.
|
||||
"""
|
||||
target_path = Path(target)
|
||||
if not target_path.exists() or not target_path.is_dir():
|
||||
return None
|
||||
|
||||
try:
|
||||
entries = [e.name for e in target_path.iterdir() if e.name != "config"]
|
||||
except OSError:
|
||||
return None
|
||||
if not entries:
|
||||
return None
|
||||
|
||||
size_bytes = 0
|
||||
for entry in entries:
|
||||
entry_path = target_path / entry
|
||||
try:
|
||||
if entry_path.is_dir():
|
||||
for path in entry_path.rglob("*"):
|
||||
try:
|
||||
if path.is_file() and not path.is_symlink():
|
||||
size_bytes += path.stat().st_size
|
||||
except OSError:
|
||||
# File disappeared mid-walk or unreadable; skip.
|
||||
continue
|
||||
elif entry_path.is_file() and not entry_path.is_symlink():
|
||||
size_bytes += entry_path.stat().st_size
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
return {"size_bytes": size_bytes, "entries": sorted(entries)}
|
||||
|
||||
|
||||
def get_mounts() -> list[dict]:
|
||||
"""
|
||||
Get current mounts under /media.
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ from controller.lib.config import (
|
|||
save_config,
|
||||
update_config,
|
||||
)
|
||||
from controller.lib.disks import get_block_devices, mount_drive, unmount_drive
|
||||
from controller.lib.disks import check_shadowed_data, get_block_devices, mount_drive, unmount_drive
|
||||
from controller.lib.fstab import add_fstab_entry
|
||||
from controller.lib.scripts import start_script
|
||||
|
||||
|
|
@ -165,25 +165,32 @@ def cancel_probe() -> dict:
|
|||
return {"success": True}
|
||||
|
||||
|
||||
def commit_onboarding(device_path: str, fstype: str, force: bool = False) -> dict:
|
||||
def commit_onboarding(
|
||||
device_path: str,
|
||||
fstype: str,
|
||||
force_shadowed: bool = False,
|
||||
) -> dict:
|
||||
"""
|
||||
Execute the full onboarding setup.
|
||||
|
||||
1. Unmount temp mount
|
||||
2. Mount primary drive at /media/wrolpi
|
||||
3. Add fstab entry for primary
|
||||
4. Reload config
|
||||
5. Mount secondary drives per config
|
||||
6. Add fstab entries for secondary drives
|
||||
7. Start repair
|
||||
2. Check for shadowed data at the mount target
|
||||
3. Mount primary drive at /media/wrolpi
|
||||
4. Add fstab entry for primary
|
||||
5. Reload config
|
||||
6. Mount secondary drives per config
|
||||
7. Add fstab entries for secondary drives
|
||||
8. Start repair
|
||||
|
||||
Args:
|
||||
device_path: Device path
|
||||
fstype: Filesystem type
|
||||
force: If True, proceed even without config on drive
|
||||
force_shadowed: If True, proceed even when existing data at the
|
||||
mount target would be hidden by the new mount
|
||||
|
||||
Returns:
|
||||
dict with success, error, mounts, repair_started
|
||||
dict with success, error, mounts, repair_started, and on a shadowed
|
||||
block, `needs_force` and `shadowed_data`
|
||||
"""
|
||||
media_dir = str(get_media_directory())
|
||||
mounted_mounts = []
|
||||
|
|
@ -201,6 +208,26 @@ def commit_onboarding(device_path: str, fstype: str, force: bool = False) -> dic
|
|||
shutil.rmtree(transient_config)
|
||||
logger.info("Removed transient config directory at %s", transient_config)
|
||||
|
||||
# Step 1c: Check for shadowed data. If the user has been downloading
|
||||
# to /media/wrolpi without a drive mounted, mounting now would hide
|
||||
# those files on the SD card / root filesystem.
|
||||
if not force_shadowed:
|
||||
shadowed = check_shadowed_data(media_dir)
|
||||
if shadowed:
|
||||
return {
|
||||
"success": False,
|
||||
"needs_force": "shadowed",
|
||||
"shadowed_data": shadowed,
|
||||
"error": (
|
||||
f"{media_dir} contains {len(shadowed['entries'])} "
|
||||
f"existing data "
|
||||
f"{'entry' if len(shadowed['entries']) == 1 else 'entries'}. "
|
||||
f"Mounting now would hide them on the underlying filesystem."
|
||||
),
|
||||
"mounts": [],
|
||||
"repair_started": False,
|
||||
}
|
||||
|
||||
# Step 2: Mount primary drive
|
||||
result = mount_drive(device_path, media_dir, fstype=fstype)
|
||||
if not result.get("success"):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue