calamares/src/modules/mount/main.py

159 lines
6.6 KiB
Python
Raw Normal View History

2014-07-22 17:38:20 +02:00
#!/usr/bin/env python3
2015-02-18 15:06:10 +01:00
# -*- coding: utf-8 -*-
#
# === This file is part of Calamares - <https://calamares.io> ===
2014-07-22 17:38:20 +02:00
#
# SPDX-FileCopyrightText: 2014 Aurélien Gâteau <agateau@kde.org>
# SPDX-FileCopyrightText: 2017 Alf Gaida <agaida@siduction.org>
# SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
# SPDX-FileCopyrightText: 2019 Kevin Kofler <kevin.kofler@chello.at>
# SPDX-FileCopyrightText: 2019-2020 Collabora Ltd
# SPDX-License-Identifier: GPL-3.0-or-later
2014-07-22 17:38:20 +02:00
#
# Calamares is Free Software: see the License-Identifier above.
2014-07-22 17:38:20 +02:00
#
import tempfile
import subprocess
import os
2014-07-22 17:38:20 +02:00
import libcalamares
2014-07-22 17:38:20 +02:00
import gettext
_ = gettext.translation("calamares-python",
localedir=libcalamares.utils.gettext_path(),
languages=libcalamares.utils.gettext_languages(),
fallback=True).gettext
def pretty_name():
return _("Mounting partitions.")
def mount_partition(root_mount_point, partition, partitions):
2019-08-19 12:31:37 +02:00
"""
Do a single mount of @p partition inside @p root_mount_point.
The @p partitions are used to handle btrfs special-cases:
then subvolumes are created for root and home.
"""
# Create mount point with `+` rather than `os.path.join()` because
# `partition["mountPoint"]` starts with a '/'.
2020-12-20 00:27:45 +01:00
global_storage = libcalamares.globalstorage
raw_mount_point = partition["mountPoint"]
2020-10-31 12:18:30 +01:00
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 libcalamares.utils.mount(device,
mount_point,
fstype,
partition.get("options", "")) != 0:
libcalamares.utils.warning("Cannot mount {}".format(device))
# If the root partition is btrfs, we create a subvolume "@"
# for the root mount point.
# If a separate /home partition isn't defined, we also create
# a subvolume "@home".
# If a swapfile is used, we also create a subvolume "@swap".
# Finally we remount all of the above on the correct paths.
if fstype == "btrfs" and partition["mountPoint"] == '/':
2021-01-26 21:24:50 +01:00
# Root has been mounted to btrfs volume -> create subvolumes from configuration
btrfs_subvolumes = libcalamares.job.configuration.get("btrfsSubvolumes") or []
subvolume_mountpoints = [d['mountPoint'] for d in btrfs_subvolumes]
# Check if listed mountpoints besides / are already present and don't create subvolume for those
for p in partitions:
if "mountPoint" not in p or not p["mountPoint"]:
continue
2021-02-02 22:07:35 +01:00
if p["mountPoint"] in subvolume_mountpoints and p["mountPoint"] != '/':
2021-02-06 00:38:03 +01:00
btrfs_subvolumes = [d for d in btrfs_subvolumes if d['mountPoint'] != p["mountPoint"]]
2021-01-26 21:35:42 +01:00
# Check if we need a subvolume for swap file
2020-12-07 21:59:29 +01:00
swap_choice = global_storage.value( "partitionChoices" )
if swap_choice:
swap_choice = swap_choice.get( "swap", None )
if swap_choice and swap_choice == "file":
2021-01-26 21:35:42 +01:00
btrfs_subvolumes.append({'mountPoint': '/swap', 'subvolume': '/@swap'})
2021-01-26 21:48:02 +01:00
# Store created list in global storage so it can be used in the fstab module
libcalamares.globalstorage.insert("btrfsSubvolumes", btrfs_subvolumes)
2021-01-26 21:35:42 +01:00
# Create the subvolumes that are in the completed list
for s in btrfs_subvolumes:
2020-12-07 21:59:29 +01:00
subprocess.check_call(['btrfs', 'subvolume', 'create',
2021-01-26 21:35:42 +01:00
root_mount_point + s['subvolume']])
subprocess.check_call(["umount", "-v", root_mount_point])
2014-09-24 18:13:05 +02:00
device = partition["device"]
2016-05-05 13:54:15 +02:00
if "luksMapperName" in partition:
device = os.path.join("/dev/mapper", partition["luksMapperName"])
2021-01-26 21:24:50 +01:00
# Mount the subvolumes
for s in btrfs_subvolumes:
2021-02-03 21:55:11 +01:00
mount_option = f"subvol={s['subvolume']}"
2021-01-26 21:24:50 +01:00
if libcalamares.utils.mount(device,
mount_point,
fstype,
2021-01-26 21:24:50 +01:00
",".join([mount_option, partition.get("options", "")])) != 0:
2021-01-26 21:35:42 +01:00
libcalamares.utils.warning(f"Cannot mount {device}")
2014-07-22 17:38:20 +02:00
def run():
"""
2019-08-19 12:31:37 +02:00
Mount all the partitions from GlobalStorage and from the job configuration.
Partitions are mounted in-lexical-order of their mountPoint.
2015-02-20 20:54:25 +01:00
"""
2014-07-29 14:45:58 +02:00
partitions = libcalamares.globalstorage.value("partitions")
if not partitions:
libcalamares.utils.warning("partitions is empty, {!s}".format(partitions))
return (_("Configuration Error"),
_("No partitions are defined for <pre>{!s}</pre> to use." ).format("mount"))
root_mount_point = tempfile.mkdtemp(prefix="calamares-root-")
# Guard against missing keys (generally a sign that the config file is bad)
extra_mounts = libcalamares.job.configuration.get("extraMounts") or []
extra_mounts_efi = libcalamares.job.configuration.get("extraMountsEfi") or []
if not extra_mounts and not extra_mounts_efi:
libcalamares.utils.warning("No extra mounts defined. Does mount.conf exist?")
if libcalamares.globalstorage.value("firmwareType") == "efi":
extra_mounts.extend(extra_mounts_efi)
# Add extra mounts to the partitions list and sort by mount points.
# This way, we ensure / is mounted before the rest, and every mount point
# is created on the right partition (e.g. if a partition is to be mounted
# under /tmp, we make sure /tmp is mounted before the partition)
mountable_partitions = [ p for p in partitions + extra_mounts if "mountPoint" in p and p["mountPoint"] ]
mountable_partitions.sort(key=lambda x: x["mountPoint"])
for partition in mountable_partitions:
mount_partition(root_mount_point, partition, partitions)
2014-07-29 14:45:58 +02:00
libcalamares.globalstorage.insert("rootMountPoint", root_mount_point)
2015-06-14 13:04:52 +02:00
# Remember the extra mounts for the unpackfs module
libcalamares.globalstorage.insert("extraMounts", extra_mounts)