Actually throw an error if libbfd is not found.
[ksplice.git] / ksplice-create.in
blob2345bde94486c49ad81fe72df7278c61b0e09e40
1 #!/usr/bin/perl
3 # Copyright (C) 2007-2008 Jeffrey Brian Arnold <jbarnold@mit.edu>
4 # Copyright (C) 2008 Anders Kaseorg <andersk@mit.edu>,
5 # Tim Abbott <tabbott@mit.edu>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License, version 2.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
18 # 02110-1301, USA.
20 use strict;
21 use warnings;
22 use lib 'KSPLICE_DATA_DIR';
23 use Ksplice;
25 my ($patchfile, $diffext, $orig_config_dir, $jobs, $kid);
26 my @only_targets;
27 my @extra_match;
28 my $standalone;
29 my ($help, $wantversion, $prebuild, $skip_prebuild) = (0, 0, 0, 0);
30 my @patch_opt = "-p1";
31 GetOptions("help|?" => \$help,
32 "version" => \$wantversion,
33 "verbose|v:+" => \$Verbose::level,
34 "id=s" => \$kid,
35 "patch=s" => \$patchfile,
36 "diffext=s" => \$diffext,
37 "prebuild" => \$prebuild,
38 "only-targets=s" => \@only_targets,
39 "extra-match=s" => \@extra_match,
40 "standalone!" => \$standalone,
41 "skip-prebuild" => \$skip_prebuild,
42 "jobs|j:i" => \$jobs,
43 "config=s" => \$orig_config_dir,
44 "patch-opt=s" => \@patch_opt) or pod2usage(1);
46 if($wantversion) {
47 print $version_str;
48 exit(0);
50 pod2usage(1) if($help || scalar(@ARGV) != 1);
51 my $actions = (defined $patchfile) + (defined $diffext) + ($prebuild);
52 pod2usage(1) if($actions != 1);
54 my ($linuxtree) = (abs_path($ARGV[0]));
56 if(!defined $orig_config_dir) {
57 $orig_config_dir = "$linuxtree/ksplice";
59 else {
60 $orig_config_dir = abs_path($orig_config_dir);
61 if($orig_config_dir =~ $linuxtree) {
62 die "Aborting: User-specified ORIG_CONFIG cannot be KERNEL_SOURCE or a subdirectory";
65 if(!defined $orig_config_dir || ! -d $orig_config_dir) {
66 die "Failed to find ORIG_CONFIG directory ($orig_config_dir)";
68 if(! -e "$orig_config_dir/.config") {
69 die "Failed to find .config file in ORIG_CONFIG directory";
71 if(! -e "$orig_config_dir/System.map") {
72 die "Failed to find System.map file in ORIG_CONFIG directory";
75 my $kernel_headers_dir = "$orig_config_dir/build";
76 $kernel_headers_dir = $linuxtree unless(-d $kernel_headers_dir);
78 my @kbuild_flags = ();
79 if(-e "$orig_config_dir/flags") {
80 open(FLAGS, '<', "$orig_config_dir/flags") or die;
81 local $/;
82 @kbuild_flags = shellwords(scalar <FLAGS>);
83 close(FLAGS);
86 $ENV{KSPLICE_VERBOSE} = $Verbose::level;
87 $ENV{KSPLICE_CONFIG_DIR} = $orig_config_dir;
89 my @chars = ('a'..'z', 0..9);
90 $kid = join '', map { $chars[int(rand(36))] } 0..7 if(!defined $kid);
91 my $ksplice = "ksplice-$kid";
92 $ENV{KSPLICE_KID} = $kid;
94 # Some versions of Fedora have System.map files whose symbol addresses disagree
95 # with the running kernel by a constant address offset. Here, Ksplice notes the
96 # System.map address for printk so that it can later compare this address against
97 # the kernel's address for printk. This comparison helps Ksplice work around
98 # this Fedora problem, and this comparison also helps Ksplice detect whether
99 # the user has provided an incorrect System.map file.
100 my $map_printk = runstr("$datadir/ksplice-obj.pl", "system_map_lookup", "printk");
102 print "Starting kernel builds (this process might take a long time)...\n";
103 if($Verbose::level < 1) {
104 print "For output during this process, run ksplice-create with the option -v\n";
107 $patchfile = abs_path($patchfile) if(defined $patchfile);
109 my $origdir = getcwd();
110 chdir($linuxtree);
112 my @jlevel = (defined $ENV{CONCURRENCY_LEVEL} ? ("-j$ENV{CONCURRENCY_LEVEL}") : ());
113 @jlevel = ("-j$jobs") if(defined $jobs);
114 my @make_ksplice = ("make", "-f", "$datadir/Makefile.ksplice", "-rR", @jlevel, @kbuild_flags);
116 sub revert_orig() {
117 for(split(/\0/, runstr(qw(find -name *.KSPLICE* -print0)))) {
118 if(my ($file) = m/^(.*)\.KSPLICE_pre$/) {
119 rename($_, $file);
120 runval("$datadir/ksplice-obj.pl", "snap", "$file.KSPLICE") if(-e "$file.KSPLICE");
121 } elsif(m/\.KSPLICE_(?:primary|helper)$/) {
122 unlink($_) if(-e $_);
126 revert_orig();
128 if(!$skip_prebuild) {
129 copy("$orig_config_dir/.config", "$linuxtree/.config");
130 utime((stat("$orig_config_dir/.config"))[8, 9], "$linuxtree/.config");
131 my @snap_flags = ("KSPLICE_MODE=snap");
132 if(-e "include/config/kernel.release") {
133 push(@snap_flags, "-o", "include/config/kernel.release");
135 runval_raw(@make_ksplice, @snap_flags) == 0 or
136 die "Aborting: Prebuild failed";
137 sleep(1);
139 exit(0) if($prebuild);
141 my $tmpdir = tempdir('ksplice-tmp-XXXXXX', TMPDIR => 1, CLEANUP => 1);
142 copy($patchfile, "$tmpdir/patch") if(defined $patchfile);
143 $patchfile = "$tmpdir/patch";
145 if(defined $diffext) {
146 open(PATCH, '>', $patchfile) or die;
147 for(split(/\0/, runstr("find", "-name", "*$diffext", "-print0"))) {
148 my ($file) = /^(.*)\Q$diffext\E/ or die;
149 print PATCH runstr("diff", "-u", "--", $file, $_);
151 close(PATCH) or die;
152 @patch_opt = ("-p0");
155 my $kmodsrc = "$tmpdir/kmodsrc";
156 runval("cp", "-a", "--", "$datadir/kmodsrc", $kmodsrc);
157 $ENV{KSPLICE_KMODSRC} = $kmodsrc;
159 my @make_kmodsrc = ("make", "-C", $kernel_headers_dir, "M=$kmodsrc", "-rR", @jlevel, "KSPLICE_KID=$kid", "KSPLICE_VERSION=PACKAGE_VERSION", "map_printk=$map_printk");
161 if (!defined($standalone)) {
162 $standalone = (runval_raw(qw(grep -q ^CONFIG_KSPLICE=[ym]$), "$linuxtree/.config") != 0);
164 push(@make_kmodsrc, "KSPLICE_STANDALONE=1") if ($standalone);
166 runval(@make_kmodsrc);
168 runval_infile($patchfile, "patch", @patch_opt, "-bz", ".KSPLICE_pre");
169 push @make_ksplice, "KSPLICE_EXTRA_MATCH=@extra_match" if (@extra_match);
170 push @make_ksplice, "KSPLICE_ONLY_TARGETS=@only_targets" if (@only_targets);
171 if(runval_raw(@make_ksplice, "KSPLICE_MODE=diff") != 0) {
172 revert_orig();
173 die "Aborting: Applying the patch appears to break the kernel build";
176 sub copy_debug {
177 my ($file) = @_;
178 my ($dir, $base) = (dirname($file), basename($file));
179 -d "$tmpdir/objects/$dir" or mkpath("$tmpdir/objects/$dir");
180 copy($file, "$tmpdir/objects/$file");
181 my $cmdfile = "$dir/.$base.cmd";
182 copy($cmdfile, "$tmpdir/objects/$cmdfile") if(-e $cmdfile);
185 mkdir("$tmpdir/objects");
186 for (split(/\0/, runstr(qw(find -name *.KSPLICE* ! ( -name *.KSPLICE -empty ) ! -name .*.KSPLICE.cmd -print0)))) {
187 copy_debug($_);
188 copy_debug($1) if (m/^(.*)\.KSPLICE_pre$/);
191 my @modules = ();
192 foreach(glob(".tmp_versions/*.mod.KSPLICE")) {
193 open MOD, '<', $_;
194 chomp(my $mod = <MOD>);
195 close MOD;
196 foreach my $collect ("$mod.o.KSPLICE", "$mod.o.KSPLICE_primary",
197 "$mod.o.KSPLICE_helper") {
198 copy($collect, "$kmodsrc/" . basename($collect));
200 push @modules, basename($mod);
203 if(!@modules) {
204 revert_orig();
205 die "Aborting: No changes detected";
208 revert_orig();
210 runval(@make_kmodsrc, "modules", "KSPLICE_MODULES=@modules");
212 chdir($tmpdir);
213 mkdir($ksplice);
214 move($patchfile, $ksplice);
215 foreach my $mod (@modules) {
216 (my $target = $mod) =~ s/-/_/g;
217 my $mid = "${kid}_$target";
218 my $module = "ksplice-$mid";
219 rename("$kmodsrc/$module.ko", "$ksplice/$module.ko");
220 rename("$kmodsrc/$module-helper.ko", "$ksplice/$module-helper.ko");
222 rename("$kmodsrc/ksplice-$kid.ko", "$ksplice/ksplice-$kid.ko") if ($standalone);
224 mkdir("$ksplice/debug");
225 rename("objects", "$ksplice/debug/objects");
226 rename("$kmodsrc", "$ksplice/debug/kmodsrc");
227 runval("tar", "czf", "$ksplice.tar.gz", "--", $ksplice);
228 copy("$ksplice.tar.gz", "$origdir/$ksplice.tar.gz");
230 print "Ksplice update tarball written to $ksplice.tar.gz\n";
231 exit(0);
233 =head1 NAME
235 ksplice-create - Create a set of kernel modules for a rebootless kernel update
237 =head1 SYNOPSIS
239 B<ksplice-create> [B<--config=>I<ORIG_CONFIG>] B<--patch=>I<PATCH_FILE> I<KERNEL_SOURCE>
241 B<ksplice-create> [B<--config=>I<ORIG_CONFIG>] B<--diffext=>I<EXTENSION> I<KERNEL_SOURCE>
243 B<ksplice-create> [B<--config=>I<ORIG_CONFIG>] B<--prebuild> I<KERNEL_SOURCE>
245 =head1 DESCRIPTION
247 B<ksplice-create> creates a set of Ksplice kernel modules that, when loaded,
248 will apply a user-specified source code patch to the running binary kernel.
250 Before you use B<ksplice-create> on a patch, you should confirm that the
251 desired source code change does not make any semantic changes to kernel data
252 structures--that is, changes that would require existing instances of kernel
253 data structures to be transformed (e.g., a patch that adds a field to a global
254 data structure would require the existing data structures to change). If you
255 use Ksplice on a patch that changes data structure semantics, Ksplice will not
256 detect the problem and you could experience kernel problems as a result.
258 The to-be-applied source code patch can be specified by providing a L<patch(1)>
259 file (B<--patch=>I<PATCH_FILE>) or by providing a file extension
260 (B<--diffext=>I<EXTENSION>).
262 If a file extension is specified, then the desired source code patch will be
263 determined by comparing all of the files in the I<KERNEL_SOURCE> directory tree
264 whose names end with the extra extension I<EXTENSION> against the corresponding
265 files without the extra extension. Only the new files containing the extra
266 extension in their filenames should be modified.
268 Here is an example of using a file extension to specify a patch:
270 $ cp KERNEL_SOURCE/kernel/sys.c KERNEL_SOURCE/kernel/sys.c.prctl_fixed
271 [edit sys.c.prctl_fixed to include the desired changes]
272 $ ksplice-create --diffext=.prctl_fixed KERNEL_SOURCE
274 KERNEL_SOURCE must be a directory containing the to-be-updated kernel's
275 original source code. If your Linux distribution applies patches to the Linux
276 kernel during the kernel build process, then those patches must be applied to
277 the I<KERNEL_SOURCE> directory before invoking B<ksplice-create> on that
278 directory. B<ksplice-create> will not modify the source code in the
279 I<KERNEL_SOURCE> directory tree, but it will perform a kernel build in that
280 directory tree.
282 I<ORIG_CONFIG> can be used to specify the directory containing the
283 to-be-updated kernel's original F<.config> file and original F<System.map> file
284 (the files should have exactly those names). I<ORIG_CONFIG> defaults to
285 I<KERNEL_SOURCE>B</ksplice>.
287 The default L<gcc(1)> compiler and L<as(1)> assembler on the system should be as
288 close to the compiler and assembler originally used to build the running kernel
289 as possible. If the current compiler and linker are too different from the
290 original compiler and linker, B<ksplice-apply> will abort when applying the
291 update.
293 B<ksplice-create> outputs a L<tar(1)> file, compressed with L<gzip(1)>,
294 containing the desired Ksplice update modules. This tarball will be created in
295 the current directory, and it can be manipulated using the other Ksplice
296 utilities, such as B<ksplice-apply>.
298 The first time that B<ksplice-create> is invoked on a I<KERNEL_SOURCE>
299 directory, it must build that kernel from scratch, which is much slower than
300 the rest of the update-creation process. B<--prebuild> can be used to perform
301 this initial kernel build without providing a source code patch.
303 In order to patch a function that has previously been patched by Ksplice, the
304 user needs to ensure that the I<KERNEL_SOURCE> directory provided to Ksplice
305 contains the source for the currently running kernel, including any patches
306 that have previously been applied by Ksplice.
308 =head1 OPTIONS
310 =over 8
312 =item B<-v>, B<--verbose>
314 Causes B<ksplice-create> to print debugging messages about its progress. Using
315 multiple -v options increases the verbosity. The maximum is 2.
317 =item B<-j> I<JOBS>, B<--jobs=>I<JOBS>
319 Specifies the number of jobs to run simultaneously while performing kernel
320 builds. B<ksplice-create> also honors the environment variable
321 CONCURRENCY_LEVEL.
323 =item B<--patch-opt=>I<OPTIONS>
325 Can be used to pass options to L<patch(1)>. If this option is NOT specified, then
326 B<-p1> is passed to B<patch>. If this option is specified, then only the
327 specified options will be passed to B<patch>. This option can be repeated in
328 order to pass multiple options to B<patch>. This option is ignored when the
329 to-be-applied source code patch is specified using B<--diffext>.
331 =item B<--id=>I<ID>
333 Specifies the unique value that will be used as the identifier of the
334 Ksplice update. This identifier will, for example, appear in the name
335 of the update tarball. By default, a random 8-character ID will be
336 generated.
338 =back
340 =head1 BUGS
342 Please report bugs to <PACKAGE_BUGREPORT>.
344 =head1 SEE ALSO
346 L<ksplice-apply(8)>, L<ksplice-view(8)>, L<ksplice-undo(8)>
348 =head1 COPYRIGHT
350 Copyright (C) 2007-2008 Jeffrey Brian Arnold <jbarnold@mit.edu>
351 Copyright (C) 2008 Anders Kaseorg <andersk@mit.edu>,
352 Tim Abbott <tabbott@mit.edu>
354 This is free software and documentation. You can redistribute and/or modify it
355 under the terms of the GNU General Public License, version 2.
357 =cut