Skip to content

Commit a022134

Browse files
committed
disk: add support for creating swap partitions
1 parent 8d923ff commit a022134

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

archinstall/lib/disk/device_handler.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ def _determine_fs_type(
151151
) -> FilesystemType | None:
152152
try:
153153
if partition.fileSystem:
154+
if partition.fileSystem.type == FilesystemType.LinuxSwap.parted_value:
155+
return FilesystemType.LinuxSwap
154156
return FilesystemType(partition.fileSystem.type)
155157
elif lsblk_info is not None:
156158
return FilesystemType(lsblk_info.fstype) if lsblk_info.fstype else None
@@ -259,6 +261,7 @@ def format(
259261
additional_parted_options: list[str] = []
260262
) -> None:
261263
mkfs_type = fs_type.value
264+
command = None
262265
options = []
263266

264267
match fs_type:
@@ -277,10 +280,15 @@ def format(
277280
options.append('--fast')
278281
case FilesystemType.Reiserfs:
279282
pass
283+
case FilesystemType.LinuxSwap:
284+
command = "mkswap"
280285
case _:
281286
raise UnknownFilesystemFormat(f'Filetype "{fs_type.value}" is not supported')
282287

283-
cmd = [f'mkfs.{mkfs_type}', *options, *additional_parted_options, str(path)]
288+
if not command:
289+
command = f'mkfs.{mkfs_type}'
290+
291+
cmd = [command, *options, *additional_parted_options, str(path)]
284292

285293
debug('Formatting filesystem:', ' '.join(cmd))
286294

@@ -536,7 +544,8 @@ def _setup_partition(
536544
length=length_sector.value
537545
)
538546

539-
filesystem = FileSystem(type=part_mod.safe_fs_type.value, geometry=geometry)
547+
fs_value = part_mod.safe_fs_type.parted_value
548+
filesystem = FileSystem(type=fs_value, geometry=geometry)
540549

541550
partition = Partition(
542551
disk=disk,
@@ -549,7 +558,7 @@ def _setup_partition(
549558
partition.setFlag(flag.flag_id)
550559

551560
debug(f'\tType: {part_mod.type.value}')
552-
debug(f'\tFilesystem: {part_mod.safe_fs_type.value}')
561+
debug(f'\tFilesystem: {fs_value}')
553562
debug(f'\tGeometry: {start_sector.value} start sector, {length_sector.value} length')
554563

555564
try:
@@ -723,6 +732,13 @@ def partition(
723732

724733
disk.commit()
725734

735+
@staticmethod
736+
def swapon(path: Path) -> None:
737+
try:
738+
SysCommand(['swapon', str(path)])
739+
except SysCallError as err:
740+
raise DiskError(f'Could not enable swap {path}:\n{err.message}')
741+
726742
def mount(
727743
self,
728744
dev_path: Path,

archinstall/lib/disk/device_model.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ class PartitionFlag(PartitionFlagDataMixin, Enum):
728728
XBOOTLDR = parted.PARTITION_BLS_BOOT, "bls_boot"
729729
ESP = parted.PARTITION_ESP
730730
LINUX_HOME = parted.PARTITION_LINUX_HOME, "linux-home"
731+
SWAP = parted.PARTITION_SWAP
731732

732733
@property
733734
def description(self) -> str:
@@ -767,6 +768,7 @@ class FilesystemType(Enum):
767768
Ntfs = 'ntfs'
768769
Reiserfs = 'reiserfs'
769770
Xfs = 'xfs'
771+
LinuxSwap = 'linux-swap'
770772

771773
# this is not a FS known to parted, so be careful
772774
# with the usage from this enum
@@ -785,6 +787,10 @@ def fs_type_mount(self) -> str:
785787
case _:
786788
return self.value
787789

790+
@property
791+
def parted_value(self) -> str:
792+
return self.value + '(v1)' if self == FilesystemType.LinuxSwap else self.value
793+
788794
@property
789795
def installation_pkg(self) -> str | None:
790796
match self:
@@ -969,6 +975,9 @@ def is_home(self) -> bool:
969975
or PartitionFlag.LINUX_HOME in self.flags
970976
)
971977

978+
def is_swap(self) -> bool:
979+
return self.fs_type == FilesystemType.LinuxSwap
980+
972981
def is_modify(self) -> bool:
973982
return self.status == ModificationStatus.Modify
974983

archinstall/lib/disk/encryption_menu.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,10 @@ def select_partitions_to_encrypt(
284284

285285
# do not allow encrypting the boot partition
286286
for mod in modification:
287-
partitions += [p for p in mod.partitions if p.mountpoint != Path('/boot')]
287+
partitions += [
288+
p for p in mod.partitions
289+
if p.mountpoint != Path('/boot') and not p.is_swap()
290+
]
288291

289292
# do not allow encrypting existing partitions that are not marked as wipe
290293
avail_partitions = [p for p in partitions if not p.exists()]

archinstall/lib/disk/partitioning_menu.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ def _create_new_partition(self, free_space: FreeSpace) -> PartitionModification:
470470
fs_type = self._prompt_partition_fs_type()
471471

472472
mountpoint = None
473-
if fs_type != FilesystemType.Btrfs:
473+
if fs_type not in (FilesystemType.Btrfs, FilesystemType.LinuxSwap):
474474
mountpoint = self._prompt_mountpoint()
475475

476476
partition = PartitionModification(
@@ -486,6 +486,8 @@ def _create_new_partition(self, free_space: FreeSpace) -> PartitionModification:
486486
partition.set_flag(PartitionFlag.BOOT)
487487
if self._using_gpt:
488488
partition.set_flag(PartitionFlag.ESP)
489+
elif partition.is_swap():
490+
partition.set_flag(PartitionFlag.SWAP)
489491

490492
return partition
491493

archinstall/lib/installer.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,21 @@ def _prepare_luks_lvm(
315315
}
316316

317317
def _mount_partition(self, part_mod: disk.PartitionModification) -> None:
318+
if not part_mod.dev_path:
319+
return
320+
318321
# it would be none if it's btrfs as the subvolumes will have the mountpoints defined
319-
if part_mod.mountpoint and part_mod.dev_path:
322+
if part_mod.mountpoint:
320323
target = self.target / part_mod.relative_mountpoint
321324
disk.device_handler.mount(part_mod.dev_path, target, options=part_mod.mount_options)
322-
323-
if part_mod.fs_type == disk.FilesystemType.Btrfs and part_mod.dev_path:
325+
elif part_mod.fs_type == disk.FilesystemType.Btrfs:
324326
self._mount_btrfs_subvol(
325327
part_mod.dev_path,
326328
part_mod.btrfs_subvols,
327329
part_mod.mount_options
328330
)
331+
elif part_mod.is_swap():
332+
disk.device_handler.swapon(part_mod.dev_path)
329333

330334
def _mount_lvm_vol(self, volume: disk.LvmVolume) -> None:
331335
if volume.fs_type != disk.FilesystemType.Btrfs:

0 commit comments

Comments
 (0)