make is_esp a singledispatch

This commit is contained in:
Michael Hudson-Doyle 2021-06-04 13:09:44 +12:00
parent faea101e12
commit 15b8bee1fb
6 changed files with 44 additions and 56 deletions

View File

@ -18,6 +18,7 @@ import functools
from subiquity.models.filesystem import (
Disk,
Bootloader,
Partition,
)
@ -58,13 +59,40 @@ def _can_be_boot_device_disk(disk, *, with_reformatting=False):
else:
return disk._partitions[0].flag == "bios_grub"
elif bl == Bootloader.UEFI:
return any(p.is_esp for p in disk._partitions)
return any(is_esp(p) for p in disk._partitions)
elif bl == Bootloader.PREP:
return any(p.flag == "prep" for p in disk._partitions)
else:
return True
@functools.singledispatch
def is_esp(device):
"""Is `device` a UEFI ESP?"""
return False
@is_esp.register(Partition)
def _is_esp_partition(partition):
if partition.device.type != "disk":
return False
if partition.device.ptable == "gpt":
return partition.flag == "boot"
else:
blockdev_raw = partition._m._probe_data['blockdev'].get(
partition._path())
if blockdev_raw is None:
return False
typecode = blockdev_raw.get("ID_PART_ENTRY_TYPE")
if typecode is None:
return False
try:
return int(typecode, 0) == 0xef
except ValueError:
# In case there was garbage in the udev entry...
return False
def all_boot_devices(model):
"""Return all current boot devices for `model`."""
return [disk for disk in model.all_disks() if is_boot_device(disk)]

View File

