Cleanup imports and disk.py a bit more
This commit is contained in:
parent
b2d20647fc
commit
658e5c0411
|
|
@ -1,19 +1,19 @@
|
||||||
"""Arch Linux installer - guided, templates etc."""
|
"""Arch Linux installer - guided, templates etc."""
|
||||||
from .lib.general import *
|
|
||||||
from .lib.disk import *
|
from .lib.disk import *
|
||||||
from .lib.user_interaction import *
|
|
||||||
from .lib.exceptions import *
|
from .lib.exceptions import *
|
||||||
|
from .lib.general import *
|
||||||
|
from .lib.hardware import *
|
||||||
from .lib.installer import __packages__, Installer
|
from .lib.installer import __packages__, Installer
|
||||||
from .lib.profiles import *
|
from .lib.locale_helpers import *
|
||||||
from .lib.luks import *
|
from .lib.luks import *
|
||||||
from .lib.mirrors import *
|
from .lib.mirrors import *
|
||||||
from .lib.networking import *
|
from .lib.networking import *
|
||||||
from .lib.locale_helpers import *
|
|
||||||
from .lib.services import *
|
|
||||||
from .lib.packages import *
|
|
||||||
from .lib.output import *
|
from .lib.output import *
|
||||||
|
from .lib.packages import *
|
||||||
|
from .lib.profiles import *
|
||||||
|
from .lib.services import *
|
||||||
from .lib.storage import *
|
from .lib.storage import *
|
||||||
from .lib.hardware import *
|
from .lib.user_interaction import *
|
||||||
|
|
||||||
__version__ = "2.2.0.dev1"
|
__version__ = "2.2.0.dev1"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,11 @@ ROOT_DIR_PATTERN = re.compile('^.*?/devices')
|
||||||
GPT = 0b00000001
|
GPT = 0b00000001
|
||||||
MBR = 0b00000010
|
MBR = 0b00000010
|
||||||
|
|
||||||
#import ctypes
|
|
||||||
#import ctypes.util
|
# import ctypes
|
||||||
#libc = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True)
|
# import ctypes.util
|
||||||
#libc.mount.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_ulong, ctypes.c_char_p)
|
# libc = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True)
|
||||||
|
# libc.mount.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_ulong, ctypes.c_char_p)
|
||||||
|
|
||||||
class BlockDevice():
|
class BlockDevice():
|
||||||
def __init__(self, path, info=None):
|
def __init__(self, path, info=None):
|
||||||
|
|
@ -50,9 +51,9 @@ class BlockDevice():
|
||||||
to give less/partial information for user readability.
|
to give less/partial information for user readability.
|
||||||
"""
|
"""
|
||||||
return {
|
return {
|
||||||
'path' : self.path,
|
'path': self.path,
|
||||||
'size' : self.info['size'] if 'size' in self.info else '<unknown>',
|
'size': self.info['size'] if 'size' in self.info else '<unknown>',
|
||||||
'model' : self.info['model'] if 'model' in self.info else '<unknown>'
|
'model': self.info['model'] if 'model' in self.info else '<unknown>'
|
||||||
}
|
}
|
||||||
|
|
||||||
def __dump__(self):
|
def __dump__(self):
|
||||||
|
|
@ -97,7 +98,7 @@ class BlockDevice():
|
||||||
def partitions(self):
|
def partitions(self):
|
||||||
o = b''.join(sys_command(['partprobe', self.path]))
|
o = b''.join(sys_command(['partprobe', self.path]))
|
||||||
|
|
||||||
#o = b''.join(sys_command('/usr/bin/lsblk -o name -J -b {dev}'.format(dev=dev)))
|
# o = b''.join(sys_command('/usr/bin/lsblk -o name -J -b {dev}'.format(dev=dev)))
|
||||||
o = b''.join(sys_command(['/usr/bin/lsblk', '-J', self.path]))
|
o = b''.join(sys_command(['/usr/bin/lsblk', '-J', self.path]))
|
||||||
|
|
||||||
if b'not a block device' in o:
|
if b'not a block device' in o:
|
||||||
|
|
@ -162,10 +163,10 @@ class Partition():
|
||||||
self.mountpoint = mountpoint
|
self.mountpoint = mountpoint
|
||||||
self.target_mountpoint = mountpoint
|
self.target_mountpoint = mountpoint
|
||||||
self.filesystem = filesystem
|
self.filesystem = filesystem
|
||||||
self.size = size # TODO: Refresh?
|
self.size = size # TODO: Refresh?
|
||||||
self._encrypted = None
|
self._encrypted = None
|
||||||
self.encrypted = encrypted
|
self.encrypted = encrypted
|
||||||
self.allow_formatting = False # A fail-safe for unconfigured partitions, such as windows NTFS partitions.
|
self.allow_formatting = False # A fail-safe for unconfigured partitions, such as windows NTFS partitions.
|
||||||
|
|
||||||
if mountpoint:
|
if mountpoint:
|
||||||
self.mount(mountpoint)
|
self.mount(mountpoint)
|
||||||
|
|
@ -190,7 +191,7 @@ class Partition():
|
||||||
left_comparitor = left_comparitor.path
|
left_comparitor = left_comparitor.path
|
||||||
else:
|
else:
|
||||||
left_comparitor = str(left_comparitor)
|
left_comparitor = str(left_comparitor)
|
||||||
return self.path < left_comparitor # Not quite sure the order here is correct. But /dev/nvme0n1p1 comes before /dev/nvme0n1p5 so seems correct.
|
return self.path < left_comparitor # Not quite sure the order here is correct. But /dev/nvme0n1p1 comes before /dev/nvme0n1p5 so seems correct.
|
||||||
|
|
||||||
def __repr__(self, *args, **kwargs):
|
def __repr__(self, *args, **kwargs):
|
||||||
mount_repr = ''
|
mount_repr = ''
|
||||||
|
|
@ -215,12 +216,13 @@ class Partition():
|
||||||
for partition in json.loads(lsblk.decode('UTF-8'))['blockdevices']:
|
for partition in json.loads(lsblk.decode('UTF-8'))['blockdevices']:
|
||||||
return partition.get('partuuid', None)
|
return partition.get('partuuid', None)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def encrypted(self):
|
def encrypted(self):
|
||||||
return self._encrypted
|
return self._encrypted
|
||||||
|
|
||||||
@encrypted.setter
|
@encrypted.setter
|
||||||
def encrypted(self, value :bool):
|
def encrypted(self, value: bool):
|
||||||
|
|
||||||
self._encrypted = value
|
self._encrypted = value
|
||||||
|
|
||||||
|
|
@ -250,7 +252,7 @@ class Partition():
|
||||||
if not get_filesystem_type(self.path):
|
if not get_filesystem_type(self.path):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
temporary_mountpoint = '/tmp/'+hashlib.md5(bytes(f"{time.time()}", 'UTF-8')+os.urandom(12)).hexdigest()
|
temporary_mountpoint = '/tmp/' + hashlib.md5(bytes(f"{time.time()}", 'UTF-8') + os.urandom(12)).hexdigest()
|
||||||
temporary_path = pathlib.Path(temporary_mountpoint)
|
temporary_path = pathlib.Path(temporary_mountpoint)
|
||||||
|
|
||||||
temporary_path.mkdir(parents=True, exist_ok=True)
|
temporary_path.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
@ -348,9 +350,9 @@ class Partition():
|
||||||
self.filesystem = 'f2fs'
|
self.filesystem = 'f2fs'
|
||||||
|
|
||||||
elif filesystem == 'crypto_LUKS':
|
elif filesystem == 'crypto_LUKS':
|
||||||
# from .luks import luks2
|
# from .luks import luks2
|
||||||
# encrypted_partition = luks2(self, None, None)
|
# encrypted_partition = luks2(self, None, None)
|
||||||
# encrypted_partition.format(path)
|
# encrypted_partition.format(path)
|
||||||
self.filesystem = 'crypto_LUKS'
|
self.filesystem = 'crypto_LUKS'
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
@ -416,16 +418,17 @@ class Partition():
|
||||||
try:
|
try:
|
||||||
self.format(self.filesystem, '/dev/null', log_formatting=False, allow_formatting=True)
|
self.format(self.filesystem, '/dev/null', log_formatting=False, allow_formatting=True)
|
||||||
except SysCallError:
|
except SysCallError:
|
||||||
pass # We supported it, but /dev/null is not formatable as expected so the mkfs call exited with an error code
|
pass # We supported it, but /dev/null is not formatable as expected so the mkfs call exited with an error code
|
||||||
except UnknownFilesystemFormat as err:
|
except UnknownFilesystemFormat as err:
|
||||||
raise err
|
raise err
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class Filesystem():
|
class Filesystem():
|
||||||
# TODO:
|
# TODO:
|
||||||
# When instance of a HDD is selected, check all usages and gracefully unmount them
|
# When instance of a HDD is selected, check all usages and gracefully unmount them
|
||||||
# as well as close any crypto handles.
|
# as well as close any crypto handles.
|
||||||
def __init__(self, blockdevice,mode):
|
def __init__(self, blockdevice, mode):
|
||||||
self.blockdevice = blockdevice
|
self.blockdevice = blockdevice
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
|
|
||||||
|
|
@ -469,11 +472,11 @@ class Filesystem():
|
||||||
if partition.target_mountpoint == mountpoint or partition.mountpoint == mountpoint:
|
if partition.target_mountpoint == mountpoint or partition.mountpoint == mountpoint:
|
||||||
return partition
|
return partition
|
||||||
|
|
||||||
def raw_parted(self, string:str):
|
def raw_parted(self, string: str):
|
||||||
x = sys_command(f'/usr/bin/parted -s {string}')
|
x = sys_command(f'/usr/bin/parted -s {string}')
|
||||||
return x
|
return x
|
||||||
|
|
||||||
def parted(self, string:str):
|
def parted(self, string: str):
|
||||||
"""
|
"""
|
||||||
Performs a parted execution of the given string
|
Performs a parted execution of the given string
|
||||||
|
|
||||||
|
|
@ -515,7 +518,7 @@ class Filesystem():
|
||||||
|
|
||||||
previous_partitions = self.blockdevice.partitions
|
previous_partitions = self.blockdevice.partitions
|
||||||
if self.mode == MBR:
|
if self.mode == MBR:
|
||||||
if len(self.blockdevice.partitions)>3:
|
if len(self.blockdevice.partitions) > 3:
|
||||||
DiskError("Too many partitions on disk, MBR disks can only have 3 parimary partitions")
|
DiskError("Too many partitions on disk, MBR disks can only have 3 parimary partitions")
|
||||||
if format:
|
if format:
|
||||||
partitioning = self.parted(f'{self.blockdevice.device} mkpart {type} {format} {start} {end}') == 0
|
partitioning = self.parted(f'{self.blockdevice.device} mkpart {type} {format} {start} {end}') == 0
|
||||||
|
|
@ -525,17 +528,18 @@ class Filesystem():
|
||||||
if partitioning:
|
if partitioning:
|
||||||
start_wait = time.time()
|
start_wait = time.time()
|
||||||
while previous_partitions == self.blockdevice.partitions:
|
while previous_partitions == self.blockdevice.partitions:
|
||||||
time.sleep(0.025) # Let the new partition come up in the kernel
|
time.sleep(0.025) # Let the new partition come up in the kernel
|
||||||
if time.time() - start_wait > 10:
|
if time.time() - start_wait > 10:
|
||||||
raise DiskError(f"New partition never showed up after adding new partition on {self} (timeout 10 seconds).")
|
raise DiskError(f"New partition never showed up after adding new partition on {self} (timeout 10 seconds).")
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def set_name(self, partition:int, name:str):
|
def set_name(self, partition: int, name: str):
|
||||||
return self.parted(f'{self.blockdevice.device} name {partition+1} "{name}"') == 0
|
return self.parted(f'{self.blockdevice.device} name {partition + 1} "{name}"') == 0
|
||||||
|
|
||||||
|
def set(self, partition: int, string: str):
|
||||||
|
return self.parted(f'{self.blockdevice.device} set {partition + 1} {string}') == 0
|
||||||
|
|
||||||
def set(self, partition:int, string:str):
|
|
||||||
return self.parted(f'{self.blockdevice.device} set {partition+1} {string}') == 0
|
|
||||||
|
|
||||||
def device_state(name, *args, **kwargs):
|
def device_state(name, *args, **kwargs):
|
||||||
# Based out of: https://askubuntu.com/questions/528690/how-to-get-list-of-all-non-removable-disk-device-names-ssd-hdd-and-sata-ide-onl/528709#528709
|
# Based out of: https://askubuntu.com/questions/528690/how-to-get-list-of-all-non-removable-disk-device-names-ssd-hdd-and-sata-ide-onl/528709#528709
|
||||||
|
|
@ -586,6 +590,7 @@ def harddrive(size=None, model=None, fuzzy=False):
|
||||||
|
|
||||||
return collection[drive]
|
return collection[drive]
|
||||||
|
|
||||||
|
|
||||||
def get_mount_info(path):
|
def get_mount_info(path):
|
||||||
try:
|
try:
|
||||||
output = b''.join(sys_command(f'/usr/bin/findmnt --json {path}'))
|
output = b''.join(sys_command(f'/usr/bin/findmnt --json {path}'))
|
||||||
|
|
@ -600,6 +605,7 @@ def get_mount_info(path):
|
||||||
|
|
||||||
return output['filesystems'][0]
|
return output['filesystems'][0]
|
||||||
|
|
||||||
|
|
||||||
def get_partitions_in_use(mountpoint):
|
def get_partitions_in_use(mountpoint):
|
||||||
try:
|
try:
|
||||||
output = b''.join(sys_command(f'/usr/bin/findmnt --json -R {mountpoint}'))
|
output = b''.join(sys_command(f'/usr/bin/findmnt --json -R {mountpoint}'))
|
||||||
|
|
@ -618,6 +624,7 @@ def get_partitions_in_use(mountpoint):
|
||||||
|
|
||||||
return mounts
|
return mounts
|
||||||
|
|
||||||
|
|
||||||
def get_filesystem_type(path):
|
def get_filesystem_type(path):
|
||||||
try:
|
try:
|
||||||
handle = sys_command(f"blkid -o value -s TYPE {path}")
|
handle = sys_command(f"blkid -o value -s TYPE {path}")
|
||||||
|
|
@ -625,6 +632,7 @@ def get_filesystem_type(path):
|
||||||
except SysCallError:
|
except SysCallError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def disk_layouts():
|
def disk_layouts():
|
||||||
try:
|
try:
|
||||||
handle = sys_command(f"lsblk -f -o+TYPE,SIZE -J")
|
handle = sys_command(f"lsblk -f -o+TYPE,SIZE -J")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue