2014-10-12 19:45:02 +02:00
|
|
|
#!/usr/bin/env python3
|
2015-02-18 15:06:10 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2014-10-12 19:45:02 +02:00
|
|
|
# === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
#
|
|
|
|
# Copyright 2014, Aurélien Gâteau <agateau@kde.org>
|
|
|
|
# Copyright 2014, Anke Boersma <demm@kaosx.us>
|
|
|
|
# Copyright 2014, Daniel Hillenbrand <codeworkx@bbqlinux.org>
|
|
|
|
# Copyright 2014, Benjamin Vaudour <benjamin.vaudour@yahoo.fr>
|
2015-02-20 00:28:44 +01:00
|
|
|
# Copyright 2014, Kevin Kofler <kevin.kofler@chello.at>
|
2015-02-17 13:53:51 +01:00
|
|
|
# Copyright 2015, Philip Mueller <philm@manjaro.org>
|
2014-10-12 19:45:02 +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 libcalamares
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
2015-08-06 12:13:21 +02:00
|
|
|
from libcalamares.utils import check_target_env_call
|
2014-10-12 19:45:02 +02:00
|
|
|
|
2014-10-16 21:08:18 +02:00
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
def get_uuid():
|
2015-02-21 10:09:01 +01:00
|
|
|
""" Checks and passes 'uuid' to other routine.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2014-10-12 19:45:02 +02:00
|
|
|
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
|
2015-02-19 19:42:09 +01:00
|
|
|
print("Root mount point: \"{!s}\"".format(root_mount_point))
|
2014-10-12 19:45:02 +02:00
|
|
|
partitions = libcalamares.globalstorage.value("partitions")
|
2015-02-19 19:42:09 +01:00
|
|
|
print("Partitions: \"{!s}\"".format(partitions))
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
for partition in partitions:
|
|
|
|
if partition["mountPoint"] == "/":
|
2015-02-19 19:42:09 +01:00
|
|
|
print("Root partition uuid: \"{!s}\"".format(partition["uuid"]))
|
2014-10-12 19:45:02 +02:00
|
|
|
return partition["uuid"]
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
return ""
|
2014-10-16 21:08:18 +02:00
|
|
|
|
2014-11-13 05:10:20 +01:00
|
|
|
|
2014-11-19 17:01:02 +01:00
|
|
|
def get_bootloader_entry_name():
|
2015-02-21 10:09:01 +01:00
|
|
|
""" Passes 'bootloader_entry_name' to other routine based on configuration file.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2015-02-19 17:59:52 +01:00
|
|
|
if "bootloaderEntryName" in libcalamares.job.configuration:
|
|
|
|
return libcalamares.job.configuration["bootloaderEntryName"]
|
2014-11-19 17:01:02 +01:00
|
|
|
else:
|
|
|
|
branding = libcalamares.globalstorage.value("branding")
|
2015-02-19 17:59:52 +01:00
|
|
|
return branding["bootloaderEntryName"]
|
2015-02-19 17:39:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_kernel_line(kernel_type):
|
2015-02-21 10:09:01 +01:00
|
|
|
""" Passes 'kernel_line' to other routine based on configuration file.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param kernel_type:
|
|
|
|
:return:
|
|
|
|
"""
|
2015-02-19 18:10:31 +01:00
|
|
|
if kernel_type == "fallback":
|
|
|
|
if "fallbackKernelLine" in libcalamares.job.configuration:
|
2015-02-19 17:39:11 +01:00
|
|
|
return libcalamares.job.configuration["fallbackKernelLine"]
|
|
|
|
else:
|
|
|
|
return " (fallback)"
|
2015-02-19 18:10:31 +01:00
|
|
|
else:
|
|
|
|
if "kernelLine" in libcalamares.job.configuration:
|
|
|
|
return libcalamares.job.configuration["kernelLine"]
|
2015-02-19 17:39:11 +01:00
|
|
|
else:
|
|
|
|
return ""
|
2014-11-19 17:01:02 +01:00
|
|
|
|
2015-02-18 16:03:57 +01:00
|
|
|
|
2015-06-11 00:32:34 +02:00
|
|
|
def create_systemd_boot_conf(uuid, conf_path, kernel_line):
|
|
|
|
""" Creates systemd-boot configuration files based on given parameters.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param uuid:
|
|
|
|
:param conf_path:
|
|
|
|
:param kernel_line:
|
|
|
|
"""
|
2014-11-19 17:01:02 +01:00
|
|
|
distribution = get_bootloader_entry_name()
|
2014-11-11 06:13:15 +01:00
|
|
|
kernel = libcalamares.job.configuration["kernel"]
|
|
|
|
img = libcalamares.job.configuration["img"]
|
2016-05-04 13:05:04 +02:00
|
|
|
kernel_params = ["quiet"]
|
|
|
|
|
2014-11-11 06:13:15 +01:00
|
|
|
partitions = libcalamares.globalstorage.value("partitions")
|
2016-05-04 13:05:04 +02:00
|
|
|
swap_uuid = ""
|
|
|
|
|
|
|
|
cryptdevice_params = []
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2014-11-11 06:13:15 +01:00
|
|
|
for partition in partitions:
|
|
|
|
if partition["fs"] == "linuxswap":
|
2016-05-04 13:05:04 +02:00
|
|
|
swap_uuid = partition["uuid"]
|
|
|
|
|
2016-05-05 13:54:15 +02:00
|
|
|
if partition["mountPoint"] == "/" and "luksMapperName" in partition:
|
2016-05-04 13:05:04 +02:00
|
|
|
cryptdevice_params = [
|
2016-05-05 13:35:42 +02:00
|
|
|
"cryptdevice=UUID={!s}:{!s}".format(partition["luksUuid"],
|
2016-05-04 13:05:04 +02:00
|
|
|
partition["luksMapperName"]),
|
|
|
|
"root=/dev/mapper/{!s}".format(partition["luksMapperName"])
|
|
|
|
]
|
|
|
|
|
|
|
|
if cryptdevice_params:
|
|
|
|
kernel_params.extend(cryptdevice_params)
|
|
|
|
else:
|
|
|
|
kernel_params.append("root=UUID={!s}".format(uuid))
|
|
|
|
|
|
|
|
if swap_uuid:
|
|
|
|
kernel_params.append("resume=UUID={!s}".format(swap_uuid))
|
2015-02-15 00:03:50 +01:00
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
lines = [
|
2015-02-19 20:24:07 +01:00
|
|
|
'## This is just an example config file.\n',
|
2014-10-12 19:45:02 +02:00
|
|
|
'## Please edit the paths and kernel parameters according to your system.\n',
|
|
|
|
'\n',
|
2015-06-14 05:08:52 +02:00
|
|
|
"title {!s}{!s}\n".format(distribution, kernel_line),
|
2015-02-17 13:53:51 +01:00
|
|
|
"linux {!s}\n".format(kernel),
|
|
|
|
"initrd {!s}\n".format(img),
|
2016-05-04 13:05:04 +02:00
|
|
|
"options {!s} rw\n".format(" ".join(kernel_params)),
|
2014-10-12 19:45:02 +02:00
|
|
|
]
|
2014-10-16 21:08:18 +02:00
|
|
|
|
2016-05-04 13:30:54 +02:00
|
|
|
with open(conf_path, 'w') as conf_file:
|
|
|
|
for line in lines:
|
|
|
|
conf_file.write(line)
|
2014-10-16 21:08:18 +02:00
|
|
|
|
2014-11-13 05:10:20 +01:00
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
def create_loader(loader_path):
|
2015-02-21 10:09:01 +01:00
|
|
|
""" Writes configuration for loader.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param loader_path:
|
|
|
|
"""
|
2014-11-19 17:01:02 +01:00
|
|
|
distribution = get_bootloader_entry_name()
|
2014-10-12 19:45:02 +02:00
|
|
|
timeout = libcalamares.job.configuration["timeout"]
|
2014-11-16 04:56:11 +01:00
|
|
|
file_name_sanitizer = str.maketrans(" /", "_-")
|
2015-02-17 13:53:51 +01:00
|
|
|
distribution_translated = distribution.translate(file_name_sanitizer)
|
2014-10-12 19:45:02 +02:00
|
|
|
lines = [
|
2015-02-17 13:53:51 +01:00
|
|
|
"timeout {!s}\n".format(timeout),
|
|
|
|
"default {!s}\n".format(distribution_translated),
|
2014-10-12 19:45:02 +02:00
|
|
|
]
|
2014-10-16 21:08:18 +02:00
|
|
|
|
2016-05-04 13:30:54 +02:00
|
|
|
with open(loader_path, 'w') as loader_file:
|
|
|
|
for line in lines:
|
|
|
|
loader_file.write(line)
|
2014-10-16 21:08:18 +02:00
|
|
|
|
|
|
|
|
2015-06-11 00:32:34 +02:00
|
|
|
def install_systemd_boot(efi_directory):
|
|
|
|
""" Installs systemd-boot as bootloader for EFI setups.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param efi_directory:
|
|
|
|
"""
|
2015-06-11 00:32:34 +02:00
|
|
|
print("Bootloader: systemd-boot")
|
2015-02-20 00:28:44 +01:00
|
|
|
install_path = libcalamares.globalstorage.value("rootMountPoint")
|
|
|
|
install_efi_directory = install_path + efi_directory
|
|
|
|
uuid = get_uuid()
|
|
|
|
distribution = get_bootloader_entry_name()
|
|
|
|
file_name_sanitizer = str.maketrans(" /", "_-")
|
|
|
|
distribution_translated = distribution.translate(file_name_sanitizer)
|
2016-05-04 13:30:54 +02:00
|
|
|
conf_path = os.path.join(install_efi_directory,
|
|
|
|
"loader",
|
|
|
|
"entries",
|
2015-06-14 05:08:52 +02:00
|
|
|
"{!s}.conf".format(distribution_translated))
|
2016-05-04 13:30:54 +02:00
|
|
|
fallback_path = os.path.join(install_efi_directory,
|
|
|
|
"loader",
|
|
|
|
"entries",
|
2015-06-14 05:08:52 +02:00
|
|
|
"{!s}-fallback.conf".format(distribution_translated))
|
2016-05-04 13:30:54 +02:00
|
|
|
loader_path = os.path.join(install_efi_directory,
|
|
|
|
"loader",
|
|
|
|
"loader.conf")
|
|
|
|
subprocess.call(["bootctl",
|
|
|
|
"--path={!s}".format(install_efi_directory),
|
|
|
|
"install"])
|
2015-02-20 18:36:20 +01:00
|
|
|
kernel_line = get_kernel_line("default")
|
2015-02-20 00:28:44 +01:00
|
|
|
print("Configure: \"{!s}\"".format(kernel_line))
|
2015-06-11 00:32:34 +02:00
|
|
|
create_systemd_boot_conf(uuid, conf_path, kernel_line)
|
2015-02-20 18:36:20 +01:00
|
|
|
kernel_line = get_kernel_line("fallback")
|
2015-02-19 17:39:11 +01:00
|
|
|
print("Configure: \"{!s}\"".format(kernel_line))
|
2015-06-11 00:32:34 +02:00
|
|
|
create_systemd_boot_conf(uuid, fallback_path, kernel_line)
|
2015-02-20 00:28:44 +01:00
|
|
|
create_loader(loader_path)
|
|
|
|
|
|
|
|
|
|
|
|
def install_grub(efi_directory, fw_type):
|
2015-02-21 10:09:01 +01:00
|
|
|
""" Installs grub as bootloader, either in pc or efi mode.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param efi_directory:
|
|
|
|
:param fw_type:
|
|
|
|
"""
|
2015-02-20 00:28:44 +01:00
|
|
|
if fw_type == "efi":
|
2015-02-22 17:34:12 +01:00
|
|
|
print("Bootloader: grub (efi)")
|
2015-02-20 00:28:44 +01:00
|
|
|
efi_directory_firmware = efi_directory + "/EFI"
|
2015-08-06 12:13:21 +02:00
|
|
|
check_target_env_call(["mkdir", "-p", "{!s}".format(efi_directory)])
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2015-02-20 00:28:44 +01:00
|
|
|
if "efiBootloaderId" in libcalamares.job.configuration:
|
|
|
|
efi_bootloader_id = libcalamares.job.configuration["efiBootloaderId"]
|
|
|
|
else:
|
|
|
|
branding = libcalamares.globalstorage.value("branding")
|
|
|
|
distribution = branding["bootloaderEntryName"]
|
|
|
|
file_name_sanitizer = str.maketrans(" /", "_-")
|
|
|
|
efi_bootloader_id = distribution.translate(file_name_sanitizer)
|
2016-03-15 13:11:25 +01:00
|
|
|
# get bitness of the underlying UEFI
|
|
|
|
try:
|
2016-05-04 13:30:54 +02:00
|
|
|
sysfile = open("/sys/firmware/efi/fw_platform_size", "r")
|
|
|
|
efi_bitness = sysfile.read(2)
|
|
|
|
except Exception:
|
|
|
|
# if the kernel is older than 4.0, the UEFI bitness likely isn't
|
|
|
|
# exposed to the userspace so we assume a 64 bit UEFI here
|
2016-03-15 13:11:25 +01:00
|
|
|
efi_bitness = "64"
|
|
|
|
bitness_translate = {"32": "--target=i386-efi", "64": "--target=x86_64-efi"}
|
2016-05-04 13:30:54 +02:00
|
|
|
check_target_env_call([libcalamares.job.configuration["grubInstall"],
|
|
|
|
bitness_translate[efi_bitness],
|
|
|
|
"--efi-directory={!s}".format(efi_directory),
|
|
|
|
"--bootloader-id={!s}".format(efi_bootloader_id),
|
|
|
|
"--force"])
|
2015-02-20 00:28:44 +01:00
|
|
|
# Workaround for some UEFI firmwares
|
2015-08-06 12:13:21 +02:00
|
|
|
check_target_env_call(["mkdir", "-p", "{!s}/boot".format(efi_directory_firmware)])
|
2016-05-04 13:30:54 +02:00
|
|
|
check_target_env_call(["cp", "{!s}/{!s}/grubx64.efi".format(efi_directory_firmware,
|
|
|
|
efi_bootloader_id),
|
|
|
|
"{!s}/boot/bootx64.efi".format(efi_directory_firmware)])
|
2015-02-20 00:28:44 +01:00
|
|
|
else:
|
2015-02-22 17:34:12 +01:00
|
|
|
print("Bootloader: grub (bios)")
|
2015-02-20 00:28:44 +01:00
|
|
|
boot_loader = libcalamares.globalstorage.value("bootLoader")
|
2016-05-04 13:30:54 +02:00
|
|
|
check_target_env_call([libcalamares.job.configuration["grubInstall"],
|
|
|
|
"--target=i386-pc",
|
|
|
|
"--recheck",
|
|
|
|
"--force",
|
|
|
|
boot_loader["installPath"]])
|
|
|
|
|
2016-05-04 13:35:40 +02:00
|
|
|
# The file specified in grubCfg should already be filled out
|
|
|
|
# by the grubcfg job module.
|
2016-05-04 13:30:54 +02:00
|
|
|
check_target_env_call([libcalamares.job.configuration["grubMkconfig"],
|
|
|
|
"-o",
|
|
|
|
libcalamares.job.configuration["grubCfg"]])
|
2015-02-20 00:28:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
def prepare_bootloader(fw_type):
|
2015-02-21 10:09:01 +01:00
|
|
|
""" Prepares bootloader and set proper flags to EFI boot partition (esp,boot).
|
2015-06-11 00:32:34 +02:00
|
|
|
Based on value 'efi_boot_loader', it either calls systemd-boot or grub to be installed.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param fw_type:
|
|
|
|
:return:
|
|
|
|
"""
|
2015-02-20 00:32:46 +01:00
|
|
|
efi_boot_loader = libcalamares.job.configuration["efiBootLoader"]
|
2015-02-20 00:33:16 +01:00
|
|
|
efi_directory = libcalamares.globalstorage.value("efiSystemPartition")
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2015-02-20 00:28:44 +01:00
|
|
|
if fw_type == "efi":
|
2014-11-11 06:13:15 +01:00
|
|
|
partitions = libcalamares.globalstorage.value("partitions")
|
2015-02-19 19:09:13 +01:00
|
|
|
boot_p = ""
|
|
|
|
device = ""
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2014-11-11 06:13:15 +01:00
|
|
|
for partition in partitions:
|
2015-02-14 22:53:00 +01:00
|
|
|
if partition["mountPoint"] == efi_directory:
|
2014-11-11 06:13:15 +01:00
|
|
|
boot_device = partition["device"]
|
|
|
|
boot_p = boot_device[-1:]
|
|
|
|
device = boot_device[:-1]
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2015-06-14 14:22:35 +02:00
|
|
|
if not boot_p or not device:
|
2016-03-10 13:37:14 +01:00
|
|
|
return ("EFI directory \"{!s}\" not found!".format(efi_directory),
|
|
|
|
"Boot partition: \"{!s}\"".format(boot_p),
|
|
|
|
"Boot device: \"{!s}\"".format(device))
|
2015-02-19 19:42:09 +01:00
|
|
|
else:
|
|
|
|
print("EFI directory: \"{!s}\"".format(efi_directory))
|
|
|
|
print("Boot partition: \"{!s}\"".format(boot_p))
|
|
|
|
print("Boot device: \"{!s}\"".format(device))
|
2016-03-10 13:37:14 +01:00
|
|
|
|
2016-03-10 17:35:20 +01:00
|
|
|
if not device:
|
2016-03-10 13:37:14 +01:00
|
|
|
print("WARNING: no EFI system partition or EFI system partition mount point not set.")
|
|
|
|
print(" >>> no EFI bootloader will be installed <<<")
|
|
|
|
return None
|
2015-02-22 18:06:15 +01:00
|
|
|
print("Set 'EF00' flag")
|
2015-06-14 05:08:52 +02:00
|
|
|
subprocess.call(["sgdisk", "--typecode={!s}:EF00".format(boot_p), "{!s}".format(device)])
|
|
|
|
|
|
|
|
if efi_boot_loader == "systemd-boot" and fw_type == "efi":
|
2015-06-11 00:32:34 +02:00
|
|
|
install_systemd_boot(efi_directory)
|
2014-10-12 19:45:02 +02:00
|
|
|
else:
|
2015-02-20 01:00:31 +01:00
|
|
|
install_grub(efi_directory, fw_type)
|
2014-10-12 19:45:02 +02:00
|
|
|
|
2014-10-16 21:08:18 +02:00
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
def run():
|
2015-02-21 10:09:01 +01:00
|
|
|
""" Starts procedure and passes 'fw_type' to other routine.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2015-07-07 19:15:48 +02:00
|
|
|
if libcalamares.globalstorage.value("bootLoader") is None:
|
|
|
|
return None
|
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
fw_type = libcalamares.globalstorage.value("firmwareType")
|
2015-02-20 00:28:44 +01:00
|
|
|
prepare_bootloader(fw_type)
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
return None
|