[mount] Move zfs code into a seperate function to improve readability

This commit is contained in:
dalto 2021-11-13 14:09:16 -06:00
parent 18ad188ef6
commit ebae698a6e

View File

@ -62,10 +62,9 @@ def get_btrfs_subvolumes(partitions):
def parse_global_storage(gs_value): def parse_global_storage(gs_value):
""" """ Something in the chain is converting on and off to true and false. This converts it back.
Something in the chain is converting on and off to true and false. This converts it back.
:param gs_value: The value from global storage :param gs_value: The value from global storage which needs to be fixed
:return: :return:
""" """
if gs_value is True: if gs_value is True:
@ -76,57 +75,31 @@ def parse_global_storage(gs_value):
return gs_value return gs_value
def mount_partition(root_mount_point, partition, partitions): def mount_zfs(root_mount_point, partition):
""" Mounts a zfs partition at @p root_mount_point
:param root_mount_point: The absolute path to the root of the install
:param partition: The partition map from global storage for this partition
:return:
""" """
Do a single mount of @p partition inside @p root_mount_point. # Get the list of zpools from global storage
"""
# Create mount point with `+` rather than `os.path.join()` because
# `partition["mountPoint"]` starts with a '/'.
raw_mount_point = partition["mountPoint"]
if not raw_mount_point:
return
mount_point = root_mount_point + raw_mount_point
# Ensure that the created directory has the correct SELinux context on
# SELinux-enabled systems.
os.makedirs(mount_point, exist_ok=True)
try:
subprocess.call(['chcon', '--reference=' + raw_mount_point, mount_point])
except FileNotFoundError as e:
libcalamares.utils.warning(str(e))
except OSError:
libcalamares.utils.error("Cannot run 'chcon' normally.")
raise
fstype = partition.get("fs", "").lower()
if fstype == "unformatted":
return
if fstype == "fat16" or fstype == "fat32":
fstype = "vfat"
device = partition["device"]
if "luksMapperName" in partition:
device = os.path.join("/dev/mapper", partition["luksMapperName"])
if fstype == "zfs":
zfs_pool_list = libcalamares.globalstorage.value("zfsPoolInfo") zfs_pool_list = libcalamares.globalstorage.value("zfsPoolInfo")
if not zfs_pool_list: if not zfs_pool_list:
libcalamares.utils.warning("Failed to locate zfsPoolInfo data in global storage") libcalamares.utils.warning("Failed to locate zfsPoolInfo data in global storage")
raise Exception("Internal error mounting zfs datasets") raise Exception("Internal error mounting zfs datasets")
# Find the zpool matching this partition
for zfs_pool in zfs_pool_list: for zfs_pool in zfs_pool_list:
if zfs_pool["mountpoint"] == partition["mountPoint"]: if zfs_pool["mountpoint"] == partition["mountPoint"]:
pool_name = zfs_pool["poolName"] pool_name = zfs_pool["poolName"]
ds_name = zfs_pool["dsName"]; ds_name = zfs_pool["dsName"]
# import the zpool # import the zpool
import_result = subprocess.run(['zpool', 'import', '-R', root_mount_point, pool_name]) import_result = subprocess.run(['zpool', 'import', '-R', root_mount_point, pool_name])
if import_result.returncode != 0: if import_result.returncode != 0:
raise Exception("Failed to import zpool") raise Exception("Failed to import zpool")
# Get the encrpytion information from global storage
zfs_info_list = libcalamares.globalstorage.value("zfsInfo") zfs_info_list = libcalamares.globalstorage.value("zfsInfo")
encrypt = False encrypt = False
if zfs_info_list: if zfs_info_list:
@ -177,6 +150,45 @@ def mount_partition(root_mount_point, partition, partitions):
set_result = subprocess.run(['zfs', 'set', 'canmount=on', pool_name + '/' + ds_name]) set_result = subprocess.run(['zfs', 'set', 'canmount=on', pool_name + '/' + ds_name])
if set_result.returncode != 0: if set_result.returncode != 0:
raise Exception("Failed to set zfs mountpoint") raise Exception("Failed to set zfs mountpoint")
def mount_partition(root_mount_point, partition, partitions):
"""
Do a single mount of @p partition inside @p root_mount_point.
"""
# Create mount point with `+` rather than `os.path.join()` because
# `partition["mountPoint"]` starts with a '/'.
raw_mount_point = partition["mountPoint"]
if not raw_mount_point:
return
mount_point = root_mount_point + raw_mount_point
# Ensure that the created directory has the correct SELinux context on
# SELinux-enabled systems.
os.makedirs(mount_point, exist_ok=True)
try:
subprocess.call(['chcon', '--reference=' + raw_mount_point, mount_point])
except FileNotFoundError as e:
libcalamares.utils.warning(str(e))
except OSError:
libcalamares.utils.error("Cannot run 'chcon' normally.")
raise
fstype = partition.get("fs", "").lower()
if fstype == "unformatted":
return
if fstype == "fat16" or fstype == "fat32":
fstype = "vfat"
device = partition["device"]
if "luksMapperName" in partition:
device = os.path.join("/dev/mapper", partition["luksMapperName"])
if fstype == "zfs":
mount_zfs(root_mount_point, partition)
else: # fstype == "zfs" else: # fstype == "zfs"
if libcalamares.utils.mount(device, if libcalamares.utils.mount(device,
mount_point, mount_point,