Bootloader.pm

filter: Statementstmt 39.0% 117 / 300 Branchbran 25.5% 47 / 184 Conditioncond 22.2% 16 / 72 MC/DCmcdc 7.1% 3 / 42 Subroutinesub 72.2% 13 / 18 Podpod 100% 11 / 11 totaltotal 33.0% 207 / 627 CC 128 SCAR 54.8
CC
128
Cov
32%
add
870.0
update
306.0
_info
156.0
CRAP 239.9
« Bootloader/Grub.pm ↑ Summary Bootloader/Detect.pm »
1 =head1 NAME
2  
3 Linux::Bootloader - Base class interacting with Linux bootloaders
4  
5 =head1 SYNOPSIS
6  
7 use Linux::Bootloader;
8
9 my $bootloader = new Linux::Bootloader();
10 my $config_file='/boot/grub/menu.lst';
11
12 $bootloader->read($config_file);
13 $bootloader->print_info('all');
14 $bootloader->add(%hash);
15 $bootloader->update(%hash);
16 $bootloader->remove(2);
17 $bootloader->get_default();
18 $bootloader->set_default(2);
19 %hash = $bootloader->read_entry(0);
20 $bootloader->write($config_file);
21  
22
23 =head1 DESCRIPTION
24  
25 This module provides base functions for working with bootloader configuration files.
26  
27 =head1 FUNCTIONS
28  
29 =head2 new()
30  
31 Creates a new Linux::Bootloader object.
32  
33 =head2 read()
34  
35 Reads configuration file into an array.
36 Takes: string.
37 Returns: undef on error.
38  
39 =head2 write()
40  
41 Writes configuration file.
42 Takes: string.
43 Returns: undef on error.
44  
45 =head2 print_info()
46  
47 Prints information from config.
48 Takes: string.
49 Returns: undef on error.
50  
51 =head2 _info()
52  
53 Parse config into array of hashes.
54 Takes: nothing.
55 Returns: array of hashes.
56  
57 =head2 get_default()
58  
59 Determine current default kernel.
60 Takes: nothing.
61 Returns: integer, undef on error.
62  
63 =head2 set_default()
64  
65 Set new default kernel.
66 Takes: integer.
67 Returns: undef on error.
68  
69 =head2 add()
70  
71 Add new kernel to config.
72 Takes: hash.
73 Returns: undef on error.
74  
75 =head2 update()
76  
77 Update args of an existing kernel entry.
78 Takes: hash.
79 Returns: undef on error.
80  
81 =head2 remove()
82  
83 Remove kernel from config.
84 Takes: string.
85 Returns: undef on error.
86  
87 =head2 read_entry()
88  
89 Read an existing entry into a hash suitable to add or update from.
90 Takes: integer or title
91 Returns: undef or hash
92  
93 =head2 debug($level)
94  
95 Sets or gets the current debug level, 0-5.
96 Returns: Debug level
97  
98 =head2 _check_config()
99  
100 Conducts a basic check for kernel validity
101 Returns: true if checks out okay,
102 false if not okay,
103 undef on error
104  
105 =head2 _lookup()
106  
107 Converts title into position.
108 Takes: string.
109 Returns: integer,
110 undef on error
111  
112 =cut
113  
114  
115 package Linux::Bootloader;
116  
117 2 ▶use Linux::Bootloader::Detect;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
119 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
120  
121 2 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
122 2 our $VERSION = '1.2';
123  
124  
125 1 ▶sub new {
Subroutine new: called CC=6 SCAR=22.6
Pod documented
126 1 my $this = shift;
127 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
128 1 ▶ if ( defined $class and $class eq 'Linux::Bootloader' ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class and $class eq "Linux::Bootloader") 0 1 1
Condition33%1/3
defined $class and $class eq "Linux::Bootloader"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $class and $class eq "Linux::Bootloader"
defined $class $class eq "Linux::Bootloader"
Truth table33%1/3
A and B
A= defined $classB= $class eq "Linux::Bootloader"
ABresult
0X0
100
111
129 0 my $detected_bootloader = Linux::Bootloader::Detect::detect_bootloader();
130 0 ▶ unless (defined $detected_bootloader) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $detected_bootloader) 0 0 0
131 0 $class = "Linux::Bootloader::" . "\u$detected_bootloader";
132 0 eval" require $class; ";
133 }
134 1 my $self = bless ({}, $class);
135 1 $self->{config_file} = shift;
136 1 ▶ unless (defined $self->{'config_file'}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config_file"}) 1 0 1
137 1 $self->_set_config_file();
138 }
139  
140 1 $self->{config} = [];
141 1 $self->{debug} = 0;
142 1 $self->{'entry'} = {};
143  
144 1 return $self;
145 }
146  
147  
148 ### Generic Functions ###
149  
150 # Read config file into array
151  
152 3 ▶sub read {
Subroutine read: called CC=7 SCAR=21.1
Pod documented
153 3 my $self=shift;
154 3 ▶ my $config_file=shift || $self->{config_file};
Condition33%1/3
shift() || $self->{'config_file'}
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() || $self->{'config_file'}
shift() $self->{'config_file'}
Truth table33%1/3
A || B
A= shift()B= $self->{'config_file'}
ABresult
1X1
011
000
155 3 ▶ print ("Reading $config_file.\n") if $self->debug()>1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 3 3
156  
157 3 ▶ open(CONFIG, "$config_file")
Branch: 1 branch
BranchTrueFalseTotal
open CONFIG, "$config_file" or warn "ERROR: Can't open $config_file.\n" and return undef 2 1 3
Condition60%3/5
open(CONFIG, "$config_file") || warn("ERROR: Can't open $config_file.\n") && (return undef)
l!l&&r!l&&!r
210
warn "ERROR: Can't open $config_file.\n" and return undef
!ll
01
MC/DC0%0/2
open(CONFIG, "$config_file") || warn("ERROR: Can't open $config_file.\n") && (return undef)
open(CONFIG, "$config_file") warn "ERROR: Can't open $config_file.\n"
Truth table66%2/3
A || B
A= open(CONFIG, "$config_file")B= warn "ERROR: Can't open $config_file.\n"
ABresult
1X1
011
000
158 || warn ("ERROR: Can't open $config_file.\n") && return undef;
159 2 @{$self->{config}}=<CONFIG>;
160 2 close(CONFIG);
161  
162 0 ▶ print ("Current config:\n @{$self->{config}}") if $self->debug()>4;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 4 0 2 2
163 2 ▶ print ("Closed $config_file.\n") if $self->debug()>2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 2 0 2 2
164 2 return 1;
165 }
166  
167  
168 # Write new config
169  
170 3 ▶sub write {
Subroutine write: called CC=10 SCAR=29.6
Pod documented
171 3 my $self=shift;
172 3 ▶ my $config_file=shift || $self->{config_file};
Condition33%1/3
shift() || $self->{'config_file'}
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() || $self->{'config_file'}
shift() $self->{'config_file'}
Truth table33%1/3
A || B
A= shift()B= $self->{'config_file'}
ABresult
1X1
011
000
173 3 my @config=@{$self->{config}};
174  
175 3 ▶ return undef unless $self->_check_config();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_config 0 3 3
176  
177 3 ▶ print ("Writing $config_file.\n") if $self->debug()>1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 3 3
178 3 ▶ print join("",@config) if $self->debug() > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 4 0 3 3
179  
180 3 ▶ if (-w $config_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-w $config_file) { } 3 0 3
181 3 system("cp","$config_file","$config_file.bak.boottool");
182 3 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) { } 0 3 3
183 0 warn "ERROR: Cannot backup $config_file.\n";
184 0 return undef;
185 } else {
186 3 print "Backed up config to $config_file.bak.boottool.\n";
187 }
188  
189 3 ▶ open(CONFIG, ">$config_file")
Branch: 1 branch
BranchTrueFalseTotal
open CONFIG, ">$config_file" or warn "ERROR: Can't open config file.\n" and return undef 3 0 3
Condition20%1/5
open(CONFIG, ">$config_file") || warn("ERROR: Can't open config file.\n") && (return undef)
l!l&&r!l&&!r
300
warn "ERROR: Can't open config file.\n" and return undef
!ll
00
MC/DC0%0/2
open(CONFIG, ">$config_file") || warn("ERROR: Can't open config file.\n") && (return undef)
open(CONFIG, ">$config_file") warn "ERROR: Can't open config file.\n"
Truth table33%1/3
A || B
A= open(CONFIG, ">$config_file")B= warn "ERROR: Can't open config file.\n"
ABresult
1X1
011
000
190 || warn ("ERROR: Can't open config file.\n") && return undef;
191 3 print CONFIG join("",@config);
192 3 close(CONFIG);
193 3 return 0;
194 } else {
195 0 ▶ print join("",@config) if $self->debug() > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 2 0 0 0
196 0 warn "WARNING: You do not have write access to $config_file.\n";
197 0 return 1;
198 }
199 }
200  
201  
202 # Parse config into array of hashes
203  
204 0 ▶sub _info {
Subroutine _info: not called CC=12 SCAR=50.5
205 0 my $self=shift;
206  
207 0 ▶ return undef unless $self->_check_config();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_config 0 0 0
208 0 my @config=@{$self->{config}};
209  
210 # remove garbarge - comments, blank lines
211 0 @config=grep(!/^#|^\n/, @config);
212  
213 0 my %matches = ( default => '^\s*default[\s+\=]+(\S+)',
214 timeout => '^\s*timeout[\s+\=]+(\S+)',
215 title => '^\s*label[\s+\=]+(\S+)',
216 root => '^\s*root[\s+\=]+(\S+)',
217 args => '^\s*append[\s+\=]+(.*)',
218 initrd => '^\s*initrd[\s+\=]+(\S+)',
219 );
220  
221 0 my @sections;
222 0 my $index=0;
223 0 foreach (@config) {
224 0 ▶ if ($_ =~ /^\s*(image|other)[\s+\=]+(\S+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^\s*(image|other)[\s+\=]+(\S+)/i) 0 0 0
225 0 $index++;
226 0 $sections[$index]{'kernel'} = $2;
227 }
228 0 foreach my $key (keys %matches) {
229 0 ▶ if ($_ =~ /$matches{$key}/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /$matches{$key}/i) 0 0 0
230 0 $sections[$index]{$key} = $1;
231 0 ▶ $sections[$index]{$key} =~ s/\"|\'//g if ($key eq 'args');
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "args" 0 0 0
232 }
233 }
234 }
235  
236 # sometimes config doesn't have a default, so goes to first
237 0 ▶ if (!(defined $sections[0]{'default'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $sections[0]{'default'}) { } 0 0 0
elsif (not $sections[0]{'default'} =~ /^\d+$/) { } 0 0 0
238 0 $sections[0]{'default'} = '0';
239  
240 # if default is label name, we need position
241 } elsif ($sections[0]{'default'} !~ m/^\d+$/) {
242 0 foreach my $index (1..$#sections) {
243 0 ▶ if ($sections[$index]{'title'} eq $sections[0]{'default'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sections[$index]{"title"} eq $sections[0]{"default"}) 0 0 0
244 0 $sections[0]{'default'} = $index-1;
245 0 last;
246 }
247 }
248 }
249  
250 # if still no valid default, set to first
251 0 ▶ if ( $sections[0]{'default'} !~ m/^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sections[0]{"default"} =~ /^\d+$/) 0 0 0
252 0 $sections[0]{'default'} = 0;
253 }
254  
255 # return array of hashes
256 0 return @sections;
257 }
258  
259  
260 # Determine current default kernel
261  
262 3 ▶sub get_default {
Subroutine get_default: called CC=4 SCAR=14.3
Pod documented
263 3 my $self = shift;
264  
265 3 ▶ print ("Getting default.\n") if $self->debug()>1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 3 3
266 3 ▶ return undef unless $self->_check_config();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_config 0 3 3
267  
268 3 my @sections = $self->_info();
269 3 my $default = $sections[0]{'default'};
270 3 ▶ if ($default =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default =~ /^\d+$/) 3 0 3
271 3 return 0+$default;
272 }
273  
274 }
275  
276  
277 # Set new default kernel
278  
279 0 ▶sub set_default {
Subroutine set_default: not called CC=10 SCAR=47.0
Pod documented
280 0 my $self=shift;
281 0 my $newdefault=shift;
282  
283 0 ▶ print ("Setting default.\n") if $self->debug()>1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 0 0
284  
285 0 ▶ return undef unless defined $newdefault;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $newdefault 0 0 0
286 0 ▶ return undef unless $self->_check_config();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_config 0 0 0
287  
288 0 my @config=@{$self->{config}};
289 0 my @sections=$self->_info();
290  
291 # if not a number, do title lookup
292 0 ▶ if ($newdefault !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newdefault =~ /^\d+$/) 0 0 0
293 0 $newdefault = $self->_lookup($newdefault);
294 }
295  
296 0 my $kcount = $#sections-1;
297 0 ▶ if ((!defined $newdefault) || ($newdefault < 0) || ($newdefault > $kcount)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $newdefault or $newdefault < 0 or $newdefault > $kcount) 0 0 0
Condition0%0/6
not defined $newdefault or $newdefault < 0 or $newdefault > $kcount
l!l&&r!l&&!r
000
not defined $newdefault or $newdefault < 0
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $newdefault or $newdefault < 0 or $newdefault > $kcount
not defined $newdefault $newdefault < 0 $newdefault > $kcount
Truth table0%0/4
not(A) or B or C
A= not defined $newdefaultB= $newdefault < 0C= $newdefault > $kcount
ABCresult
1XX1
01X1
0011
0000
298 0 warn "ERROR: Enter a default between 0 and $kcount.\n";
299 0 return undef;
300 }
301  
302 # convert position to title
303 0 $newdefault = $sections[++$newdefault]{title};
304
305 0 foreach my $index (0..$#config) {
306 0 ▶ if ($config[$index] =~ /^\s*default/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config[$index] =~ /^\s*default/i) 0 0 0
307 0 $config[$index] = "default=$newdefault # set by $0\n";
308 0 last;
309 }
310 }
311 0 @{$self->{config}} = @config;
312 }
313  
314  
315 # Add new kernel to config
316  
317 0 ▶sub add {
Subroutine add: not called CC=29 SCAR=67.7
Pod documented
318 0 my $self=shift;
319 0 my %param=@_;
320  
321 0 ▶ print ("Adding kernel.\n") if $self->debug()>1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 0 0
322  
323 0 ▶ if (!defined $param{'add-kernel'} && defined $param{'kernel'}) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $param{'add-kernel'} and defined $param{'kernel'}) { } 0 0 0
elsif (not defined $param{'add-kernel'} && defined $param{'title'}) { } 0 0 0
elsif (not -f "$param{'add-kernel'}") { } 0 0 0
elsif (defined $param{'initrd'} and not -f "$param{'initrd'}") { } 0 0 0
Condition0%0/3
not defined $param{"add-kernel"} and defined $param{"kernel"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $param{"add-kernel"} and defined $param{"kernel"}
not defined $param{"add-kernel"} defined $param{"kernel"}
Truth table0%0/3
not(A) and B
A= not defined $param{"add-kernel"}B= defined $param{"kernel"}
ABresult
0X0
100
111
324 0 $param{'add-kernel'} = $param{'kernel'};
325 ▶ } elsif (!defined $param{'add-kernel'} || !defined $param{'title'}) {
Condition0%0/3
defined $param{'add-kernel'} && defined $param{'title'}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $param{'add-kernel'} && defined $param{'title'}
defined $param{'add-kernel'} defined $param{'title'}
Truth table0%0/3
A && B
A= defined $param{'add-kernel'}B= defined $param{'title'}
ABresult
0X0
100
111
326 0 warn "ERROR: kernel path (--add-kernel), title (--title) required.\n";
327 0 return undef;
328 } elsif (!(-f "$param{'add-kernel'}")) {
329 0 warn "ERROR: kernel $param{'add-kernel'} not found!\n";
330 0 return undef;
331 ▶ } elsif (defined $param{'initrd'} && !(-f "$param{'initrd'}")) {
Condition0%0/3
defined $param{"initrd"} and not -f qq[$param{"initrd"}]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $param{"initrd"} and not -f qq[$param{"initrd"}]
defined $param{"initrd"} not -f qq[$param{"initrd"}]
Truth table0%0/3
A and not(B)
A= defined $param{"initrd"}B= not -f qq[$param{"initrd"}]
ABresult
0X0
100
111
332 0 warn "ERROR: initrd $param{'initrd'} not found!\n";
333 0 return undef;
334 }
335  
336 0 ▶ return undef unless $self->_check_config();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_config 0 0 0
337  
338 # remove title spaces and truncate if more than 15 chars
339 0 $param{title} =~ s/\s+//g;
340 0 ▶ $param{title} = substr($param{title}, 0, 15) if length($param{title}) > 15;
Branch: 1 branch
BranchTrueFalseTotal
if length $param{"title"} > 15 0 0 0
341  
342 0 my @sections=$self->_info();
343  
344 # check if title already exists
345 0 ▶ if (defined $self->_lookup($param{title})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_lookup($param{"title"})) 0 0 0
346 0 warn ("WARNING: Title already exists.\n");
347 0 ▶ if (defined $param{force}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{'force'}) { } 0 0 0
348 0 $self->remove($param{title});
349 } else {
350 0 return undef;
351 }
352 }
353  
354 0 my @config = @{$self->{config}};
355 0 @sections=$self->_info();
356
357 # Use default kernel to fill in missing info
358 0 my $default=$self->get_default();
359 0 $default++;
360  
361 0 foreach my $p ('args', 'root') {
362 0 ▶ if (! defined $param{$p}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $param{$p}) 0 0 0
363 0 $param{$p} = $sections[$default]{$p};
364 }
365 }
366  
367 # use default entry to determine if path (/boot) should be removed
368 0 ▶ if ($sections[$default]{'kernel'} !~ /^\/boot/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sections[$default]{"kernel"} =~ m[^/boot]) 0 0 0
369 0 $param{'add-kernel'} =~ s/^\/boot//;
370 0 ▶ $param{'initrd'} =~ s/^\/boot// unless (!defined $param{'initrd'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"initrd"} 0 0 0
371 }
372  
373 0 my @newkernel;
374 0 push (@newkernel, "image=$param{'add-kernel'}\n", "\tlabel=$param{title}\n");
375 0 ▶ push (@newkernel, "\tappend=\"$param{args}\"\n") if defined $param{args};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"args"} 0 0 0
376 0 ▶ push (@newkernel, "\tinitrd=$param{initrd}\n") if defined $param{initrd};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"initrd"} 0 0 0
377 0 ▶ push (@newkernel, "\troot=$param{root}\n") if defined $param{root};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"root"} 0 0 0
378 0 push (@newkernel, "\tread-only\n\n");
379  
380 0 ▶ if (!defined $param{position} || $param{position} !~ /end|\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $param{"position"} and $param{"position"} =~ /end|\d+/) 0 0 0
Condition0%0/3
defined $param{"position"} and $param{"position"} =~ /end|\d+/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $param{"position"} and $param{"position"} =~ /end|\d+/
defined $param{"position"} $param{"position"} =~ /end|\d+/
Truth table0%0/3
A and B
A= defined $param{"position"}B= $param{"position"} =~ /end|\d+/
ABresult
0X0
100
111
381 0 $param{position}=0;
382 }
383  
384 0 my @newconfig;
385 0 ▶ if ($param{position}=~/end/ || $param{position} >= $#sections) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'position'} =~ /end/ or $param{'position'} >= $#sections) { } 0 0 0
Condition0%0/3
$param{"position"} =~ /end/ or $param{"position"} >= $#sections
l!l&&r!l&&!r
000
MC/DC0%0/2
$param{"position"} =~ /end/ or $param{"position"} >= $#sections
$param{"position"} =~ /end/ $param{"position"} >= $#sections
Truth table0%0/3
A or B
A= $param{"position"} =~ /end/B= $param{"position"} >= $#sections
ABresult
1X1
011
000
386 0 $param{position}=$#sections;
387 0 push (@newconfig,@config);
388 0 ▶ if ($newconfig[$#newconfig] =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newconfig[$#newconfig] =~ /\S/) 0 0 0
389 0 push (@newconfig, "\n");
390 }
391 0 push (@newconfig,@newkernel);
392 } else {
393 0 my $index=0;
394 0 foreach (@config) {
395 0 ▶ if ($_ =~ /^\s*(image|other)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^\s*(image|other)/i) 0 0 0
396 0 ▶ if ($index==$param{position}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == $param{"position"}) 0 0 0
397 0 push (@newconfig, @newkernel);
398 }
399 0 $index++;
400 }
401 0 push (@newconfig, $_);
402 }
403 }
404  
405 0 @{$self->{config}} = @newconfig;
406  
407 0 ▶ if (defined $param{'make-default'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"make-default"}) 0 0 0
408 0 $self->set_default($param{position});
409 }
410 }
411  
412  
413 # Update kernel args
414  
415 0 ▶sub update {
Subroutine update: not called CC=17 SCAR=57.2
Pod documented
416 0 my $self=shift;
417 0 my %params=@_;
418  
419 0 ▶ print ("Updating kernel.\n") if $self->debug()>1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 0 0
420  
421 0 ▶ if (!defined $params{'update-kernel'} || (!defined $params{'args'} && !defined $params{'remove-args'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"update-kernel"} and defined $params{"args"} || defined $params{"remove-args"}) 0 0 0
Condition0%0/6
defined $params{"update-kernel"} and defined $params{"args"} || defined $params{"remove-args"}
!ll&&!rl&&r
000
defined $params{"args"} or defined $params{"remove-args"}
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $params{"update-kernel"} and defined $params{"args"} || defined $params{"remove-args"}
defined $params{"update-kernel"} defined $params{"args"} defined $params{"remove-args"}
Truth table0%0/4
A and B or C
A= defined $params{"update-kernel"}B= defined $params{"args"}C= defined $params{"remove-args"}
ABCresult
0XX0
1000
11X1
1011
422 0 warn "ERROR: kernel position or title (--update-kernel) and args (--args or --remove-args) required.\n";
423 0 return undef;
424 }
425  
426 0 ▶ return undef unless $self->_check_config();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_config 0 0 0
427  
428 0 my @config = @{$self->{config}};
429 0 my @sections=$self->_info();
430  
431 # if not a number, do title lookup
432 0 ▶ if ($params{'update-kernel'} !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"update-kernel"} =~ /^\d+$/) 0 0 0
433 0 $params{'update-kernel'} = $self->_lookup($params{'update-kernel'});
434 }
435  
436 0 my $kcount = $#sections-1;
437 0 ▶ if ($params{'update-kernel'} !~ /^\d+$/ || $params{'update-kernel'} < 0 || $params{'update-kernel'} > $kcount) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $params{"update-kernel"} =~ /^\d+$/ or $params{"update-kernel"} < 0 or $params{"update-kernel"} > $kcount) 0 0 0
Condition0%0/6
not $params{"update-kernel"} =~ /^\d+$/ or $params{"update-kernel"} < 0 or $params{"update-kernel"} > $kcount
l!l&&r!l&&!r
000
not $params{"update-kernel"} =~ /^\d+$/ or $params{"update-kernel"} < 0
l!l&&r!l&&!r
000
MC/DC0%0/3
not $params{"update-kernel"} =~ /^\d+$/ or $params{"update-kernel"} < 0 or $params{"update-kernel"} > $kcount
not $params{"update-kernel"} =~ /^\d+$/ $params{"update-kernel"} < 0 $params{"update-kernel"} > $kcount
Truth table0%0/4
not(A) or B or C
A= not $params{"update-kernel"} =~ /^\d+$/B= $params{"update-kernel"} < 0C= $params{"update-kernel"} > $kcount
ABCresult
1XX1
01X1
0011
0000
438 0 warn "ERROR: Enter a default between 0 and $kcount.\n";
439 0 return undef;
440 }
441  
442 0 my $index=-1;
443 0 foreach (@config) {
444 0 ▶ if ($_ =~ /^\s*(image|other)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^\s*(image|other)/i) 0 0 0
445 0 $index++;
446 }
447 0 ▶ if ($index==$params{'update-kernel'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == $params{"update-kernel"}) 0 0 0
448 0 ▶ if ($_ =~ /(^\s*append[\s\=]+)(.*)\n/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /(^\s*append[\s\=]+)(.*)\n/i) 0 0 0
449 0 my $append = $1;
450 0 my $args = $2;
451 0 $args =~ s/\"|\'//g;
452 0 ▶ $args =~ s/\s*$params{'remove-args'}\=*\S*//ig if defined $params{'remove-args'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"remove-args"} 0 0 0
453 0 ▶ $args = $args . " ". $params{'args'} if defined $params{'args'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"args"} 0 0 0
454 0 ▶ if ($_ eq "$append\"$args\"\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq qq[$append"$args"\n]) { } 0 0 0
455 0 warn "WARNING: No change made to args.\n";
456 0 return undef;
457 } else {
458 0 $_ = "$append\"$args\"\n";
459 }
460 0 next;
461 }
462 }
463 }
464 0 @{$self->{config}} = @config;
465 }
466  
467  
468 # Remove kernel from config
469  
470 3 ▶sub remove {
Subroutine remove: called CC=20 SCAR=33.2
Pod documented
471 3 my $self=shift;
472 3 my $position=shift;
473 3 my @newconfig;
474  
475 3 ▶ return undef unless defined $position;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $position 0 3 3
476 3 ▶ return undef unless $self->_check_config();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_config 0 3 3
477  
478 3 my @config=@{$self->{config}};
479 3 my @sections=$self->_info();
480  
481 3 ▶ if ($position=~/^end$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($position =~ /^end$/i) { } 0 3 3
elsif ($position =~ /^start$/i) { } 0 3 3
482 0 $position=$#sections-1;
483 } elsif ($position=~/^start$/i) {
484 0 $position=0;
485 }
486  
487 3 ▶ print ("Removing kernel $position.\n") if $self->debug()>1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 3 3
488  
489 # remove based on title
490 3 ▶ if ($position !~ /^\d+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $position =~ /^\d+$/) { } 1 2 3
elsif ($position =~ /^\d+$/) { } 2 0 2
491 1 my $removed=0;
492 1 ▶ for (my $index=$#sections; $index > 0; $index--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index > 0) 3 1 4
493 3 ▶ if (defined $sections[$index]{title} && $position eq $sections[$index]{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sections[$index]{"title"} and $position eq $sections[$index]{"title"}) 0 3 3
Condition33%1/3
defined $sections[$index]{"title"} and $position eq $sections[$index]{"title"}
!ll&&!rl&&r
030
MC/DC0%0/2
defined $sections[$index]{"title"} and $position eq $sections[$index]{"title"}
defined $sections[$index]{"title"} $position eq $sections[$index]{"title"}
Truth table33%1/3
A and B
A= defined $sections[$index]{"title"}B= $position eq $sections[$index]{"title"}
ABresult
0X0
100
111
494 0 ▶ $removed++ if $self->remove($index-1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->remove($index - 1) 0 0 0
495 }
496 }
497 1 ▶ if (! $removed) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $removed) 1 0 1
498 1 warn "ERROR: No kernel with specified title.\n";
499 1 return undef;
500 }
501  
502 # remove based on position
503 } elsif ($position =~ /^\d+$/) {
504  
505 2 ▶ if ($position < 0 || $position > $#sections) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position < 0 or $position > $#sections) 1 1 2
Condition66%2/3
$position < 0 or $position > $#sections
l!l&&r!l&&!r
011
MC/DC50%1/2
$position < 0 or $position > $#sections
$position < 0 $position > $#sections
Truth table66%2/3
A or B
A= $position < 0B= $position > $#sections
ABresult
1X1
011
000
506 1 warn "ERROR: Enter a position between 0 and $#sections.\n";
507 1 return undef;
508 }
509  
510 1 my $index=-1;
511 1 foreach (@config) {
512 19 ▶ if ($_ =~ /^\s*(image|other|title)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^\s*(image|other|title)/i) 3 16 19
513 3 $index++
514 }
515 # add everything to newconfig, except removed kernel (keep comments)
516 19 ▶ if ($index != $position || $_ =~ /^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index != $position or $_ =~ /^#/) 15 4 19
Condition66%2/3
$index != $position or $_ =~ /^#/
l!l&&r!l&&!r
1504
MC/DC50%1/2
$index != $position or $_ =~ /^#/
$index != $position $_ =~ /^#/
Truth table66%2/3
A or B
A= $index != $positionB= $_ =~ /^#/
ABresult
1X1
011
000
517 15 push (@newconfig, $_)
518 }
519 }
520 1 @{$self->{config}} = @newconfig;
521  
522  
523 # if we removed the default, set new default to first
524 1 ▶ $self->set_default(0) if $position == $sections[0]{'default'};
Branch: 1 branch
BranchTrueFalseTotal
if $position == $sections[0]{"default"} 1 0 1
525  
526 1 print "Removed kernel $position.\n";
527 1 return 1;
528  
529 } else {
530 0 warn "WARNING: problem removing entered position.\n";
531 0 return undef;
532 }
533  
534 }
535  
536  
537 # Print info from config
538  
539 3 ▶sub print_info {
Subroutine print_info: called CC=11 SCAR=35.1
Pod documented
540 3 my $self=shift;
541 3 my $info=shift;
542  
543 3 ▶ return undef unless defined $info;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info 0 3 3
544 3 ▶ return undef unless $self->_check_config();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_config 0 3 3
545  
546 3 ▶ print ("Printing config info.\n") if $self->debug()>1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 3 3
547  
548 3 my @config=@{$self->{config}};
549 3 my @sections=$self->_info();
550  
551 3 my ($start,$end);
552 3 ▶ if ($info =~ /default/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($info =~ /default/i) { } 0 3 3
elsif ($info =~ /all/i) { } 0 3 3
elsif ($info =~ /^\d+/) { } 0 3 3
553 0 $start=$end=$self->get_default()
554 } elsif ($info =~ /all/i) {
555 0 $start=0; $end=$#sections-1
556 } elsif ($info =~ /^\d+/) {
557 0 $start=$end=$info
558 } else {
559 3 warn "ERROR: input should be: #, default, or all.\n";
560 3 return undef;
561 }
562  
563 0 ▶ if ($start < 0 || $end > $#sections-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0 or $end > $#sections - 1) 0 0 0
Condition0%0/3
$start < 0 or $end > $#sections - 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$start < 0 or $end > $#sections - 1
$start < 0 $end > $#sections - 1
Truth table0%0/3
A or B
A= $start < 0B= $end > $#sections - 1
ABresult
1X1
011
000
564 0 warn "ERROR: No kernels with that index.\n";
565 0 return undef;
566 }
567  
568 0 for my $index ($start..$end) {
569 0 print "\nindex\t: $index\n";
570 0 $index++;
571 0 foreach ( sort keys(%{$sections[$index]}) ) {
572 0 print "$_\t: $sections[$index]{$_}\n";
573 }
574 }
575 }
576  
577  
578 # Set/get debug level
579  
580 69 ▶sub debug {
Subroutine debug: called CC=3 SCAR=12.5
Pod documented
581 69 my $self=shift;
582 69 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 69 69
583 0 $self->{debug} = shift;
584 }
585 69 ▶ return $self->{debug} || 0;
Condition50%1/2
$self->{'debug'} || 0
l!l
069
MC/DC0%0/1
$self->{'debug'} || 0
$self->{'debug'}
Truth table50%1/2
A
A= $self->{'debug'}
Aresult
11
00
586 }
587  
588 # Get a bootloader entry as a hash to edit or update.
589 0 ▶sub read_entry {
Subroutine read_entry: not called CC=4 SCAR=30.0
Pod documented
590 0 my $self=shift;
591 0 my $entry=shift;
592  
593 0 ▶ if ($entry !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry =~ /^\d+$/) 0 0 0
594 0 $entry = $self->_lookup($entry);
595 }
596 0 my @sections=$self->_info();
597  
598 0 my $index = $entry + 1;
599 0 ▶ if ((defined $sections[$index]{'title'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sections[$index]{'title'}) { } 0 0 0
600 0 $self->{'entry'}->{'index'} = $index;
601 0 foreach my $key ( keys %{$sections[$index]} ){
602 0 $self->{'entry'}->{'data'}->{ $key } = $sections[$index]{$key};
603 }
604 0 return $self->{'entry'}->{'data'};
605 } else {
606 0 return undef;
607 }
608 }
609  
610 # Basic check for valid config
611  
612 41 ▶sub _check_config {
Subroutine _check_config: called CC=3 SCAR=12.3
613 41 my $self=shift;
614  
615 41 ▶ print ("Verifying config.\n") if $self->debug()>3;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 3 0 41 41
616  
617 41 ▶ if ($#{$self->{config}} < 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{"config"};} < 5) 0 41 41
618 0 warn "ERROR: you must read a valid config file first.\n";
619 0 return undef;
620 }
621 41 return 1;
622 }
623  
624  
625 # lookup position using title
626  
627 4 ▶sub _lookup {
Subroutine _lookup: called CC=5 SCAR=16.4
628 4 my $self=shift;
629 4 my $title=shift;
630
631 0 ▶ unless ( defined $title ){ return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $title) 0 4 4
632  
633 4 my @sections=$self->_info();
634  
635 4 for my $index (1..$#sections) {
636 10 my $tmp = $sections[$index]{title};
637 10 ▶ if (defined $tmp and $title eq $tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tmp and $title eq $tmp) 1 9 10
Condition66%2/3
defined $tmp and $title eq $tmp
!ll&&!rl&&r
091
MC/DC50%1/2
defined $tmp and $title eq $tmp
defined $tmp $title eq $tmp
Truth table66%2/3
A and B
A= defined $tmpB= $title eq $tmp
ABresult
0X0
100
111
638 1 return $index-1;
639 }
640 }
641 3 return undef;
642 }
643  
644  
645 =head1 AUTHOR
646  
647 Jason N., Open Source Development Labs, Engineering Department <eng@osdl.org>
648  
649 =head1 COPYRIGHT
650  
651 Copyright (C) 2006 Open Source Development Labs
652 All Rights Reserved.
653  
654 This script is free software; you can redistribute it and/or modify it
655 under the same terms as Perl itself.
656  
657 =head1 SEE ALSO
658  
659 L<boottool>, L<Linux::Bootloader::Grub>, L<Linux::Bootloader::Lilo>,
660 L<Linux::Bootloader::Elilo>, L<Linux::Bootloader::Yaboot>
661  
662 =cut
663  
664  
665 2 1;
« Bootloader/Grub.pm ↑ Summary Bootloader/Detect.pm »