Document the ksplice-create --build-modules option.
[ksplice.git] / ksplice-apply.in
blob1689741ddb842598ff9017e0ffa16644733dcbfe
1 #!/usr/bin/perl
3 # Copyright (C) 2007-2008 Ksplice, Inc.
4 # Authors: Jeff Arnold, Anders Kasoerg, Tim Abbott
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License, version 2.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
17 # 02110-1301, USA.
19 use strict;
20 use warnings;
21 use lib 'KSPLICE_DATA_DIR';
22 use Ksplice;
24 my $debug;
25 my $debugon = 0;
26 my $partial = 0;
27 GetOptions(@common_options,
28 "partial" => \$partial,
29 "debug" => \$debugon,
30 "debugfile=s" => \$debug) or pod2usage(1);
32 pod2usage(1) if($help || scalar(@ARGV) != 1);
34 my $file = abs_path($ARGV[0]);
35 $debugon = 1 if(defined $debug);
36 $debug = abs_path($debug) if (defined $debug);
38 my $tmpdir = tempdir('ksplice-tmp-XXXXXX', TMPDIR => 1, CLEANUP => 1);
39 chdir($tmpdir);
40 my $ksplice = unpack_update($file);
41 chdir($ksplice);
43 die "No contents file in $file\n" if (!-e "contents");
44 open(CONTENTS, '<', "contents");
45 my $core;
46 while (<CONTENTS>) {
47 my @line = split(' ', $_);
48 if ($line[0] eq 'core') {
49 die "Multiple core modules in $file!" if (defined $core);
50 $core = {
51 module => $line[1],
52 file => $line[2],
56 close(CONTENTS);
58 my $nounload = runstr("lsmod") =~ m/- $/m;
60 (my $kid = $ksplice) =~s|ksplice[-_]([^-_]+)|$1|;
61 my $update = "ksplice_$kid";
62 if(update_loaded($kid)) {
63 my $stage = get_stage($kid);
64 if ($stage eq "applied") {
65 print STDERR "Ksplice update $kid already applied.\n";
66 exit(0);
68 die "Reversed Ksplice module $update already loaded!" if ($stage eq "reversed");
71 runstr_err(qw(modprobe -q ksplice)) eq "" || die;
72 if (defined $core) {
73 die "Could not find Ksplice core module $core->{file}\n" if (!-e $core->{file});
74 if (runstr("lsmod") =~ m/^\Q$core->{module}\E\s+/) {
75 die "Ksplice core module $core already loaded.";
77 if (!load_module($core->{file}, "debug=$debugon")) {
78 die "Error loading Ksplice core module $core->{module} for update $kid";
82 my @helpers = ();
83 foreach my $helper (glob('ksplice-*-helper.ko')) {
84 (my $primary = $helper) =~ s/\-helper\.ko$/.ko/;
85 die unless(-e $primary && -e $helper);
86 (my $target = $primary) =~ s/^ksplice-[^_]+_(.*)\.ko/$1/;
87 if ($target ne 'vmlinux' && runstr("lsmod") !~ m/^${target}\s/m) {
88 if (!$partial) {
89 cleanup_modules();
90 print_error("target_not_loaded");
91 die "Module $target to be patched not loaded";
94 push @helpers, $helper;
96 foreach my $helper (@helpers) {
97 (my $primary = $helper) =~ s/\-helper\.ko$/.ko/;
98 if(!load_module($primary)) {
99 die "Error loading primary module $primary";
101 if(!load_module($helper)) {
102 my $debugfile = get_debug_output("init_$kid", $debug);
103 print STDERR "Error loading helper module $helper\n";
104 (my $module = $primary) =~ s/-/_/g;
105 $module =~ s/\.ko//;
106 cleanup_modules();
107 if($debugon && defined $debugfile) {
108 print("Debugging output saved to $debugfile\n");
110 die;
114 set_debug_level($kid, $debugon);
115 set_partial($kid, $partial);
116 set_stage($kid, "applied");
117 my $stage = get_stage($kid);
118 if($stage ne 'applied') {
119 my $debugfile = get_debug_output($kid, $debug);
120 my $abort_cause = get_abort_cause($kid);
121 my $conflicts = get_conflicts($kid);
122 cleanup_modules();
123 print STDERR "Error applying Ksplice update $kid:\n";
124 print_error($abort_cause);
125 if ($abort_cause eq 'code_busy') {
126 print $conflicts;
128 if ($debugon && defined $debugfile) {
129 print("Debugging output saved to $debugfile\n");
131 die;
133 mkpath("/var/run/ksplice/updates/$kid");
134 copy("patch", "/var/run/ksplice/updates/$kid/patch") if (-e "patch");
135 copy("description", "/var/run/ksplice/updates/$kid/description") if (-e "description");
136 if (!$nounload) {
137 foreach my $helper (@helpers) {
138 $helper =~ s/-/_/g;
139 $helper =~ s/\.ko$//;
140 (my $primary = $helper) =~ s/_helper$//;
141 runval('rmmod', $helper);
142 runval('rmmod', $primary) if ($partial && refcount($primary) == 0);
145 print "Done!\n";
146 exit(0);
148 my @modules_loaded = qw();
150 sub load_module {
151 my ($module, @params) = @_;
152 if (runval_raw("insmod", $module, @params) != 0) {
153 child_error();
154 return 0;
156 $module =~ s/\.ko//;
157 $module =~ s/-/_/g;
158 push @modules_loaded, $module;
159 return 1;
162 sub refcount {
163 my ($module) = @_;
164 foreach(split(/\n/, runstr("lsmod"))) {
165 if (m/^(\S+)\s+[0-9]+\s+([0-9])+\s/) {
166 return $2 if ($1 eq $module);
169 return -1;
172 sub cleanup_modules {
173 foreach my $module (reverse(@modules_loaded)) {
174 runval("rmmod", $module) if(!$nounload);
178 sub print_error {
179 my ($error) = @_;
180 my %errors = (
181 "no_match" => <<'END',
182 Ksplice has aborted the upgrade because Ksplice has been unable to match the
183 object code produced by your current compiler and assembler against the running
184 kernel's object code. If you provided the exact kernel source to the running
185 kernel, then it appears that your current compiler and/or assembler are
186 behaving differently from the compiler and assembler used to build the running
187 kernel. If possible, please use the exact compiler and assembler that were
188 used to build the running kernel. If you are using exactly the same compiler
189 and assembler, consider reporting a bug to PACKAGE_BUGREPORT.
191 "code_busy" => <<'END',
192 Ksplice has aborted the upgrade because it appears that the code that you are
193 trying to patch is continuously in use by the system. More specifically,
194 Ksplice has been unable to find a moment when one or more of the to-be-patched
195 functions is not on a thread's kernel stack.
197 "bad_system_map" => <<'END',
198 Ksplice has aborted the upgrade because it appears that the System.map file
199 provided to ksplice-create does not match the running kernel.
201 "failed_to_find" => <<'END',
202 Ksplice has aborted the upgrade because it was unable to resolve some of the
203 symbols used in the update.
205 "already_reversed" => <<'END',
206 The Ksplice update that you are attempting to apply has already been applied
207 and reversed. You need to unload the Ksplice modules associated with this
208 update before you can apply this update again.
210 "missing_export" => <<'END',
211 Ksplice has aborted the upgrade because the symbols exported by the kernel
212 did not match Ksplice's expectations.
214 "unexpected_running_task" => <<'END',
215 Ksplice has aborted the upgrade because of an unexpected failure during the
216 kernel stack check. Please consider reporting a bug to PACKAGE_BUGREPORT.
218 "target_not_loaded" => <<'END',
219 Ksplice has aborted the upgrade because one of the modules to be
220 patched by the update was not loaded. If you want to apply this
221 update only to those modules that are loaded, then you should use the
222 --partial option.
224 "out_of_memory" => <<'END',
225 Ksplice has aborted the upgrade because the kernel ran out of memory.
227 "call_failed" => <<'END',
228 Ksplice has aborted the upgrade at the request of a one of the
229 pre-application hooks that were included as part of this Ksplice
230 update. This is likely the result of a bug in the patch used to
231 generate this update.
233 "unexpected" => <<'END',
234 Ksplice has aborted because of an unexpected error.
235 Please consider reporting a bug to PACKAGE_BUGREPORT.
237 "UNKNOWN" => <<'END',
238 The Ksplice kernel component has returned an error code that this version of
239 ksplice-apply does not understand.
241 "ok" => <<'END',
242 Ksplice has aborted the upgrade for unknown reasons.
243 Please consider reporting a bug to PACKAGE_BUGREPORT.
246 $error = "UNKNOWN" if (!exists $errors{$error});
247 print STDERR "\n$errors{$error}\n";
250 =head1 NAME
252 ksplice-apply - Apply an on-disk Ksplice update to the running kernel
254 =head1 SYNOPSIS
256 B<ksplice-apply> [I<OPTIONS>] I<UPDATE_TARBALL>
258 =head1 DESCRIPTION
260 B<ksplice-apply> takes as input a Ksplice update tarball, as generated by
261 L<ksplice-create(8)>, and it applies the update to the running binary kernel.
263 The update tarball used with B<ksplice-apply> must have been generated for the
264 running kernel's version.
266 =head1 OPTIONS
268 =over 8
270 =item B<--debug>
272 Applies the update with debugging output enabled. Recommended only for
273 debugging.
275 =item B<--debugfile=>I<filename>
277 Sets the location where debugging output should be saved. Implies --debug.
279 =item B<--partial>
281 Applies the update only to those modules which are loaded. Any
282 modules patched by the update that are not loaded are ignored (without
283 this option, Ksplice aborts if any modules patched by the update are
284 not loaded).
286 =back
288 =head1 SEE ALSO
290 L<ksplice-create(8)>, L<ksplice-view(8)>, L<ksplice-undo(8)>
292 =head1 BUGS
294 Please report bugs to <PACKAGE_BUGREPORT>.
296 =head1 AUTHORS
298 Jeff Arnold, Anders Kaseorg, and Tim Abbott
300 =head1 COPYRIGHT
302 Copyright (C) 2007-2008 Ksplice, Inc.
304 This is free software and documentation. You can redistribute and/or modify it
305 under the terms of the GNU General Public License, version 2.
307 =cut