-
Notifications
You must be signed in to change notification settings - Fork 355
/
startup_utils.py
394 lines (316 loc) · 14.3 KB
/
startup_utils.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#
# startup_utils.py - code used during early startup with minimal dependencies
#
# Copyright (C) 2014 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties 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 this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
from pyanaconda.core.configuration.anaconda import conf
from pyanaconda.core.i18n import _
from pyanaconda.anaconda_loggers import get_stdout_logger, get_storage_logger, get_packaging_logger
stdout_log = get_stdout_logger()
from pyanaconda.anaconda_loggers import get_module_logger
log = get_module_logger(__name__)
import sys
import time
import os
from pyanaconda.core import util, constants
from pyanaconda import product
from pyanaconda import anaconda_logging
from pyanaconda import network
from pyanaconda import safe_dbus
from pyanaconda import kickstart
from pyanaconda.flags import flags
from pyanaconda.screensaver import inhibit_screensaver
from pyanaconda.payload.source import SourceFactory, PayloadSourceTypeUnrecognized
import blivet
def gtk_warning(title, reason):
"""A simple warning dialog for use during early startup of the Anaconda GUI.
:param str title: title of the warning dialog
:param str reason: warning message
TODO: this should be abstracted out to some kind of a "warning API" + UI code
that shows the actual warning
"""
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
dialog = Gtk.MessageDialog(type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.CLOSE,
message_format=reason)
dialog.set_title(title)
dialog.run()
dialog.destroy()
def check_memory(anaconda, options, display_mode=None):
"""Check is the system has enough RAM for installation.
:param anaconda: instance of the Anaconda class
:param options: command line/boot options
:param display_mode: a display mode to use for the check
(graphical mode usually needs more RAM, etc.)
"""
from pyanaconda import isys
reason_strict = _("%(product_name)s requires %(needed_ram)s MB of memory to "
"install, but you only have %(total_ram)s MB on this machine.\n")
reason_graphical = _("The %(product_name)s graphical installer requires %(needed_ram)s "
"MB of memory, but you only have %(total_ram)s MB\n.")
reboot_extra = _('\n'
'Press [Enter] to reboot your system.\n')
livecd_title = _("Not enough RAM")
livecd_extra = _(" Try the text mode installer by running:\n\n"
"'/usr/bin/liveinst -T'\n\n from a root terminal.")
nolivecd_extra = _(" Starting text mode.")
# skip the memory check in rescue mode
if options.rescue:
return
if not display_mode:
display_mode = anaconda.display_mode
reason = reason_strict
total_ram = int(isys.total_memory() / 1024)
needed_ram = int(isys.MIN_RAM)
graphical_ram = int(isys.MIN_GUI_RAM)
# count the squashfs.img in if it is kept in RAM
if not util.persistent_root_image():
needed_ram += isys.SQUASHFS_EXTRA_RAM
graphical_ram += isys.SQUASHFS_EXTRA_RAM
log.info("check_memory(): total:%s, needed:%s, graphical:%s",
total_ram, needed_ram, graphical_ram)
if not options.memcheck:
log.warning("CHECK_MEMORY DISABLED")
return
reason_args = {"product_name": product.productName,
"needed_ram": needed_ram,
"total_ram": total_ram}
if needed_ram > total_ram:
if options.liveinst:
# pylint: disable=logging-not-lazy
stdout_log.warning(reason % reason_args)
gtk_warning(livecd_title, reason % reason_args)
else:
reason += reboot_extra
print(reason % reason_args)
print(_("The installation cannot continue and the system will be rebooted"))
print(_("Press ENTER to continue"))
input()
util.ipmi_report(constants.IPMI_ABORTED)
sys.exit(1)
# override display mode if machine cannot nicely run X
if display_mode != constants.DisplayModes.TUI and not flags.usevnc:
needed_ram = graphical_ram
reason_args["needed_ram"] = graphical_ram
reason = reason_graphical
if needed_ram > total_ram:
if options.liveinst:
reason += livecd_extra
# pylint: disable=logging-not-lazy
stdout_log.warning(reason % reason_args)
title = livecd_title
gtk_warning(title, reason % reason_args)
util.ipmi_report(constants.IPMI_ABORTED)
sys.exit(1)
else:
reason += nolivecd_extra
# pylint: disable=logging-not-lazy
stdout_log.warning(reason % reason_args)
anaconda.display_mode = constants.DisplayModes.TUI
time.sleep(2)
def setup_logging_from_options(options):
"""Configure logging according to Anaconda command line/boot options.
:param options: Anaconda command line/boot options
"""
if (options.debug or options.updateSrc) and not options.loglevel:
# debugging means debug logging if an explicit level hasn't been st
options.loglevel = "debug"
if options.loglevel and options.loglevel in anaconda_logging.logLevelMap:
log.info("Switching logging level to %s", options.loglevel)
level = anaconda_logging.logLevelMap[options.loglevel]
anaconda_logging.logger.loglevel = level
anaconda_logging.setHandlersLevel(log, level)
storage_log = get_storage_logger()
anaconda_logging.setHandlersLevel(storage_log, level)
packaging_log = get_packaging_logger()
anaconda_logging.setHandlersLevel(packaging_log, level)
if conf.system.can_modify_syslog:
if options.syslog:
anaconda_logging.logger.updateRemote(options.syslog)
if options.remotelog:
try:
host, port = options.remotelog.split(":", 1)
port = int(port)
anaconda_logging.logger.setup_remotelog(host, port)
except ValueError:
log.error("Could not setup remotelog with %s", options.remotelog)
def prompt_for_ssh():
"""Prompt the user to ssh to the installation environment on the s390."""
# Do some work here to get the ip addr / hostname to pass
# to the user.
import socket
ip = network.get_first_ip_address()
if not ip:
stdout_log.error("No IP addresses found, cannot continue installation.")
util.ipmi_report(constants.IPMI_ABORTED)
sys.exit(1)
ipstr = ip
try:
hinfo = socket.gethostbyaddr(ipstr)
except socket.herror as e:
stdout_log.debug("Exception caught trying to get host name of %s: %s", ipstr, e)
name = network.get_hostname()
else:
if len(hinfo) == 3:
name = hinfo[0]
if ip.find(':') != -1:
ipstr = "[%s]" % (ip,)
if (name is not None) and (not name.startswith('localhost')) and (ipstr is not None):
connxinfo = "%s (%s)" % (socket.getfqdn(name=name), ipstr,)
elif ipstr is not None:
connxinfo = "%s" % (ipstr,)
else:
connxinfo = None
if connxinfo:
stdout_log.info(_("Please ssh install@%s to begin the install."), connxinfo)
else:
stdout_log.info(_("Please ssh install@HOSTNAME to continue installation."))
def clean_pstore():
"""Remove files stored in nonvolatile ram created by the pstore subsystem.
Files in pstore are Linux (not distribution) specific, but we want to
make sure the entirety of them are removed so as to ensure that there
is sufficient free space on the flash part. On some machines this will
take effect immediately, which is the best case. Unfortunately on some,
an intervening reboot is needed.
"""
util.dir_tree_map("/sys/fs/pstore", os.unlink, files=True, dirs=False)
def print_startup_note(options):
"""Print Anaconda version and short usage instructions.
Print Anaconda version and short usage instruction to the TTY where Anaconda is running.
:param options: command line/boot options
"""
verdesc = "%s for %s %s" % (util.get_anaconda_version_string(build_time_version=True),
product.productName, product.productVersion)
logs_note = " * installation log files are stored in /tmp during the installation"
shell_and_tmux_note = " * shell is available on TTY2"
shell_only_note = " * shell is available on TTY2 and in second TMUX pane (ctrl+b, then press 2)"
tmux_only_note = " * shell is available in second TMUX pane (ctrl+b, then press 2)"
text_mode_note = " * if the graphical installation interface fails to start, try again with the\n"\
" inst.text bootoption to start text installation"
separate_attachements_note = " * when reporting a bug add logs from /tmp as separate text/plain attachments"
if product.isFinal:
print("anaconda %s started." % verdesc)
else:
print("anaconda %s (pre-release) started." % verdesc)
if not options.images and not options.dirinstall:
print(logs_note)
# no fancy stuff like TTYs on a s390...
if not blivet.arch.is_s390():
if "TMUX" in os.environ and os.environ.get("TERM") == "screen":
print(shell_and_tmux_note)
else:
print(shell_only_note) # TMUX is not running
# ...but there is apparently TMUX during the manual installation on s390!
elif not options.ksfile:
print(tmux_only_note) # but not during kickstart installation
# no need to tell users how to switch to text mode
# if already in text mode
if options.display_mode == constants.DisplayModes.TUI:
print(text_mode_note)
print(separate_attachements_note)
def live_startup(anaconda):
"""Live environment startup tasks.
:param anaconda: instance of the Anaconda class
"""
try:
anaconda.dbus_session_connection = safe_dbus.get_new_session_connection()
except safe_dbus.DBusCallError as e:
log.info("Unable to connect to DBus session bus: %s", e)
else:
anaconda.dbus_inhibit_id = inhibit_screensaver(anaconda.dbus_session_connection)
def set_installation_method_from_anaconda_options(anaconda, ksdata):
"""Set the installation method from Anaconda options.
This basically means to set the installation method from options provided
to Anaconda via command line/boot options.
:param anaconda: instance of the Anaconda class
:param ksdata: data model corresponding to the installation kickstart
"""
try:
source = SourceFactory.parse_repo_cmdline_string(anaconda.methodstr)
except PayloadSourceTypeUnrecognized:
log.error("Unknown method: %s", anaconda.methodstr)
return
ksdata.method.method = source.method_type
if source.is_nfs:
ksdata.method.server = source.server
ksdata.method.dir = source.path
ksdata.method.opts = source.options
elif source.is_harddrive:
ksdata.method.partition = source.partition
ksdata.method.dir = source.path
elif source.is_http or source.is_https or source.is_ftp:
ksdata.method.url = source.url
ksdata.method.mirrorlist = None
ksdata.method.metalink = None
# file is not url based but it is stored same way
elif source.is_file:
ksdata.method.url = source.path
ksdata.method.mirrorlist = None
ksdata.method.metalink = None
elif source.is_livecd:
ksdata.method.partition = source.partition
def find_kickstart(options):
"""Find a kickstart to parse.
If we were given a kickstart file, return that one. Otherwise, return
a default kickstart file shipped with the installation media.
Pick up any changes from interactive-defaults.ks that would otherwise
be covered by the dracut kickstart parser.
:param options: command line/boot options
:returns: a path to a kickstart file or None
"""
if options.ksfile and not options.liveinst:
if not os.path.exists(options.ksfile):
stdout_log.error("Kickstart file %s is missing.", options.ksfile)
util.ipmi_report(constants.IPMI_ABORTED)
sys.exit(1)
flags.automatedInstall = True
flags.eject = False
ks_files = [options.ksfile]
elif os.path.exists("/run/install/ks.cfg") and not options.liveinst:
# this is to handle such cases where a user has pre-loaded a
# ks.cfg onto an OEMDRV labeled device
flags.automatedInstall = True
flags.eject = False
ks_files = ["/run/install/ks.cfg"]
else:
ks_files = ["/tmp/updates/interactive-defaults.ks",
"/usr/share/anaconda/interactive-defaults.ks"]
for ks in ks_files:
if not os.path.exists(ks):
continue
return ks
return None
def run_pre_scripts(ks):
"""Run %pre scripts.
:param ks: a path to a kickstart file or None
"""
if ks is not None:
kickstart.preScriptPass(ks)
def parse_kickstart(ks, addon_paths, strict_mode=False):
"""Parse the given kickstart file.
:param ks: a path to a kickstart file or None
:param addon_paths: a dictionary of addon paths
:param strict_mode: process warnings as errors if True
:returns: kickstart parsed to a data model
"""
ksdata = kickstart.AnacondaKSHandler(addon_paths["ks"])
if ks is not None:
log.info("Parsing kickstart: %s", ks)
kickstart.parseKickstart(ksdata, ks, strict_mode=strict_mode, pass_to_boss=True)
return ksdata