Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions archinstall/lib/disk/device_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
get_lsblk_info,
linux_root_guid,
mount,
swapoff,
udev_sync,
umount,
)
Expand Down Expand Up @@ -513,6 +514,8 @@ def umount_all_existing(self, device_path: Path) -> None:
# un-mount for existing encrypted partitions
if partition.fs_type == FilesystemType.CRYPTO_LUKS:
Luks2(partition.path).lock()
elif partition.fs_type == FilesystemType.LINUX_SWAP:
swapoff(partition.path)
else:
umount(partition.path, recursive=True)

Expand Down
19 changes: 16 additions & 3 deletions archinstall/lib/disk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,18 @@ def mount(
def umount(mountpoint: Path | str, recursive: bool = False) -> None:
lsblk_info = get_lsblk_info(mountpoint)

if not lsblk_info.mountpoints:
mountpoints = [p for p in lsblk_info.mountpoints if p != Path('[SWAP]')]
if not mountpoints:
return

debug(f'Partition {mountpoint} is currently mounted at: {[str(m) for m in lsblk_info.mountpoints]}')
debug(f'Partition {mountpoint} is currently mounted at: {[str(m) for m in mountpoints]}')

cmd = ['umount']

if recursive:
cmd.append('-R')

for path in lsblk_info.mountpoints:
for path in mountpoints:
debug(f'Unmounting mountpoint: {path}')
SysCommand([*cmd, str(path)])

Expand All @@ -198,6 +199,18 @@ def swapon(path: Path) -> None:
raise DiskError(f'Could not enable swap {path}:\n{err.message}')


def swapoff(path: Path) -> None:
lsblk_info = get_lsblk_info(path)
if Path('[SWAP]') not in lsblk_info.mountpoints:
return

debug(f'Disabling swap on: {path}')
try:
SysCommand(['swapoff', str(path)])
except SysCallError as err:
raise DiskError(f'Could not disable swap {path}:\n{err.message}')


def linux_root_guid(arch: str | None) -> PartitionGUID:
if arch == 'aarch64':
return PartitionGUID.LINUX_ROOT_AARCH64
Expand Down
73 changes: 73 additions & 0 deletions tests/test_swapoff_existing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from pathlib import Path
from unittest.mock import MagicMock

from pytest import MonkeyPatch

from archinstall.lib.disk.device_handler import DeviceHandler
from archinstall.lib.disk.utils import swapoff, umount
from archinstall.lib.models.device import FilesystemType


def test_swapoff_runs_only_when_swap_active(monkeypatch: MonkeyPatch) -> None:
cmds: list[list[str]] = []

monkeypatch.setattr(
'archinstall.lib.disk.utils.SysCommand',
lambda cmd: cmds.append(list(cmd)),
)

active_lsblk = MagicMock(mountpoints=[Path('[SWAP]')])
inactive_lsblk = MagicMock(mountpoints=[])

monkeypatch.setattr(
'archinstall.lib.disk.utils.get_lsblk_info',
lambda path: active_lsblk if path == Path('/dev/sda2') else inactive_lsblk,
)

swapoff(Path('/dev/sda2'))
swapoff(Path('/dev/sda3'))

assert cmds == [['swapoff', '/dev/sda2']]


def test_umount_ignores_swap_pseudo_mountpoint(monkeypatch: MonkeyPatch) -> None:
cmds: list[list[str]] = []

monkeypatch.setattr(
'archinstall.lib.disk.utils.SysCommand',
lambda cmd: cmds.append(list(cmd)),
)
monkeypatch.setattr(
'archinstall.lib.disk.utils.get_lsblk_info',
lambda _path: MagicMock(mountpoints=[Path('[SWAP]')]),
)

umount(Path('/dev/sda2'), recursive=True)

assert cmds == []


def test_umount_all_existing_disables_swap_and_unmounts_filesystems(monkeypatch: MonkeyPatch) -> None:
handler = object.__new__(DeviceHandler)

swap_part = MagicMock(path=Path('/dev/sda2'), fs_type=FilesystemType.LINUX_SWAP)
ext4_part = MagicMock(path=Path('/dev/sda3'), fs_type=FilesystemType.EXT4)
mock_device = MagicMock(partition_infos=[swap_part, ext4_part])
handler._devices = {Path('/dev/sda'): mock_device}

swapoff_paths: list[Path] = []
umount_paths: list[Path | str] = []

monkeypatch.setattr(
'archinstall.lib.disk.device_handler.swapoff',
lambda path: swapoff_paths.append(path),
)
monkeypatch.setattr(
'archinstall.lib.disk.device_handler.umount',
lambda path, **_kwargs: umount_paths.append(path),
)

handler.umount_all_existing(Path('/dev/sda'))

assert swapoff_paths == [Path('/dev/sda2')]
assert umount_paths == [Path('/dev/sda3')]