File Coverage

lib/Rex/Commands/Kernel.pm
Criterion Covered Total %
statement 23 66 34.8
branch 0 24 0.0
condition 0 3 0.0
subroutine 8 10 80.0
pod 1 1 100.0
total 32 104 30.7


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             =head1 NAME
6              
7             Rex::Commands::Kernel - Load/Unload Kernel Modules
8              
9             =head1 DESCRIPTION
10              
11             With this module you can load and unload kernel modules.
12              
13             Version <= 1.0: All these functions will not be reported.
14              
15             All these functions are not idempotent.
16              
17             =head1 SYNOPSIS
18              
19             kmod load => "ipmi_si";
20              
21             kmod unload => "ipmi_si";
22              
23             =head1 EXPORTED FUNCTIONS
24              
25             =cut
26              
27             package Rex::Commands::Kernel;
28              
29 32     32   695 use v5.12.5;
  32         262  
30 32     32   299 use warnings;
  32         167  
  32         2847  
31              
32             our $VERSION = '1.14.3'; # VERSION
33              
34 32     32   362 use Rex::Logger;
  32         191  
  32         464  
35 32     32   1384 use Rex::Helper::Run;
  32         235  
  32         4851  
36 32     32   256 use Rex::Commands::Gather;
  32         115  
  32         272  
37              
38 32     32   292 use Data::Dumper;
  32         122  
  32         2989  
39              
40             require Rex::Exporter;
41              
42 32     32   334 use base qw(Rex::Exporter);
  32         253  
  32         2947  
43 32     32   308 use vars qw(@EXPORT);
  32         133  
  32         20780  
44              
45             @EXPORT = qw(kmod);
46              
47             =head2 kmod($action => $module)
48              
49             This function loads or unloads a kernel module.
50              
51             task "load", sub {
52             kmod load => "ipmi_si";
53             };
54              
55             task "unload", sub {
56             kmod unload => "ipmi_si";
57             };
58              
59             If you're using NetBSD or OpenBSD you have to specify the complete path and, if needed the entry function.
60              
61             task "load", sub {
62             kmod load => "/usr/lkm/ntfs.o";
63             kmod load => "/path/to/module.o", entry => "entry_function";
64             };
65              
66             =cut
67              
68             sub kmod {
69 0     0 1   my ( $action, $module, @rest ) = @_;
70              
71 0           my $options = {@_};
72              
73 0           my $os = get_operating_system();
74              
75 0           my $load_command = "modprobe";
76 0           my $unload_command = "rmmod";
77              
78 0 0 0       if ( $os eq "FreeBSD" ) {
    0          
    0          
    0          
79 0           $load_command = "kldload";
80 0           $unload_command = "kldunload";
81             }
82             elsif ( $os eq "NetBSD" || $os eq "OpenBSD" ) {
83 0           $load_command = "modload";
84 0           $unload_command = "modunload";
85              
86 0 0         if ( $options->{"entry"} ) {
87 0           $load_command .= " -e " . $options->{"entry"};
88             }
89             }
90             elsif ( $os eq "SunOS" ) {
91 0           $load_command = "modload -p ";
92              
93 0 0         if ( $options->{"exec_file"} ) {
94 0           $load_command .= " -e " . $options->{"exec_file"} . " ";
95             }
96              
97             $unload_command = sub {
98 0     0     my @mod_split = split( /\//, $module );
99 0           my $mod = $mod_split[-1];
100              
101 0           my ($mod_id) = map { /^\s*(\d+)\s+.*$mod/ } i_run "modinfo";
  0            
102 0           my $cmd = "modunload -i $mod_id";
103              
104 0 0         if ( $options->{"exec_file"} ) {
105 0           $cmd .= " -e " . $options->{"exec_file"};
106             }
107              
108 0           return $cmd;
109 0           };
110             }
111             elsif ( $os eq "OpenWrt" ) {
112 0           $load_command = "insmod";
113             }
114              
115 0 0         if ( $action eq "load" ) {
    0          
116 0           Rex::Logger::debug("Loading Kernel Module: $module");
117 0           i_run "$load_command $module", fail_ok => 1;
118 0 0         unless ( $? == 0 ) {
119 0           Rex::Logger::info( "Error loading Kernel Module: $module", "warn" );
120 0           die("Error loading Kernel Module: $module");
121             }
122             else {
123 0           Rex::Logger::debug("Kernel Module $module loaded.");
124             }
125             }
126             elsif ( $action eq "unload" ) {
127 0           Rex::Logger::debug("Unloading Kernel Module: $module");
128 0           my $unload_command_str = $unload_command;
129 0 0         if ( ref($unload_command) eq "CODE" ) {
130 0           $unload_command_str = &$unload_command();
131             }
132              
133 0           i_run "$unload_command_str $module", fail_ok => 1;
134 0 0         unless ( $? == 0 ) {
135 0           Rex::Logger::info( "Error unloading Kernel Module: $module", "warn" );
136 0           die("Error unloading Kernel Module: $module");
137             }
138             else {
139 0           Rex::Logger::debug("Kernel Module $module unloaded.");
140             }
141             }
142             else {
143 0           Rex::Logger::info("Unknown action $action");
144 0           die("Unknown action $action");
145             }
146             }
147              
148             1;