-
Notifications
You must be signed in to change notification settings - Fork 386
/
main.py
266 lines (214 loc) · 8.2 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# === This file is part of Calamares - <https://calamares.io> ===
#
# SPDX-FileCopyrightText: 2014 Rohan Garg <rohan@kde.org>
# SPDX-FileCopyrightText: 2015 2019-2020, Philip Müller <philm@manjaro.org>
# SPDX-FileCopyrightText: 2017 Alf Gaida <agaida@sidution.org>
# SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Calamares is Free Software: see the License-Identifier above.
#
import libcalamares
from libcalamares.utils import debug, target_env_call
import os
from collections import OrderedDict
import gettext
_ = gettext.translation("calamares-python",
localedir=libcalamares.utils.gettext_path(),
languages=libcalamares.utils.gettext_languages(),
fallback=True).gettext
def pretty_name():
return _("Configuring mkinitcpio.")
def detect_plymouth():
"""
Checks existence (runnability) of plymouth in the target system.
@return True if plymouth exists in the target, False otherwise
"""
# Used to only check existence of path /usr/bin/plymouth in target
return target_env_call(["sh", "-c", "which plymouth"]) == 0
class cpuinfo(object):
"""
Object describing the current CPU's characteristics. It may be
be considered a named tuple, there's no behavior here.
Fields in the object:
- is_intel (if it's definitely an Intel CPU)
- is_amd (if it's definitely an AMD CPU)
- number_of_cores
It is possible for both is_* fields to be False.
"""
def __init__(self):
self.is_intel = False
self.is_amd = False
self.number_of_cores = 0
cpu = self._cpuinfo()
if 'vendor_id' in cpu['proc0']:
self.is_intel = cpu['proc0']['vendor_id'].lower() == "genuineintel"
self.is_amd = cpu['proc0']['vendor_id'].lower() == "authenticamd"
self.number_of_cores = len(cpu)
@staticmethod
def _cpuinfo():
"""
Return the information in /proc/cpuinfo as a dictionary in the following
format:
cpu_info['proc0']={...}
cpu_info['proc1']={...}
"""
cpu_info = OrderedDict()
procinfo = OrderedDict()
nprocs = 0
with open('/proc/cpuinfo') as cpuinfo_file:
for line in cpuinfo_file:
if not line.strip():
# end of one processor
cpu_info["proc{!s}".format(nprocs)] = procinfo
nprocs += 1
# Reset
procinfo = OrderedDict()
else:
if len(line.split(':')) == 2:
splitted_line = line.split(':')[1].strip()
procinfo[line.split(':')[0].strip()] = splitted_line
else:
procinfo[line.split(':')[0].strip()] = ''
return cpu_info
def get_host_initcpio():
"""
Reads the host system mkinitcpio.conf and returns all
the lines from that file, or an empty list if it does
not exist.
"""
hostfile = "/etc/mkinitcpio.conf"
try:
with open(hostfile, "r") as mkinitcpio_file:
mklins = [x.strip() for x in mkinitcpio_file.readlines()]
except FileNotFoundError:
libcalamares.utils.debug(f"Could not open host file {hostfile}")
mklins = []
return mklins
def write_mkinitcpio_lines(hooks, modules, files, binaries, root_mount_point):
"""
Set up mkinitcpio.conf.
:param hooks:
:param modules:
:param files:
:param root_mount_point:
"""
mklins = get_host_initcpio()
target_path = os.path.join(root_mount_point, "etc/mkinitcpio.conf")
with open(target_path, "w") as mkinitcpio_file:
for line in mklins:
# Replace HOOKS, MODULES, BINARIES and FILES lines with what we
# have found via find_initcpio_features()
if line.startswith("HOOKS"):
line = 'HOOKS="{!s}"'.format(' '.join(hooks))
elif line.startswith("BINARIES"):
line = 'BINARIES="{!s}"'.format(' '.join(binaries))
elif line.startswith("MODULES"):
line = 'MODULES="{!s}"'.format(' '.join(modules))
elif line.startswith("FILES"):
line = 'FILES="{!s}"'.format(' '.join(files))
mkinitcpio_file.write(line + "\n")
def find_initcpio_features(partitions, root_mount_point):
"""
Returns a tuple (hooks, modules, files) needed to support
the given @p partitions (filesystems types, encryption, etc)
in the target.
:param partitions: (from GS)
:param root_mount_point: (from GS)
:return 3-tuple of lists
"""
hooks = [
"autodetect",
"kms",
"modconf",
"block",
"keyboard",
]
systemd_hook_allowed = libcalamares.job.configuration.get("useSystemdHook", False)
use_systemd = systemd_hook_allowed and target_env_call(["sh", "-c", "which systemd-cat"]) == 0
if use_systemd:
hooks.insert(0, "systemd")
hooks.append("sd-vconsole")
else:
hooks.insert(0, "udev")
hooks.insert(0, "base")
hooks.append("keymap")
hooks.append("consolefont")
modules = []
files = []
binaries = []
swap_uuid = ""
uses_btrfs = False
uses_zfs = False
uses_lvm2 = False
encrypt_hook = False
openswap_hook = False
unencrypted_separate_boot = False
# It is important that the plymouth hook comes before any encrypt hook
if detect_plymouth():
hooks.append("plymouth")
for partition in partitions:
if partition["fs"] == "linuxswap" and not partition.get("claimed", None):
# Skip foreign swap
continue
if partition["fs"] == "linuxswap":
swap_uuid = partition["uuid"]
if "luksMapperName" in partition:
openswap_hook = True
if partition["fs"] == "btrfs":
uses_btrfs = True
# In addition to checking the filesystem, check to ensure that zfs is enabled
if partition["fs"] == "zfs" and libcalamares.globalstorage.contains("zfsPoolInfo"):
uses_zfs = True
if "lvm2" in partition["fs"]:
uses_lvm2 = True
if partition["mountPoint"] == "/" and "luksMapperName" in partition:
encrypt_hook = True
if partition["mountPoint"] == "/boot" and "luksMapperName" not in partition:
unencrypted_separate_boot = True
if partition["mountPoint"] == "/usr":
hooks.append("usr")
if encrypt_hook:
if use_systemd:
hooks.append("sd-encrypt")
else:
hooks.append("encrypt")
crypto_file = "crypto_keyfile.bin"
if not unencrypted_separate_boot and os.path.isfile(os.path.join(root_mount_point, crypto_file)):
files.append(f"/{crypto_file}")
if uses_lvm2:
hooks.append("lvm2")
if uses_zfs:
hooks.append("zfs")
if swap_uuid != "":
if encrypt_hook and openswap_hook:
hooks.extend(["openswap"])
hooks.extend(["resume", "filesystems"])
else:
hooks.extend(["filesystems"])
if uses_btrfs:
modules.append("crc32c-intel" if cpuinfo().is_intel else "crc32c")
else:
hooks.append("fsck")
return hooks, modules, files, binaries
def run():
"""
Calls routine with given parameters to modify "/etc/mkinitcpio.conf".
:return:
"""
partitions = libcalamares.globalstorage.value("partitions")
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
if not partitions:
libcalamares.utils.warning(f"partitions are empty, {partitions}")
return (_("Configuration Error"),
_("No partitions are defined for <pre>initcpiocfg</pre>."))
if not root_mount_point:
libcalamares.utils.warning(f"rootMountPoint is empty, {root_mount_point}")
return (_("Configuration Error"),
_("No root mount point for <pre>initcpiocfg</pre>."))
hooks, modules, files, binaries = find_initcpio_features(partitions, root_mount_point)
write_mkinitcpio_lines(hooks, modules, files, binaries, root_mount_point)
return None