forked from calamares/calamares
/
main.py
229 lines (188 loc) · 8.18 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# === This file is part of Calamares - <https://github.com/calamares> ===
#
# Copyright 2014-2018, Anke Boersma <demm@kaosx.us>
# Copyright 2014, Benjamin Vaudour <benjamin.vaudour@yahoo.fr>
#
# 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
from libcalamares.utils import check_target_env_call
def detect_firmware_type():
# Check for EFI variables support
if(os.path.exists("/sys/firmware/efi/efivars")):
fw_type = 'efi'
else:
fw_type = 'bios'
libcalamares.globalstorage.insert("firmwareType", fw_type)
libcalamares.utils.debug("Firmware type: {!s}".format(fw_type))
def get_uuid():
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
print(root_mount_point)
partitions = libcalamares.globalstorage.value("partitions")
print(partitions)
for partition in partitions:
if partition["mountPoint"] == "/":
libcalamares.utils.debug(partition["uuid"])
return partition["uuid"]
return ""
def create_conf(uuid, conf_path):
distribution = libcalamares.job.configuration["distribution"]
kernel = libcalamares.job.configuration["kernel"]
img = libcalamares.job.configuration["img"]
partitions = libcalamares.globalstorage.value("partitions")
kernel_params = ["quiet systemd.show_status=0"]
swap = ""
swap_luks = ""
cryptdevice_params = []
btrfs_params = []
for partition in partitions:
if partition["fs"] == "linuxswap" and not "luksMapperName" in partition:
swap = partition["uuid"]
if partition["fs"] == "linuxswap" and "luksMapperName" in partition:
swap_luks = partition["luksMapperName"]
if partition["mountPoint"] == "/" and "luksMapperName" in partition:
cryptdevice_params = [
"cryptdevice=UUID={!s}:{!s}".format(partition["luksUuid"],
partition["luksMapperName"]),
"root=/dev/mapper/{!s}".format(partition["luksMapperName"]),
"resume=/dev/mapper/{!s}".format(partition["luksMapperName"])
]
if partition["fs"] == "btrfs" in partition:
btrfs_params = "rootflags=subvol=@"
if cryptdevice_params:
kernel_params.extend(cryptdevice_params)
else:
kernel_params.append("root=UUID={!s}".format(uuid))
if swap:
kernel_params.append("resume=UUID={!s}".format(swap))
if swap_luks:
kernel_params.append("resume=/dev/mapper/{!s}".format(swap_luks))
if btrfs_params:
kernel_params.append(btrfs_params)
lines = [
'## Please edit the paths and kernel parameters according to your system.\n',
'\n',
'title {!s} GNU/Linux, with Linux kernel\n'.format(distribution),
'linux {!s}\n'.format(kernel),
'initrd {!s}\n'.format(img),
'options {!s} rw\n'.format(" ".join(kernel_params)),
]
with open(conf_path, 'w') as f:
for l in lines:
f.write(l)
f.close()
def create_fallback(uuid, fallback_path):
distribution = libcalamares.job.configuration["distribution"]
kernel = libcalamares.job.configuration["kernel"]
fb_img = libcalamares.job.configuration["fallback"]
partitions = libcalamares.globalstorage.value("partitions")
kernel_params = ["quiet systemd.show_status=0"]
swap = ""
cryptdevice_params = []
for partition in partitions:
if partition["fs"] == "linuxswap":
swap = partition["uuid"]
if partition["mountPoint"] == "/" and "luksMapperName" in partition:
cryptdevice_params = [
"cryptdevice=UUID={!s}:{!s}".format(partition["luksUuid"],
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:
kernel_params.append("resume=UUID={!s}".format(swap))
lines = [
'## Please edit the paths and kernel parameters according to your system.\n',
'\n',
'title {!s} GNU/Linux, with Linux fallback kernel\n'.format(distribution),
'linux {!s}\n'.format(kernel),
'initrd {!s}\n'.format(fb_img),
'options {!s} rw\n'.format(" ".join(kernel_params)),
]
with open(fallback_path, 'w') as f:
for l in lines:
f.write(l)
f.close()
def create_loader(loader_path):
distribution = libcalamares.job.configuration["distribution"]
timeout = libcalamares.job.configuration["timeout"]
lines = [
'timeout {!s}\n'.format(timeout),
'default {!s}\n'.format(distribution),
]
with open(loader_path, 'w') as f:
for l in lines:
f.write(l)
f.close()
def install_bootloader(boot_loader, fw_type):
if fw_type == 'efi':
install_path = libcalamares.globalstorage.value("rootMountPoint")
uuid = get_uuid()
distribution = libcalamares.job.configuration["distribution"]
conf_path = os.path.join(
install_path, "boot", "loader", "entries",
"{!s}.conf".format(distribution))
fallback_path = os.path.join(
install_path, "boot", "loader", "entries", "{!s}-fallback.conf".format(distribution))
loader_path = os.path.join(
install_path, "boot", "loader", "loader.conf")
partitions = libcalamares.globalstorage.value("partitions")
device = ""
for partition in partitions:
if partition["mountPoint"] == "/boot":
libcalamares.utils.debug(partition["device"])
boot_device = partition["device"]
boot_p = boot_device[-1:]
device = boot_device[:-1]
if boot_device.startswith('/dev/nvme'):
device = boot_device[:-2]
if not boot_p or not device:
return ("EFI directory /boot not found!",
"Boot partition: \"{!s}\"",
"Boot device: \"{!s}\"".format(boot_p, device))
else:
print("EFI directory: /boot")
print("Boot partition: \"{!s}\"".format(boot_p))
print("Boot device: \"{!s}\"".format(device))
if not device:
print("WARNING: no EFI system partition or EFI system partition mount point not set.")
print(" >>> no EFI bootloader will be installed <<<")
return None
print("Set 'EF00' flag")
subprocess.call(
["sgdisk", "--typecode={!s}:EF00".format(boot_p), "{!s}".format(device)])
subprocess.call(
["bootctl", "--path={!s}/boot".format(install_path), "install"])
create_conf(uuid, conf_path)
create_fallback(uuid, fallback_path)
create_loader(loader_path)
else:
install_path = boot_loader["installPath"]
check_target_env_call(["grub-install", install_path])
check_target_env_call(["grub-mkconfig", "-o", "/boot/grub/grub.cfg"])
def run():
#detect_firmware_type()
boot_loader = libcalamares.globalstorage.value("bootLoader")
fw_type = libcalamares.globalstorage.value("firmwareType")
if libcalamares.globalstorage.value("bootLoader") is None and fw_type != "efi":
libcalamares.utils.debug('no bootloader install')
return None
install_bootloader(boot_loader, fw_type)
return None