diff --git a/subiquity/controllers/filesystem.py b/subiquity/controllers/filesystem.py index 5dbd5437..03cb64ce 100644 --- a/subiquity/controllers/filesystem.py +++ b/subiquity/controllers/filesystem.py @@ -161,7 +161,7 @@ class FilesystemController(BaseController): raise Exception("could not resolve {}".format(id)) def _action_clean_fstype(self, fstype): - return self.model.fs_by_name[fstype] + return self.model.get_fs_by_name(fstype) def _action_clean_devices_raid(self, devices): return { @@ -306,7 +306,7 @@ class FilesystemController(BaseController): disk, dict( size=part_size, - fstype=self.model.fs_by_name['fat32'], + fstype=self.model.get_fs_by_name('fat32'), mount='/boot/efi'), flag="boot") elif self.is_prep(): diff --git a/subiquity/models/filesystem.py b/subiquity/models/filesystem.py index 212f8b98..2b5060f6 100644 --- a/subiquity/models/filesystem.py +++ b/subiquity/models/filesystem.py @@ -807,7 +807,7 @@ class Filesystem: def _available(self): # False if mounted or if fs does not require a mount, True otherwise. if self._mount is None: - fs_obj = FilesystemModel.fs_by_name[self.fstype] + fs_obj = FilesystemModel.get_fs_by_name(self.fstype) return fs_obj.is_mounted else: return False @@ -850,22 +850,25 @@ class FilesystemModel(object): ('ext4', True, FS('ext4', True)), ('xfs', True, FS('xfs', True)), ('btrfs', True, FS('btrfs', True)), - ('---', False), + ('---', False, None), ('swap', True, FS('swap', False)), - ('---', False), + ('---', False, None), ('leave unformatted', True, FS(None, False)), ] - fs_by_name = {} - longest_fs_name = 0 + _fs_by_name = {} for t in supported_filesystems: - if len(t) > 2: - fs = t[2] - if fs.label is not None: - if len(fs.label) > longest_fs_name: - longest_fs_name = len(fs.label) - fs_by_name[fs.label] = fs - fs_by_name['fat32'] = FS('fat32', True) + fs = t[2] + if fs is not None: + _fs_by_name[fs.label] = fs + + @classmethod + def get_fs_by_name(cls, fstype): + if fstype in cls._fs_by_name: + return cls._fs_by_name[fstype] + else: + fs = cls._fs_by_name[fstype] = FS(fstype, True) + return fs def __init__(self): self._disk_info = [] diff --git a/subiquity/ui/views/filesystem/guided.py b/subiquity/ui/views/filesystem/guided.py index 3a9ea22a..67af5ac8 100644 --- a/subiquity/ui/views/filesystem/guided.py +++ b/subiquity/ui/views/filesystem/guided.py @@ -145,10 +145,11 @@ class GuidedDiskSelectionView(BaseView): def choose_disk(self, btn, disk_path): self.model.reset() disk = self.model.disk_by_path(disk_path) + ext4 = self.model.get_fs_by_name("ext4") if self.method == "direct": result = { "size": disk.free_for_partitions, - "fstype": self.model.fs_by_name["ext4"], + "fstype": ext4, "mount": "/", } self.controller.partition_disk_handler(disk, None, result) @@ -158,7 +159,7 @@ class GuidedDiskSelectionView(BaseView): self.controller.create_partition( device=disk, spec=dict( size=dehumanize_size('1G'), - fstype=self.model.fs_by_name['ext4'], + fstype=ext4, mount='/boot' )) part = self.controller.create_partition( @@ -175,7 +176,7 @@ class GuidedDiskSelectionView(BaseView): vg=vg, spec=dict( size=dehumanize_size("4G"), name="ubuntu-lv", - fstype=self.model.fs_by_name['ext4'], + fstype=ext4, mount="/", )) else: diff --git a/subiquity/ui/views/filesystem/partition.py b/subiquity/ui/views/filesystem/partition.py index 49347fb1..42b1fbe2 100644 --- a/subiquity/ui/views/filesystem/partition.py +++ b/subiquity/ui/views/filesystem/partition.py @@ -245,7 +245,7 @@ class PartitionStretchy(Stretchy): fs = self.partition.fs() if fs is not None: if partition.flag != "boot": - initial['fstype'] = self.model.fs_by_name[fs.fstype] + initial['fstype'] = self.model.get_fs_by_name(fs.fstype) mount = fs.mount() if mount is not None: initial['mount'] = mount.path @@ -253,7 +253,7 @@ class PartitionStretchy(Stretchy): else: initial['mount'] = None else: - initial['fstype'] = self.model.fs_by_name[None] + initial['fstype'] = self.model.get_fs_by_name(None) if isinstance(disk, LVM_VolGroup): initial['name'] = partition.name lvm_names.remove(partition.name) @@ -281,7 +281,9 @@ class PartitionStretchy(Stretchy): if partition is not None: if partition.flag == "boot": opts = [ - Option(("fat32", True, self.model.fs_by_name["fat32"])), + Option( + ("fat32", True, self.model.get_fs_by_name("fat32")), + ), ] self.form.fstype.widget.options = opts self.form.fstype.widget.index = 0 @@ -344,7 +346,8 @@ class PartitionStretchy(Stretchy): log.debug("Add Partition Result: {}".format(form.as_data())) data = form.as_data() if self.partition is not None and self.partition.flag == "boot": - data['fstype'] = self.model.fs_by_name[self.partition.fs().fstype] + data['fstype'] = self.model.get_fs_by_name( + self.partition.fs().fstype) data['mount'] = self.partition.fs().mount().path if isinstance(self.disk, LVM_VolGroup): handler = self.controller.logical_volume_handler @@ -369,13 +372,13 @@ class FormatEntireStretchy(Stretchy): initial = {} fs = device.fs() if fs is not None: - initial['fstype'] = self.model.fs_by_name[fs.fstype] + initial['fstype'] = self.model.get_fs_by_name(fs.fstype) mount = fs.mount() if mount is not None: initial['mount'] = mount.path del mountpoints[mount.path] else: - initial['fstype'] = self.model.fs_by_name[None] + initial['fstype'] = self.model.get_fs_by_name(None) self.form = PartitionForm(mountpoints, 0, initial, False, {}) self.form.remove_field('size') self.form.remove_field('name') diff --git a/subiquity/ui/views/filesystem/tests/test_partition.py b/subiquity/ui/views/filesystem/tests/test_partition.py index 9dd1cc60..7226e56c 100644 --- a/subiquity/ui/views/filesystem/tests/test_partition.py +++ b/subiquity/ui/views/filesystem/tests/test_partition.py @@ -54,7 +54,7 @@ class PartitionViewTests(unittest.TestCase): def test_create_partition(self): valid_data = { 'size': "1M", - 'fstype': FilesystemModel.fs_by_name["ext4"], + 'fstype': FilesystemModel.get_fs_by_name("ext4"), } model, disk = make_model_and_disk() view, stretchy = make_view(model, disk) @@ -68,7 +68,7 @@ class PartitionViewTests(unittest.TestCase): def test_edit_partition(self): form_data = { 'size': "256M", - 'fstype': FilesystemModel.fs_by_name['xfs'], + 'fstype': FilesystemModel.get_fs_by_name("xfs"), } model, disk = make_model_and_disk() partition = model.add_partition(disk, 512*(2**20)) @@ -79,7 +79,7 @@ class PartitionViewTests(unittest.TestCase): view_helpers.click(stretchy.form.done_btn.base_widget) expected_data = { 'size': dehumanize_size(form_data['size']), - 'fstype': FilesystemModel.fs_by_name['xfs'], + 'fstype': FilesystemModel.get_fs_by_name('xfs'), 'mount': None, } view.controller.partition_disk_handler.assert_called_once_with( @@ -98,7 +98,7 @@ class PartitionViewTests(unittest.TestCase): view_helpers.click(stretchy.form.done_btn.base_widget) expected_data = { 'size': dehumanize_size(form_data['size']), - 'fstype': FilesystemModel.fs_by_name["fat32"], + 'fstype': FilesystemModel.get_fs_by_name("fat32"), 'mount': '/boot/efi', } view.controller.partition_disk_handler.assert_called_once_with(