@ -16,6 +16,7 @@
import functools
from subiquity.common import types
from subiquity.common.filesystem import boot
from subiquity.models.filesystem import (
Disk,
LVM_LogicalVolume,
@ -65,7 +66,7 @@ def _annotations_partition(partition):
else:
# boot loader partition
r.append(_("unconfigured"))
elif partition.is_esp:
elif boot.is_esp(partition):
if partition.fs() and partition.fs().mount():
r.append(_("primary ESP"))
elif partition.grub_device:
@ -194,7 +195,7 @@ def _usage_labels_generic(device):
if m:
# A filesytem
r.append(_("mounted at {path}").format(path=m.path))
elif not getattr(device, 'is_esp', False):
elif not boot.is_esp(device):
# A filesytem
r.append(_("not mounted"))
elif fs.preserve:

View File

@ -343,7 +343,7 @@ class FilesystemManipulator:
elif bootloader == Bootloader.UEFI:
should_mount = self.model._mount_for_path('/boot/efi') is None
for p in new_boot_disk.partitions():
if p.is_esp:
if boot.is_esp(p):
p.grub_device = True
if should_mount:
self._mount_esp(p)

View File

@ -677,31 +677,13 @@ class Partition(_Formattable):
def _path(self):
return partition_kname(self.device.path, self._number)
@property
def is_esp(self):
if self.device.type != "disk":
return False
if self.device.ptable == "gpt":
return self.flag == "boot"
else:
blockdev_raw = self._m._probe_data['blockdev'].get(self._path())
if blockdev_raw is None:
return False
typecode = blockdev_raw.get("ID_PART_ENTRY_TYPE")
if typecode is None:
return False
try:
return int(typecode, 0) == 0xef
except ValueError:
# In case there was garbage in the udev entry...
return False
@property
def is_bootloader_partition(self):
from subiquity.common.filesystem import boot
if self._m.bootloader == Bootloader.BIOS:
return self.flag == "bios_grub"
elif self._m.bootloader == Bootloader.UEFI:
return self.is_esp
return boot.is_esp(self)
elif self._m.bootloader == Bootloader.PREP:
return self.flag == "prep"
else:
@ -817,10 +799,6 @@ class LVM_LogicalVolume(_Formattable):
def flag(self):
return None # hack!
@property
def is_esp(self):
return False # another hack!
ok_for_raid = False
ok_for_lvm_vg = False
@ -892,6 +870,7 @@ class Mount:
spec = attr.ib(default=None)
def can_delete(self):
from subiquity.common.filesystem import boot
# Can't delete mount of /boot/efi or swap, anything else is fine.
if not self.path:
# swap mount
@ -899,7 +878,7 @@ class Mount:
if not isinstance(self.device.volume, Partition):
# Can't be /boot/efi if volume is not a partition
return True
if self.device.volume.is_esp:
if boot.is_esp(self.device.volume):
# /boot/efi
return False
return True

View File

@ -285,26 +285,6 @@ class TestFilesystemModel(unittest.TestCase):
self.assertFalse(lv.ok_for_raid)
self.assertFalse(lv.ok_for_lvm_vg)
def test_is_esp(self):
model = make_model(Bootloader.UEFI)
gpt_disk = make_disk(model, ptable='gpt')
not_gpt_esp = make_partition(model, gpt_disk)
self.assertFalse(not_gpt_esp.is_esp)
gpt_esp = make_partition(model, gpt_disk, flag='boot')
self.assertTrue(gpt_esp.is_esp)
dos_disk = make_disk(model, ptable='msdos')
not_dos_esp = make_partition(model, dos_disk)
dos_esp = make_partition(model, dos_disk)
model._probe_data = {
'blockdev': {
dos_esp._path(): {'ID_PART_ENTRY_TYPE': '0xef'},
not_dos_esp._path(): {'ID_PART_ENTRY_TYPE': '0x83'},
}
}
self.assertFalse(not_dos_esp.is_esp)
self.assertTrue(dos_esp.is_esp)
def fake_up_blockdata_disk(disk, **kw):
model = disk._m

View File

@ -37,7 +37,7 @@ from subiquitycore.ui.container import Pile
from subiquitycore.ui.stretchy import Stretchy
from subiquitycore.ui.utils import rewrap
from subiquity.common.filesystem import labels
from subiquity.common.filesystem import boot, labels
from subiquity.models.filesystem import (
align_up,
Disk,
@ -189,7 +189,7 @@ class PartitionForm(Form):
self.form_pile.contents[i] = (self.use_swap._table, o)
elif w is self.use_swap._table and not show_use:
self.form_pile.contents[i] = (self.mount._table, o)
if not getattr(self.device, 'is_esp', False):
if not boot.is_esp(self.device):
fstype_for_check = fstype
if fstype_for_check is None:
fstype_for_check = self.existing_fs_type
@ -381,14 +381,14 @@ class PartitionStretchy(Stretchy):
if partition.flag in ["bios_grub", "prep"]:
label = None
initial['mount'] = None
elif partition.is_esp and not partition.grub_device:
elif boot.is_esp(partition) and not partition.grub_device:
label = None
else:
label = _("Save")
initial['size'] = humanize_size(self.partition.size)
max_size += self.partition.size
if not partition.is_esp:
if not boot.is_esp(partition):
initial.update(initial_data_for_fs(self.partition.fs()))
else:
if partition.fs() and partition.fs().mount():
@ -422,7 +422,7 @@ class PartitionStretchy(Stretchy):
self.form.buttons.base_widget[0].set_label(_("OK"))
if partition is not None:
if partition.is_esp:
if boot.is_esp(partition):
if partition.original_fstype():
opts = [
Option((
@ -465,7 +465,7 @@ class PartitionStretchy(Stretchy):
rows = []
focus_index = 0
if partition is not None:
if self.partition.is_esp:
if boot.is_esp(self.partition):
if self.partition.grub_device:
desc = _(configured_boot_partition_description)
if self.partition.preserve:
@ -536,7 +536,7 @@ class PartitionStretchy(Stretchy):
def done(self, form):
log.debug("Add Partition Result: {}".format(form.as_data()))
data = form.as_data()
if self.partition is not None and self.partition.is_esp:
if self.partition is not None and boot.is_esp(self.partition):
if self.partition.original_fstype() is None:
data['fstype'] = self.partition.fs().fstype
if self.partition.fs().mount() is not None: