flash: Fix gnome automounting bug
This commit is contained in:
@@ -5,7 +5,8 @@ import logging
|
||||
import os
|
||||
import shutil
|
||||
import textwrap
|
||||
from collections.abc import Sequence
|
||||
from collections.abc import Generator, Sequence
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
@@ -37,6 +38,60 @@ class SystemConfig:
|
||||
wifi_settings: list[WifiConfig] | None = field(default=None)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def pause_automounting(
|
||||
devices: list[Path], no_udev: bool
|
||||
) -> Generator[None, None, None]:
|
||||
"""
|
||||
Pause automounting on the device for the duration of this context
|
||||
manager
|
||||
"""
|
||||
if no_udev:
|
||||
yield None
|
||||
return
|
||||
|
||||
if shutil.which("udevadm") is None:
|
||||
msg = "udev is required to disable automounting"
|
||||
log.warning(msg)
|
||||
yield None
|
||||
return
|
||||
|
||||
if os.geteuid() != 0:
|
||||
msg = "root privileges are required to disable automounting"
|
||||
raise ClanError(msg)
|
||||
try:
|
||||
# See /usr/lib/udisks2/udisks2-inhibit
|
||||
rules_dir = Path("/run/udev/rules.d")
|
||||
rules_dir.mkdir(exist_ok=True)
|
||||
rule_files: list[Path] = []
|
||||
for device in devices:
|
||||
devpath: str = str(device)
|
||||
rule_file: Path = (
|
||||
rules_dir / f"90-udisks-inhibit-{devpath.replace('/', '_')}.rules"
|
||||
)
|
||||
with rule_file.open("w") as fd:
|
||||
print(
|
||||
'SUBSYSTEM=="block", ENV{DEVNAME}=="'
|
||||
+ devpath
|
||||
+ '*", ENV{UDISKS_IGNORE}="1"',
|
||||
file=fd,
|
||||
)
|
||||
fd.flush()
|
||||
os.fsync(fd.fileno())
|
||||
rule_files.append(rule_file)
|
||||
run(["udevadm", "control", "--reload"])
|
||||
run(["udevadm", "trigger", "--settle", "--subsystem-match=block"])
|
||||
|
||||
yield None
|
||||
except Exception as ex:
|
||||
log.fatal(ex)
|
||||
finally:
|
||||
for rule_file in rule_files:
|
||||
rule_file.unlink(missing_ok=True)
|
||||
run(["udevadm", "control", "--reload"], check=False)
|
||||
run(["udevadm", "trigger", "--settle", "--subsystem-match=block"], check=False)
|
||||
|
||||
|
||||
@API.register
|
||||
def list_possible_keymaps() -> list[str]:
|
||||
cmd = nix_build(["nixpkgs#kbd"])
|
||||
@@ -95,8 +150,11 @@ def flash_machine(
|
||||
dry_run: bool,
|
||||
write_efi_boot_entries: bool,
|
||||
debug: bool,
|
||||
no_udev: bool = False,
|
||||
extra_args: list[str] | None = None,
|
||||
) -> None:
|
||||
devices = [Path(device) for device in disks.values()]
|
||||
with pause_automounting(devices, no_udev):
|
||||
if extra_args is None:
|
||||
extra_args = []
|
||||
system_config_nix: dict[str, Any] = {}
|
||||
@@ -201,6 +259,7 @@ class FlashOptions:
|
||||
mode: str
|
||||
write_efi_boot_entries: bool
|
||||
nix_options: list[str]
|
||||
no_udev: bool
|
||||
system_config: SystemConfig
|
||||
|
||||
|
||||
@@ -236,6 +295,7 @@ def flash_command(args: argparse.Namespace) -> None:
|
||||
wifi_settings=None,
|
||||
),
|
||||
write_efi_boot_entries=args.write_efi_boot_entries,
|
||||
no_udev=args.no_udev,
|
||||
nix_options=args.option,
|
||||
)
|
||||
|
||||
@@ -274,6 +334,7 @@ def flash_command(args: argparse.Namespace) -> None:
|
||||
dry_run=opts.dry_run,
|
||||
debug=opts.debug,
|
||||
write_efi_boot_entries=opts.write_efi_boot_entries,
|
||||
no_udev=opts.no_udev,
|
||||
extra_args=opts.nix_options,
|
||||
)
|
||||
|
||||
@@ -343,6 +404,12 @@ def register_parser(parser: argparse.ArgumentParser) -> None:
|
||||
default=False,
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--no-udev",
|
||||
help="Disable udev rules to block automounting",
|
||||
default=False,
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--keymap",
|
||||
type=str,
|
||||
|
||||
Reference in New Issue
Block a user