Skip to content

Commit 2d4535e

Browse files
committed
Refactor
1 parent 1a42e37 commit 2d4535e

File tree

7 files changed

+567
-596
lines changed

7 files changed

+567
-596
lines changed

archinstall/lib/disk/__init__.py

-7
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,3 @@
4646
)
4747

4848
from .disk_menu import DiskLayoutConfigurationMenu
49-
50-
from .lvm_menu import (
51-
LvmConfigurationMenu,
52-
LvmVolumeList,
53-
select_lvm_pvs,
54-
select_lvm_vol_group
55-
)

archinstall/lib/disk/device_handler.py

+34-56
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66
import logging
77
from pathlib import Path
8-
from typing import List, Dict, Any, Optional, TYPE_CHECKING
8+
from typing import List, Dict, Any, Optional, TYPE_CHECKING, Literal
99

1010
from parted import ( # type: ignore
1111
Disk, Geometry, FileSystem,
@@ -272,76 +272,54 @@ def format_enc(
272272
info(f'luks2 locking device: {dev_path}')
273273
luks_handler.lock()
274274

275-
def lvm_vol_info(self, lv_name: str) -> Optional[LvmVolumeInfo]:
276-
cmd = 'lvs --reportformat json ' \
277-
'--unit B ' \
278-
f'-S lv_name={lv_name}'
279-
275+
def _lvm_info(self, cmd: str, name: str, info_type: Literal['lv', 'vg']) -> Optional[Any]:
280276
raw_info = SysCommand(cmd).decode().split('\n')
281277

282278
# for whatever reason the output sometimes contains
283279
# "File descriptor X leaked leaked on vgs invocation
284-
data = '\n'.join([l for l in raw_info if 'File descriptor' not in l])
280+
data = '\n'.join([raw for raw in raw_info if 'File descriptor' not in raw])
285281

286-
debug(f'LVM info: {data}')
282+
debug(f'LVM raw: {data}')
287283

288284
reports = json.loads(data)
289285

290-
lv = None
286+
type_name = f'{info_type}_name'
291287

292288
for report in reports['report']:
293-
lvs = report.get('lv', [])
294-
for lv_data in lvs:
295-
if lv_data['lv_name'] == lv_name:
296-
lv = lv_data
297-
298-
if lv is None:
299-
return None
289+
entries = report.get(info_type, [])
290+
for entry in entries:
291+
if entry[type_name] == name:
292+
lv_size = int(entry[f'{info_type}_size'][:-1])
293+
size = Size(lv_size, Unit.B, SectorSize.default())
294+
295+
match info_type:
296+
case 'lv':
297+
return LvmVolumeInfo(
298+
lv_name=entry['lv_name'],
299+
vg_name=entry['vg_name'],
300+
lv_size=size
301+
)
302+
case 'vg':
303+
return LvmGroupInfo(
304+
vg_size=size,
305+
vg_uuid=entry['vg_uuid']
306+
)
307+
return None
300308

301-
lv_size = int(lv['lv_size'][:-1])
302-
size = Size(lv_size, Unit.B, SectorSize.default())
309+
def lvm_vol_info(self, lv_name: str) -> Optional[LvmVolumeInfo]:
310+
cmd = 'lvs --reportformat json ' \
311+
'--unit B ' \
312+
f'-S lv_name={lv_name}'
303313

304-
return LvmVolumeInfo(
305-
lv_name=lv['lv_name'],
306-
vg_name=lv['vg_name'],
307-
lv_size=size
308-
)
314+
return self._lvm_info(cmd, lv_name, 'lv')
309315

310316
def lvm_group_info(self, vg_name: str) -> Optional[LvmGroupInfo]:
311317
cmd = 'vgs --reportformat json ' \
312-
'--unit B ' \
313-
'-o vg_name,vg_uuid,vg_size ' \
314-
f'-S vg_name={vg_name}'
318+
'--unit B ' \
319+
'-o vg_name,vg_uuid,vg_size ' \
320+
f'-S vg_name={vg_name}'
315321

316-
debug(f'LVM group info: {cmd}')
317-
raw_info = SysCommand(cmd).decode().strip().split('\n')
318-
319-
# for whatever reason the output sometimes contains
320-
# "File descriptor X leaked leaked on vgs invocation
321-
data = '\n'.join([l for l in raw_info if 'File descriptor' not in l])
322-
323-
debug(f'VG info: {data}')
324-
325-
reports = json.loads(data)
326-
327-
vg = None
328-
329-
for report in reports['report']:
330-
vgs = report.get('vg', [])
331-
for vg_data in vgs:
332-
if vg_data['vg_name'] == vg_name:
333-
vg = vg_data
334-
335-
if vg is None:
336-
return None
337-
338-
vg_size = int(vg['vg_size'][:-1])
339-
size = Size(vg_size, Unit.B, SectorSize.default())
340-
341-
return LvmGroupInfo(
342-
vg_size=size,
343-
vg_uuid=vg['vg_uuid']
344-
)
322+
return self._lvm_info(cmd, vg_name, 'vg')
345323

346324
def lvm_vol_reduce(self, vol_path: Path, amount: Size):
347325
val = amount.format_size(Unit.B, include_unit=False)
@@ -383,7 +361,7 @@ def lvm_vol_create(self, vg_name: str, volume: LvmVolume, offset: Optional[Size]
383361
worker.poll()
384362
worker.write(b'y\n', line_ending=False)
385363

386-
volume.dev_path = f'/dev/{vg_name}/{volume.name}'
364+
volume.dev_path = Path(f'/dev/{vg_name}/{volume.name}')
387365

388366
def _perform_partitioning(
389367
self,

archinstall/lib/disk/device_model.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def json(self) -> Dict[str, Any]:
6868
return config
6969

7070
@classmethod
71-
def parse_arg(cls, disk_config: Dict[str, Dict[str, Any]]) -> Optional[DiskLayoutConfiguration]:
71+
def parse_arg(cls, disk_config: Dict[str, Any]) -> Optional[DiskLayoutConfiguration]:
7272
from .device_handler import device_handler
7373

7474
device_modifications: List[DeviceModification] = []
@@ -136,7 +136,7 @@ def parse_arg(cls, disk_config: Dict[str, Dict[str, Any]]) -> Optional[DiskLayou
136136
device_modifications.append(device_modification)
137137

138138
# Parse LVM configuration from settings
139-
if lvm_arg := disk_config.get('lvm_config', None):
139+
if (lvm_arg := disk_config.get('lvm_config', None)) is not None:
140140
config.lvm_config = LvmConfiguration.parse_arg(lvm_arg, config)
141141

142142
return config

archinstall/lib/disk/filesystem.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ def setup_lvm(
148148
# figure out what the actual available size in the group is
149149
lvm_gp_info = device_handler.lvm_group_info(vol_gp.name)
150150

151+
if not lvm_gp_info:
152+
raise ValueError('Unable to fetch VG info')
153+
151154
# the actual available LVM Group size will be smaller than the
152155
# total PVs size due to reserved metadata storage etc.
153156
# so we'll have a look at the total avail. size, check the delta
@@ -184,7 +187,7 @@ def setup_lvm(
184187
device_handler.format(lv.fs_type, lv.safe_dev_path)
185188

186189
if lv.fs_type == FilesystemType.Btrfs:
187-
device_handler.create_btrfs_volumes(lv, enc_conf=self._enc_config)
190+
device_handler.create_lvm_btrfs_subvolumes(lv, enc_conf=self._enc_config)
188191

189192
def _lvm_vol_handle_e2scrub(self, vol_gp: LvmVolumeGroup):
190193
# from arch wiki:

0 commit comments

Comments
 (0)