2019-11-06 02:21:04 -05:00
|
|
|
import os
|
|
|
|
import uuid
|
2019-11-04 23:18:28 -05:00
|
|
|
##
|
2019-11-06 02:21:04 -05:00
|
|
|
import blkinfo
|
2019-11-05 05:52:46 -05:00
|
|
|
import psutil # Do I need this if I can have libblockdev's mounts API? Is there a way to get current mounts?
|
2019-11-04 23:18:28 -05:00
|
|
|
##
|
|
|
|
import aif.constants
|
|
|
|
import aif.utils
|
2019-11-04 23:38:32 -05:00
|
|
|
from . import _common
|
2019-09-30 22:08:37 -04:00
|
|
|
|
2019-11-04 23:38:32 -05:00
|
|
|
|
|
|
|
_BlockDev = _common.BlockDev
|
2019-11-03 01:40:23 -05:00
|
|
|
|
2019-11-06 02:21:04 -05:00
|
|
|
# TODO: LOGGING!
|
2019-11-04 23:18:28 -05:00
|
|
|
class Partition(object):
|
|
|
|
def __init__(self, part_xml, diskobj, start_sector, partnum, tbltype, part_type = None):
|
|
|
|
# Belive it or not, dear reader, but this *entire method* is just to set attributes.
|
|
|
|
if tbltype not in ('gpt', 'msdos'):
|
|
|
|
raise ValueError('{0} must be one of gpt or msdos'.format(tbltype))
|
|
|
|
if tbltype == 'msdos' and part_type not in ('primary', 'extended', 'logical'):
|
|
|
|
raise ValueError(('You must specify if this is a '
|
|
|
|
'primary, extended, or logical partition for msdos partition tables'))
|
|
|
|
aif.disk._common.addBDPlugin('part')
|
|
|
|
self.xml = part_xml
|
|
|
|
self.id = part_xml.attrib['id']
|
|
|
|
self.table_type = getattr(_BlockDev.PartTableType, tbltype.upper())
|
|
|
|
if tbltype == 'msdos':
|
2019-11-05 05:52:46 -05:00
|
|
|
# Could technically be _BlockDev.PartTypeReq.NEXT BUT that doesn't *quite* work
|
|
|
|
# with this project's structure.
|
2019-11-04 23:18:28 -05:00
|
|
|
if part_type == 'primary':
|
|
|
|
self.part_type = _BlockDev.PartTypeReq.NORMAL
|
|
|
|
elif part_type == 'extended':
|
|
|
|
self.part_type = _BlockDev.PartTypeReq.EXTENDED
|
|
|
|
elif part_type == 'logical':
|
|
|
|
self.part_type = _BlockDev.PartTypeReq.LOGICAL
|
2019-11-06 02:21:04 -05:00
|
|
|
elif tbltype == 'gpt':
|
|
|
|
self.part_type = _BlockDev.PartTypeReq.NORMAL
|
2019-11-04 23:18:28 -05:00
|
|
|
self.flags = []
|
|
|
|
self.partnum = partnum
|
2019-11-06 02:21:04 -05:00
|
|
|
self.fs_type = self.xml.attrib['fsType']
|
2019-11-04 23:18:28 -05:00
|
|
|
self.disk = diskobj
|
2019-11-05 05:52:46 -05:00
|
|
|
self.device = self.disk.path
|
2019-11-06 02:21:04 -05:00
|
|
|
self.devpath = '{0}{1}'.format(self.device, self.partnum)
|
2019-11-04 23:18:28 -05:00
|
|
|
self.is_hiformatted = False
|
|
|
|
sizes = {}
|
|
|
|
for s in ('start', 'stop'):
|
|
|
|
x = dict(zip(('from_bgn', 'size', 'type'),
|
|
|
|
aif.utils.convertSizeUnit(self.xml.attrib[s])))
|
|
|
|
sectors = x['size']
|
|
|
|
if x['type'] == '%':
|
2019-11-06 02:21:04 -05:00
|
|
|
sectors = int(int(self.disk.size / self.disk.sector_size) * (0.01 * x['size']))
|
2019-11-04 23:18:28 -05:00
|
|
|
else:
|
|
|
|
sectors = int(aif.utils.size.convertStorage(x['size'],
|
|
|
|
x['type'],
|
2019-11-06 02:21:04 -05:00
|
|
|
target = 'B') / self.disk.sector_size)
|
2019-11-04 23:18:28 -05:00
|
|
|
sizes[s] = (sectors, x['from_bgn'])
|
|
|
|
if sizes['start'][1] is not None:
|
|
|
|
if sizes['start'][1]:
|
|
|
|
self.begin = sizes['start'][0] + 0
|
|
|
|
else:
|
2019-11-06 02:21:04 -05:00
|
|
|
self.begin = int(self.disk.size / self.disk.sector_size) - sizes['start'][0]
|
2019-11-04 23:18:28 -05:00
|
|
|
else:
|
|
|
|
self.begin = sizes['start'][0] + start_sector
|
|
|
|
if sizes['stop'][1] is not None:
|
|
|
|
if sizes['stop'][1]:
|
|
|
|
self.end = sizes['stop'][0] + 0
|
|
|
|
else:
|
|
|
|
# This *technically* should be - 34, at least for gpt, but the alignment optimizer fixes it for us.
|
2019-11-06 02:21:04 -05:00
|
|
|
self.end = (int(self.disk.size / self.disk.sector_size) - 1) - sizes['stop'][0]
|
2019-11-04 23:18:28 -05:00
|
|
|
else:
|
|
|
|
self.end = self.begin + sizes['stop'][0]
|
2019-11-06 02:21:04 -05:00
|
|
|
self.size = (self.end - self.begin)
|
2019-11-04 23:18:28 -05:00
|
|
|
self.part_name = self.xml.attrib.get('name')
|
2019-11-06 02:21:04 -05:00
|
|
|
self.partition = None
|
|
|
|
self._initFlags()
|
|
|
|
self._initFstype()
|
|
|
|
|
|
|
|
def _initFlags(self):
|
|
|
|
for f in self.xml.findall('partitionFlag'):
|
|
|
|
# *Technically* we could use e.g. getattr(_BlockDev.PartFlag, f.text.upper()), *but* we lose compat
|
|
|
|
# with parted's flags if we do that. :| So we do some funky logic both here and in the constants.
|
|
|
|
if f.text in aif.constants.PARTED_BD_MAP:
|
|
|
|
flag_id = aif.constants.BD_PART_FLAGS_FLAG_IDX[aif.constants.PARTED_BD_MAP[f.text]]
|
|
|
|
elif f.text in aif.constants.BD_PART_FLAGS_FRIENDLY:
|
|
|
|
flag_id = aif.constants.BD_PART_FLAGS_FLAG_IDX[aif.constants.BD_PART_FLAGS_FRIENDLY[f.text]]
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
self.flags.append(_BlockDev.PartFlag(flag_id))
|
|
|
|
return()
|
|
|
|
|
|
|
|
def _initFstype(self):
|
|
|
|
_err = ('{0} is not a valid partition filesystem type; '
|
|
|
|
'must be one of {1} or an fdisk-compatible GPT GUID').format(
|
|
|
|
self.xml.attrib['fsType'],
|
|
|
|
', '.join(sorted(aif.constants.PARTED_FSTYPES)))
|
|
|
|
if self.fs_type in aif.constants.PARTED_FSTYPES_GUIDS.keys():
|
|
|
|
self.fs_type = aif.constants.PARTED_FSTYPES_GUIDS[self.fs_type]
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
self.fs_type = uuid.UUID(hex = self.fs_type)
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError(_err)
|
|
|
|
if self.fs_type not in aif.constants.GPT_GUID_IDX.keys():
|
|
|
|
raise ValueError(_err)
|
|
|
|
return()
|
|
|
|
|
|
|
|
def format(self):
|
|
|
|
# This is a safeguard. We do *not* want to partition a disk that is mounted.
|
|
|
|
aif.utils.checkMounted(self.devpath)
|
|
|
|
self.partition = _BlockDev.part.create_part(self.device,
|
|
|
|
self.part_type,
|
|
|
|
self.begin,
|
|
|
|
self.size,
|
|
|
|
_BlockDev.PartAlign.OPTIMAL)
|
|
|
|
self.devpath = self.partition.path
|
|
|
|
_BlockDev.part.set_part_type(self.device, self.devpath, str(self.fs_type).upper())
|
|
|
|
if self.part_name:
|
|
|
|
_BlockDev.part.set_part_name(self.device, self.devpath, self.part_name)
|
|
|
|
if self.flags:
|
|
|
|
for f in self.flags:
|
|
|
|
_BlockDev.part.set_part_flag(self.device, self.devpath, f, True)
|
|
|
|
return()
|
2019-11-04 23:18:28 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# def detect(self):
|
|
|
|
# pass # TODO; blkinfo?
|
|
|
|
|
|
|
|
|
|
|
|
class Disk(object):
|
|
|
|
def __init__(self, disk_xml):
|
|
|
|
self.xml = disk_xml
|
2019-11-06 02:21:04 -05:00
|
|
|
self.devpath = os.path.realpath(self.xml.attrib['device'])
|
|
|
|
aif.disk._common.addBDPlugin('part')
|
2019-11-04 23:18:28 -05:00
|
|
|
self.is_lowformatted = None
|
|
|
|
self.is_hiformatted = None
|
|
|
|
self.is_partitioned = None
|
|
|
|
self.partitions = None
|
|
|
|
self._initDisk()
|
|
|
|
|
|
|
|
def _initDisk(self):
|
2019-11-06 02:21:04 -05:00
|
|
|
if self.devpath == 'auto':
|
|
|
|
self.devpath = '/dev/{0}'.format(blkinfo.BlkDiskInfo().get_disks()[0]['kname'])
|
|
|
|
if not os.path.isfile(self.devpath):
|
|
|
|
raise ValueError('{0} does not exist; please specify an explicit device path'.format(self.devpath))
|
|
|
|
self.table_type = self.xml.attrib.get('diskFormat', 'gpt').lower()
|
|
|
|
if self.table_type in ('bios', 'mbr', 'dos', 'msdos'):
|
|
|
|
self.table_type = _BlockDev.PartTableType.MSDOS
|
|
|
|
elif self.table_type == 'gpt':
|
|
|
|
self.table_type = _BlockDev.PartTableType.GPT
|
|
|
|
else:
|
|
|
|
raise ValueError(('Disk format {0} is not valid for this architecture;'
|
|
|
|
'must be one of: gpt or msdos'.format(self.table_type)))
|
|
|
|
self.device = self.disk = _BlockDev.part.get_disk_spec(self.devpath)
|
|
|
|
self.is_lowformatted = False
|
|
|
|
self.is_hiformatted = False
|
|
|
|
self.is_partitioned = False
|
|
|
|
self.partitions = []
|
|
|
|
return()
|
2019-11-04 23:18:28 -05:00
|
|
|
|
|
|
|
def diskFormat(self):
|
2019-11-06 02:21:04 -05:00
|
|
|
if self.is_lowformatted:
|
|
|
|
return ()
|
|
|
|
# This is a safeguard. We do *not* want to low-format a disk that is mounted.
|
|
|
|
aif.utils.checkMounted(self.devpath)
|
|
|
|
# TODO: BlockDev.part.set_disk_flag(<disk>,
|
|
|
|
# BlockDev.PartDiskFlag(1),
|
|
|
|
# True) ??
|
|
|
|
# https://lazka.github.io/pgi-docs/BlockDev-2.0/enums.html#BlockDev.PartDiskFlag
|
|
|
|
# https://unix.stackexchange.com/questions/325886/bios-gpt-do-we-need-a-boot-flag
|
|
|
|
_BlockDev.part.create_table(self.devpath, self.table_type, True)
|
|
|
|
self.is_lowformatted = True
|
|
|
|
self.is_partitioned = False
|
|
|
|
return()
|
2019-11-04 23:18:28 -05:00
|
|
|
|
|
|
|
def getPartitions(self):
|
2019-11-06 02:21:04 -05:00
|
|
|
# For GPT, this *technically* should be 34 -- or, more precisely, 2048 (see FAQ in manual), but the alignment
|
|
|
|
# optimizer fixes it for us automatically.
|
|
|
|
# But for DOS tables, it's required.
|
|
|
|
if self.table_type == 'msdos':
|
|
|
|
start_sector = 2048
|
|
|
|
else:
|
|
|
|
start_sector = 0
|
|
|
|
self.partitions = []
|
|
|
|
xml_partitions = self.xml.findall('part')
|
|
|
|
for idx, part in enumerate(xml_partitions):
|
|
|
|
partnum = idx + 1
|
|
|
|
if self.table_type == 'gpt':
|
|
|
|
p = Partition(part, self.disk, start_sector, partnum, self.table_type)
|
|
|
|
else:
|
|
|
|
parttype = 'primary'
|
|
|
|
if len(xml_partitions) > 4:
|
|
|
|
if partnum == 4:
|
|
|
|
parttype = 'extended'
|
|
|
|
elif partnum > 4:
|
|
|
|
parttype = 'logical'
|
|
|
|
p = Partition(part, self.disk, start_sector, partnum, self.table_type, part_type = parttype)
|
|
|
|
start_sector = p.end + 1
|
|
|
|
self.partitions.append(p)
|
|
|
|
return()
|
2019-11-04 23:18:28 -05:00
|
|
|
|
|
|
|
def partFormat(self):
|
2019-11-06 02:21:04 -05:00
|
|
|
if self.is_partitioned:
|
|
|
|
return()
|
|
|
|
if not self.is_lowformatted:
|
|
|
|
self.diskFormat()
|
|
|
|
# This is a safeguard. We do *not* want to partition a disk that is mounted.
|
|
|
|
aif.utils.checkMounted(self.devpath)
|
|
|
|
if not self.partitions:
|
|
|
|
self.getPartitions()
|
|
|
|
if not self.partitions:
|
|
|
|
return()
|
|
|
|
for p in self.partitions:
|
|
|
|
p.format()
|
|
|
|
p.is_hiformatted = True
|
|
|
|
self.is_partitioned = True
|
|
|
|
return ()
|