File Coverage

blib/lib/KIF/Bootloader/grub.pm
Criterion Covered Total %
statement 24 72 33.3
branch 0 12 0.0
condition n/a
subroutine 8 12 66.6
pod 0 4 0.0
total 32 100 32.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             #
3             # Revision History:
4             #
5             # 26-Nov-2002 Dick Munroe (munroe@csworks.com)
6             # Initial Version Created.
7             #
8             # 18-May-2003 Dick Munroe (munroe@csworks.com)
9             # Make sure package variables can't leak.
10             #
11             # 19-May-2003 Dick Munroe (munroe@csworks.com)
12             # Use Carp.
13             # Isolate KIF related classes in a KIF namespace.
14             #
15              
16             package KIF::Bootloader::grub ;
17              
18 1     1   716 use vars qw($VERSION @ISA) ;
  1         2  
  1         72  
19              
20             our $VERSION = "1.03" ;
21              
22 1     1   13 use strict ;
  1         2  
  1         35  
23              
24 1     1   4 use Carp ;
  1         2  
  1         71  
25 1     1   6 use File::Basename ;
  1         2  
  1         101  
26 1     1   5 use File::Copy ;
  1         1  
  1         34  
27 1     1   4 use FileHandle ;
  1         2  
  1         10  
28 1     1   300 use KIF::Bootloader ;
  1         1  
  1         20  
29 1     1   759 use StanzaFile::Grub ;
  1         5901  
  1         878  
30              
31             our @ISA = qw(KIF::Bootloader) ;
32              
33             sub new
34             {
35 0     0 0   my $thePackage = shift ;
36              
37 0           my $theObject = $thePackage->SUPER::new() ;
38              
39 0 0         if (-e "/boot/grub/grub.conf")
40             {
41 0           $theObject->filename("/boot/grub/grub.conf") ;
42             }
43             else
44             {
45 0           croak "No grub.conf file found" ;
46             } ;
47              
48 0           return $theObject ;
49             } ;
50              
51             sub initrdFile
52             {
53 0     0 0   my $theObject = shift ;
54            
55 0           my $theGrubFile = new StanzaFile::Grub(file_name=>$theObject->filename()) ;
56              
57 0           return basename($theGrubFile->stanza(($theGrubFile->order())[$theGrubFile->header()->item('default')])->item('initrd')) ;
58             } ;
59              
60             sub modify
61             {
62 0     0 0   my $theObject = shift ;
63 0           my $theBuildObject = shift ;
64              
65             #
66             # Modify the grub.conf file only if we're not just rebuilding the
67             # current default kernel.
68             #
69              
70 0           my $theReleaseTag = $theBuildObject->releaseTag() ;
71              
72 0           my $theCurrentKernel = readlink "/boot/vmlinuz" ;
73              
74 0           $theCurrentKernel =~ s,.*/,, ;
75 0           $theCurrentKernel =~ s,.*?-,, ;
76              
77 0 0         return if ($theCurrentKernel eq $theReleaseTag);
78              
79 0           my $theGrubFile = new StanzaFile::Grub(file_name=>$theObject->filename()) ;
80              
81 0           foreach ($theGrubFile->order())
82             {
83             #
84             # The current kernel is already in the grub.conf file, so
85             # we can safely not modify the file.
86             #
87             # FIX ME Should we point the default to this entry?
88             #
89              
90 0 0         return if ($theGrubFile->stanza($_)->item('kernel') =~ m,-$theReleaseTag( |$),) ;
91             }
92              
93             #
94             # At this point we know we have to modify the grub.conf file
95             #
96              
97 0           my $theIndex = $theGrubFile->header()->item('default') ;
98 0           my $theNewStanza = ($theGrubFile->stanza(($theGrubFile->order())[$theIndex]))->new() ;
99              
100             #
101             # New Stanza is a clone of the default entry, modify it so that
102             # everything will point to the new kernel.
103             #
104              
105 0           my $theCurrentRelease = $theNewStanza->item('kernel') ;
106 0           $theCurrentRelease =~ s/.*?vmlinuz-(.*?)( |$).*/$1/ ;
107              
108 0           foreach ($theNewStanza->order())
109             {
110 0           my $theString ;
111              
112 0           ($theString = $theNewStanza->item($_)) =~ s/$theCurrentRelease/$theReleaseTag/ ;
113              
114 0           $theNewStanza->item($_, $theString) ;
115              
116             } ;
117              
118 0           $theNewStanza->name("KIF ($theReleaseTag)");
119              
120 0           $theGrubFile->add($theNewStanza) ;
121              
122 0           $theGrubFile->header()->item('default', scalar($theGrubFile->order())-1) ;
123              
124 0 0         if (!$theBuildObject->testFlag())
125             {
126 0           move($theObject->filename(), $theObject->filename() . ".old") ;
127              
128 0           $theBuildObject->_print("Moving " . $theObject->filename() . " => " . $theObject->filename() . ".old\n",1) ;
129              
130 0           my $theString = $theGrubFile->write(file_name=>$theObject->filename()) ;
131              
132 0           $theBuildObject->_print($theString, 3) ;
133             }
134             else
135             {
136 0           $theBuildObject->_print($theGrubFile->asString(), 3) ;
137             } ;
138             } ;
139              
140             sub validate
141             {
142             #
143             # A valid grub.conf has a default entry and a valid stanza
144             # at that entry.
145             #
146              
147 0     0 0   my $theObject = shift ;
148              
149 0           my $theBuildObject = shift ;
150              
151 0           $theObject->SUPER::validate($theBuildObject) ;
152              
153 0           my $theFileName = $theObject->filename() ;
154              
155 0           my $theGrubFile = new StanzaFile::Grub(file_name=>$theFileName) ;
156 0           my $theIndex ;
157              
158 0 0         if (!(defined($theIndex = $theGrubFile->header()->item('default'))))
159             {
160 0           print STDERR <<"EOT" ;
161              
162             $theFileName must be in a standard form for kif to work properly.
163             There must be a default entry in the header portion of the file
164             of the form:
165              
166             default=n
167              
168             where n is the index to the default grub configuration to use
169             when booting. Remember that n starts with 0 and the first entry
170             is number 0.
171              
172             Please add an appropriate line to $theFileName and rerun kif.
173             EOT
174 0           croak "$theFileName is invalid." ;
175             } ;
176              
177             #
178             # Now make sure that the particular configuration is actually
179             # in the configuration file.
180             #
181              
182 0 0         if (!(defined($theGrubFile->stanza(($theGrubFile->order())[$theIndex]))))
183             {
184 0           print STDERR << "EOT" ;
185             The stanza indicated by the default header item in $theFileName does
186             not exist. You must correct the default value and rerun kif.
187             EOT
188 0           croak "Invalid default configuration header in $theFileName" ;
189             } ;
190             } ;
191              
192             1;