'--prebuild' is now compatible with SuSE kernels.
[lufs.git] / kernel / Linux / prepmod.in
1 #! /usr/bin/perl -w
2
3 # $Id$
4 # Check and possibly rebuild lufs kernel module for the current kernel.
5 # Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; exactly version 2 of June 1991 is required
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
21 use strict;
22 use Carp qw(cluck confess);
23 use Getopt::Long;
24 # Do not use any non-core Perl modules here.
25 # Be perl-5.0 compatible (is it needed if we require Linux kernel >= 2.4 ?)
26
27
28 my $basedir='@datadir@/lufs';
29 my $vardir='@localstatedir@/lib/lufs';
30 $basedir=~s#\$\Q{prefix}\E#'@prefix@';#ge;
31 $vardir=~s#\$\Q{prefix}\E#'@prefix@';#ge;
32 $vardir="" if $vardir=~/^@/;
33 sub srcdir { my($uname_r)=@_; $basedir."/".($uname_r lt "2.5" ? "2.4" : "2.6"); }
34 my $modbindir=$basedir."/modbin";
35 my @sources=qw(proc.c inode.c dir.c file.c symlink.c);
36
37 my $quiet;
38 my $kernel;
39 my $prebuild;
40 use vars qw($kernel_gcc_args);
41 $kernel_gcc_args="";
42
43 sub modext { my($uname_r)=@_; ($uname_r lt "2.5" ? "o" : "ko"); }
44
45 # mount(8) will util-linux/lib/env.c/sanitize_env()
46 # including the clearance our $PATH:
47 $ENV{"PATH"}=join(":",qw(/usr/local/bin /usr/bin /bin),($ENV{"PATH"} || ()));
48
49 my $lufsd_base='lufsd';
50 do { eval '$lufsd_base=~'.$_.';' if !/^@/; } for ('@program_transform_name@');
51 my $lufsd_bin;
52 if ($0 eq $lufsd_base || $0=~m#/\Q$lufsd_base\E$#) {
53         $quiet=1;
54         $lufsd_bin='@bindir@/'.$lufsd_base.'-bin';
55         $lufsd_bin=~s#\$\Q{exec_prefix}\E#'@exec_prefix@';#ge;
56         $lufsd_bin=~s#\$\Q{prefix}\E#'@prefix@';#ge;
57         }
58 else {
59         die if !GetOptions(
60                         "q|quiet",\$quiet,
61                           "kernel",\$kernel,    # it must contain "include" subdir
62                           "modbindir=s",\$modbindir,
63                           "basedir=s",\$basedir,
64                           "prebuild",\&prebuild,
65                           "kernel-gcc-args=s",\$kernel_gcc_args,
66                         );
67         }
68
69
70 sub _readfile
71 {
72 my($filename,$optional)=@_;
73
74         local $/=undef();
75         local *F;
76         open F,$filename or do { cluck "Open \"$filename\": $!" if !$optional; return ""; };
77         my $r=<F>;
78         close F or cluck "Close \"$filename\": $!";
79         return $r;
80 }
81
82 sub _writefile
83 {
84 my($filename,@content)=@_;
85
86         local *F;
87         open F,($filename=~/^[|]/ ? "" : ">").$filename or confess "rewrite \"$filename\": $!";
88         print F @content;
89         close F or confess "close \"$filename\": $!";   # Do not &cluck as it may be pipe result
90 }
91
92 sub _system
93 {
94 my(@args)=@_;
95
96         print STDERR "+ ".join(" ",@args)."\n" if !$quiet;
97         return system @args;
98 }
99
100 sub _pass
101 {
102 my($load)=@_;
103
104         my $modproberc;
105         if ($load) {
106                 do { ($modproberc=_system $_) and cluck "$_ failed - ignoring"; }
107                                 for ("/sbin/depmod -aq","/sbin/modprobe lufs");
108                 }
109         exit ($modproberc ? 1 : 0) if !$lufsd_bin;
110         do { exec $lufsd_bin,@ARGV; };
111         confess "Failed to exec '$lufsd_bin': $!";
112 }
113
114
115 my $filesystems=_readfile "/proc/filesystems";  # 'lufs' may be built-in
116 _pass if $filesystems=~/\blufs$/m;
117 my $modules=_readfile "/proc/modules";  # 'lufs' may be already loaded
118 _pass if $modules=~/^lufs\b/m;
119 _pass if !_system "/sbin/modprobe lufs 2>/dev/null";
120
121 print STDERR "Preparing LUFS kernel module - this may take several minutes...\n";       # if !$quiet;
122
123 my $proc_version=_readfile "/proc/version";
124 my $uname_r=($proc_version=~/^Linux version (\S+)/)[0] || _readfile "uname -r|";
125 chomp $uname_r;
126 confess "Failed to detect kernel version" if !$uname_r;
127 my $uname_smp=($uname_r=~s/smp$// && "smp");
128 my $uname_r_base=($uname_r=~/^([^-]+)/)[0];
129 print STDERR "Running kernel version: $uname_r (base version $uname_r_base)\n" if !$quiet;
130
131 my $lufs_o="lufs.".modext($uname_r);
132
133 my $moduledir="/lib/modules/$uname_r$uname_smp/kernel/fs/lufs";
134 print STDERR "Destination module directory: $moduledir\n" if !$quiet;
135
136 my @kernel_paths=(
137                                 "/lib/modules/$uname_r/build",
138                                 "/usr/src/kernel-headers-$uname_r",
139                                 "/usr/src/linux-$uname_r",
140                                 "/usr/src/linux-$uname_r_base",
141                                 "/usr/src/linux",
142                                 "/usr/src/kernel-source-$uname_r",      # no .config?
143                                 );
144 do { $kernel||=$_ if -d $_; } for (@kernel_paths);
145 if (!$kernel) {
146         print STDERR "Failed to find kernel headers for $uname_r\n" if !$kernel && !$quiet;
147         }
148 else {
149         # We need /usr/include/lufs/* for standard package compilation.
150         # There is no sense to make separate 'devel' package.
151         for ("$vardir/$lufs_o") {
152                 next if !$vardir;
153                 # Create the 'lufs.o' in our /var/lib directory and only link it
154                 # to prevent using obsolete modules after upgrading 'lufs' package.
155                 # depmod(1) will take the larget symlink name - we must create directory for  it.
156                 if (build($kernel,$uname_r,$_)) {
157                         do { cluck "Failed to symlink $_"; next; }
158                                         if _system "/bin/rm -rf $moduledir; /bin/mkdir -p $moduledir; /bin/ln -s $_ $moduledir/$lufs_o";
159                         _pass 1;
160                         }
161                 }
162         }
163
164 local $_;
165 for (<$modbindir/*-$uname_r*/$lufs_o>,<$modbindir/*-${uname_r_base}*/$lufs_o>,<$modbindir/*/$lufs_o>) {
166         # Do not: /sbin/insmod -o lufs -p $_ 2>/dev/null
167         # as 2.6 insmod has no options at all.
168         next if _system "/sbin/rmmod lufs 2>/dev/null; /sbin/insmod $_ 2>/dev/null";
169         do { cluck "Failed to symlink $_"; next; }
170                         if _system "/bin/rm -rf $moduledir; /bin/mkdir -p $moduledir; /bin/ln -s $_ $moduledir/$lufs_o";
171         # 0 as already insmod(8)ed.
172         _pass 0;
173         }
174 confess "lufs module not loaded: Try running $basedir/prepmod to see more." if $quiet;
175 confess "Failed to prepare $lufs_o module for your Linux kernel $uname_r.\n"
176                 .($kernel ? "Detected Linux kernel sources \"$kernel\" do not appear to be valid.\n"
177                                 : "No Linux kernel sources for your running kernel were found.\n")
178                 ."Please install kernel-source-x.y.z.i386.rpm or kernel-headers_x.y.z_i386.deb.\n"
179                 ."The following directory paths were search (first existing directory used):\n"
180                 .join("",map("\t\t$_\n",@kernel_paths));
181
182
183 sub build_gcc
184 {
185 my($kernel,$uname_r,$destmodule)=@_;
186
187         print STDERR "Using kernel sources: $kernel\n" if !$quiet;
188         confess "Kernel sources $kernel do not contain 'include' subdirectory" if ! -d $kernel."/include";
189
190         my $kdebug="";
191         do { $kdebug=$_ if !/^@/; } for ('@KDEBUG_FLAGS@');
192         my $cmdline="/usr/bin/gcc"
193                         .($quiet ? " 2>/dev/null" : "")
194                         ." -Wall -Wstrict-prototypes -Wno-trigraphs"
195                         ." -O2 -fno-strict-aliasing -fno-common -fomit-frame-pointer -pipe -mpreferred-stack-boundary=2"
196                         #." -march=i586"
197                         ." -D__KERNEL__ -DMODULE -DLINUX -DKBUILD_MODNAME=lufs"
198                         ." ".$kdebug
199                         ." ".$kernel_gcc_args
200                         ." -I$kernel/include"
201                         ." -I$kernel/include/asm-i386/mach-default";    # gcc should not care if this 2.6 dir does not exist
202
203         my $config=_readfile "$kernel/.config","optional";
204         my $autoconf=_readfile "$kernel/include/linux/autoconf.h","optional";
205         if (0
206                         || $config=~/^CONFIG_MODVERSIONS=y\b/m
207                         || $autoconf=~/^#define CONFIG_MODVERSIONS\b/m
208                         || (!$config && !$autoconf)) {  # assume modversions if not known
209                 $cmdline.=" -DMODVERSIONS";
210                 $cmdline.=" -include $kernel/include/linux/version.h"
211                                 if -f "$kernel/include/linux/version.h";
212                 $cmdline.=" -include $kernel/include/linux/modversions.h";
213                 }
214
215         my @objects=map({ my $o=$_; $o=~s/[.]c$/.o/; $o; } @sources);
216         return !_system "set -e; /bin/mkdir -p `dirname $destmodule`; /bin/rm -f $destmodule;"
217                         .(!$vardir ? "" : " cd $vardir;")
218                         ." $cmdline -c ".join(" ",map({ srcdir($uname_r)."/".$_; } @sources)).";"
219                         ." /usr/bin/ld -r -o $destmodule ".join(" ",@objects).";"
220                         ." /bin/rm -f ".join(" ",@objects);
221 }
222
223
224 sub build_make
225 {
226 my($kernel,$uname_r,$destmodule,%args)=@_;
227
228         print STDERR "Using kernel sources: $kernel\n" if !$quiet;
229         # 'Rules.make' not present in 2.6+ kernels
230         do { confess "Kernel sources $kernel do not contain '$_' file" if ! -f $_; } for ($kernel."/arch/i386/Makefile");
231
232         my $kdebug="";
233         do { $kdebug=$_ if !/^@/; } for ('@KDEBUG_FLAGS@');
234         my $predir=srcdir $uname_r;
235         $predir=$ENV{"PWD"}."/$predir" if $predir!~m#^/#;
236         # Workaround a bug in at least Red Hat 2.4.18-18.8.0
237         if (-f "$kernel/include/linux/version.h") {
238                 for ("$kernel/include/linux/modversions.h") {
239                         _system "cp -p $_ $_-orig" if ! -f "$_-orig";
240                         _writefile $_,"#include <linux/version.h>\t/* lufs */\n"._readfile $_;
241                         }
242                 }
243         my $cmdline="make -C $kernel"
244                         ." SUBDIRS=\"$predir\" modules"
245                         ." EXTRA_CFLAGS=\"$kernel_gcc_args\""
246                         .($quiet ? ' &>/dev/null' : '');
247         my @objects=map({ my $o=$_; $o=~s/[.]c$/.o/; $o; } @sources,"lufs.mod.c","lufs.c");
248         $cmdline="set -e; /bin/mkdir -p `dirname $destmodule`; /bin/rm -f $destmodule;"
249                         ." $cmdline;"
250                         ." /bin/mv -f $predir/lufs.".modext($uname_r)." $destmodule;"
251                         ." /bin/rm -f ".join(" ",map(("$predir/$_","$predir/.$_.flags","$predir/.$_.cmd"),@objects),"$predir/lufs.mod.c","$predir/.lufs.ko.cmd").";";
252         my $r=!_system $cmdline;
253         return $r if $r;
254         # Rebuild existing '$kernel/tmp_include_depends' or '$kernel/.depend'
255         # as it may contain non-existing pathnames:
256         _system "make -C $kernel dep"
257                                 .($quiet ? ' &>/dev/null' : '');
258         return !_system $cmdline;
259 }
260
261
262 sub build
263 {
264 my($kernel,$uname_r,$destmodule,%args)=@_;
265
266         # Debian uname(1) does not support '-p'.
267         my $arch=_readfile "uname -p 2>/dev/null || true|"
268                         || _readfile "uname -m|";
269         chomp $arch;
270         my $single_config=-f "$kernel/.config";
271         if (!$single_config) {
272                 my $spec_config="$kernel/configs/kernel-$uname_r_base-$arch".($uname_smp ? "-smp" : "").".config";
273                 _system "ln -s $spec_config $kernel/.config"
274                                 if -f $spec_config;
275                 }
276         my $r;
277         # 'Rules.make' not present in 2.6+ kernels
278         if (-f $kernel."/arch/i386/Makefile" && -f "$kernel/.config")
279                 { $r=build_make $kernel,$uname_r,$destmodule,%args; }
280         else
281                 { $r=build_gcc $kernel,$uname_r,$destmodule,%args; }
282         _system "rm -f $kernel/.config"
283                         if !$single_config;
284         return $r;
285 }
286
287
288 sub prebuild_kernel
289 {
290 my($dir,$vendor,$uname_r,%args)=@_;
291
292         confess "Unrecognized vendor for dir: $dir" if !$vendor;
293         confess "Unrecognized uname_r for dir: $dir" if !$uname_r;
294         confess "Failed to build $dir" if !build $dir,$uname_r,$modbindir."/lufs-$vendor-$uname_r.".modext($uname_r),%args;
295 }
296
297
298 sub prebuild_rpm
299 {
300 my($rpm)=@_;
301
302         my $vendor=_readfile "rpm 2>/dev/null --qf '%{VENDOR}' -qp '$rpm' |";
303         $vendor=~s/,.*//;
304         $vendor=~tr/ //d;
305         my $uname_r;
306         $uname_r||=($rpm=~m#/kernel-source-([^-]+-[^-]+)[.][^.]+[.]rpm$#)[0];
307         my $uname_r_base=($uname_r=~/^([^-]+)/)[0];
308         # Do not: rpm -i ...
309         # as SuSE kernels require 1.5GB of VM during later 'rpm -e' in Shrike.
310         _system "rm -rf modbin-tmp;mkdir modbin-tmp;rpm2cpio '$rpm'|(cd modbin-tmp;cpio -id)"
311                         and confess "RPM extraction of $rpm";
312         my $dir;
313         # Do not: -f $_
314         # as we are satisfied with symlink
315         # (such as base 'linux' used for some SuSE kernels with weird directory name).
316         do { $dir||=$_ if -e $_; } for ($ENV{"PWD"}."/modbin-tmp/usr/src/linux-$uname_r-include");      # new SuSE kernels
317         do { $dir||=$_ if -e $_; } for ($ENV{"PWD"}."/modbin-tmp/usr/src/linux-$uname_r");
318         do { $dir||=$_ if -e $_; } for ($ENV{"PWD"}."/modbin-tmp/usr/src/linux-$uname_r_base"); # older RedHat kernels
319         do { $dir||=$_ if -e $_; } for ($ENV{"PWD"}."/modbin-tmp/usr/src/linux");
320         $dir or confess "Kernel source tree not found in: $rpm";
321         _system "chmod -R u+w $dir" and confess "Make kernel source writable in $dir";
322         _system "cp -p $dir/include/linux/rhconfig.h $dir/include/linux/rhconfig.h-orig"
323                                 if -f "$dir/include/linux/rhconfig.h";
324         if (-d "$dir/default" && -d "$dir/smp") {
325                 # new SuSE kernels
326                 for my $arch_d (glob "$dir/*") {
327                         (my $arch=$arch_d)=~s#^.*/##;
328                         prebuild_kernel $arch_d,$vendor,$uname_r.".".$arch;
329                         }
330                 }
331         else {
332                 my $built_total=0;
333                 # RedHat/Mandrake/old-SuSE kernels
334                 for my $smp ("","smp") {
335                         my @archs=qw(i386 i586 i686 athlon);
336                         for my $arch (@archs) {
337                                 my %boot=(
338                                                 "__BOOT_KERNEL_SMP"=>$smp,
339                                                 map({ ("__MODULE_KERNEL_$_"=>($arch eq $_)); } @archs),
340                                                 "__BOOT_KERNEL_BOOT"=>0,
341                                                 "__BOOT_KERNEL_BOOTSMP"=>0,
342                                                 "__BOOT_KERNEL_ENTERPRISE"=>0,
343                                                 "__BOOT_KERNEL_BIGMEM"=>0,
344                                                 "__BOOT_KERNEL_DEBUG"=>0,
345                                                 );
346                                 if (-f "$dir/include/linux/rhconfig.h") {
347                                         my $defines="";
348                                         for (keys(%boot)) {
349                                                 $defines.="+#define $_ ".($boot{$_} ? 1 : 0)."\n";
350                                                 }
351                                         _system "cp -p -f $dir/include/linux/rhconfig.h-orig $dir/include/linux/rhconfig.h";
352                                         _writefile "| patch $dir/include/linux/rhconfig.h",<<"RHCONFIG_H_NOBOOT_EOF";
353 --- ./include/linux/rhconfig.h-orig     Thu Aug 21 14:50:16 2003
354 +++ ./include/linux/rhconfig.h  Thu Aug 21 14:59:22 2003
355 \@\@ -10,7 +10,@{[ 7+keys(%boot) ]} \@\@
356   * /boot/kernel.h - initscripts should create it for us
357   */
358  
359 -#include "/boot/kernel.h"
360 +/* lufs: #include "/boot/kernel.h" */
361 $defines
362  #if defined(__BOOT_KERNEL_SMP) && (__BOOT_KERNEL_SMP == 1)
363  #define __module__smp
364 RHCONFIG_H_NOBOOT_EOF
365                                         }
366                                 # Mandrake packages have no configs/ and they have just that '.config' file.
367                                 my $single_config=-f "$dir/.config";
368                                 if (!$single_config) {
369                                         my $spec_config="$dir/configs/kernel-$uname_r_base-$arch".($smp ? "-smp" : "").".config";
370                                         next if ! -f $spec_config;
371                                         _system "ln -s $spec_config $dir/.config";
372                                         }
373                                 prebuild_kernel $dir,$vendor,$uname_r.$smp.".".$arch;
374                                 $built_total++;
375                                 _system "rm -f $dir/.config"
376                                                 if !$single_config;
377                                 }
378                         }
379                 # Old SuSE kernels have no '.config' or 'configs/'.
380                 confess "Not a buildable kernel: $rpm" if !$built_total;
381                 }
382         _system "rm -rf modbin-tmp" and confess "Remove of extracted kernel-source-$uname_r";
383 }
384
385 sub prebuild_dir
386 {
387 my($dir)=@_;
388
389         my $vendor;
390         chomp (my $debian=_readfile "/etc/debian_version","optional");
391         $debian=~tr#/#_#;
392         $vendor="Debian".$debian if $debian && $dir=~m#^/usr/src/#;
393         my $uname_r;
394         $uname_r||=($dir=~m#/kernel-headers-([^/]+)/*$#)[0];
395         $uname_r||=($dir=~m#/linux-([^/]+)/*$#)[0];
396         $uname_r||=($dir=~m#^/lib/modules/([^/]+)/build/*$#)[0];
397         prebuild_kernel $dir,$vendor,$uname_r;
398 }
399
400 sub prebuild
401 {
402         $vardir=undef();        # we may not yet have /var/lib/lufs
403         for (@ARGV) {
404                 do { prebuild_rpm $_; next; } if /[.]rpm$/;
405                 do { prebuild_dir $_; next; } if -d $_;
406                 confess "Unrecognized kernel: $_";
407                 }
408         exit 0;
409 }