Bootloader/Grub.pm

filter: Statementstmt 45.6% 120 / 263 Branchbran 34.6% 52 / 150 Conditioncond 20.6% 13 / 63 MC/DCmcdc 14.2% 5 / 35 Subroutinesub 66.6% 10 / 15 Podpod 100% 7 / 7 totaltotal 38.8% 207 / 533 CC 106 SCAR 56.8
CC
106
Cov
39%
update
930.0
add
140.4
set_default
53.1
CRAP 291.6
↑ Summary Bootloader.pm »
1 package Linux::Bootloader::Grub;
2  
3 =head1 NAME
4  
5 Linux::Bootloader::Grub - Parse and modify GRUB configuration files.
6  
7 =head1 SYNOPSIS
8  
9 use Linux::Bootloader;
10 use Linux::Bootloader::Grub;
11  
12 my $config_file='/boot/grub/menu.lst';
13 $bootloader = Linux::Bootloader::Grub->new($config_file);
14  
15 $bootloader->read();
16  
17 # add a kernel
18 $bootloader->add(%hash)
19  
20 # remove a kernel
21 $bootloader->remove(2)
22  
23 # print config info
24 $bootloader->print_info('all')
25  
26 # set new default
27 $bootloader->set_default(1)
28  
29 $bootloader->write();
30  
31  
32 =head1 DESCRIPTION
33  
34 This module provides functions for working with GRUB configuration files.
35  
36 Adding a kernel:
37 - add kernel at start, end, or any index position.
38 - kernel path and title are required.
39 - root, kernel args, initrd, savedefault, module are optional.
40 - any options not specified are copied from default.
41 - remove any conflicting kernels first if force is specified.
42
43 Removing a kernel:
44 - remove by index position
45 - or by title/label
46  
47  
48 =head1 FUNCTIONS
49  
50 Also see L<Linux::Bootloader> for functions available from the base class.
51  
52 =head2 new()
53  
54 Creates a new Linux::Bootloader::Grub object.
55  
56 =head2 _info()
57  
58 Parse config into array of hashes.
59 Takes: nothing.
60 Returns: array of hashes containing config file options and boot entries,
61 undef on error.
62  
63 =head2 set_default()
64  
65 Set new default kernel.
66 Takes: integer or string, boot menu position or title.
67 Returns: undef on error.
68  
69 =head2 add()
70  
71 Add new kernel to config.
72 Takes: hash containing kernel path, title, etc.
73 Returns: undef on error.
74  
75 =head2 update()
76  
77 Update args of an existing kernel entry.
78 Takes: hash containing args and entry to update.
79 Returns: undef on error.
80  
81 =head2 install()
82  
83 Prints message on how to re-install grub.
84 Takes: nothing.
85 Returns: nothing.
86  
87 =head2 update_main_options()
88  
89 This updates or adds a general line anywhere before the first 'title' line.
90 it is called with the 'update' and 'option' options, when no 'update-kernel'
91 is specified.
92  
93 =head2 boot_once()
94  
95 This is a special case of using 'fallback'. This function makes the current
96 default the fallback kernel and sets the passed argument to be the default
97 kernel which saves to the fallback kernel after booting. The file
98 '/boot/grub/default' is created if it does not exist.
99  
100 This only works with grub versions 0.97 or better.
101  
102 =head2 _get_bootloader_version()
103  
104 Prints detected grub version.
105 Takes: nothing.
106 Returns: nothing.
107  
108 =cut
109  
110 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 use Linux::Bootloader
113  
114 1 ▶@Linux::Bootloader::Grub::ISA = qw(Linux::Bootloader);
Subroutine BEGIN: called CC=1 SCAR=0.0
115 1 ▶use base 'Linux::Bootloader';
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117  
118 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
119 1 our $VERSION = '1.2';
120  
121  
122 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
123 1 my $this = shift;
124 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
125 1 my $self = bless({}, $class);
126 #my $self = fields::new($class);
127  
128 1 $self->SUPER::new();
129  
130 1 return $self;
131 }
132  
133 1 ▶sub _set_config_file {
Subroutine _set_config_file: called CC=1 SCAR=0.0
134 1 my $self=shift;
135 1 $self->{'config_file'}='/boot/grub/menu.lst';
136 }
137  
138  
139 ### GRUB functions ###
140  
141 # Parse config into array of hashes
142  
143 22 ▶sub _info {
Subroutine _info: called CC=13 SCAR=29.8
144 22 my $self=shift;
145  
146 22 ▶ return undef unless $self->_check_config();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_config 0 22 22
147  
148 22 my @config=@{$self->{config}};
149 22 @config=grep(!/^#|^\n/, @config);
150  
151 22 my %matches = ( default => '^\s*default\s*\=*\s*(\S+)',
152 timeout => '^\s*timeout\s*\=*\s*(\S+)',
153 fallback => '^\s*fallback\s*\=*\s*(\S+)',
154 kernel => '^\s*kernel\s+(\S+)',
155 root => '^\s*kernel\s+.*\s+.*root=(\S+)',
156 args => '^\s*kernel\s+\S+\s+(.*)\n',
157 boot => '^\s*root\s+(.*)',
158 initrd => '^\s*initrd\s+(.*)',
159 savedefault => '^\s*savedefault\s+(.*)',
160 module => '^\s*module\s+(.+)',
161 );
162  
163 22 my @sections;
164 22 my $index=0;
165 22 foreach (@config) {
166 305 ▶ if ($_ =~ /^\s*title\s+(.*)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^\s*title\s+(.*)/i) 65 240 305
167 65 $index++;
168 65 $sections[$index]{title} = $1;
169 }
170 305 foreach my $key (keys %matches) {
171 3050 ▶ if ($_ =~ /$matches{$key}/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /$matches{$key}/i) 326 2724 3050
172 326 ▶ $key .= '2' if exists $sections[$index]{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sections[$index]{$key} 0 326 326
173 326 $sections[$index]{$key} = $1;
174 326 ▶ if ($key eq 'args') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "args") 65 261 326
175 65 $sections[$index]{$key} =~ s/root=\S+\s*//i;
176 65 ▶ delete $sections[$index]{$key} if ($sections[$index]{$key} !~ /\S/);
Branch: 1 branch
BranchTrueFalseTotal
unless $sections[$index]{$key} =~ /\S/ 22 43 65
177 }
178 }
179 }
180 }
181  
182 # sometimes config doesn't have a default, so goes to first
183 22 ▶ if (!(defined $sections[0]{'default'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $sections[0]{'default'}) { } 0 22 22
elsif ($sections[0]{'default'} =~ /^saved$/i) { } 0 22 22
184 0 $sections[0]{'default'} = '0';
185  
186 # if default is 'saved', read from grub default file
187 } elsif ($sections[0]{'default'} =~ m/^saved$/i) {
188 0 ▶ open(DEFAULT_FILE, '/boot/grub/default')
Branch: 1 branch
BranchTrueFalseTotal
open DEFAULT_FILE, "/boot/grub/default" or warn "ERROR: cannot read grub default file.\n" and return undef 0 0 0
Condition0%0/5
open(DEFAULT_FILE, '/boot/grub/default') || warn("ERROR: cannot read grub default file.\n") && (return undef)
l!l&&r!l&&!r
000
warn "ERROR: cannot read grub default file.\n" and return undef
!ll
00
MC/DC0%0/2
open(DEFAULT_FILE, '/boot/grub/default') || warn("ERROR: cannot read grub default file.\n") && (return undef)
open(DEFAULT_FILE, '/boot/grub/default') warn "ERROR: cannot read grub default file.\n"
Truth table0%0/3
A || B
A= open(DEFAULT_FILE, '/boot/grub/default')B= warn "ERROR: cannot read grub default file.\n"
ABresult
1X1
011
000
189 || warn ("ERROR: cannot read grub default file.\n") && return undef;
190 0 my @default_config = <DEFAULT_FILE>;
191 0 close(DEFAULT_FILE);
192 0 $default_config[0] =~ /^(\d+)/;
193 0 $sections[0]{'default'} = $1;
194 }
195  
196 # return array of hashes
197 22 return @sections;
198 }
199  
200  
201 # Set new default kernel
202  
203 5 ▶sub set_default {
Subroutine set_default: called CC=17 SCAR=39.7
Pod documented
204 5 my $self=shift;
205 5 my $newdefault=shift;
206  
207 5 ▶ return undef unless defined $newdefault;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $newdefault 0 5 5
208 5 ▶ return undef unless $self->_check_config();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_config 0 5 5
209  
210 5 my @config=@{$self->{config}};
211 5 my @sections=$self->_info();
212  
213 # if not a number, do title lookup
214 5 ▶ if ($newdefault !~ /^\d+$/ && $newdefault !~ m/^saved$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newdefault =~ /^\d+$/ or $newdefault =~ /^saved$/) 2 3 5
Condition66%2/3
$newdefault =~ /^\d+$/ or $newdefault =~ /^saved$/
l!l&&r!l&&!r
302
MC/DC50%1/2
$newdefault =~ /^\d+$/ or $newdefault =~ /^saved$/
$newdefault =~ /^\d+$/ $newdefault =~ /^saved$/
Truth table66%2/3
A or B
A= $newdefault =~ /^\d+$/B= $newdefault =~ /^saved$/
ABresult
1X1
011
000
215 2 $newdefault = $self->_lookup($newdefault);
216 2 ▶ return undef unless (defined $newdefault);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $newdefault 2 0 2
217 }
218  
219 3 my $kcount = $#sections-1;
220 3 ▶ if ($newdefault !~ m/saved/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newdefault =~ /saved/) 3 0 3
221 3 ▶ if (($newdefault < 0) || ($newdefault > $kcount)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newdefault < 0 or $newdefault > $kcount) 1 2 3
Condition66%2/3
$newdefault < 0 or $newdefault > $kcount
l!l&&r!l&&!r
012
MC/DC50%1/2
$newdefault < 0 or $newdefault > $kcount
$newdefault < 0 $newdefault > $kcount
Truth table66%2/3
A or B
A= $newdefault < 0B= $newdefault > $kcount
ABresult
1X1
011
000
222 1 warn "ERROR: Enter a default between 0 and $kcount.\n";
223 1 return undef;
224 }
225 }
226  
227 2 foreach my $index (0..$#config) {
228  
229 4 ▶ if ($config[$index] =~ /(^\s*default\s*\=*\s*)\d+/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config[$index] =~ /(^\s*default\s*\=*\s*)\d+/i) { } 2 2 4
elsif ($config[$index] =~ /^\s*default\s*\=*\s*saved/i) { } 0 2 2
230 2 $config[$index] = "$1$newdefault # set by $0\n";
231 2 last;
232 } elsif ($config[$index] =~ /^\s*default\s*\=*\s*saved/i) {
233 0 my @default_config;
234 0 my $default_config_file='/boot/grub/default';
235  
236 0 ▶ open(DEFAULT_FILE, $default_config_file)
Branch: 1 branch
BranchTrueFalseTotal
open DEFAULT_FILE, $default_config_file or warn "ERROR: cannot open default file.\n" and return undef 0 0 0
Condition0%0/5
open(DEFAULT_FILE, $default_config_file) || warn("ERROR: cannot open default file.\n") && (return undef)
l!l&&r!l&&!r
000
warn "ERROR: cannot open default file.\n" and return undef
!ll
00
MC/DC0%0/2
open(DEFAULT_FILE, $default_config_file) || warn("ERROR: cannot open default file.\n") && (return undef)
open(DEFAULT_FILE, $default_config_file) warn "ERROR: cannot open default file.\n"
Truth table0%0/3
A || B
A= open(DEFAULT_FILE, $default_config_file)B= warn "ERROR: cannot open default file.\n"
ABresult
1X1
011
000
237 || warn ("ERROR: cannot open default file.\n") && return undef;
238 0 @default_config = <DEFAULT_FILE>;
239 0 close(DEFAULT_FILE);
240  
241 0 ▶ if ($newdefault eq 'saved') {
Branch: 1 branch
BranchTrueFalseTotal
if ($newdefault eq "saved") 0 0 0
242 0 warn "WARNING: Setting new default to '0'\n";
243 0 $newdefault = 0;
244 }
245  
246 0 $default_config[0] = "$newdefault\n";
247  
248 0 ▶ open(DEFAULT_FILE, ">$default_config_file")
Branch: 1 branch
BranchTrueFalseTotal
open DEFAULT_FILE, ">$default_config_file" or warn "ERROR: cannot open default file.\n" and return undef 0 0 0
Condition0%0/5
open(DEFAULT_FILE, ">$default_config_file") || warn("ERROR: cannot open default file.\n") && (return undef)
l!l&&r!l&&!r
000
warn "ERROR: cannot open default file.\n" and return undef
!ll
00
MC/DC0%0/2
open(DEFAULT_FILE, ">$default_config_file") || warn("ERROR: cannot open default file.\n") && (return undef)
open(DEFAULT_FILE, ">$default_config_file") warn "ERROR: cannot open default file.\n"
Truth table0%0/3
A || B
A= open(DEFAULT_FILE, ">$default_config_file")B= warn "ERROR: cannot open default file.\n"
ABresult
1X1
011
000
249 || warn ("ERROR: cannot open default file.\n") && return undef;
250 0 print DEFAULT_FILE join("",@default_config);
251 0 close(DEFAULT_FILE);
252 0 last;
253 }
254 }
255 2 @{$self->{config}} = @config;
256 }
257  
258  
259 # Add new kernel to config
260  
261 6 ▶sub add {
Subroutine add: called CC=40 SCAR=49.4
Pod documented
262 6 my $self=shift;
263 6 my %param=@_;
264  
265 6 ▶ print ("Adding kernel.\n") if $self->debug()>1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 6 6
266  
267 6 ▶ if (!defined $param{'add-kernel'} || !defined $param{'title'}) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $param{'add-kernel'} && defined $param{'title'}) { } 3 3 6
elsif (not -f "$param{'add-kernel'}") { } 0 3 3
elsif (defined $param{'initrd'} and not -f "$param{'initrd'}") { } 1 4 5
Condition100%3/3
defined $param{'add-kernel'} && defined $param{'title'}
!ll&&!rl&&r
123
MC/DC100%2/2
defined $param{'add-kernel'} && defined $param{'title'}
defined $param{'add-kernel'} defined $param{'title'}
Truth table100%3/3
A && B
A= defined $param{'add-kernel'}B= defined $param{'title'}
ABresult
0X0
100
111
268 3 warn "ERROR: kernel path (--add-kernel), title (--title) required.\n";
269 3 return undef;
270 } elsif (!(-f "$param{'add-kernel'}")) {
271 0 warn "ERROR: kernel $param{'add-kernel'} not found!\n";
272 0 return undef;
273 ▶ } elsif (defined $param{'initrd'} && !(-f "$param{'initrd'}")) {
Condition66%2/3
defined $param{"initrd"} and not -f qq[$param{"initrd"}]
!ll&&!rl&&r
201
MC/DC50%1/2
defined $param{"initrd"} and not -f qq[$param{"initrd"}]
defined $param{"initrd"} not -f qq[$param{"initrd"}]
Truth table66%2/3
A and not(B)
A= defined $param{"initrd"}B= not -f qq[$param{"initrd"}]
ABresult
0X0
100
111
274 1 warn "ERROR: initrd $param{'initrd'} not found!\n";
275 1 return undef;
276 }
277  
278 2 ▶ return undef unless $self->_check_config();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_config 0 2 2
279  
280 2 my @sections=$self->_info();
281  
282 # check if title already exists
283 2 ▶ if (defined $self->_lookup($param{title})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_lookup($param{"title"})) 1 1 2
284 1 warn ("WARNING: Title already exists.\n");
285 1 ▶ if (defined $param{force}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{'force'}) { } 0 1 1
286 0 $self->remove($param{title});
287 } else {
288 1 return undef;
289 }
290 }
291  
292 1 my @config = @{$self->{config}};
293 1 @sections=$self->_info();
294  
295 # Use default kernel to fill in missing info
296 1 my $default=$self->get_default();
297 1 $default++;
298  
299 1 foreach my $p ('args', 'root', 'boot', 'savedefault') {
300 4 ▶ if (! defined $param{$p}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $param{$p}) 1 3 4
301 1 $param{$p} = $sections[$default]{$p};
302 }
303 }
304  
305 # use default entry to determine if path (/boot) should be removed
306 1 ▶ if ($sections[$default]{'kernel'} !~ /^\/boot/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sections[$default]{"kernel"} =~ m[^/boot]) 1 0 1
307 1 $param{'add-kernel'} =~ s/^\/boot//;
308 1 ▶ $param{'initrd'} =~ s/^\/boot// unless !defined $param{'initrd'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"initrd"} 0 1 1
309 }
310  
311 1 my @newkernel;
312 1 ▶ push(@newkernel, "title\t$param{title}\n") if defined $param{title};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"title"} 1 0 1
313 1 ▶ push(@newkernel, "\troot $param{boot}\n") if defined $param{boot};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"boot"} 1 0 1
314  
315 1 my $line;
316 1 ▶ if ( defined $param{xen} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{'xen'}) { } 0 1 1
317 0 $line = "\tkernel $sections[$default]{kernel}";
318 0 ▶ $line .= " $sections[$default]{root}" if defined $sections[$default]{root};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sections[$default]{"root"} 0 0 0
319 0 ▶ $line .= " $sections[$default]{args}" if defined $sections[$default]{args};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sections[$default]{"args"} 0 0 0
320 0 push( @newkernel, "$line\n" );
321 0 ▶ push( @newkernel, "\tinitrd $sections[$default]{'initrd'}\n" ) if defined $sections[$default]{'initrd'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sections[$default]{"initrd"} 0 0 0
322 0 ▶ $line = "\tmodule $param{'add-kernel'}" if defined $param{'add-kernel'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"add-kernel"} 0 0 0
323 0 ▶ $line .= " root=$param{root}" if defined $param{root};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"root"} 0 0 0
324 0 ▶ $line .= " $param{args}" if defined $param{args};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"args"} 0 0 0
325 0 push( @newkernel, "$line\n" );
326 0 ▶ push( @newkernel, "\tmodule $param{initrd}\n" ) if defined $param{initrd};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"initrd"} 0 0 0
327 } else {
328 1 ▶ $line = "\tkernel $param{'add-kernel'}" if defined $param{'add-kernel'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"add-kernel"} 1 0 1
329 1 ▶ $line .= " root=$param{root}" if defined $param{root};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"root"} 1 0 1
330 1 ▶ $line .= " $param{args}" if defined $param{args};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"args"} 1 0 1
331 1 push( @newkernel, "$line\n" );
332 1 ▶ push( @newkernel, "\tinitrd $param{initrd}\n" ) if defined $param{initrd};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"initrd"} 0 1 1
333 }
334  
335 1 ▶ push(@newkernel, "\tsavedefault $param{savedefault}\n") if defined $param{savedefault};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{"savedefault"} 0 1 1
336  
337 1 foreach my $module (@{$param{'module'}}) {
338 0 push(@newkernel, "\tmodule " . $module . "\n");
339 }
340  
341 1 push(@newkernel, "\n");
342  
343 1 ▶ if (!defined $param{position} || $param{position} !~ /end|\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $param{"position"} and $param{"position"} =~ /end|\d+/) 1 0 1
Condition33%1/3
defined $param{"position"} and $param{"position"} =~ /end|\d+/
!ll&&!rl&&r
100
MC/DC0%0/2
defined $param{"position"} and $param{"position"} =~ /end|\d+/
defined $param{"position"} $param{"position"} =~ /end|\d+/
Truth table33%1/3
A and B
A= defined $param{"position"}B= $param{"position"} =~ /end|\d+/
ABresult
0X0
100
111
344 1 $param{position}=0
345 }
346  
347 1 my @newconfig;
348 1 ▶ if ($param{position}=~/end/ || $param{position} >= $#sections) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{'position'} =~ /end/ or $param{'position'} >= $#sections) { } 0 1 1
Condition33%1/3
$param{"position"} =~ /end/ or $param{"position"} >= $#sections
l!l&&r!l&&!r
001
MC/DC0%0/2
$param{"position"} =~ /end/ or $param{"position"} >= $#sections
$param{"position"} =~ /end/ $param{"position"} >= $#sections
Truth table33%1/3
A or B
A= $param{"position"} =~ /end/B= $param{"position"} >= $#sections
ABresult
1X1
011
000
349 0 $param{position}=$#sections;
350 0 push (@newconfig,@config);
351 0 ▶ if ($newconfig[$#newconfig] =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newconfig[$#newconfig] =~ /\S/) 0 0 0
352 0 push (@newconfig, "\n");
353 }
354 0 push (@newconfig,@newkernel);
355 } else {
356 1 my $index=0;
357 1 foreach (@config) {
358 19 ▶ if ($_ =~ /^\s*title/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^\s*title/i) 3 16 19
359 3 ▶ if ($index==$param{position}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == $param{"position"}) 1 2 3
360 1 push (@newconfig, @newkernel);
361 }
362 3 $index++;
363 }
364 19 push (@newconfig, $_);
365 }
366 }
367  
368 1 @{$self->{config}} = @newconfig;
369  
370 1 ▶ if (defined $param{'make-default'} || defined $param{'boot-once'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"make-default"} or defined $param{"boot-once"}) 0 1 1
Condition33%1/3
defined $param{"make-default"} or defined $param{"boot-once"}
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $param{"make-default"} or defined $param{"boot-once"}
defined $param{"make-default"} defined $param{"boot-once"}
Truth table33%1/3
A or B
A= defined $param{"make-default"}B= defined $param{"boot-once"}
ABresult
1X1
011
000
371 0 $self->set_default($param{position});
372 }
373 1 print "Added: $param{'title'}.\n";
374 }
375  
376  
377 # Update kernel args
378  
379 0 ▶sub update {
Subroutine update: not called CC=30 SCAR=68.4
Pod documented
380 0 my $self=shift;
381 0 my %params=@_;
382  
383 0 ▶ print ("Updating kernel.\n") if $self->debug()>1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 0 0
384  
385 0 ▶ if (defined $params{'option'} && !defined $params{'update-kernel'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $params{'option'} and not defined $params{'update-kernel'}) { } 0 0 0
elsif (not defined $params{'update-kernel'} && (defined $params{'args'} || defined $params{'remove-args'} || defined $params{'option'})) { } 0 0 0
Condition0%0/3
defined $params{"option"} and not defined $params{"update-kernel"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params{"option"} and not defined $params{"update-kernel"}
defined $params{"option"} not defined $params{"update-kernel"}
Truth table0%0/3
A and not(B)
A= defined $params{"option"}B= not defined $params{"update-kernel"}
ABresult
0X0
100
111
386 0 return $self->update_main_options(%params);
387 ▶ } elsif (!defined $params{'update-kernel'} || (!defined $params{'args'} && !defined $params{'remove-args'} && !defined $params{'option'})) {
Condition0%0/9
defined $params{'update-kernel'} && (defined $params{'args'} || defined $params{'remove-args'} || defined $params{'option'})
!ll&&!rl&&r
000
defined $params{"args"} or defined $params{"remove-args"} or defined $params{"option"}
l!l&&r!l&&!r
000
defined $params{"args"} or defined $params{"remove-args"}
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $params{'update-kernel'} && (defined $params{'args'} || defined $params{'remove-args'} || defined $params{'option'})
defined $params{'update-kernel'} defined $params{"args"} defined $params{"remove-args"} defined $params{"option"}
Truth table0%0/5
A && B or C or D
A= defined $params{'update-kernel'}B= defined $params{"args"}C= defined $params{"remove-args"}D= defined $params{"option"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
388 0 warn "ERROR: kernel position or title (--update-kernel) and args (--args or --remove-args) required.\n";
389 0 return undef;
390 }
391  
392 0 ▶ return undef unless $self->_check_config();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_config 0 0 0
393  
394 # my @config = @{$self->{config}};
395 0 my @sections=$self->_info();
396  
397 # if not a number, do title lookup
398 0 ▶ if (defined $params{'update-kernel'} and $params{'update-kernel'} !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"update-kernel"} and not $params{"update-kernel"} =~ /^\d+$/) 0 0 0
Condition0%0/3
defined $params{"update-kernel"} and not $params{"update-kernel"} =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params{"update-kernel"} and not $params{"update-kernel"} =~ /^\d+$/
defined $params{"update-kernel"} not $params{"update-kernel"} =~ /^\d+$/
Truth table0%0/3
A and not(B)
A= defined $params{"update-kernel"}B= not $params{"update-kernel"} =~ /^\d+$/
ABresult
0X0
100
111
399 0 $params{'update-kernel'} = $self->_lookup($params{'update-kernel'});
400 }
401  
402 0 my $kcount = $#sections-1;
403 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
404 0 warn "ERROR: Enter a default between 0 and $kcount.\n";
405 0 return undef;
406 }
407  
408 0 my $kregex = '(^\s*kernel\s+\S+)(.*)';
409 0 ▶ $kregex = '(^\s*module\s+\S+vmlinuz\S+)(.*)' if defined $params{'xen'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"xen"} 0 0 0
410  
411 0 my $index=-1;
412 0 my $config_line = -1;
413 0 my $line = '';
414 0 foreach $line (@{$self->{config}}) {
415 0 $config_line = $config_line + 1;
416 0 ▶ if ($line =~ /^\s*title/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*title/i) 0 0 0
417 0 $index++;
418 }
419 0 ▶ if ($index==$params{'update-kernel'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($index == $params{'update-kernel'}) { } 0 0 0
elsif ($index > $params{'update-kernel'}) { } 0 0 0
420 0 ▶ if (defined $params{'args'} or defined $params{'remove-args'}){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $params{'args'} or defined $params{'remove-args'}) { } 0 0 0
elsif (defined $params{'option'}) { } 0 0 0
Condition0%0/3
defined $params{"args"} or defined $params{"remove-args"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $params{"args"} or defined $params{"remove-args"}
defined $params{"args"} defined $params{"remove-args"}
Truth table0%0/3
A or B
A= defined $params{"args"}B= defined $params{"remove-args"}
ABresult
1X1
011
000
421 0 ▶ if ( $line =~ /$kregex/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$kregex/i) 0 0 0
422 0 my $kernel = $1;
423 0 my $args = $2;
424 0 ▶ $args =~ s/\s+$params{'remove-args'}(\=\S+|\s+|$)/ /ig if defined $params{'remove-args'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"remove-args"} 0 0 0
425 0 ▶ if ( defined $params{'args'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"args"}) 0 0 0
426 0 my $base_arg = $params{'args'};
427 0 $base_arg =~ s/\=.*//;
428 0 $args =~ s/\s+$base_arg(\=\S+|\s+|$)/ /ig;
429 0 $args = $args . " " . $params{'args'};
430 }
431 0 ▶ if ($line eq $kernel . $args . "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq $kernel . $args . "\n") { } 0 0 0
432 0 warn "WARNING: No change made to args.\n";
433 0 return undef;
434 } else {
435 0 $line = $kernel . $args . "\n";
436 }
437 0 next;
438 }
439 } elsif (defined $params{'option'}){
440 0 foreach my $val ( keys %params){
441 0 ▶ if ($line =~ m/^\s*$val.*/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*$val.*/i) 0 0 0
442 0 splice @{$self->{config}},$config_line,1,"$val $params{$val}\n";
443 0 delete $params{$val};
444 0 $config_line += 1;
445 }
446 }
447 }
448 } elsif ($index > $params{'update-kernel'}){
449 0 last;
450 }
451 }
452 # Add any leftover parameters
453 0 delete $params{'update-kernel'};
454 0 ▶ if (defined $params{'option'}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"option"}) 0 0 0
455 0 delete $params{'option'};
456 0 $config_line -= 1;
457 0 foreach my $val ( keys %params){
458 0 splice @{$self->{config}},$config_line,0,"$val $params{$val}\n";
459 0 $config_line += 1;
460 }
461 }
462 }
463  
464  
465 # Run command to install bootloader
466  
467 0 ▶sub install {
Subroutine install: not called CC=1 SCAR=6.9
Pod documented
468 0 my $self=shift;
469 0 my $device;
470  
471 0 warn "Re-installing grub is currently unsupported.\n";
472 0 warn "If you really need to re-install grub, use 'grub-install <device>'.\n";
473 0 return undef;
474  
475 #system("grub-install $device");
476 #if ($? != 0) {
477 # warn ("ERROR: Failed to run grub-install.\n") && return undef;
478 #}
479 #return 1;
480 }
481  
482  
483 0 ▶sub update_main_options{
Subroutine update_main_options: not called CC=5 SCAR=34.0
Pod documented
484 0 my $self=shift;
485 0 my %params=@_;
486 0 delete $params{'option'};
487 0 foreach my $val (keys %params){
488 0 my $x=0;
489 0 foreach my $line ( @{$self->{config}} ) {
490 # Replace
491 0 ▶ if ($line =~ m/^\s*$val/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*$val/) 0 0 0
492 0 splice (@{$self->{config}},$x,1,"$val $params{$val}\n");
493 0 last;
494 }
495 # Add
496 0 ▶ if ($line =~ /^\s*title/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*title/i) 0 0 0
497 # This is a new option, add it before here
498 0 print "Your option is not in current configuration. Adding.\n";
499 0 splice @{$self->{config}},$x,0,"$val $params{$val}\n";
500 0 last;
501 }
502 0 $x+=1;
503 }
504 }
505 }
506  
507  
508 0 ▶sub boot_once {
Subroutine boot_once: not called CC=5 SCAR=34.0
Pod documented
509 0 my $self=shift;
510 0 my $entry_to_boot_once = shift;
511  
512 0 ▶ unless ( $entry_to_boot_once ) { print "No kernel\n"; return undef;}
Branch: 1 branch
BranchTrueFalseTotal
unless ($entry_to_boot_once) 0 0 0
513 0 $self->read();
514 0 my $default=$self->get_default();
515  
516 0 ▶ if ( $default == $self->_lookup($entry_to_boot_once)){
Branch: 1 branch
BranchTrueFalseTotal
if ($default == $self->_lookup($entry_to_boot_once)) 0 0 0
517 0 warn "The default and once-boot kernels are the same. No action taken. \nSet default to something else, then re-try.\n";
518 0 return undef;
519 }
520 0 ▶ if ( $self->_get_bootloader_version() < 0.97 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_bootloader_version < "0.97") 0 0 0
521 0 warn "This function works for grub version 0.97 and up. No action taken. \nUpgrade, then re-try.\n";
522 0 return undef;
523 }
524  
525 0 $self->set_default('saved');
526 0 ▶ if ( ! -f '/boot/grub/default' ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-f "/boot/grub/default") 0 0 0
527 0 open FH, '>/boot/grub/default';
528 0 my $file_contents="default
529 #
530 #
531 #
532 #
533 #
534 #
535 #
536 #
537 #
538 #
539 # WARNING: If you want to edit this file directly, do not remove any line
540 # from this file, including this warning. Using `grub-set-default\' is
541 # strongly recommended.
542 ";
543 0 print FH $file_contents;
544 0 close FH;
545 }
546 0 $self->set_default( "$entry_to_boot_once" );
547 0 $self->update( 'option'=>'','fallback' => $default );
548 0 $self->update( 'update-kernel'=>"$entry_to_boot_once",'option'=>'','savedefault' => 'fallback' );
549 0 $self->update( 'update-kernel'=>"$default",'option'=>'', 'savedefault' => '' );
550 0 $self->write();
551
552 }
553  
554 0 ▶sub _get_bootloader_version {
Subroutine _get_bootloader_version: not called CC=1 SCAR=6.9
555 0 my $self = shift;
556 0 return `grub --version | sed 's/grub (GNU GRUB //' | sed 's/)//'`;
557 }
558  
559  
560 1 1;
561  
562  
563 =head1 AUTHOR
564  
565 Open Source Development Labs, Engineering Department <eng@osdl.org>
566  
567 =head1 COPYRIGHT
568  
569 Copyright (C) 2006 Open Source Development Labs
570 All Rights Reserved.
571  
572 This script is free software; you can redistribute it and/or modify it
573 under the same terms as Perl itself.
574  
575 =head1 SEE ALSO
576  
577 L<Linux::Bootloader>
578  
579 =cut
580  
↑ Summary Bootloader.pm »