2014-07-28 14:59:22 +02:00
|
|
|
#!/usr/bin/env python3
|
2015-02-18 15:06:10 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2014-07-28 14:59:22 +02:00
|
|
|
# === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
#
|
|
|
|
# Copyright 2014, Aurélien Gâteau <agateau@kde.org>
|
2016-06-17 15:58:32 +02:00
|
|
|
# Copyright 2016, Teo Mrnjavac <teo@kde.org>
|
2014-07-28 14:59:22 +02:00
|
|
|
#
|
|
|
|
# Calamares is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Calamares is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
2014-08-04 16:16:53 +02:00
|
|
|
import re
|
2016-11-10 15:58:48 +01:00
|
|
|
import subprocess
|
2014-07-28 14:59:22 +02:00
|
|
|
|
|
|
|
import libcalamares
|
|
|
|
|
|
|
|
|
2016-06-17 17:04:20 +02:00
|
|
|
FSTAB_HEADER = """# /etc/fstab: static file system information.
|
2014-07-28 14:59:22 +02:00
|
|
|
#
|
|
|
|
# Use 'blkid' to print the universally unique identifier for a device; this may
|
|
|
|
# be used with UUID= as a more robust way to name devices that works even if
|
|
|
|
# disks are added and removed. See fstab(5).
|
|
|
|
#
|
|
|
|
# <file system> <mount point> <type> <options> <dump> <pass>"""
|
|
|
|
|
2016-06-17 17:04:20 +02:00
|
|
|
CRYPTTAB_HEADER = """# /etc/crypttab: mappings for encrypted partitions.
|
|
|
|
#
|
|
|
|
# Each mapped device will be created in /dev/mapper, so your /etc/fstab
|
|
|
|
# should use the /dev/mapper/<name> paths for encrypted devices.
|
|
|
|
#
|
|
|
|
# See crypttab(5) for the supported syntax.
|
|
|
|
#
|
|
|
|
# NOTE: Do not list your root (/) partition here, it must be set up
|
|
|
|
# beforehand by the initramfs (/etc/mkinitcpio.conf). The same applies
|
|
|
|
# to encrypted swap, which should be set up with mkinitcpio-openswap
|
|
|
|
# for resume support.
|
|
|
|
#
|
|
|
|
# <name> <device> <password> <options>"""
|
|
|
|
|
2014-08-04 16:16:53 +02:00
|
|
|
# Turn Parted filesystem names into fstab names
|
2014-07-28 14:59:22 +02:00
|
|
|
FS_MAP = {
|
2014-08-04 16:18:57 +02:00
|
|
|
"fat16": "vfat",
|
|
|
|
"fat32": "vfat",
|
2014-07-28 14:59:22 +02:00
|
|
|
"linuxswap": "swap",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-29 14:26:19 +02:00
|
|
|
def mkdir_p(path):
|
2015-02-21 10:18:46 +01:00
|
|
|
""" Create directory.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param path:
|
|
|
|
"""
|
2014-07-29 14:26:19 +02:00
|
|
|
if not os.path.exists(path):
|
|
|
|
os.makedirs(path)
|
2014-07-28 14:59:22 +02:00
|
|
|
|
|
|
|
|
2014-08-04 16:16:53 +02:00
|
|
|
def is_ssd_disk(disk_name):
|
2015-02-21 10:18:46 +01:00
|
|
|
""" Checks if given disk is actually a ssd disk.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param disk_name:
|
|
|
|
:return:
|
|
|
|
"""
|
2014-08-04 16:16:53 +02:00
|
|
|
filename = os.path.join("/sys/block", disk_name, "queue/rotational")
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2014-08-04 16:16:53 +02:00
|
|
|
if not os.path.exists(filename):
|
|
|
|
# Should not happen unless sysfs changes, but better safe than sorry
|
|
|
|
return False
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2016-06-17 17:10:09 +02:00
|
|
|
with open(filename) as sysfile:
|
|
|
|
return sysfile.read() == "0\n"
|
2014-08-04 16:16:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
def disk_name_for_partition(partition):
|
2015-02-21 10:18:46 +01:00
|
|
|
""" Returns disk name for each found partition.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param partition:
|
|
|
|
:return:
|
|
|
|
"""
|
2014-08-04 16:16:53 +02:00
|
|
|
name = os.path.basename(partition["device"])
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2016-06-17 15:57:38 +02:00
|
|
|
if name.startswith("/dev/mmcblk") or name.startswith("/dev/nvme"):
|
2015-06-14 12:56:56 +02:00
|
|
|
return re.sub("p[0-9]+$", "", name)
|
|
|
|
|
2014-08-04 16:16:53 +02:00
|
|
|
return re.sub("[0-9]+$", "", name)
|
|
|
|
|
|
|
|
|
|
|
|
class FstabGenerator(object):
|
2015-02-21 10:18:46 +01:00
|
|
|
""" Class header
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param partitions:
|
|
|
|
:param root_mount_point:
|
|
|
|
:param mount_options:
|
|
|
|
:param ssd_extra_mount_options:
|
|
|
|
"""
|
2016-10-13 19:01:13 +02:00
|
|
|
def __init__(self, partitions, root_mount_point, mount_options,
|
|
|
|
ssd_extra_mount_options, crypttab_options):
|
2014-08-04 16:16:53 +02:00
|
|
|
self.partitions = partitions
|
|
|
|
self.root_mount_point = root_mount_point
|
|
|
|
self.mount_options = mount_options
|
|
|
|
self.ssd_extra_mount_options = ssd_extra_mount_options
|
2016-10-13 19:01:13 +02:00
|
|
|
self.crypttab_options = crypttab_options
|
2014-08-04 16:16:53 +02:00
|
|
|
self.ssd_disks = set()
|
|
|
|
self.root_is_ssd = False
|
|
|
|
|
|
|
|
def run(self):
|
2015-02-21 10:18:46 +01:00
|
|
|
""" Calls needed sub routines.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2014-08-04 16:16:53 +02:00
|
|
|
self.find_ssd_disks()
|
|
|
|
self.generate_fstab()
|
2016-06-17 17:04:20 +02:00
|
|
|
self.generate_crypttab()
|
2014-08-04 16:16:53 +02:00
|
|
|
self.create_mount_points()
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2014-08-04 16:16:53 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def find_ssd_disks(self):
|
2015-02-21 10:18:46 +01:00
|
|
|
""" Checks for ssd disks """
|
2014-08-04 16:16:53 +02:00
|
|
|
disks = {disk_name_for_partition(x) for x in self.partitions}
|
|
|
|
self.ssd_disks = {x for x in disks if is_ssd_disk(x)}
|
|
|
|
|
2016-06-17 17:04:20 +02:00
|
|
|
def generate_crypttab(self):
|
|
|
|
""" Create crypttab. """
|
|
|
|
mkdir_p(os.path.join(self.root_mount_point, "etc"))
|
|
|
|
crypttab_path = os.path.join(self.root_mount_point, "etc", "crypttab")
|
|
|
|
|
2016-06-17 17:10:09 +02:00
|
|
|
with open(crypttab_path, "w") as crypttab_file:
|
|
|
|
print(CRYPTTAB_HEADER, file=crypttab_file)
|
2016-06-17 17:04:20 +02:00
|
|
|
|
|
|
|
for partition in self.partitions:
|
|
|
|
dct = self.generate_crypttab_line_info(partition)
|
|
|
|
|
|
|
|
if dct:
|
2016-06-17 17:10:09 +02:00
|
|
|
self.print_crypttab_line(dct, file=crypttab_file)
|
2016-06-17 17:04:20 +02:00
|
|
|
|
|
|
|
def generate_crypttab_line_info(self, partition):
|
|
|
|
""" Generates information for each crypttab entry. """
|
2016-06-17 17:35:33 +02:00
|
|
|
if "luksMapperName" not in partition or "luksUuid" not in partition:
|
|
|
|
return None
|
|
|
|
|
2016-06-17 17:04:20 +02:00
|
|
|
mapper_name = partition["luksMapperName"]
|
|
|
|
mount_point = partition["mountPoint"]
|
|
|
|
luks_uuid = partition["luksUuid"]
|
|
|
|
if not mapper_name or not luks_uuid:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return dict(
|
|
|
|
name=mapper_name,
|
|
|
|
device="UUID=" + luks_uuid,
|
|
|
|
password="/crypto_keyfile.bin",
|
2016-10-13 19:01:13 +02:00
|
|
|
options=self.crypttab_options,
|
2016-06-17 17:04:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def print_crypttab_line(self, dct, file=None):
|
|
|
|
""" Prints line to '/etc/crypttab' file. """
|
2016-10-13 19:01:13 +02:00
|
|
|
line = "{:21} {:<45} {} {}".format(dct["name"],
|
|
|
|
dct["device"],
|
|
|
|
dct["password"],
|
|
|
|
dct["options"],
|
|
|
|
)
|
2016-06-17 17:04:20 +02:00
|
|
|
|
|
|
|
print(line, file=file)
|
|
|
|
|
2014-08-04 16:16:53 +02:00
|
|
|
def generate_fstab(self):
|
2015-02-20 22:18:21 +01:00
|
|
|
""" Create fstab. """
|
2014-08-04 16:16:53 +02:00
|
|
|
mkdir_p(os.path.join(self.root_mount_point, "etc"))
|
|
|
|
fstab_path = os.path.join(self.root_mount_point, "etc", "fstab")
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2016-06-17 17:10:09 +02:00
|
|
|
with open(fstab_path, "w") as fstab_file:
|
|
|
|
print(FSTAB_HEADER, file=fstab_file)
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2014-08-04 16:16:53 +02:00
|
|
|
for partition in self.partitions:
|
2016-11-10 15:58:48 +01:00
|
|
|
# Special treatment for a btrfs root with @ and @home subvolumes
|
|
|
|
if partition["fs"] == "btrfs" and partition["mountPoint"] == "/":
|
|
|
|
output = subprocess.check_output(['btrfs',
|
|
|
|
'subvolume',
|
|
|
|
'list',
|
|
|
|
self.root_mount_point])
|
|
|
|
output_lines = output.splitlines()
|
|
|
|
for line in output_lines:
|
|
|
|
if line.endswith(b'path @'):
|
|
|
|
root_entry = partition
|
|
|
|
root_entry["subvol"] = "@"
|
|
|
|
dct = self.generate_fstab_line_info(root_entry)
|
|
|
|
if dct:
|
|
|
|
self.print_fstab_line(dct, file=fstab_file)
|
|
|
|
elif line.endswith(b'path @home'):
|
|
|
|
home_entry = partition
|
|
|
|
home_entry["mountPoint"] = "/home"
|
|
|
|
home_entry["subvol"] = "@home"
|
|
|
|
dct = self.generate_fstab_line_info(home_entry)
|
|
|
|
if dct:
|
|
|
|
self.print_fstab_line(dct, file=fstab_file)
|
|
|
|
|
|
|
|
else:
|
|
|
|
dct = self.generate_fstab_line_info(partition)
|
|
|
|
|
|
|
|
if dct:
|
|
|
|
self.print_fstab_line(dct, file=fstab_file)
|
2014-08-04 16:16:53 +02:00
|
|
|
|
|
|
|
if self.root_is_ssd:
|
|
|
|
# Mount /tmp on a tmpfs
|
2015-06-14 12:56:56 +02:00
|
|
|
dct = dict(device="tmpfs",
|
|
|
|
mount_point="/tmp",
|
|
|
|
fs="tmpfs",
|
|
|
|
options="defaults,noatime,mode=1777",
|
|
|
|
check=0,
|
2016-06-17 17:04:20 +02:00
|
|
|
)
|
2016-06-17 17:10:09 +02:00
|
|
|
self.print_fstab_line(dct, file=fstab_file)
|
2014-08-04 16:16:53 +02:00
|
|
|
|
|
|
|
def generate_fstab_line_info(self, partition):
|
2016-06-17 17:04:20 +02:00
|
|
|
""" Generates information for each fstab entry. """
|
2016-06-17 17:10:09 +02:00
|
|
|
filesystem = partition["fs"]
|
2014-08-04 16:16:53 +02:00
|
|
|
mount_point = partition["mountPoint"]
|
|
|
|
disk_name = disk_name_for_partition(partition)
|
|
|
|
is_ssd = disk_name in self.ssd_disks
|
2016-06-17 17:10:09 +02:00
|
|
|
filesystem = FS_MAP.get(filesystem, filesystem)
|
2014-08-04 16:16:53 +02:00
|
|
|
|
2016-06-17 17:10:09 +02:00
|
|
|
if not mount_point and not filesystem == "swap":
|
2014-08-04 16:16:53 +02:00
|
|
|
return None
|
|
|
|
|
2016-06-17 17:10:09 +02:00
|
|
|
options = self.mount_options.get(filesystem,
|
|
|
|
self.mount_options["default"])
|
2014-08-04 16:16:53 +02:00
|
|
|
if is_ssd:
|
2016-06-17 17:10:09 +02:00
|
|
|
extra = self.ssd_extra_mount_options.get(filesystem)
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2014-08-04 16:16:53 +02:00
|
|
|
if extra:
|
|
|
|
options += "," + extra
|
|
|
|
|
|
|
|
if mount_point == "/":
|
|
|
|
check = 1
|
|
|
|
elif mount_point:
|
|
|
|
check = 2
|
|
|
|
else:
|
|
|
|
check = 0
|
|
|
|
|
|
|
|
if mount_point == "/":
|
|
|
|
self.root_is_ssd = is_ssd
|
|
|
|
|
2016-11-10 15:58:48 +01:00
|
|
|
if filesystem == "btrfs" and "subvol" in partition:
|
|
|
|
return dict(device="UUID=" + partition["uuid"],
|
|
|
|
mount_point=mount_point,
|
|
|
|
fs=filesystem,
|
|
|
|
options=",".join(["subvol={}".format(partition["subvol"]),
|
|
|
|
options]),
|
|
|
|
check=check,
|
|
|
|
)
|
|
|
|
|
2015-06-14 12:56:56 +02:00
|
|
|
return dict(device="UUID=" + partition["uuid"],
|
|
|
|
mount_point=mount_point or "swap",
|
2016-06-17 17:10:09 +02:00
|
|
|
fs=filesystem,
|
2015-06-14 12:56:56 +02:00
|
|
|
options=options,
|
|
|
|
check=check,
|
2016-11-10 15:58:48 +01:00
|
|
|
)
|
2014-08-04 16:16:53 +02:00
|
|
|
|
|
|
|
def print_fstab_line(self, dct, file=None):
|
2016-06-17 17:04:20 +02:00
|
|
|
""" Prints line to '/etc/fstab' file. """
|
2015-06-14 12:56:56 +02:00
|
|
|
line = "{:41} {:<14} {:<7} {:<10} 0 {}".format(dct["device"],
|
|
|
|
dct["mount_point"],
|
|
|
|
dct["fs"],
|
|
|
|
dct["options"],
|
|
|
|
dct["check"],
|
2016-06-17 17:04:20 +02:00
|
|
|
)
|
2014-08-04 16:16:53 +02:00
|
|
|
print(line, file=file)
|
|
|
|
|
|
|
|
def create_mount_points(self):
|
2015-02-21 10:18:46 +01:00
|
|
|
""" Creates mount points """
|
2014-08-04 16:16:53 +02:00
|
|
|
for partition in self.partitions:
|
|
|
|
if partition["mountPoint"]:
|
|
|
|
mkdir_p(self.root_mount_point + partition["mountPoint"])
|
2014-07-28 14:59:22 +02:00
|
|
|
|
2015-02-18 15:37:03 +01:00
|
|
|
|
2014-08-04 16:16:53 +02:00
|
|
|
def run():
|
2015-02-21 10:18:46 +01:00
|
|
|
""" Configures fstab.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2016-06-17 17:10:09 +02:00
|
|
|
global_storage = libcalamares.globalstorage
|
2014-08-04 16:16:53 +02:00
|
|
|
conf = libcalamares.job.configuration
|
2016-06-17 17:10:09 +02:00
|
|
|
partitions = global_storage.value("partitions")
|
|
|
|
root_mount_point = global_storage.value("rootMountPoint")
|
2014-08-04 16:16:53 +02:00
|
|
|
mount_options = conf["mountOptions"]
|
|
|
|
ssd_extra_mount_options = conf.get("ssdExtraMountOptions", {})
|
2016-10-13 19:01:13 +02:00
|
|
|
crypttab_options = conf.get("crypttabOptions", "luks")
|
2016-06-17 17:10:09 +02:00
|
|
|
generator = FstabGenerator(partitions,
|
|
|
|
root_mount_point,
|
|
|
|
mount_options,
|
2016-10-13 19:01:13 +02:00
|
|
|
ssd_extra_mount_options,
|
|
|
|
crypttab_options)
|
2014-07-28 14:59:22 +02:00
|
|
|
|
2014-08-04 16:16:53 +02:00
|
|
|
return generator.run()
|