source: calamares/trunk/fuentes/src/modules/initcpiocfg/main.py @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 5.3 KB
Line 
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# === This file is part of Calamares - <https://github.com/calamares> ===
5#
6#   Copyright 2014, Rohan Garg <rohan@kde.org>
7#   Copyright 2015, Philip Müller <philm@manjaro.org>
8#   Copyright 2017, Alf Gaida <agaida@sidution.org>
9#
10#   Calamares is free software: you can redistribute it and/or modify
11#   it under the terms of the GNU General Public License as published by
12#   the Free Software Foundation, either version 3 of the License, or
13#   (at your option) any later version.
14#
15#   Calamares is distributed in the hope that it will be useful,
16#   but WITHOUT ANY WARRANTY; without even the implied warranty of
17#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18#   GNU General Public License for more details.
19#
20#   You should have received a copy of the GNU General Public License
21#   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
22
23import libcalamares
24import os
25from collections import OrderedDict
26
27
28def cpuinfo():
29    """
30    Return the information in /proc/cpuinfo as a dictionary in the following
31    format:
32
33    cpu_info['proc0']={...}
34    cpu_info['proc1']={...}
35    """
36    cpu_info = OrderedDict()
37    procinfo = OrderedDict()
38
39    nprocs = 0
40
41    with open('/proc/cpuinfo') as cpuinfo_file:
42        for line in cpuinfo_file:
43            if not line.strip():
44                # end of one processor
45                cpu_info["proc{!s}".format(nprocs)] = procinfo
46                nprocs += 1
47                # Reset
48                procinfo = OrderedDict()
49            else:
50                if len(line.split(':')) == 2:
51                    splitted_line = line.split(':')[1].strip()
52                    procinfo[line.split(':')[0].strip()] = splitted_line
53                else:
54                    procinfo[line.split(':')[0].strip()] = ''
55
56    return cpu_info
57
58
59def write_mkinitcpio_lines(hooks, modules, files, root_mount_point):
60    """
61    Set up mkinitcpio.conf.
62
63    :param hooks:
64    :param modules:
65    :param files:
66    :param root_mount_point:
67    """
68    hostfile = "/etc/mkinitcpio.conf"
69    try:
70        with open(hostfile, "r") as mkinitcpio_file:
71            mklins = [x.strip() for x in mkinitcpio_file.readlines()]
72    except FileNotFoundError:
73        libcalamares.utils.debug("Could not open host file '%s'" % hostfile)
74        mklins = []
75
76    for i in range(len(mklins)):
77        if mklins[i].startswith("HOOKS"):
78            joined_hooks = ' '.join(hooks)
79            mklins[i] = "HOOKS=\"{!s}\"".format(joined_hooks)
80        elif mklins[i].startswith("MODULES"):
81            joined_modules = ' '.join(modules)
82            mklins[i] = "MODULES=\"{!s}\"".format(joined_modules)
83        elif mklins[i].startswith("FILES"):
84            joined_files = ' '.join(files)
85            mklins[i] = "FILES=\"{!s}\"".format(joined_files)
86
87    path = os.path.join(root_mount_point, "etc/mkinitcpio.conf")
88
89    with open(path, "w") as mkinitcpio_file:
90        mkinitcpio_file.write("\n".join(mklins) + "\n")
91
92
93def modify_mkinitcpio_conf(partitions, root_mount_point):
94    """
95    Modifies mkinitcpio.conf
96
97    :param partitions:
98    :param root_mount_point:
99    """
100    cpu = cpuinfo()
101    swap_uuid = ""
102    btrfs = ""
103    lvm2 = ""
104    hooks = ["base", "udev", "autodetect", "modconf", "block", "keyboard",
105             "keymap"]
106    modules = []
107    files = []
108    encrypt_hook = False
109    openswap_hook = False
110    unencrypted_separate_boot = False
111
112    # It is important that the plymouth hook comes before any encrypt hook
113    plymouth_bin = os.path.join(root_mount_point, "usr/bin/plymouth")
114    if os.path.exists(plymouth_bin):
115        hooks.append("plymouth")
116
117    for partition in partitions:
118        if partition["fs"] == "linuxswap":
119            swap_uuid = partition["uuid"]
120            if "luksMapperName" in partition:
121                openswap_hook = True
122
123        if partition["fs"] == "btrfs":
124            btrfs = "yes"
125
126        if "lvm2" in partition["fs"]:
127            lvm2 = "yes"
128
129        if partition["mountPoint"] == "/" and "luksMapperName" in partition:
130            encrypt_hook = True
131
132        if (partition["mountPoint"] == "/boot"
133                and "luksMapperName" not in partition):
134            unencrypted_separate_boot = True
135
136    if encrypt_hook:
137        hooks.append("encrypt")
138        if not unencrypted_separate_boot and \
139           os.path.isfile(
140               os.path.join(root_mount_point, "crypto_keyfile.bin")
141               ):
142            files.append("/crypto_keyfile.bin")
143
144    if lvm2:
145        hooks.append("lvm2")
146
147    if swap_uuid != "":
148        if encrypt_hook and openswap_hook:
149            hooks.extend(["openswap"])
150        hooks.extend(["resume", "filesystems"])
151    else:
152        hooks.extend(["filesystems"])
153
154    if btrfs == "yes" and cpu['proc0']['vendor_id'].lower() != "genuineintel":
155        modules.append("crc32c")
156    elif (btrfs == "yes"
157          and cpu['proc0']['vendor_id'].lower() == "genuineintel"):
158        modules.append("crc32c-intel")
159    else:
160        hooks.append("fsck")
161
162    write_mkinitcpio_lines(hooks, modules, files, root_mount_point)
163
164
165def run():
166    """
167    Calls routine with given parameters to modify '/etc/mkinitcpio.conf'.
168
169    :return:
170    """
171    partitions = libcalamares.globalstorage.value("partitions")
172    root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
173    modify_mkinitcpio_conf(partitions, root_mount_point)
174
175    return None
Note: See TracBrowser for help on using the repository browser.