File: EFI.pm

package info (click to toggle)
systemconfigurator 2.0.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 724 kB
  • ctags: 313
  • sloc: perl: 7,423; makefile: 48
file content (415 lines) | stat: -rw-r--r-- 9,555 bytes parent folder | download | duplicates (3)
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
package Boot::EFI;

#   $Header: /cvsroot/systemconfig/systemconfig/lib/Boot/EFI.pm,v 1.10 2004/03/15 01:56:47 dannf Exp $

#   Copyright (c) 2001 International Business Machines

#   This program 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 2 of the License, or
#   (at your option) any later version.
 
#   This program 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 this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#   dann frazier <dannf@ldl.fc.hp.com>
#   based on code by:  Donghwa John Kim <johkim@us.ibm.com>

#   EFI caveats:
#   the kernel path names in elilo.conf (and therefore in systemconfig.conf)
#   must be relative to the efipartition, and apparently in efi syntax.

#   i.e.:x

#   image=/boot/efi/vmlinuz         BAD
#   image=vmlinuz                   GOOD
#   image=/vmlinuz                  BAD
#   image=\vmlinuz                  GOOD
    

=head1 NAME

Boot::EFI - EFI bootloader configuration module.

=head1 SYNOPSIS

  my $bootloader = new Boot::EFI(%bootvars);

  if($bootloader->footprint()) {
      $bootloader->setup();
  }

  my @fileschanged = $bootloader->files();

=cut

use strict;
use Carp;
use vars qw($VERSION);
use Boot;
use Util::Log qw(:all);
use Util::Cmd qw(:all);

