| 1 | package Linux::Bootloader::Detect; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Linux::Bootloader::Detect - detects the bootloader and architecture of the system. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Attempts to determine the bootloader by checking for configuration files | ||||||||||||||||||||||||||||
| 10 | for grub, lilo, elilo and yaboot then searching the master boot record | ||||||||||||||||||||||||||||
| 11 | for GRUB, LILO, ELILO and YABOOT. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Determines the architecture by running uname -m. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | To attempt to discover the bootloader being used by the system | ||||||||||||||||||||||||||||
| 18 | detect_bootloader first calls detect_bootloader_from_conf attempts to locate | ||||||||||||||||||||||||||||
| 19 | /boot/grub/menu.lst, /etc/lilo.conf, /boot/efi/elilo.conf and | ||||||||||||||||||||||||||||
| 20 | /etc/yaboot.conf and returns the corresponding bootloader name. If | ||||||||||||||||||||||||||||
| 21 | either undef of multiple are returned because no configuration files or | ||||||||||||||||||||||||||||
| 22 | multiple configuration files were found detect_bootloader calls | ||||||||||||||||||||||||||||
| 23 | detect_bootloader_from_mbr which generates a list of all devices accessable from | ||||||||||||||||||||||||||||
| 24 | the /dev directory reading in the first 512 bytes from each hd and sd | ||||||||||||||||||||||||||||
| 25 | device using head then redirects the output to grep to determine if | ||||||||||||||||||||||||||||
| 26 | "GRUB", "LILO", "ELILO" or "YABOOT" is present returning the | ||||||||||||||||||||||||||||
| 27 | corresponding value if exactly one mbr on the system contained a | ||||||||||||||||||||||||||||
| 28 | bootloader or multiple if more than one was found and undef if none were | ||||||||||||||||||||||||||||
| 29 | found. detect_bootloader returns either grub, lilo, elilo, yaboot or | ||||||||||||||||||||||||||||
| 30 | undef. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | To attempt to discover the architecture of the system | ||||||||||||||||||||||||||||
| 33 | detect_architecture makes a uname -m system call returning x86, ppc, | ||||||||||||||||||||||||||||
| 34 | ia64 or undef. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 3 | our $VERSION = '1.2'; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head3 detect_architecture([style]) | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Input: | ||||||||||||||||||||||||||||
| 49 | Output: string | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | This function determines the architecture by calling uname -m. By | ||||||||||||||||||||||||||||
| 52 | default it will report back exactly what uname -m reports, but if you | ||||||||||||||||||||||||||||
| 53 | specify a "style", detect_architecture will do some mappings. Possible | ||||||||||||||||||||||||||||
| 54 | styles include: | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Style Example return values (not an exhaustive list...) | ||||||||||||||||||||||||||||
| 57 | [none] i386, i686, sparc, sun4u, ppc64, s390x, x86_64, parisc64 | ||||||||||||||||||||||||||||
| 58 | linux i386, i386, sparc, sparc, ppc64, s390, x86_64, parisc | ||||||||||||||||||||||||||||
| 59 | gentoo x86, x86, sparc, sparc, ppc64, amd64, hppa | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Returns undef on error. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 4 | ▶ | sub detect_architecture { | ||||||||||||||||||||||||||
|
Subroutine
detect_architecture: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | my $arch_style = shift || 'uname'; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 'uname'
MC/DC100%1/1
shift() || 'uname'
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 4 | my $arch; | |||||||||||||||||||||||||||
| 69 | 4 | ▶ | if ($arch_style eq 'linux') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 70 | 1 | $arch = `uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/ -e s/s390x/s390/ -e s/parisc64/parisc/`; | |||||||||||||||||||||||||||
| 71 | 1 | chomp $arch; | |||||||||||||||||||||||||||
| 72 | } elsif ($arch_style eq 'gentoo') { | ||||||||||||||||||||||||||||
| 73 | 1 | $arch = `uname -m | sed -e s/i.86/x86/ -e s/sun4u/sparc/ -e s/arm.*/arm/ -e s/sa110/arm/ -e s/x86_64/amd64/ -e s/sparc.*/sparc/ -e s/parisc.*/hppa/`; | |||||||||||||||||||||||||||
| 74 | 1 | chomp $arch; | |||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||
| 76 | 2 | $arch = `uname -m`; | |||||||||||||||||||||||||||
| 77 | 2 | chomp $arch; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 4 | return $arch; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head3 detect_bootloader(['device1', 'device2', ...]) | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Input: devices to detect against (optional) | ||||||||||||||||||||||||||||
| 85 | Output: string | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | This function attempts to determine the bootloader being used on the | ||||||||||||||||||||||||||||
| 88 | system by first checking for conf files and then falling back to check | ||||||||||||||||||||||||||||
| 89 | the master boot record. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Possible return values: | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | grub grub was determined to be the bootloader in use | ||||||||||||||||||||||||||||
| 94 | lilo lilo was determined to be is the bootloader in use | ||||||||||||||||||||||||||||
| 95 | elilo elilo was determined to be the bootloader in use | ||||||||||||||||||||||||||||
| 96 | yaboot yaboot was determined to be the bootloader in use | ||||||||||||||||||||||||||||
| 97 | undef it was impossible to determine which bootloader was being used | ||||||||||||||||||||||||||||
| 98 | due either to configuration files for multiple bootloaders or | ||||||||||||||||||||||||||||
| 99 | bootloader on multiple hard disks | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | ▶ | sub detect_bootloader { | ||||||||||||||||||||||||||
|
Subroutine
detect_bootloader: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 1 | ▶ | return detect_bootloader_from_conf(@_) | ||||||||||||||||||||||||||
|
Condition33%1/3
detect_bootloader_from_conf(@_) || detect_bootloader_from_mbr(@_)
MC/DC0%0/2
detect_bootloader_from_conf(@_) || detect_bootloader_from_mbr(@_)
detect_bootloader_from_conf(@_)
detect_bootloader_from_mbr(@_)
Truth table33%1/3
A || B
A= detect_bootloader_from_conf(@_)B= detect_bootloader_from_mbr(@_)
| |||||||||||||||||||||||||||||
| 105 | || detect_bootloader_from_mbr(@_); | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 detect_bootloader_from_conf() | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Detects bootloaders by the presence of config files. This is not as | ||||||||||||||||||||||||||||
| 111 | reliable of a mechanism as looking in the MBR, but tends to be | ||||||||||||||||||||||||||||
| 112 | significantly faster. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | If called in list context, it will return a list of the bootloaders that | ||||||||||||||||||||||||||||
| 115 | it found. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | If called in scalar context and only a single bootloader config file is | ||||||||||||||||||||||||||||
| 118 | present it will return the name of that bootloader. Otherwise, if | ||||||||||||||||||||||||||||
| 119 | multiple (or no) bootloaders are detected, it will return undef. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | ▶ | sub detect_bootloader_from_conf { | ||||||||||||||||||||||||||
|
Subroutine
detect_bootloader_from_conf: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 2 | my @boot_loader = (); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 2 | my %boot_list = ( grub => '/boot/grub/menu.lst', | |||||||||||||||||||||||||||
| 127 | lilo => '/etc/lilo.conf', | ||||||||||||||||||||||||||||
| 128 | elilo => '/etc/elilo.conf', | ||||||||||||||||||||||||||||
| 129 | yaboot => '/etc/yaboot.conf' | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 2 | foreach my $key ( sort keys %boot_list ) { | |||||||||||||||||||||||||||
| 133 | 8 | ▶ | if ( -f $boot_list{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | push ( @boot_loader, $key ); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 2 | ▶ | if (wantarray()) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 139 | 1 | return @boot_loader; | |||||||||||||||||||||||||||
| 140 | } elsif (@boot_loader == 1) { | ||||||||||||||||||||||||||||
| 141 | 0 | return pop( @boot_loader ); | |||||||||||||||||||||||||||
| 142 | } else { | ||||||||||||||||||||||||||||
| 143 | 1 | return undef; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 detect_bootloader_from_mbr([@devices]) | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Detects the bootloader by scanning the master boot record (MBR) of the | ||||||||||||||||||||||||||||
| 150 | specified devices (or all devices if not indicated). | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | The device arguments must be relative to the /dev/ directory. I.e., | ||||||||||||||||||||||||||||
| 153 | ('hda', 'sdb', 'cdroms/cdrom0', etc.) | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 2 | ▶ | sub detect_bootloader_from_mbr { | ||||||||||||||||||||||||||
|
Subroutine
detect_bootloader_from_mbr: called CC=12 SCAR=38.5
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 2 | my @filelist = @_; | |||||||||||||||||||||||||||
| 159 | 2 | my @boot_loader = (); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 2 | my %map = ( | |||||||||||||||||||||||||||
| 162 | "GRUB" => 'grub', | ||||||||||||||||||||||||||||
| 163 | "LILO" => 'lilo', | ||||||||||||||||||||||||||||
| 164 | "EFI" => 'elilo', | ||||||||||||||||||||||||||||
| 165 | "yaboot" => 'yaboot', | ||||||||||||||||||||||||||||
| 166 | ); | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 2 | ▶ | if ( ! @filelist && opendir( DIRH, "/sys/block" ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not @filelist and opendir DIRH, "/sys/block"
MC/DC0%0/2
not @filelist and opendir DIRH, "/sys/block"
not @filelist
opendir DIRH, "/sys/block"
Truth table33%1/3
not(A) and B
A= not @filelistB= opendir DIRH, "/sys/block"
| |||||||||||||||||||||||||||||
| 169 | 2 | @filelist = grep { /^[sh]d.$/ } readdir(DIRH); | |||||||||||||||||||||||||||
| 170 | 2 | closedir(DIRH); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 2 | foreach ( @filelist ) { | |||||||||||||||||||||||||||
| 174 | 0 | ▶ | if ( -b "/dev/$_" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 0 | my $strings = `dd if=/dev/$_ bs=512 count=1 2>/dev/null | strings`; | |||||||||||||||||||||||||||
| 176 | 0 | foreach my $loader (keys %map) { | |||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ($strings =~ /$loader/ms) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | push @boot_loader, $map{$loader}; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 2 | ▶ | if (wantarray()) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 185 | # Show them all | ||||||||||||||||||||||||||||
| 186 | 0 | return @boot_loader; | |||||||||||||||||||||||||||
| 187 | } elsif (@boot_loader == 1) { | ||||||||||||||||||||||||||||
| 188 | # Found exactly one | ||||||||||||||||||||||||||||
| 189 | 0 | return pop @boot_loader; | |||||||||||||||||||||||||||
| 190 | } elsif (@boot_loader == 2) { | ||||||||||||||||||||||||||||
| 191 | # This is the Lilo/Grub exception | ||||||||||||||||||||||||||||
| 192 | # Grub on MBR with previous Lilo install | ||||||||||||||||||||||||||||
| 193 | # Are they lilo and grub in that order? | ||||||||||||||||||||||||||||
| 194 | 0 | ▶ | if ($boot_loader[0] eq 'lilo' and $boot_loader[1] eq 'grub'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$boot_loader[0] eq "lilo" and $boot_loader[1] eq "grub"
MC/DC0%0/2
$boot_loader[0] eq "lilo" and $boot_loader[1] eq "grub"
$boot_loader[0] eq "lilo"
$boot_loader[1] eq "grub"
Truth table0%0/3
A and B
A= $boot_loader[0] eq "lilo"B= $boot_loader[1] eq "grub"
| |||||||||||||||||||||||||||||
| 195 | 0 | warn "Warning: Grub appears to be used currently, but Lilo was in pasti.\n"; | |||||||||||||||||||||||||||
| 196 | 0 | return $boot_loader[1]; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # Either none or too many to choose from | ||||||||||||||||||||||||||||
| 201 | 2 | return undef; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 3 | 1; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Open Source Development Labs, Engineering Department <eng@osdl.org> | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Copyright (C) 2006 Open Source Development Labs | ||||||||||||||||||||||||||||
| 213 | All Rights Reserved. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | This script is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 216 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | L<Linux::Bootloader> | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =cut | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||