| 1 | # | ||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||
| 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 | ▶ | use v5.14.4; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 30 | 32 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 31 | |||||||||||||||||||||||
| 32 | 32 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | 32 | ▶ | use Rex::Logger; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 35 | 32 | ▶ | use Rex::Helper::Run; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 36 | 32 | ▶ | use Rex::Commands::Gather; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | 32 | ▶ | use Data::Dumper; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 39 | |||||||||||||||||||||||
| 40 | 32 | require Rex::Exporter; | |||||||||||||||||||||
| 41 | |||||||||||||||||||||||
| 42 | 32 | ▶ | use base qw(Rex::Exporter); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 43 | 32 | ▶ | use vars qw(@EXPORT); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | 32 | @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 | 0 | ▶ | sub kmod { | ||||||||||||||||||||
|
Subroutine
kmod: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||
| 69 | 0 | 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 | ▶ | if ( $os eq "FreeBSD" ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 79 | 0 | $load_command = "kldload"; | |||||||||||||||||||||
| 80 | 0 | $unload_command = "kldunload"; | |||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||
| 82 | ▶ | elsif ( $os eq "NetBSD" || $os eq "OpenBSD" ) { | |||||||||||||||||||||
|
Condition0%0/3
$os eq "NetBSD" or $os eq "OpenBSD"
MC/DC0%0/2
$os eq "NetBSD" or $os eq "OpenBSD"
$os eq "NetBSD"
$os eq "OpenBSD"
Truth table0%0/3
A or B
A= $os eq "NetBSD"B= $os eq "OpenBSD"
| |||||||||||||||||||||||
| 83 | 0 | $load_command = "modload"; | |||||||||||||||||||||
| 84 | 0 | $unload_command = "modunload"; | |||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | 0 | ▶ | if ( $options->{"entry"} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 87 | 0 | $load_command .= " -e " . $options->{"entry"}; | |||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||
| 90 | elsif ( $os eq "SunOS" ) { | ||||||||||||||||||||||
| 91 | 0 | $load_command = "modload -p "; | |||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | 0 | ▶ | if ( $options->{"exec_file"} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 94 | 0 | $load_command .= " -e " . $options->{"exec_file"} . " "; | |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | 0 | ▶ | $unload_command = sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||
| 98 | 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"; | |||||||||||||||||||||
| 102 | 0 | my $cmd = "modunload -i $mod_id"; | |||||||||||||||||||||
| 103 | |||||||||||||||||||||||
| 104 | 0 | ▶ | if ( $options->{"exec_file"} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 | ▶ | if ( $action eq "load" ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 116 | 0 | Rex::Logger::debug("Loading Kernel Module: $module"); | |||||||||||||||||||||
| 117 | 0 | i_run "$load_command $module", fail_ok => 1; | |||||||||||||||||||||
| 118 | 0 | ▶ | unless ( $? == 0 ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 | ▶ | if ( ref($unload_command) eq "CODE" ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 130 | 0 | $unload_command_str = &$unload_command(); | |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||
| 132 | |||||||||||||||||||||||
| 133 | 0 | i_run "$unload_command_str $module", fail_ok => 1; | |||||||||||||||||||||
| 134 | 0 | ▶ | unless ( $? == 0 ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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 | 32 | 1; | |||||||||||||||||||||