11import json
22import time
3+ from collections .abc import Iterable
34from pathlib import Path
45from 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
79from archinstall .lib .exceptions import SysCallError
810from 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+
119128def 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