Skip to content

Commit

Permalink
disk: add support for creating swap partitions
Browse files Browse the repository at this point in the history
  • Loading branch information
codefiles committed Jan 18, 2025
1 parent 8d923ff commit a022134
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
22 changes: 19 additions & 3 deletions archinstall/lib/disk/device_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def _determine_fs_type(
) -> FilesystemType | None:
try:
if partition.fileSystem:
if partition.fileSystem.type == FilesystemType.LinuxSwap.parted_value:
return FilesystemType.LinuxSwap
return FilesystemType(partition.fileSystem.type)
elif lsblk_info is not None:
return FilesystemType(lsblk_info.fstype) if lsblk_info.fstype else None
Expand Down Expand Up @@ -259,6 +261,7 @@ def format(
additional_parted_options: list[str] = []
) -> None:
mkfs_type = fs_type.value
command = None
options = []

match fs_type:
Expand All @@ -277,10 +280,15 @@ def format(
options.append('--fast')
case FilesystemType.Reiserfs:
pass
case FilesystemType.LinuxSwap:
command = "mkswap"
case _:
raise UnknownFilesystemFormat(f'Filetype "{fs_type.value}" is not supported')

cmd = [f'mkfs.{mkfs_type}', *options, *additional_parted_options, str(path)]
if not command:
command = f'mkfs.{mkfs_type}'

cmd = [command, *options, *additional_parted_options, str(path)]

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

Expand Down Expand Up @@ -536,7 +544,8 @@ def _setup_partition(
length=length_sector.value
)

filesystem = FileSystem(type=part_mod.safe_fs_type.value, geometry=geometry)
fs_value = part_mod.safe_fs_type.parted_value
filesystem = FileSystem(type=fs_value, geometry=geometry)

partition = Partition(
disk=disk,
Expand All @@ -549,7 +558,7 @@ def _setup_partition(
partition.setFlag(flag.flag_id)

debug(f'\tType: {part_mod.type.value}')
debug(f'\tFilesystem: {part_mod.safe_fs_type.value}')
debug(f'\tFilesystem: {fs_value}')
debug(f'\tGeometry: {start_sector.value} start sector, {length_sector.value} length')

try:
Expand Down Expand Up @@ -723,6 +732,13 @@ def partition(

disk.commit()

@staticmethod
def swapon(path: Path) -> None:
try:
SysCommand(['swapon', str(path)])
except SysCallError as err:
raise DiskError(f'Could not enable swap {path}:\n{err.message}')

def mount(
self,
dev_path: Path,
Expand Down
9 changes: 9 additions & 0 deletions archinstall/lib/disk/device_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ class PartitionFlag(PartitionFlagDataMixin, Enum):
XBOOTLDR = parted.PARTITION_BLS_BOOT, "bls_boot"
ESP = parted.PARTITION_ESP
LINUX_HOME = parted.PARTITION_LINUX_HOME, "linux-home"
SWAP = parted.PARTITION_SWAP

@property
def description(self) -> str:
Expand Down Expand Up @@ -767,6 +768,7 @@ class FilesystemType(Enum):
Ntfs = 'ntfs'
Reiserfs = 'reiserfs'
Xfs = 'xfs'
LinuxSwap = 'linux-swap'

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

@property
def parted_value(self) -> str:
return self.value + '(v1)' if self == FilesystemType.LinuxSwap else self.value

@property
def installation_pkg(self) -> str | None:
match self:
Expand Down Expand Up @@ -969,6 +975,9 @@ def is_home(self) -> bool:
or PartitionFlag.LINUX_HOME in self.flags
)

def is_swap(self) -> bool:
return self.fs_type == FilesystemType.LinuxSwap

def is_modify(self) -> bool:
return self.status == ModificationStatus.Modify

Expand Down
5 changes: 4 additions & 1 deletion archinstall/lib/disk/encryption_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ def select_partitions_to_encrypt(

# do not allow encrypting the boot partition
for mod in modification:
partitions += [p for p in mod.partitions if p.mountpoint != Path('/boot')]
partitions += [
p for p in mod.partitions
if p.mountpoint != Path('/boot') and not p.is_swap()
]

# do not allow encrypting existing partitions that are not marked as wipe
avail_partitions = [p for p in partitions if not p.exists()]
Expand Down
4 changes: 3 additions & 1 deletion archinstall/lib/disk/partitioning_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def _create_new_partition(self, free_space: FreeSpace) -> PartitionModification:
fs_type = self._prompt_partition_fs_type()

mountpoint = None
if fs_type != FilesystemType.Btrfs:
if fs_type not in (FilesystemType.Btrfs, FilesystemType.LinuxSwap):
mountpoint = self._prompt_mountpoint()

partition = PartitionModification(
Expand All @@ -486,6 +486,8 @@ def _create_new_partition(self, free_space: FreeSpace) -> PartitionModification:
partition.set_flag(PartitionFlag.BOOT)
if self._using_gpt:
partition.set_flag(PartitionFlag.ESP)
elif partition.is_swap():
partition.set_flag(PartitionFlag.SWAP)

return partition

Expand Down
10 changes: 7 additions & 3 deletions archinstall/lib/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,21 @@ def _prepare_luks_lvm(
}

def _mount_partition(self, part_mod: disk.PartitionModification) -> None:
if not part_mod.dev_path:
return

# it would be none if it's btrfs as the subvolumes will have the mountpoints defined
if part_mod.mountpoint and part_mod.dev_path:
if part_mod.mountpoint:
target = self.target / part_mod.relative_mountpoint
disk.device_handler.mount(part_mod.dev_path, target, options=part_mod.mount_options)

if part_mod.fs_type == disk.FilesystemType.Btrfs and part_mod.dev_path:
elif part_mod.fs_type == disk.FilesystemType.Btrfs:
self._mount_btrfs_subvol(
part_mod.dev_path,
part_mod.btrfs_subvols,
part_mod.mount_options
)
elif part_mod.is_swap():
disk.device_handler.swapon(part_mod.dev_path)

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

0 comments on commit a022134

Please sign in to comment.