Skip to content

Commit af92c25

Browse files
committed
Move all LVM helpers to dedicated module
1 parent 452abe4 commit af92c25

File tree

3 files changed

+73
-63
lines changed

3 files changed

+73
-63
lines changed

archinstall/lib/disk/device_handler.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import logging
22
import os
3-
from collections.abc import Iterable
43
from pathlib import Path
54

65
from parted import Device, Disk, DiskException, FileSystem, Geometry, IOException, Partition, PartitionException, freshDisk, getAllDevices, getDevice, newDisk
76

8-
from archinstall.lib.command import SysCommand, SysCommandWorker
7+
from archinstall.lib.command import SysCommand
98
from archinstall.lib.disk.utils import (
109
find_lsblk_info,
1110
get_all_lsblk_info,
@@ -24,14 +23,11 @@
2423
DiskEncryption,
2524
FilesystemType,
2625
LsblkInfo,
27-
LvmVolume,
28-
LvmVolumeGroup,
2926
ModificationStatus,
3027
PartitionFlag,
3128
PartitionGUID,
3229
PartitionModification,
3330
PartitionTable,
34-
Size,
3531
SubvolumeModification,
3632
Unit,
3733
_BtrfsSubvolumeInfo,
@@ -339,58 +335,6 @@ def format_encrypted(
339335
info(f'luks2 locking device: {dev_path}')
340336
luks_handler.lock()
341337

342-
def lvm_export_vg(self, vg: LvmVolumeGroup) -> None:
343-
cmd = f'vgexport {vg.name}'
344-
345-
debug(f'vgexport: {cmd}')
346-
SysCommand(cmd)
347-
348-
def lvm_vol_reduce(self, vol_path: Path, amount: Size) -> None:
349-
val = amount.format_size(Unit.B, include_unit=False)
350-
cmd = f'lvreduce -L -{val}B {vol_path}'
351-
352-
debug(f'Reducing LVM volume size: {cmd}')
353-
SysCommand(cmd)
354-
355-
def lvm_pv_create(self, pvs: Iterable[Path]) -> None:
356-
pvs_str = ' '.join(str(pv) for pv in pvs)
357-
# Signatures are already wiped by wipefs, -f is just for safety
358-
cmd = f'pvcreate -f --yes {pvs_str}'
359-
# note flags used in scripting
360-
debug(f'Creating LVM PVS: {cmd}')
361-
SysCommand(cmd)
362-
363-
# Sync with udev to ensure the PVs are visible
364-
udev_sync()
365-
366-
def lvm_vg_create(self, pvs: Iterable[Path], vg_name: str) -> None:
367-
pvs_str = ' '.join(str(pv) for pv in pvs)
368-
cmd = f'vgcreate --yes --force {vg_name} {pvs_str}'
369-
370-
debug(f'Creating LVM group: {cmd}')
371-
SysCommand(cmd)
372-
373-
# Sync with udev to ensure the VG is visible
374-
udev_sync()
375-
376-
def lvm_vol_create(self, vg_name: str, volume: LvmVolume, offset: Size | None = None) -> None:
377-
if offset is not None:
378-
length = volume.length - offset
379-
else:
380-
length = volume.length
381-
382-
length_str = length.format_size(Unit.B, include_unit=False)
383-
cmd = f'lvcreate --yes -L {length_str}B {vg_name} -n {volume.name}'
384-
385-
debug(f'Creating volume: {cmd}')
386-
387-
worker = SysCommandWorker(cmd)
388-
worker.poll()
389-
worker.write(b'y\n', line_ending=False)
390-
391-
volume.vg_name = vg_name
392-
volume.dev_path = Path(f'/dev/{vg_name}/{volume.name}')
393-
394338
def _setup_partition(
395339
self,
396340
part_mod: PartitionModification,

archinstall/lib/disk/filesystem.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
from pathlib import Path
44

55
from archinstall.lib.disk.device_handler import device_handler
6-
from archinstall.lib.disk.lvm import lvm_group_info, lvm_vol_info
6+
from archinstall.lib.disk.lvm import (
7+
lvm_group_info,
8+
lvm_pv_create,
9+
lvm_vg_create,
10+
lvm_vol_create,
11+
lvm_vol_info,
12+
lvm_vol_reduce,
13+
)
714
from archinstall.lib.disk.utils import udev_sync
815
from archinstall.lib.interactions.general_conf import confirm_abort
916
from archinstall.lib.luks import Luks2
@@ -166,7 +173,7 @@ def _setup_lvm(
166173
for vg in lvm_config.vol_groups:
167174
pv_dev_paths = self._get_all_pv_dev_paths(vg.pvs, enc_mods)
168175

169-
device_handler.lvm_vg_create(pv_dev_paths, vg.name)
176+
lvm_vg_create(pv_dev_paths, vg.name)
170177

171178
# figure out what the actual available size in the group is
172179
vg_info = lvm_group_info(vg.name)
@@ -199,7 +206,7 @@ def _setup_lvm(
199206
offset = max_vol_offset if lv == max_vol else None
200207

201208
debug(f'vg: {vg.name}, vol: {lv.name}, offset: {offset}')
202-
device_handler.lvm_vol_create(vg.name, lv, offset)
209+
lvm_vol_create(vg.name, lv, offset)
203210

204211
while True:
205212
debug('Fetching LVM volume info')
@@ -241,7 +248,7 @@ def _lvm_create_pvs(
241248
for vg in lvm_config.vol_groups:
242249
pv_paths |= self._get_all_pv_dev_paths(vg.pvs, enc_mods)
243250

244-
device_handler.lvm_pv_create(pv_paths)
251+
lvm_pv_create(pv_paths)
245252

246253
def _get_all_pv_dev_paths(
247254
self,
@@ -319,7 +326,7 @@ def _lvm_vol_handle_e2scrub(self, vol_gp: LvmVolumeGroup) -> None:
319326
if any([vol.fs_type == FilesystemType.Ext4 for vol in vol_gp.volumes]):
320327
largest_vol = max(vol_gp.volumes, key=lambda x: x.length)
321328

322-
device_handler.lvm_vol_reduce(
329+
lvm_vol_reduce(
323330
largest_vol.safe_dev_path,
324331
Size(256, Unit.MiB, SectorSize.default()),
325332
)

archinstall/lib/disk/lvm.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import json
22
import time
3+
from collections.abc import Iterable
34
from pathlib import Path
45
from typing import Literal, overload
56

6-
from archinstall.lib.command import SysCommand
7+
from archinstall.lib.command import SysCommand, SysCommandWorker
8+
from archinstall.lib.disk.utils import udev_sync
79
from archinstall.lib.exceptions import SysCallError
810
from archinstall.lib.models.device import (
911
LvmGroupInfo,
@@ -116,6 +118,13 @@ def lvm_vol_change(vol: LvmVolume, activate: bool) -> None:
116118
SysCommand(cmd)
117119

118120

121+
def lvm_export_vg(vg: LvmVolumeGroup) -> None:
122+
cmd = f'vgexport {vg.name}'
123+
124+
debug(f'vgexport: {cmd}')
125+
SysCommand(cmd)
126+
127+
119128
def lvm_import_vg(vg: LvmVolumeGroup) -> None:
120129
# Check if the VG is actually exported before trying to import it
121130
check_cmd = f'vgs --noheadings -o vg_exported {vg.name}'
@@ -135,3 +144,53 @@ def lvm_import_vg(vg: LvmVolumeGroup) -> None:
135144
cmd = f'vgimport {vg.name}'
136145
debug(f'vgimport: {cmd}')
137146
SysCommand(cmd)
147+
148+
149+
def lvm_vol_reduce(vol_path: Path, amount: Size) -> None:
150+
val = amount.format_size(Unit.B, include_unit=False)
151+
cmd = f'lvreduce -L -{val}B {vol_path}'
152+
153+
debug(f'Reducing LVM volume size: {cmd}')
154+
SysCommand(cmd)
155+
156+
157+
def lvm_pv_create(pvs: Iterable[Path]) -> None:
158+
pvs_str = ' '.join(str(pv) for pv in pvs)
159+
# Signatures are already wiped by wipefs, -f is just for safety
160+
cmd = f'pvcreate -f --yes {pvs_str}'
161+
# note flags used in scripting
162+
debug(f'Creating LVM PVS: {cmd}')
163+
SysCommand(cmd)
164+
165+
# Sync with udev to ensure the PVs are visible
166+
udev_sync()
167+
168+
169+
def lvm_vg_create(pvs: Iterable[Path], vg_name: str) -> None:
170+
pvs_str = ' '.join(str(pv) for pv in pvs)
171+
cmd = f'vgcreate --yes --force {vg_name} {pvs_str}'
172+
173+
debug(f'Creating LVM group: {cmd}')
174+
SysCommand(cmd)
175+
176+
# Sync with udev to ensure the VG is visible
177+
udev_sync()
178+
179+
180+
def lvm_vol_create(vg_name: str, volume: LvmVolume, offset: Size | None = None) -> None:
181+
if offset is not None:
182+
length = volume.length - offset
183+
else:
184+
length = volume.length
185+
186+
length_str = length.format_size(Unit.B, include_unit=False)
187+
cmd = f'lvcreate --yes -L {length_str}B {vg_name} -n {volume.name}'
188+
189+
debug(f'Creating volume: {cmd}')
190+
191+
worker = SysCommandWorker(cmd)
192+
worker.poll()
193+
worker.write(b'y\n', line_ending=False)
194+
195+
volume.vg_name = vg_name
196+
volume.dev_path = Path(f'/dev/{vg_name}/{volume.name}')

0 commit comments

Comments
 (0)