$VERSION = sprintf("%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/);

push @Boot::boottypes, qw(Boot::EFI);

sub new {
    my $class = shift;
    my %this = (
                root => "",
                filesmod => [],
		bootloader_exe => "",
                bootmenu_exe => "",
		boot_bootdev => "",
                boot_timeout => 50,
                @_,
                config_file => "/boot/efi/elilo.conf",
               );

    my @elilo_locations = qw(
			      /boot/efi
			      /boot/efi/efi/redhat
			      /boot/efi/efi/sgi
			      /boot/efi/SuSE
			      /boot/efi/efi/SuSE	
			      );

    foreach my $possible (@elilo_locations) {
	my $file = $this{root} . $possible . "/elilo.conf";
	if ( -e $file ) {
	    $this{config_file} = $file;
	}
    }
    debug("config_file has been set to " . $this{config_file});

    foreach my $possible (@elilo_locations) {
	my $file = $this{root} . $possible . "/elilo.efi";
	if ( -e $file ) {
	    $this{bootloader_exe} = $file;
	}
    }
    debug("bootloader_exe has been set to " . $this{bootloader_exe});

    $this{bootmenu_exe} = which('efibootmgr');

    debug("bootmenu_exe has been set to " . $this{bootmenu_exe});

    bless \%this, $class;
}

=head1 METHODS

The following methods exist in this module:

=over 4

=item files()

The files() method is merely an accessor method for the all files
touched by the instance during its run.

=cut

sub files {
    my $this = shift;
    return @{$this->{filesmod}};
}

=item footprint()

This method returns 1 if EFI bootloader is installed. 

=cut

sub footprint_config {
    my $this = shift;
    return -e $$this{config_file};
}

sub footprint_loader {
    my $this = shift;
    
    return ((-e $$this{root} . "/boot/efi/elilo.efi" or
	     -e $$this{root} . "/boot/efi/efi/redhat/elilo.efi" or
	     -e $$this{root} . "/boot/efi/efi/sgi/elilo.efi" or
	     -e $$this{root} . "/boot/efi/efi/SuSE/elilo.efi" or
	     -e $$this{root} . "/boot/efi/SuSE/elilo.efi")
	    and $$this{bootmenu_exe} and $$this{bootloader_exe});
}


=item setup_config()

#This is for parsing the /etc/fstab and getting the boot device and
#partition number
This method read the System Configurator's config file and translates it
into the bootloader's "native" config file. 

=cut

sub install_config {
    my $this = shift;

    if(!$$this{boot_defaultboot}) 
    {
	croak("Error: DEFAULTBOOT must be specified.\n");;
    }

    open(OUT,">$$this{config_file}") or croak("Couldn\'t open $$this{config_file} for writing");
    print OUT <<ELILOCONF;
##################################################
# This file is generated by System Configurator. #
##################################################

# The number of deciseconds (0.1 seconds) to wait before booting
prompt
timeout=$$this{boot_timeout}
$this->{boot_extras}

# the default label to boot
default=$$this{boot_defaultboot}

ELILOCONF
  
  # Now we append the items that may have not been there previously
  
    if ($this->{boot_rootdev}) {
        print OUT "# Device to be mounted as the root ('/') \n";
        print OUT "root=" . $this->{boot_rootdev} . "\n";
    }
    if ($this->{boot_append}) {
        print OUT "# Kernel command line options. \n"; 
        print OUT "append=" . "\"" . $this->{boot_append} . "\"" . "\n";
    }   
    
    foreach my $key (sort keys %$this) {
        if ($key =~ /^(kernel\d+)_path/) {
            $this->setup_kernel($1,\*OUT);
        }
    }
    
    close(OUT);

    push @{$this->{filesmod}}, "$$this{config_file}";
    1;
}

=item setup_kernel()

An "internal" method.
This method sets up a kernel image as specified in the config file.

=cut

sub setup_kernel {
    my ($this, $kernel, $outfh) = @_;
    
    if ($$this{$kernel . "_label"} eq $$this{boot_defaultboot}) {
	unless ($$this{boot_rootdev} || $$this{$kernel . "_rootdev"}) {
	    croak("ROOTDEV must be specified either globally or locally.");
	    close($outfh);
	}
    }
    
    my $image = efi_friendly($$this{$kernel . "_path"});

    print $outfh <<LILOCONF;
#----- Options for \U$kernel\E -----#
image=$image
\tlabel=$$this{$kernel . "_label"}
\tread-only
LILOCONF

    ### Check for command line kernel options. 
    if ($this->{$kernel . "_append"}) {
	print $outfh "\tappend=" . "\"" . $this->{$kernel . "_append"} . "\"" . "\n";
    }

    ### Override global rootdev option?
    if ($this->{$kernel. "_rootdev"}) {
        print $outfh "\troot=" . $this->{$kernel . "_rootdev"} . "\n";
    }    

    ### Initrd image
    if ($this->{$kernel. "_initrd"}) {
        print $outfh "\tinitrd=" . efi_friendly($this->{$kernel . "_initrd"}) . "\n";
    }        
}

#   EFI caveats:
#   the kernel path names in elilo.conf (and therefore in systemconfig.conf)
#   must be relative to the efipartition, and apparently in efi syntax.

#   i.e.:

#   image=/boot/efi/vmlinuz         BAD
#   image=vmlinuz                   GOOD
#   image=/vmlinuz                  BAD
#   image=\vmlinuz                  GOOD


sub efi_friendly {
    my $path = shift;
    $path =~ s{/boot/efi/}{};
    $path =~ s{\\}{/}g;
    return $path;
}

sub install_loader {
    my $this = shift;
    $this->cleanup_bootlist();
    $this->create_entry();
}

sub create_entry {
    my $this = shift;
    my $bootmgr = $$this{bootmenu_exe};
    my $elilo = $$this{bootloader_exe};
    my $conf = $$this{config_file};
    
    my $efi_elilo = "";
    my $efi_conf = "";

    $_ = $elilo;
    if (m!^/boot/efi(/.*)$!) {
	$efi_elilo = $1;
	$efi_elilo =~ s!/!\\\\!g;
    }
    else {
	croak("elilo.efi must be in a subdirectory of /boot/efi.");
    }
    $_ = $conf;
    if (m!^/boot/efi(/.*)$!) {
	$efi_conf = $1;
	$efi_conf =~ s!/!\\\\!g;
    }
    else {
	croak("elilo.conf must be in a subdirectory of /boot/efi.");
    }

    ## This will check /etc/fstab for the boot
    ## device and partition
    if(!$$this{boot_bootdev}) 
    {
	open(FSTAB, '</etc/fstab');
	while (<FSTAB>) {
	    if (m!\s*(\S*)\s*/boot/efi\s*.*$!) {
		my $boot_partition = $1;
		$boot_partition =~ /^(.+)(\d+)$/;
		if (!$$this{boot_bootdev}) {
		    $$this{boot_bootdev} = $1;
		}
		if (!$$this{boot_bootpart}) {
		    $$this{boot_bootpart} = $2;
		}
	    }
	}
    	close(FSTAB);
    }
    

    my $cmd = "$bootmgr -c ";
    if ($$this{boot_bootdev}) {
	$cmd .= "-d $$this{boot_bootdev} ";
    }
    if ($$this{boot_bootpart}) {
	$cmd .= "-p $$this{boot_bootpart} ";
    }

    $cmd .= "-w -l $efi_elilo -u -- elilo -C $efi_conf";

    if($$this{root}) {
        if($bootmgr =~ /^($$this{root})(.*)/) {
            my $cmd2 = $2;
            $cmd = "chroot $$this{root} $cmd2 -c";
        } 
    }
    verbose("About to run '$cmd'");
    return !system("$cmd > /dev/null 2> /dev/null");
}

sub linux_efi_entries {
    my $this = shift;
    my @linuxentries = ();
    my $cmd = $$this{bootmenu_exe};
    if($$this{root}) {
        if($cmd =~ /^($$this{root})(.*)/) {
            my $cmd2 = $2;
            $cmd = "chroot $$this{root} $cmd2";
        }
    }
    open(IN,"$cmd |") or return ();
    
    while(<IN>) {
        if(/^Boot(\w{4}).*Linux/) {
            push @linuxentries, $1;
        }
    }
    close(IN);
    return @linuxentries;
}

sub cleanup_bootlist {
    my $this = shift;
    my @linuxentries = $this->linux_efi_entries();
    foreach my $entry (@linuxentries) {
        $this->remove_entry($entry);
    }
    return 1;
}

sub remove_entry {
    my ($this, $entry) = @_;

    my $e = $$this{bootmenu_exe};
    my $cmd = "$e -b $entry -B";
    if($$this{root}) {
        if($e =~ /^($$this{root})(.*)/) {
            my $cmd2  = $2;
            $cmd = "chroot $$this{root} $cmd2 -b $entry -B";
        }
    } 
    verbose("About to run '$cmd'\n");
    return !system("$cmd > /dev/null 2> /dev/null");
}

=back

=head1 AUTHOR

  dann frazier <dannf@ldl.fc.hp.com>

=head1 SEE ALSO

L<Boot>, L<perl>

=cut

1;