| 1 | #!/usr/bin/perl -w |
|---|
| 2 | # |
|---|
| 3 | # $Id:$ |
|---|
| 4 | |
|---|
| 5 | =head1 AUTHOR |
|---|
| 6 | |
|---|
| 7 | Thomas Lundquist <thomasez@zelow.no> |
|---|
| 8 | |
|---|
| 9 | =head1 NAME |
|---|
| 10 | |
|---|
| 11 | modpick.pl |
|---|
| 12 | |
|---|
| 13 | =head1 SYNOPSIS |
|---|
| 14 | |
|---|
| 15 | modpick.pl [options] -m <Module(s)> |
|---|
| 16 | |
|---|
| 17 | =head1 DESCRIPTION |
|---|
| 18 | |
|---|
| 19 | Gives the right option it will return a list with the required modules. |
|---|
| 20 | |
|---|
| 21 | And no, this script ain't made for efficiency and I've used a script |
|---|
| 22 | I made a few years ago as the base, it may not be perfect.. |
|---|
| 23 | |
|---|
| 24 | =head1 OPTIONS |
|---|
| 25 | |
|---|
| 26 | =over |
|---|
| 27 | |
|---|
| 28 | =item -m, --module <STRING> REQUIRED |
|---|
| 29 | |
|---|
| 30 | A module name or list of modules. |
|---|
| 31 | |
|---|
| 32 | with options in () after the module. |
|---|
| 33 | |
|---|
| 34 | =item -k, --kernelversion <STRING> |
|---|
| 35 | |
|---|
| 36 | The kernel version. |
|---|
| 37 | |
|---|
| 38 | (This can also be set in modpick.conf) |
|---|
| 39 | |
|---|
| 40 | =item -r, --moduleroot <STRING> |
|---|
| 41 | |
|---|
| 42 | The root of the module tree. |
|---|
| 43 | |
|---|
| 44 | (This can also be set in modpick.conf) |
|---|
| 45 | |
|---|
| 46 | =item -c, --copyto <STRING> |
|---|
| 47 | |
|---|
| 48 | The base directory for copying the modules to. |
|---|
| 49 | |
|---|
| 50 | (This can also be set in modpick.conf) |
|---|
| 51 | |
|---|
| 52 | =item -d --deplist |
|---|
| 53 | |
|---|
| 54 | Prints a list of modules, with path relative to the moduleroot, needed |
|---|
| 55 | for the specified module. |
|---|
| 56 | |
|---|
| 57 | =item -h --help |
|---|
| 58 | |
|---|
| 59 | Prints short help message about usage. |
|---|
| 60 | |
|---|
| 61 | =item --debug |
|---|
| 62 | |
|---|
| 63 | Prints debug stuff. |
|---|
| 64 | |
|---|
| 65 | =back |
|---|
| 66 | |
|---|
| 67 | =cut |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | use strict; |
|---|
| 71 | use Getopt::Long qw(:config gnu_getopt); #); |
|---|
| 72 | use Pod::Usage; |
|---|
| 73 | # use Data::Dumper; |
|---|
| 74 | use File::Basename; |
|---|
| 75 | use Text::ParseWords; |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | use vars qw($help $moduleroot $kernelversion $modules $copyto $deplist |
|---|
| 79 | $debug $help); |
|---|
| 80 | |
|---|
| 81 | if ( -f "./modpick.conf" ) { require ("./modpick.conf"); } |
|---|
| 82 | |
|---|
| 83 | my %opts = ("help|h" => \$help, |
|---|
| 84 | "kernelversion|k=s" => \$kernelversion, |
|---|
| 85 | "moduleroot|r=s" => \$moduleroot, |
|---|
| 86 | "module|m=s" => \$modules, |
|---|
| 87 | "copyto|c=s" => \$copyto, |
|---|
| 88 | "deplist|d" => \$deplist, |
|---|
| 89 | "debug" => \$debug, |
|---|
| 90 | ); |
|---|
| 91 | |
|---|
| 92 | GetOptions(%opts); |
|---|
| 93 | |
|---|
| 94 | if ($help) { |
|---|
| 95 | pod2usage(1); |
|---|
| 96 | exit(0); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | unless ($kernelversion && $moduleroot && $modules) { |
|---|
| 100 | pod2usage(1); |
|---|
| 101 | exit(1); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | my $foo; |
|---|
| 105 | my ($modext) = ".o"; |
|---|
| 106 | $kernelversion =~ /^2\.4/ ? $foo = ".ko" : $foo = ".o"; |
|---|
| 107 | |
|---|
| 108 | # $debug = 1; |
|---|
| 109 | |
|---|
| 110 | my ($allmodules, $p) = &readModulesDep(); |
|---|
| 111 | |
|---|
| 112 | # print STDERR Dumper($allmodules) if ($debug); |
|---|
| 113 | |
|---|
| 114 | my (@deplist); |
|---|
| 115 | my (%optlist); |
|---|
| 116 | my ($last_module); |
|---|
| 117 | |
|---|
| 118 | foreach my $module ( quotewords('\s+', 0, $modules) ) { |
|---|
| 119 | |
|---|
| 120 | print STDERR "this: ($module)\n" if ($debug); |
|---|
| 121 | |
|---|
| 122 | # Ok, first we have to check for options. |
|---|
| 123 | # If there are only one option inside we'll both start and end |
|---|
| 124 | # here. |
|---|
| 125 | if ( $module =~ /(.*)\)/ && $module !~ /\(/ ) { |
|---|
| 126 | |
|---|
| 127 | $optlist{$last_module} .= ($1 . " "); |
|---|
| 128 | # So, get rid of the (first) eventual ( |
|---|
| 129 | $optlist{$last_module} =~ s/\(//; |
|---|
| 130 | |
|---|
| 131 | $last_module = undef; |
|---|
| 132 | next; |
|---|
| 133 | |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | if ($last_module && exists($optlist{$last_module})) { |
|---|
| 137 | $optlist{$last_module} .= ($module . " "); |
|---|
| 138 | next; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | if ($module =~ /(.*)\((.*)/) { |
|---|
| 142 | |
|---|
| 143 | $module = $last_module = ($1 || $last_module); |
|---|
| 144 | # Too much of .o and not .o for my liking. |
|---|
| 145 | $last_module =~ s/\.[ko|o]$//; |
|---|
| 146 | $optlist{$last_module} .= ($2 . " "); |
|---|
| 147 | |
|---|
| 148 | # $last_module = undef if ($optlist{$last_module} =~ s/\)//); |
|---|
| 149 | if ($optlist{$last_module} =~ /\)/) { |
|---|
| 150 | $optlist{$last_module} =~ s/\)//; |
|---|
| 151 | $last_module = undef; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | } else { |
|---|
| 155 | $last_module = $module; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | next unless ($module); |
|---|
| 160 | |
|---|
| 161 | $module .= "$modext" unless ($module =~ /\$modext$/); |
|---|
| 162 | |
|---|
| 163 | unless (defined($allmodules->{$module})) { |
|---|
| 164 | print join(" ", keys(%$allmodules)); |
|---|
| 165 | die "Module $module not found.\n"; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | print STDERR "getDependFiles($module)\n" if ($debug); |
|---|
| 169 | |
|---|
| 170 | push @deplist, getDependFiles($module); |
|---|
| 171 | |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | if ($copyto) { |
|---|
| 175 | die "No directory" unless ( -d $copyto ); |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | foreach my $module (@deplist) { |
|---|
| 179 | print STDERR "copyto($module)\n" if ($debug); |
|---|
| 180 | # Quick and dirty: |
|---|
| 181 | my ($dn) = dirname($module); |
|---|
| 182 | my ($bn) = basename($module); |
|---|
| 183 | my ($nd) = "$copyto/lib/modules/$kernelversion/kernel/$dn"; |
|---|
| 184 | |
|---|
| 185 | print STDERR "$nd\n" if ($debug); |
|---|
| 186 | |
|---|
| 187 | system('mkdir', '-p', $nd); |
|---|
| 188 | system('cp', "$moduleroot/lib/modules/$kernelversion/kernel/$module", $nd . "/."); |
|---|
| 189 | |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | # This is not really the deplist but the list you can use |
|---|
| 195 | # for modules.lst |
|---|
| 196 | if ($deplist) { |
|---|
| 197 | |
|---|
| 198 | map { s/.*\/// } @deplist; |
|---|
| 199 | map { s/\.[ko|o]// } @deplist; |
|---|
| 200 | |
|---|
| 201 | # I shall say this only once. |
|---|
| 202 | # And never read it again, thanks Peder. |
|---|
| 203 | # printf "%s\n", join "\n", do { my %f; grep { ! $f{$_}++ } @deplist }; |
|---|
| 204 | # But, this oneliner was neat altho I need to add options. |
|---|
| 205 | my %f; |
|---|
| 206 | foreach my $d (@deplist) { |
|---|
| 207 | next if ($f{$d}); |
|---|
| 208 | $f{$d}++; |
|---|
| 209 | print $d; |
|---|
| 210 | if (exists($optlist{$d})) { print " " . $optlist{$d}; }; |
|---|
| 211 | print "\n"; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | } else { |
|---|
| 215 | # Not sure I need this. |
|---|
| 216 | print join(" ", @deplist) . "\n"; |
|---|
| 217 | |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | print STDERR "PATHS:\n\n" if ($debug); |
|---|
| 221 | # &printPaths($m, $p); |
|---|
| 222 | |
|---|
| 223 | sub getDependFiles { |
|---|
| 224 | my ($mod) = @_; |
|---|
| 225 | my (@retarr); |
|---|
| 226 | |
|---|
| 227 | print STDERR "Testing $mod\n" if ($debug); |
|---|
| 228 | |
|---|
| 229 | # First, check if there are (more) dependencies: |
|---|
| 230 | # If so, recurseively, ask for more files: |
|---|
| 231 | if (exists($allmodules->{$mod}->{depends})) { |
|---|
| 232 | foreach my $mo (@{$allmodules->{$mod}->{depends}}) { |
|---|
| 233 | print STDERR "Checking $mo\n" if ($debug); |
|---|
| 234 | push @retarr, getDependFiles($mo); |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | push @retarr, $allmodules->{$mod}->{file}; |
|---|
| 239 | |
|---|
| 240 | return @retarr; |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | sub readModulesDep { |
|---|
| 246 | |
|---|
| 247 | my ($modules, $paths); |
|---|
| 248 | my ($depfile) = $moduleroot . "/lib/modules/" . $kernelversion . "/modules.dep"; |
|---|
| 249 | open (DEP, "$depfile" ) |
|---|
| 250 | or die "No modules.dep.\n"; |
|---|
| 251 | |
|---|
| 252 | my ($last_module); |
|---|
| 253 | while (<DEP>) { |
|---|
| 254 | |
|---|
| 255 | chomp; |
|---|
| 256 | next unless (/kernel/); |
|---|
| 257 | |
|---|
| 258 | my ($line) = /\w+\/kernel\/(.*)/; |
|---|
| 259 | |
|---|
| 260 | print STDERR "\n\nLINE: " . $line . "\n" if ($debug); |
|---|
| 261 | |
|---|
| 262 | if ($line =~ /\:/) { |
|---|
| 263 | $last_module = undef; |
|---|
| 264 | |
|---|
| 265 | # To fix busybox depmod.pl: |
|---|
| 266 | $line =~ s/\s+$//; |
|---|
| 267 | |
|---|
| 268 | my ($modline, $dep) = split (/\:/, $line); |
|---|
| 269 | my (@path) = split (/\//, $modline); |
|---|
| 270 | |
|---|
| 271 | my ($module, $lastp, $lastp1, $lastp2, $lastp3); |
|---|
| 272 | |
|---|
| 273 | foreach my $p (@path) { |
|---|
| 274 | # |
|---|
| 275 | # We have a module; |
|---|
| 276 | # |
|---|
| 277 | |
|---|
| 278 | ($module) = ( $p =~ /(.*$modext)/ ); |
|---|
| 279 | |
|---|
| 280 | print STDERR "MODP: $p ($module) \n" if ($debug); |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | if ($module) { |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | # Gawd this is ugly. |
|---|
| 287 | |
|---|
| 288 | if ($lastp1) { push @{$paths->{$lastp}->{'modules'}}, $module; } |
|---|
| 289 | |
|---|
| 290 | if ($lastp2) { push @{$paths->{$lastp}->{$lastp1}->{'modules'}}, $module; } |
|---|
| 291 | |
|---|
| 292 | if ($lastp3) { |
|---|
| 293 | push @{$paths->{$lastp}->{$lastp1}->{$lastp2}->{'modules'}}, $module; |
|---|
| 294 | push @{$paths->{$lastp}->{$lastp1}->{$lastp2}->{$lastp3}->{'modules'}}, $module; |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | $modules->{$module}->{'file'}=$modline; |
|---|
| 298 | |
|---|
| 299 | next; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | # Gawd this is ugly. Part II. |
|---|
| 304 | $lastp3 = $p if ($lastp && $lastp1 && $lastp2 && !$lastp3); |
|---|
| 305 | $lastp2 = $p if ($lastp && $lastp1 && !$lastp2); |
|---|
| 306 | $lastp1 = $p if ($lastp && !$lastp1); |
|---|
| 307 | $lastp = $p if (!$lastp); |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | } |
|---|
| 311 | # print STDERR $module . ";" . $lastp . ";" . $lastp1 . ";" . $lastp2 . ";" . "$lastp3;\n" if ($debug); |
|---|
| 312 | |
|---|
| 313 | if ($dep) { |
|---|
| 314 | print STDERR "DEP: $dep ($module) \n" if ($debug); |
|---|
| 315 | foreach my $single (split(/\s+/, $dep)) { |
|---|
| 316 | my ($dmodule) = ($single =~ /\/(\w+-?\w+$modext)/); |
|---|
| 317 | next unless $dmodule; |
|---|
| 318 | print STDERR "DEPM: $module-$dmodule-$single\n" if ($debug); |
|---|
| 319 | push @{$modules->{$module}->{'depends'}}, $dmodule; |
|---|
| 320 | $last_module = $module; |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | if ($last_module && $line =~ /^\t/) { |
|---|
| 330 | my ($dmodule) = ( $_ =~ /(.*$modext)/ ); |
|---|
| 331 | |
|---|
| 332 | print STDERR "DEPM2: $dmodule\n" if ($debug); |
|---|
| 333 | |
|---|
| 334 | push @{$modules->{$last_module}->{'depends'}}, $dmodule; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | close (DEP); |
|---|
| 341 | return ($modules, $paths); |
|---|
| 342 | |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | sub printPaths { |
|---|
| 346 | my ($modules, $path, $level) = @_; |
|---|
| 347 | |
|---|
| 348 | $level++; |
|---|
| 349 | |
|---|
| 350 | foreach my $pat (sort(keys (%$path))) { |
|---|
| 351 | print "Path:$pat\n" if ($pat && $pat ne "modules"); |
|---|
| 352 | if ($pat eq "modules") { |
|---|
| 353 | foreach my $mod (@{$path->{'modules'}}) { |
|---|
| 354 | print "\t$mod - "; |
|---|
| 355 | print join (";", @{$modules->{$mod}->{'depends'}}) if (exists($modules->{$mod}->{'depends'})); |
|---|
| 356 | print "\n"; |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | } |
|---|
| 360 | next; |
|---|
| 361 | } |
|---|
| 362 | &printPaths($modules, $path->{$pat}, $level) if (ref($path->{$pat}) eq "HASH"); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | } |
|---|