3 # Retrieve modules required for an initrd image
7 die "Syntax: $0 [ -d target_directory ] module_1 module_2 module_3\n"
10 # Parse command line arguments
11 my @requested_modules = ();
15 chomp ( my $current_kernel_ver = `uname -r` );
16 while ( $_ = shift ) {
17 if ( /-d/ ) { $target_dir = shift }
18 elsif ( /-k/ ) { $kernel_ver = shift }
19 elsif ( /-q/ ) { $quiet = 1 }
20 else { push @requested_modules, $_ };
23 # Create target directory if required
25 print STDERR
"Target directory is $target_dir\n" unless $quiet;
26 system ( "mkdir -p $target_dir" );
30 # Use modprobe -nav to retrieve locations of modules and their dependencies
31 print STDERR
"Requested modules ". join (' ', @requested_modules)."\n" unless $quiet;
33 foreach my $module ( @requested_modules ) {
34 my @module_list = map { /^\S+\s+(.*)$/ ; $1 } `/sbin/modprobe -nva $module`;
35 die "Cannot find any modules matching $module\n" unless @module_list;
36 push @modules_dups, @module_list;
39 # Remove duplicates from list
40 my %module_basenames = ();
42 foreach my $module ( @modules_dups ) {
43 # Ugly hack : assume that dependencies are independent of kernel version
44 # This seems to be necessary because we can't run modprobe and specify
45 # an alternate modules.dep file; it refuses to understand lines of the
46 # form "depfile=XXX" as documented in modules.conf(5)
47 $module =~ s/$current_kernel_ver/$kernel_ver/ if $kernel_ver;
48 push @modules, $module unless $module_basenames{$module};
49 ( my $basename ) = ( $module =~ /([^\/]+)\
.o
/ );
50 $module_basenames{$module} = $basename;
55 foreach my $module ( @modules ) {
56 my $basename = $module_basenames{$module};
58 print STDERR
"Using module $basename from $module\n" unless $quiet;
59 # Copy uncompressed module to current directory
60 system ("gunzip -c $module > $basename.o");
61 # Print insmod line to stdout
62 print "insmod $basename\n";