+Workaround SuSE kernel 2.4.21-144 containing precompiled 'split-include'.
[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 "find $kernel -name .depend|xargs rm -f;"
257                         # Red Hat 2.4.18-14 contains precompiled .so-dependent 'mkdep'
258                         ." rm -f $kernel/scripts/mkdep;"
259                         # SuSE 2.4.21-144 contains precompiled .so-dependent 'split-include'
260                         ." rm -f $kernel/scripts/split-include;"
261                         ." make -C $kernel dep"
262                                     .($quiet ? ' &>/dev/null' : '');
263         return !_system $cmdline;
264 }
265
266
267 sub build
268 {
269 my($kernel,$uname_r,$destmodule,%args)=@_;
270
271         # Debian uname(1) does not support '-p'.
272         my $arch=_readfile "uname -p 2>/dev/null || true|"
273                         || _readfile "uname -m|";
274         chomp $arch;
275         my $single_config=-f "$kernel/.config";
276         if (!$single_config) {
277                 my $spec_config="$kernel/configs/kernel-$uname_r_base-$arch".($uname_smp ? "-smp" : "").".config";
278                 _system "ln -s $spec_config $kernel/.config"
279                                 if -f $spec_config;
280                 }
281         my $r;
282         # 'Rules.make' not present in 2.6+ kernels
283         if (-f $kernel."/arch/i386/Makefile" && -f "$kernel/.config")
284                 { $r=build_make $kernel,$uname_r,$destmodule,%args; }
285         else
286                 { $r=build_gcc $kernel,$uname_r,$destmodule,%args; }
287         _system "rm -f $kernel/.config"
288                         if !$single_config;
289         return $r;
290 }
291
292
293 sub prebuild_kernel
294 {
295 my($dir,$vendor,$uname_r,%args)=@_;
296
297         confess "Unrecognized vendor for dir: $dir" if !$vendor;
298         confess "Unrecognized uname_r for dir: $dir" if !$uname_r;
299         confess "Failed to build $dir" if !build $dir,$uname_r,$modbindir."/lufs-$vendor-$uname_r.".modext($uname_r),%args;
300 }
301
302
303 sub prebuild_rpm
304 {
305 my($rpm)=@_;
306
307         my $vendor=_readfile "rpm 2>/dev/null --qf '%{VENDOR}' -qp '$rpm' |";
308         $vendor="unknown" if $vendor eq "(none)";       # TurboLinux 2.4.18-14
309         $vendor=~s/[,<].*//;
310         $vendor=~tr/ //d;
311         my $uname_r;
312         $uname_r||=($rpm=~m#/kernel-source-([^-]+-[^-]+)[.][^.]+[.]rpm$#)[0];
313         my $uname_r_base=($uname_r=~/^([^-]+)/)[0];
314         # Do not: rpm -i ...
315         # as SuSE kernels require 1.5GB of VM during later 'rpm -e' in Shrike.
316         _system "rm -rf modbin-tmp;mkdir modbin-tmp;rpm2cpio '$rpm'|(cd modbin-tmp;cpio -id)"
317                         and confess "RPM extraction of $rpm";
318         my $dir;
319         # Do not: -f $_
320         # as we are satisfied with symlink
321         # (such as base 'linux' used for some SuSE kernels with weird directory name).
322         do { $dir||=$_ if -e $_; } for ($ENV{"PWD"}."/modbin-tmp/usr/src/linux-$uname_r-include");      # new SuSE kernels
323         do { $dir||=$_ if -e $_; } for ($ENV{"PWD"}."/modbin-tmp/usr/src/linux-$uname_r");
324         do { $dir||=$_ if -e $_; } for ($ENV{"PWD"}."/modbin-tmp/usr/src/linux-$uname_r_base"); # older RedHat kernels
325         do { $dir||=$_ if -e $_; } for ($ENV{"PWD"}."/modbin-tmp/usr/src/linux");
326         $dir or confess "Kernel source tree not found in: $rpm";
327         _system "chmod -R u+w $dir" and confess "Make kernel source writable in $dir";
328         _system "cp -p $dir/include/linux/rhconfig.h $dir/include/linux/rhconfig.h-orig"
329                                 if -f "$dir/include/linux/rhconfig.h";
330         my $built_total=0;
331         if (-d "$dir/default" && -d "$dir/smp") {
332                 # new SuSE kernels
333                 for my $arch_d (glob "$dir/*") {
334                         (my $arch=$arch_d)=~s#^.*/##;
335                         prebuild_kernel $arch_d,$vendor,$uname_r.".".$arch;
336                         $built_total++;
337                         }
338                 }
339         elsif (-e "$dir/SetupKernelSource.sh") {
340                 # TurboLinux
341                 if (! -e "$dir/include") {
342                         (my $kheaders=$rpm)=~s#/kernel-source-([^/]*)$#/kernel-headers-$1#
343                                         or confess "Invalid RPM basename of: $rpm";
344                         _system "rpm2cpio '$kheaders'|(cd modbin-tmp;cpio -id)"
345                                         and confess "RPM kheaders extraction of $rpm";
346                         }
347                 for my $TYPE ("","smp","smp64G") {
348                         for my $ARCH (qw(i386 i586 i686 athlon)) {
349                                 # Use even 'A-Z' for the 'release' for TurboLinux 2.4.9-3D
350                                 next if !glob "$dir/configs/kernel-*-*[0-9A-Z]$TYPE-$ARCH.config";
351                                 _system "set -ex; cd '$dir';"
352                                                                 # Forbid 'cd /usr/src/linux' in './SetupKernelSource.sh'
353                                                                 # but permit later 'cd's by make(1) subprocesses.
354                                                                 # Therefore do not 'export -f cd;' but 'source ./SetupKernelSource.sh'.
355                                                                 .' function cd { :; };' # Do not: 'export -f cd;'
356 #                                                               .' function make {(set -ex;unset make; make "$@"; make symlinks; );}; export -f make;'
357                                                                 # Permit 'errors' for 'grep smp' in TurboLinux 2.4.5-0.5
358                                                                 ." set +e; source ./SetupKernelSource.sh $ARCH$TYPE;"
359                                                 and confess "SetupKernelSource.sh $ARCH$TYPE in $dir";
360                                 prebuild_kernel $dir,$vendor,$uname_r.".".$ARCH.$TYPE;
361                                 $built_total++;
362                                 }
363                         }
364                 }
365         else {
366                 # RedHat/Mandrake/old-SuSE kernels
367                 for my $smp ("","smp") {
368                         my @archs=qw(i386 i586 i686 athlon);
369                         for my $arch (@archs) {
370                                 my %boot=(
371                                                 "__BOOT_KERNEL_SMP"=>$smp,
372                                                 map({ ("__MODULE_KERNEL_$_"=>($arch eq $_)); } @archs),
373                                                 "__BOOT_KERNEL_BOOT"=>0,
374                                                 "__BOOT_KERNEL_BOOTSMP"=>0,
375                                                 "__BOOT_KERNEL_ENTERPRISE"=>0,
376                                                 "__BOOT_KERNEL_BIGMEM"=>0,
377                                                 "__BOOT_KERNEL_DEBUG"=>0,
378                                                 );
379                                 if (-f "$dir/include/linux/rhconfig.h") {
380                                         my $defines="";
381                                         for (keys(%boot)) {
382                                                 $defines.="+#define $_ ".($boot{$_} ? 1 : 0)."\n";
383                                                 }
384                                         _system "cp -p -f $dir/include/linux/rhconfig.h-orig $dir/include/linux/rhconfig.h";
385                                         _writefile "| patch $dir/include/linux/rhconfig.h",<<"RHCONFIG_H_NOBOOT_EOF";
386 --- ./include/linux/rhconfig.h-orig     Thu Aug 21 14:50:16 2003
387 +++ ./include/linux/rhconfig.h  Thu Aug 21 14:59:22 2003
388 \@\@ -10,7 +10,@{[ 7+keys(%boot) ]} \@\@
389   * /boot/kernel.h - initscripts should create it for us
390   */
391  
392 -#include "/boot/kernel.h"
393 +/* lufs: #include "/boot/kernel.h" */
394 $defines
395  #if defined(__BOOT_KERNEL_SMP) && (__BOOT_KERNEL_SMP == 1)
396  #define __module__smp
397 RHCONFIG_H_NOBOOT_EOF
398                                         }
399                                 # Mandrake packages have no configs/ and they have just that '.config' file.
400                                 my $single_config=-f "$dir/.config";
401                                 if (!$single_config) {
402                                         my $spec_config="$dir/configs/kernel-$uname_r_base-$arch".($smp ? "-smp" : "").".config";
403                                         next if ! -f $spec_config;
404                                         _system "ln -s $spec_config $dir/.config";
405                                         }
406                                 prebuild_kernel $dir,$vendor,$uname_r.$smp.".".$arch;
407                                 $built_total++;
408                                 _system "rm -f $dir/.config"
409                                                 if !$single_config;
410                                 }
411                         }
412                 }
413         # Old SuSE kernels have no '.config' or 'configs/'.
414         confess "Not a buildable kernel: $rpm" if !$built_total;
415         _system "rm -rf modbin-tmp" and confess "Remove of extracted kernel-source-$uname_r";
416 }
417
418 sub prebuild_dir
419 {
420 my($dir)=@_;
421
422         my $vendor;
423         chomp (my $debian=_readfile "/etc/debian_version","optional");
424         $debian=~tr#/#_#;
425         $vendor="Debian".$debian if $debian && $dir=~m#^/usr/src/#;
426         my $uname_r;
427         $uname_r||=($dir=~m#/kernel-headers-([^/]+)/*$#)[0];
428         $uname_r||=($dir=~m#/linux-([^/]+)/*$#)[0];
429         $uname_r||=($dir=~m#^/lib/modules/([^/]+)/build/*$#)[0];
430         prebuild_kernel $dir,$vendor,$uname_r;
431 }
432
433 sub prebuild
434 {
435         $vardir=undef();        # we may not yet have /var/lib/lufs
436         for (@ARGV) {
437                 do { prebuild_rpm $_; next; } if /[.]rpm$/;
438                 do { prebuild_dir $_; next; } if -d $_;
439                 confess "Unrecognized kernel: $_";
440                 }
441         exit 0;
442 }