forked from openSUSE/update-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-bootloader
More file actions
executable file
·574 lines (455 loc) · 14.5 KB
/
update-bootloader
File metadata and controls
executable file
·574 lines (455 loc) · 14.5 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#! /usr/bin/perl
use strict;
use POSIX;
use Getopt::Long;
use Bootloader::Tools;
use Bootloader::MBRTools;
# keep Pod::Usage optional (bnc #760464)
eval "use Pod::Usage";
if(!exists $::{pod2usage}) {
sub pod2usage {
die "usage: update-bootloader [operation] [options]\n";
}
}
my %oper;
my ($opt_default, $opt_force, $opt_force_default, $opt_help, $opt_man, $opt_previous, $opt_xen)
= (0,0,0,0,0,0,0);
my ($opt_image, $opt_initrd, $opt_name, $opt_xen_name, $opt_failsafe, $opt_xen_kernel)
= ('','','','','',undef);
my $add_product = 0;
my $logger;
=head1 NAME
update-bootloader - update/change bootloader configuration using
Bootloader::Tools perl module
=head1 SYNOPSIS
update-bootloader [operation] [options]
operation is one of --add, --remove, --refresh or --reinit.
valid options are --help, --man, --image <file>, --initrd <file>,
--xen-kernel <file>, --xen, --default, --previous, --name <string>, --force,
--force-default.
=head1 DESCRIPTION
B<update-bootloader> will let you modify your bootloader configuration using
Bootloader::Tools perl module.
=head1 OPERATIONS
=over 8
=item B<--add>
add an new image section.
Needs a call to --refresh to take effect.
=item B<--remove>
remove the specified image section.
Needs a call to --refresh to take effect.
=item B<--refresh>
activate the new config and write boot loader to disk if necessary
=item B<--reinit>
reinitize the bootloader by refreshing config and reinstalling it
=back
=head1 PARAMETER
=over 8
=item B<--help>
Print a brief help message and exits.
=item B<--man>
Prints the manual page and exits.
=item B<--image> F<file>
specify path to kernel image
=item B<--initrd> F<file>
specify path to initrd file
=item B<--xen>
specify that you what to add a xen and not a regular image section
=item B<--xen-kernel> F<file>
specify that you what to add a xen section with a specific image.
Implies --xen option.
=item B<--default>
let the new section to be added be the default section if appropriate. Only
allowed together with --add operation
=item B<--previous>
set some usuable defaults for image, initrd and name when
=item B<--name> F<string>
specify the name of the section to be added/removed
=item B<--force>
dont complain, just do the right thing
=item B<--force-default>
force the new section to be added to be the default section. Only
allowed together with --add operation
=back
=cut
# Get product name.
# If zypper is not available use /etc/products.d/baseproduct and /etc/SuSE-release.
sub GetProduct {
my $prod;
my $src;
my $loader = Bootloader::Tools::GetBootloader();
my $long_names_ok = $loader eq "grub" || $loader eq "grub2" || $loader eq "grub2-efi";
# 1st try: ask zypper
# note: may fail as older zypper versions don't support '--label'
if(-x '/usr/bin/zypper') {
if(`zypper --terse targetos --label 2>/dev/null` =~ /^labelLong\s*(.*?)\s*\nlabelShort\s*(.*?)\s*$/) {
$prod = $1 ne "" && $long_names_ok ? $1 : $2;
$src = "zypper";
}
}
# 2nd try: read /etc/products.d/baseproduct
if($prod eq "" && open(my $f, "</etc/products.d/baseproduct")) {
my $tag = $long_names_ok ? "summary" : "shortsummary";
while(<$f>) {
$prod = $1 if /<$tag>\s*(.*?)\s*<\/$tag>/o;
}
close $f;
$src = "baseproduct";
}
# 3rd try: read /etc/os-release
if($prod eq "" && $long_names_ok && open(my $f, "</etc/os-release")) {
while(<$f>) {
$prod = $1, last if /^\s*PRETTY_NAME\s*=(.*?)\(/;
}
close $f;
$prod =~ s/^\s*"?\s*//;
$prod =~ s/\s*"?\s*$//;
$prod = ">$prod<";
$src = "os-release";
}
# 4th try: read /etc/SuSE-release
if($prod eq "" && $long_names_ok && open(my $f, "</etc/SuSE-release")) {
($prod = <$f>) =~ s/\s*\(.*//s;
while(<$f>) {
$prod .= " SP$1", last if /^\s*PATCHLEVEL\s*=\s*(\d+)/ && $1 > 0;
}
close $f;
$src = "SuSE-release";
}
# the last line of defense...
if($prod eq "") {
$prod = "Linux";
$src = "fallback";
}
$logger->milestone("update-bootloader::GetProduct($src) = $prod");
return $prod;
}
sub test_gettext {
my $filename = "Locale/gettext.pm";
my $realfilename;
foreach my $prefix (@INC) {
$realfilename = "$prefix/$filename";
if (-f $realfilename) {
return 1;
}
}
return undef
}
my $cmdline = join " ", ($0, @ARGV);
GetOptions (\%oper,
'add|a' ,
'refresh' ,
'reinit' ,
'remove|r' ,
'examinembr|e=s',
'default|d' => \$opt_default,
'force-default' => \$opt_force_default,
'help|h' => \$opt_help,
'force' => \$opt_force,
'image=s' => \$opt_image,
'initrd=s' => \$opt_initrd,
'man|m' => \$opt_man,
'xen' => \$opt_xen,
'xen-kernel=s' => \$opt_xen_kernel,
'name=s' => \$opt_name,
'previous|p' => \$opt_previous)
or pod2usage(2);
pod2usage(1) if $opt_help;
pod2usage(-exitstatus => 0, -verbose => 2) if $opt_man;
die("Specify exactly one operation, either 'add', 'remove', 'refresh' or 'reinit'\n")
unless scalar keys(%oper) == 1;
die("Option 'default' is only allowed for operation 'add'\n")
if ($opt_default and not defined $oper{add});
die("Option 'force-default' is only allowed for operation 'add'\n")
if ($opt_force_default and not defined $oper{add});
$logger = Bootloader::Library->new();
$logger->milestone($cmdline);
if (defined $oper{"examinembr"}) {
my $ret = $logger->examineMBR($oper{"examinembr"});
if (defined $ret){
print "$ret\n";
exit 0;
} else {
exit 1;
}
}
if (Bootloader::Tools::GetBootloader() eq "none")
{
$logger->milestone("none bootloader, skipped updating");
exit 0;
}
if (Bootloader::Tools::GetBootloader() =~ /^(grub2|grub2-efi)$/ and
!-x "/usr/sbin/grub2-mkconfig")
{
$logger->milestone("grub2 utilties not in place, skipped updating");
exit 0;
}
if ($opt_image and $opt_image !~ m;^/;) {
$opt_image = getcwd . '/' . $opt_image
}
if ($opt_initrd and $opt_initrd !~ m;^/;) {
$opt_initrd = getcwd . '/' . $opt_initrd;
}
if (defined $opt_xen_kernel) {
$opt_xen = 1;
} elsif ($opt_xen) {
my $xen_flavor = $opt_image;
$xen_flavor =~ s/.*-(\w+)/$1/;
if ($xen_flavor eq "xenpae") {
$opt_xen_kernel = "/boot/xen-pae.gz";
}
else {
$opt_xen_kernel = "/boot/xen.gz";
}
}
my $type = $opt_xen ? "xen" : "image";
InitLibrary();
if ($opt_previous) {
unless ($opt_image) {
$opt_image = GetDefaultImage() . ".previous";
}
unless($opt_initrd) {
$opt_initrd = GetDefaultInitrd() . ".previous";
}
}
# grub2 handles input options differently here ..
# when add or remove, only update config ($avoid_init=1) by calling grub2-mkconfig
if (Bootloader::Tools::GetBootloader() =~ /^(grub2|grub2-efi)$/)
{
if (defined $oper{add} || defined $oper{remove}) {
# The add or remove should trigger config update (bnc#780622)
# Set $avoid_init=1 to avoid installing bootloader
my $ret = UpdateBootloader(1);
exit 1 if ( !$ret );
}
# clear handled option
delete $oper{add};
delete $oper{remove};
}
# FIXME: these section names should be unified somehow together with multi
# language and grafical menu handling
if (defined $oper{add}) {
my $loader = Bootloader::Tools::GetBootloader();
unless ($opt_name) {
if ($opt_xen and $opt_previous) {
if ($loader eq "grub" || $loader eq "lilo" || $loader eq "grub2") {
$opt_name = "Previous Xen";
$add_product = 1;
}
else {
$opt_name = "previous xen";
}
}
elsif ($opt_xen) {
if ($loader eq "grub" || $loader eq "lilo" || $loader eq "grub2") {
$opt_name = "Xen";
$add_product = 1;
}
else {
$opt_name = "xen";
}
}
elsif ($opt_previous) {
if ($loader eq "grub" || $loader eq "lilo" || $loader eq "grub2") {
$opt_name = "Previous Kernel";
$add_product = 1;
}
else {
$opt_name = "previous";
}
}
else {
$opt_name = $opt_image;
$opt_name =~ s/.*\///;
}
}
# only localize on grub and lilo
#FIXME jreidinger: I think this now doesn't work. because pbl get only version + type of kernel....maybe help translate strings like failsafe etc
if (($loader eq "grub" || $loader eq "lilo") && defined(test_gettext())) {
require Locale::gettext;
setlocale(LC_MESSAGES, Bootloader::Tools::GetSystemLanguage());
my $d = Locale::gettext->domain("bootloader");
$d->dir("/usr/share/YaST2/locale");
my $opt_trans = $d->get($opt_name);
chomp ($opt_trans);
#log what is translated
$logger->milestone("translate: " . $0 . " " . join (" ", @ARGV));
# check whether translation only contains [a-zA-Z0-9 _]
# otherwise fall back to untranslated string
if ($opt_trans =~ /^[a-zA-Z\d _]+$/g ) {
$opt_name = $opt_trans;
}
}
# Naming scheme for default, smp, bigsmp and pae kernels
if (($opt_name =~ /-default/) ||
($opt_name =~ /-smp/) ||
($opt_name =~ /-bigsmp/) ||
($opt_name =~ /-pae/)) {
if ($loader eq "grub" || $loader eq "grub2") {
$opt_name =~ s/-[^-]*$//;
$opt_failsafe = "Failsafe -- " . GetProduct() . " - " . $opt_name;
$opt_name = GetProduct() . " - " . $opt_name;
}
else {
$opt_failsafe = "Failsafe";
$opt_name = GetProduct();
}
}
# Naming scheme for all xen kernels, thus xen and xenpae
elsif ($opt_xen) {
if ($loader eq "grub" || $loader eq "grub2") {
my $version = $opt_name;
$version =~ s/-xen.*$//;
$opt_name =~ s/^.*(xen.*)$/$1/;
$opt_name = ucfirst ($opt_name);
$opt_xen_name = "$opt_name -- " . GetProduct() . " - " . $version;
}
else {
$opt_xen_name =~ s/^.*(xen.*)$/$1/;
$opt_xen_name = ucfirst ($opt_xen_name);
}
}
# Naming scheme for all other kernels
else {
my $flavor = $opt_name;
$flavor =~ s/.*-(\w+)/$1/;
$flavor = ucfirst ($flavor);
# Create long labels for grub
if ($loader eq "grub" || $loader eq "grub2") {
my $version = $opt_name;
$version =~ s/-[^-]*$//;
$opt_name = $flavor . " -- " . GetProduct() . " - " . $version;
$opt_failsafe = "Failsafe -- " . GetProduct() . " - " . $version;
}
# Create short labels for all other loaders
else {
$opt_name = GetProduct();
$opt_failsafe = "Failsafe";
}
}
}
#
# execute selected operation
#
if (defined $oper{add}) {
$logger->milestone("update-bootloader: now executing operation add");
$logger->milestone("update-bootloader: opt_name = $opt_name");
die("Please specify name and kernel image for new section\n")
unless $opt_name and $opt_image;
my @params = (
type => $type,
image => $opt_image,
);
if (CountSections(@params) != 0) {
if (not $opt_force) {
die("There are already sections with image '$opt_image'\n");
}
$logger->milestone("update-bootloader: section already exist. Skip add.");
} else {
# If, if option $opt_force_default is set, let this new kernel be
# the default one.
if ($opt_force_default) {
push @params, default => $opt_default;
}
# Else, find out the flavor of the default kernel. If it is the same
# flavor as the one of the new kernel, let the new kernel be the
# default one.
else {
my $default_image = GetDefaultImage();
if (-l $default_image) {
$default_image = readlink ($default_image);
}
$default_image =~ s/^.*-//;
if ($opt_image =~ m/.*-${default_image}$/) {
push @params, default => $opt_default;
}
}
push @params, xen => $opt_xen_kernel if $type eq "xen";
push @params, initrd => $opt_initrd if $opt_initrd;
# Add "xen" section
if ($opt_xen) {
# Add original_name to params to be able to create comment line
push @params, original_name => "xen";
$logger->milestone("update-bootloader: calling Tools::AddSection (XEN)");
AddSection($opt_xen_name, @params);
}
# Add "normal" section
else {
# Add original_name to params to be able to create comment line
push @params, original_name => "linux";
$logger->milestone("update-bootloader: calling Tools::AddSection (normal)");
AddSection($opt_name, @params);
}
my $arch = `uname --hardware-platform`;
chomp ($arch);
# Add a "Failsafe" section, but only if the underlying architecture is
# one of i386, x86_84 or ia64 and the kernel flavor is either default,
# smp, bigsmp or pae and not a xen kernel.
if ((($arch eq "i386") ||
($arch eq "x86_64") ||
($arch eq "s390x") ||
($arch eq "ia64")) &&
(($opt_image =~ /-default/) ||
($opt_image =~ /-smp/) ||
($opt_image =~ /-desktop/) ||
($opt_image =~ /-bigsmp/) ||
($opt_image =~ /-pae/)) &&
($opt_xen != 1)) {
# Add original_name to params to be able to create comment line
push @params, original_name => "failsafe";
$logger->milestone("update-bootloader: calling Tools::AddSection (failsafe)");
AddSection($opt_failsafe, @params);
}
}
}
if (defined $oper{remove}) {
my @params = (
type => $type,
image => $opt_image,
);
push @params, xen => $opt_xen_kernel if $type eq "xen";
push @params, initrd => $opt_initrd if $opt_initrd;
push @params, name => $opt_name if $opt_name;
$logger->milestone("update-bootloader: now executing operation remove");
my $num = CountSections(@params);
if ($num > 0) {
if ($num > 1 and not $opt_force) {
$logger->milestone("update-bootloader: found $num sections, no opt_force: not removing");
die("There is more than one section with image '$opt_image'\n");
} else {
$logger->milestone("update-bootloader: calling Tools::RemoveSections");
RemoveSections(@params);
}
} else {
$logger->milestone("update-bootloader: no $opt_image found");
}
$logger->milestone("update-bootloader: finished operation remove");
}
# The function refresh will update config and reinstall if necessary
# Usually used by package when they want bootloader config be written and take effect
if (defined $oper{refresh}) {
my $loader = Bootloader::Tools::GetBootloader();
my $ret;
if ($loader eq "lilo") {
# set avoid_init=0 because lilo requires reinstall for any config change
$ret = UpdateBootloader(0);
} else {
# set avoid_init=1 to only update config for the rest (ie grub etc)
$ret = UpdateBootloader(1);
}
exit 1 if ( !$ret );
}
# The function reinit will reinitialize bootloader by reinstalling and updating it's config
# Usually used when updating bootloader package
if (defined $oper{reinit}) {
my $ret = UpdateBootloader(0);
exit 1 if ( !$ret );
}
#
# Local variables:
# mode: perl
# mode: font-lock
# mode: auto-fill
# fill-column: 78
# End:
#