[3219] | 1 | #!/usr/bin/perl -w
|
---|
| 2 | #
|
---|
| 3 | # Subroutines related to Kernel brought by the MondoRescue project
|
---|
| 4 | #
|
---|
| 5 | # $Id$
|
---|
| 6 | #
|
---|
| 7 | # Copyright B. Cornec 2008-2014
|
---|
| 8 | # Provided under the GPL v2
|
---|
| 9 |
|
---|
| 10 | package MondoRescue::Kernel;
|
---|
| 11 |
|
---|
| 12 | use strict 'vars';
|
---|
| 13 | use Data::Dumper;
|
---|
[3249] | 14 | use POSIX "uname";
|
---|
[3219] | 15 | use lib qw (lib);
|
---|
| 16 | use ProjectBuilder::Base;
|
---|
| 17 | use ProjectBuilder::Conf;
|
---|
| 18 | use MondoRescue::Base;
|
---|
| 19 | use MondoRescue::Inventory;
|
---|
| 20 |
|
---|
| 21 | # Inherit from the "Exporter" module which handles exporting functions.
|
---|
| 22 |
|
---|
| 23 | use Exporter;
|
---|
| 24 |
|
---|
| 25 | # Export, by default, all the functions into the namespace of
|
---|
| 26 | # any code which uses this module.
|
---|
| 27 |
|
---|
| 28 | our @ISA = qw(Exporter);
|
---|
[3249] | 29 | our @EXPORT = qw(mr_kernel_get_version mr_kernel_get_modules);
|
---|
[3219] | 30 |
|
---|
| 31 | =pod
|
---|
| 32 |
|
---|
| 33 | =head1 NAME
|
---|
| 34 |
|
---|
| 35 | MondoRescue::Kernel, part of the mondorescue.org
|
---|
| 36 |
|
---|
| 37 | =head1 DESCRIPTION
|
---|
| 38 |
|
---|
| 39 | This modules provides low level functions for Kernel support in the Mondorescue project
|
---|
| 40 |
|
---|
| 41 | =head1 USAGE
|
---|
| 42 |
|
---|
| 43 | =over 4
|
---|
| 44 |
|
---|
| 45 | =item B<mr_kernel_get_version>
|
---|
| 46 |
|
---|
| 47 | This function checks the kernel and returns back its version
|
---|
| 48 |
|
---|
| 49 | =cut
|
---|
| 50 |
|
---|
| 51 | sub mr_kernel_get_version {
|
---|
| 52 |
|
---|
| 53 | my ($os,$ver,$kernelver,$rest);
|
---|
| 54 |
|
---|
| 55 | # By default we don't know how it works for other OSes
|
---|
| 56 | $kernelver = "unknown";
|
---|
| 57 |
|
---|
[3249] | 58 | my ($sysname, $nodename, $release, $version, $machine ) = uname();
|
---|
| 59 | $kernelver = $release if (defined $release);
|
---|
[3219] | 60 |
|
---|
| 61 | return($kernelver);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[3249] | 64 |
|
---|
| 65 | =item B<mr_kernel_get_modules>
|
---|
| 66 |
|
---|
| 67 | Tis function takes as input the kernel version to examined (can be undef in which case the running kernel is used) and a list of modules names to examined as well (can be undef in which case all modules ar taken)
|
---|
| 68 | This function returns back the modules path as a first argument and the list of relative modules path for the modules names passed.
|
---|
| 69 |
|
---|
| 70 | Example:
|
---|
| 71 | mr_kernel_get_modules("3.8.13.4-desktop-1.mga3","ext3") returns
|
---|
| 72 | ("/lib/modules/3.8.13.4-desktop-1.mga3", "kernel/fs/ext3/ext3.ko.xz","kernel/fs/jbd/jbd.ko.xz")
|
---|
| 73 |
|
---|
| 74 | =cut
|
---|
| 75 |
|
---|
| 76 | sub mr_kernel_get_modules {
|
---|
| 77 |
|
---|
[3250] | 78 | my @allmodules = @_;
|
---|
| 79 | my $ver = shift @allmodules;
|
---|
[3249] | 80 | $ver = mr_kernel_get_version() if (not defined ($ver));
|
---|
| 81 |
|
---|
| 82 | my $module = "";
|
---|
| 83 | my %modlist;
|
---|
| 84 | my $void = "";
|
---|
[3254] | 85 | my @alllivemodules;
|
---|
[3249] | 86 | my @allmodpaths;
|
---|
| 87 | my $modulepath = "";
|
---|
| 88 |
|
---|
[3250] | 89 | #print Dumper(@allmodules);
|
---|
| 90 |
|
---|
[3254] | 91 | # First compute the list of "live" modules - run on the system
|
---|
| 92 | open(LSMOD, "cat /proc/modules |") or die "Unable to launch lsmod";
|
---|
| 93 | while (<LSMOD>) {
|
---|
| 94 | next if (/^Module/);
|
---|
| 95 | ($module, $void) = split(/ /);
|
---|
[3257] | 96 | pb_log(2,"***$module***|***$void***\n");
|
---|
[3254] | 97 | push @alllivemodules,$module;
|
---|
| 98 | }
|
---|
| 99 | close(LSMOD);
|
---|
| 100 |
|
---|
| 101 | # If no module name list passed as parameter, then work on all modules of the system
|
---|
[3249] | 102 | if (not defined $allmodules[0]) {
|
---|
[3254] | 103 | @allmodules = @alllivemodules;
|
---|
[3249] | 104 | }
|
---|
[3250] | 105 | #print Dumper(@allmodules);
|
---|
[3249] | 106 |
|
---|
[3254] | 107 | # Now computes the dependencies of each module and store them in %modlist
|
---|
[3249] | 108 | open(DEPMOD, "/sbin/depmod -n $ver |") or die "Unable to launch depmod";
|
---|
| 109 | while (<DEPMOD>) {
|
---|
| 110 | ($module, $void) = split(/:/);
|
---|
| 111 | last if ($module =~ /^#/);
|
---|
| 112 | chomp($void);
|
---|
[3250] | 113 | $void =~ s/\s+//;
|
---|
[3249] | 114 | $modlist{$module} = $void;
|
---|
| 115 | pb_log(2,"Depmod on $module gives $void\n");
|
---|
| 116 | }
|
---|
| 117 | close(DEPMOD);
|
---|
| 118 |
|
---|
| 119 | #print Dumper(%modlist)."\n";
|
---|
| 120 |
|
---|
| 121 | my $lib;
|
---|
| 122 | my $modulep;
|
---|
| 123 | my $kernelv;
|
---|
| 124 | my $modpath;
|
---|
| 125 |
|
---|
[3254] | 126 | # Analyze each module to find its full path name
|
---|
[3249] | 127 | foreach my $m (@allmodules) {
|
---|
| 128 | pb_log(1,"Analyzing $m\n");
|
---|
| 129 | open(MODINFO, "/sbin/modinfo -n -k $ver $m 2>/dev/null |") or die "Unable to launch modinfo";
|
---|
| 130 | $module = <MODINFO>;
|
---|
| 131 | close(MODINFO);
|
---|
| 132 | if ((not defined $module) || ($module =~ '^$')) {
|
---|
| 133 | pb_log(1,"WARNING: No modinfo for module $m\n");
|
---|
| 134 | next;
|
---|
| 135 | }
|
---|
| 136 | chomp($module);
|
---|
| 137 | ($void,$lib,$modulep,$kernelv,$modpath) = split(/\//,$module,5);
|
---|
| 138 | next if (not defined $modpath);
|
---|
[3250] | 139 | $modulepath = "/$lib/$modulep/$kernelv";
|
---|
| 140 | pb_log(2,"modpath: $modulepath/$modpath\n");
|
---|
| 141 | push @allmodpaths,"$modulepath/$modpath",map { "$modulepath/".$_ } split(/ /,$modlist{$modpath});
|
---|
[3249] | 142 | }
|
---|
| 143 | pb_log(1,"all modpaths: ".join(' ',@allmodpaths)."\n");
|
---|
[3250] | 144 | # From List::More
|
---|
| 145 | my %seen = ();
|
---|
| 146 | return(grep { not $seen{$_}++ } @allmodpaths);
|
---|
[3249] | 147 | }
|
---|
| 148 |
|
---|
[3219] | 149 | =back
|
---|
| 150 |
|
---|
| 151 | =head1 WEB SITES
|
---|
| 152 |
|
---|
| 153 | The main Web site of the project is available at L<http://www.mondorescue.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.mondorescue.org/>.
|
---|
| 154 |
|
---|
| 155 | =head1 USER MAILING LIST
|
---|
| 156 |
|
---|
| 157 | The mailing list of the project is available at L<mailto:mondo@lists.sf.net>
|
---|
| 158 |
|
---|
| 159 | =head1 AUTHORS
|
---|
| 160 |
|
---|
| 161 | The Mondorescue.org team L<http://www.mondorescue.org/> lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
|
---|
| 162 |
|
---|
| 163 | =head1 COPYRIGHT
|
---|
| 164 |
|
---|
| 165 | This module is distributed under the GPL v2.0 license
|
---|
| 166 | described in the file C<COPYING> included with the distribution.
|
---|
| 167 |
|
---|
| 168 | =cut
|
---|
| 169 |
|
---|
| 170 | 1;
|
---|
| 171 |
|
---|