'prepmod'-wrap 'lufsd' instead of 'lufsmnt' as 'prepmod' fail is safe this way.
[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 my $lufsd_bin;
46 if ($0 eq "lufsd" || $0=~m#/lufsd$#) {
47         $quiet=1;
48         $lufsd_bin='@bindir@/lufsd-bin';
49         $lufsd_bin=~s#\$\Q{exec_prefix}\E#'@exec_prefix@';#ge;
50         $lufsd_bin=~s#\$\Q{prefix}\E#'@prefix@';#ge;
51         }
52 else {
53         die if !GetOptions(
54                         "q|quiet",\$quiet,
55                           "kernel",\$kernel,    # it must contain "include" subdir
56                           "modbindir=s",\$modbindir,
57                           "basedir=s",\$basedir,
58                           "prebuild",\&prebuild,
59                           "kernel-gcc-args=s",\$kernel_gcc_args,
60                         );
61         }
62
63
64 sub _readfile
65 {
66 my($filename,$optional)=@_;
67
68         local $/=undef();
69         local *F;
70         open F,$filename or do { cluck "Open \"$filename\": $!" if !$optional; return ""; };
71         my $r=<F>;
72         close F or cluck "Close \"$filename\": $!";
73         return $r;
74 }
75
76 sub _writefile
77 {
78 my($filename,@content)=@_;
79
80         local *F;
81         open F,($filename=~/^[|]/ ? "" : ">").$filename or confess "rewrite \"$filename\": $!";
82         print F @content;
83         close F or confess "close \"$filename\": $!";   # Do not &cluck as it may be pipe result
84 }
85
86 sub _system
87 {
88 my(@args)=@_;
89
90         print STDERR "+ ".join(" ",@args)."\n" if !$quiet;
91         return system @args;
92 }
93
94 sub _pass
95 {
96 my($load)=@_;
97
98         my $modproberc;
99         if ($load) {
100                 do { ($modproberc=_system $_) and cluck "$_ failed - ignoring"; }
101                                 for ("/sbin/depmod -aq","/sbin/modprobe lufs");
102                 }
103         exit ($modproberc ? 1 : 0) if !$lufsd_bin;
104         do { exec $lufsd_bin,@ARGV; };
105         confess "Failed to exec '$lufsd_bin': $!";
106 }
107
108
109 my $filesystems=_readfile "/proc/filesystems";  # 'lufs' may be built-in
110 _pass if $filesystems=~/\blufs$/m;
111 my $modules=_readfile "/proc/modules";  # 'lufs' may be already loaded
112 _pass if $modules=~/^lufs\b/m;
113 _pass if !_system "/sbin/modprobe lufs 2>/dev/null";
114
115 my $proc_version=_readfile "/proc/version";
116 my $uname_r=($proc_version=~/^Linux version (\S+)/)[0] || _readfile "uname -r|";
117 chomp $uname_r;
118 confess "Failed to detect kernel version" if !$uname_r;
119 my $uname_smp=($uname_r=~s/smp$// && "smp");
120 my $uname_r_base=($uname_r=~/^([^-]+)/)[0];
121 print STDERR "Running kernel version: $uname_r (base version $uname_r_base)\n" if !$quiet;
122
123 my $lufs_o="lufs.".modext($uname_r);
124
125 my $moduledir="/lib/modules/$uname_r$uname_smp/kernel/fs/lufs";
126 print STDERR "Destination module directory: $moduledir\n" if !$quiet;
127
128 my @kernel_paths=(
129                                 "/lib/modules/$uname_r/build",
130                                 "/usr/src/kernel-headers-$uname_r",
131                                 "/usr/src/linux-$uname_r",
132                                 "/usr/src/linux-$uname_r_base",
133                                 "/usr/src/linux",
134                                 "/usr/src/kernel-source-$uname_r",      # no .config?
135                                 );
136 do { $kernel||=$_ if -d $_; } for (@kernel_paths);
137 if (!$kernel) {
138         print STDERR "Failed to find kernel headers for $uname_r\n" if !$kernel && !$quiet;
139         }
140 else {
141         # We need /usr/include/lufs/* for standard package compilation.
142         # There is no sense to make separate 'devel' package.
143         for ("$vardir/$lufs_o") {
144                 next if !$vardir;
145                 # Create the 'lufs.o' in our /var/lib directory and only link it
146                 # to prevent using obsolete modules after upgrading 'lufs' package.
147                 # depmod(1) will take the larget symlink name - we must create directory for  it.
148                 if (build($kernel,$uname_r,$_)) {
149                         do { cluck "Failed to symlink $_"; next; }
150                                         if _system "/bin/rm -rf $moduledir; /bin/mkdir -p $moduledir; /bin/ln -s $_ $moduledir/$lufs_o";
151                         _pass 1;
152                         }
153                 }
154         }
155
156 local $_;
157 for (<$modbindir/*-$uname_r*/$lufs_o>,<$modbindir/*-${uname_r_base}*/$lufs_o>,<$modbindir/*/$lufs_o>) {
158         # Do not: /sbin/insmod -o lufs -p $_ 2>/dev/null
159         # as 2.6 insmod has no options at all.
160         next if _system "/sbin/rmmod lufs 2>/dev/null; /sbin/insmod $_ 2>/dev/null";
161         do { cluck "Failed to symlink $_"; next; }
162                         if _system "/bin/rm -rf $moduledir; /bin/mkdir -p $moduledir; /bin/ln -s $_ $moduledir/$lufs_o";
163         # 0 as already insmod(8)ed.
164         _pass 0;
165         }
166 confess "lufs module not loaded: Try running $basedir/prepmod to see more." if $quiet;
167 confess "Failed to prepare $lufs_o module for your Linux kernel $uname_r.\n"
168                 .($kernel ? "Detected Linux kernel sources \"$kernel\" do not appear to be valid.\n"
169                                 : "No Linux kernel sources for your running kernel were found.\n")
170                 ."Please install kernel-source-x.y.z.i386.rpm or kernel-headers_x.y.z_i386.deb.\n"
171                 ."The following directory paths were search (first existing directory used):\n"
172                 .join("",map("\t\t$_\n",@kernel_paths));
173
174
175 sub build_gcc
176 {
177 my($kernel,$uname_r,$destmodule)=@_;
178
179         print STDERR "Using kernel sources: $kernel\n" if !$quiet;
180         confess "Kernel sources $kernel do not contain 'include' subdirectory" if ! -d $kernel."/include";
181
182         my $kdebug="";
183         do { $kdebug=$_ if !/^@/; } for ('@KDEBUG_FLAGS@');
184         my $cmdline="/usr/bin/gcc"
185                         .($quiet ? " 2>/dev/null" : "")
186                         ." -Wall -Wstrict-prototypes -Wno-trigraphs"
187                         ." -O2 -fno-strict-aliasing -fno-common -fomit-frame-pointer -pipe -mpreferred-stack-boundary=2"
188                         #." -march=i586"
189                         ." -D__KERNEL__ -DMODULE -DLINUX -DKBUILD_MODNAME=lufs"
190                         ." ".$kdebug
191                         ." ".$kernel_gcc_args
192                         ." -I$kernel/include"
193                         ." -I$kernel/include/asm-i386/mach-default";    # gcc should not care if this 2.6 dir does not exist
194
195         my $config=_readfile "$kernel/.config","optional";
196         my $autoconf=_readfile "$kernel/include/linux/autoconf.h","optional";
197         if (0
198                         || $config=~/^CONFIG_MODVERSIONS=y\b/m
199                         || $autoconf=~/^#define CONFIG_MODVERSIONS\b/m
200                         || (!$config && !$autoconf)) {  # assume modversions if not known
201                 $cmdline.=" -DMODVERSIONS";
202                 $cmdline.=" -include $kernel/include/linux/version.h"
203                                 if -f "$kernel/include/linux/version.h";
204                 $cmdline.=" -include $kernel/include/linux/modversions.h";
205                 }
206
207         my @objects=map({ my $o=$_; $o=~s/[.]c$/.o/; $o; } @sources);
208         return !_system "set -e; /bin/mkdir -p `dirname $destmodule`; /bin/rm -f $destmodule;"
209                         .(!$vardir ? "" : " cd $vardir;")
210                         ." $cmdline -c ".join(" ",map({ srcdir($uname_r)."/".$_; } @sources)).";"
211                         ." /usr/bin/ld -r -o $destmodule ".join(" ",@objects).";"
212                         ." /bin/rm -f ".join(" ",@objects);
213 }
214
215
216 sub build_make
217 {
218 my($kernel,$uname_r,$destmodule,%args)=@_;
219
220         print STDERR "Using kernel sources: $kernel\n" if !$quiet;
221         # 'Rules.make' not present in 2.6+ kernels
222         do { confess "Kernel sources $kernel do not contain '$_' file" if ! -f $_; } for ($kernel."/arch/i386/Makefile");
223
224         my $kdebug="";
225         do { $kdebug=$_ if !/^@/; } for ('@KDEBUG_FLAGS@');
226         my $predir=srcdir $uname_r;
227         $predir=$ENV{"PWD"}."/$predir" if $predir!~m#^/#;
228         # Workaround a bug in at least Red Hat 2.4.18-18.8.0
229         if (-f "$kernel/include/linux/version.h") {
230                 for ("$kernel/include/linux/modversions.h") {
231                         _system "cp -p $_ $_-orig" if ! -f "$_-orig";
232                         _writefile $_,"#include <linux/version.h>\t/* lufs */\n"._readfile $_;
233                         }
234                 }
235         my $cmdline="make -C $kernel"
236                         ." SUBDIRS=\"$predir\" modules"
237                         ." EXTRA_CFLAGS=\"$kernel_gcc_args\""
238                         .($quiet ? ' &>/dev/null' : '');
239         my @objects=map({ my $o=$_; $o=~s/[.]c$/.o/; $o; } @sources,"lufs.mod.c","lufs.c");
240         $cmdline="set -e; /bin/mkdir -p `dirname $destmodule`; /bin/rm -f $destmodule;"
241                         ." $cmdline;"
242                         ." /bin/mv -f $predir/lufs.".modext($uname_r)." $destmodule;"
243                         ." /bin/rm -f ".join(" ",map(("$predir/$_","$predir/.$_.flags","$predir/.$_.cmd"),@objects),"$predir/lufs.mod.c","$predir/.lufs.ko.cmd").";";
244         my $r=!_system $cmdline;
245         return $r if $r;
246         # Rebuild existing '$kernel/tmp_include_depends' or '$kernel/.depend'
247         # as it may contain non-existing pathnames:
248         _system "make -C $kernel dep"
249                                 .($quiet ? ' &>/dev/null' : '');
250         return !_system $cmdline;
251 }
252
253
254 sub build
255 {
256 my($kernel,$uname_r,$destmodule,%args)=@_;
257
258         # Debian uname(1) does not support '-p'.
259         my $arch=_readfile "uname -p 2>/dev/null || true|"
260                         || _readfile "uname -m|";
261         chomp $arch;
262         my $single_config=-f "$kernel/.config";
263         if (!$single_config) {
264                 my $spec_config="$kernel/configs/kernel-$uname_r_base-$arch".($uname_smp ? "-smp" : "").".config";
265                 _system "ln -s $spec_config $kernel/.config"
266                                 if -f $spec_config;
267                 }
268         my $r;
269         # 'Rules.make' not present in 2.6+ kernels
270         if (-f $kernel."/arch/i386/Makefile" && -f "$kernel/.config")
271                 { $r=build_make $kernel,$uname_r,$destmodule,%args; }
272         else
273                 { $r=build_gcc $kernel,$uname_r,$destmodule,%args; }
274         _system "rm -f $kernel/.config"
275                         if !$single_config;
276         return $r;
277 }
278
279
280 sub prebuild_kernel
281 {
282 my($dir,$vendor,$uname_r,%args)=@_;
283
284         confess "Unrecognized vendor for dir: $dir" if !$vendor;
285         confess "Unrecognized uname_r for dir: $dir" if !$uname_r;
286         confess "Failed to build $dir" if !build $dir,$uname_r,$modbindir."/lufs-$vendor-$uname_r.".modext($uname_r),%args;
287 }
288
289
290 sub prebuild_rpm
291 {
292 my($rpm)=@_;
293
294         my $vendor=_readfile "rpm 2>/dev/null --qf '%{VENDOR}' -qp '$rpm' |";
295         $vendor=~s/,.*//;
296         $vendor=~tr/ //d;
297         my $uname_r;
298         $uname_r||=($rpm=~m#/kernel-source-([^-]+-[^-]+)[.][^.]+[.]rpm$#)[0];
299         my $uname_r_base=($uname_r=~/^([^-]+)/)[0];
300         my $dir;
301         do { $dir||=$_ if -d $_; } for ("/usr/src/linux-$uname_r");
302         do { $dir||=$_ if -d $_; } for ("/usr/src/linux-$uname_r_base");        # older RedHat kernels
303         confess "Kernel sources already found in $dir" if $dir;
304         # '--nodeps' to prevent: error: Failed dependencies: perl-base is needed by *mdk
305         _system "rpm -i --nodeps '$rpm'" and confess "RPM installation of $rpm";
306         do { $dir||=$_ if -d $_; } for ("/usr/src/linux-$uname_r");
307         do { $dir||=$_ if -d $_; } for ("/usr/src/linux-$uname_r_base");        # older RedHat kernels
308         $dir or confess "Kernel source tree not found in: $rpm";
309         _system "cp -p $dir/include/linux/rhconfig.h $dir/include/linux/rhconfig.h-orig"
310                                 if -f "$dir/include/linux/rhconfig.h";
311         for my $smp ("","smp") {
312                 my @archs=qw(i386 i586 i686 athlon);
313                 for my $arch (@archs) {
314                         my %boot=(
315                                         "__BOOT_KERNEL_SMP"=>$smp,
316                                         map({ ("__MODULE_KERNEL_$_"=>($arch eq $_)); } @archs),
317                                         "__BOOT_KERNEL_BOOT"=>0,
318                                         "__BOOT_KERNEL_BOOTSMP"=>0,
319                                         "__BOOT_KERNEL_ENTERPRISE"=>0,
320                                         "__BOOT_KERNEL_BIGMEM"=>0,
321                                         "__BOOT_KERNEL_DEBUG"=>0,
322                                         );
323                         if (-f "$dir/include/linux/rhconfig.h") {
324                                 my $defines="";
325                                 for (keys(%boot)) {
326                                         $defines.="+#define $_ ".($boot{$_} ? 1 : 0)."\n";
327                                         }
328                                 _system "cp -p -f $dir/include/linux/rhconfig.h-orig $dir/include/linux/rhconfig.h";
329                                 _writefile "| patch $dir/include/linux/rhconfig.h",<<"RHCONFIG_H_NOBOOT_EOF";
330 --- ./include/linux/rhconfig.h-orig     Thu Aug 21 14:50:16 2003
331 +++ ./include/linux/rhconfig.h  Thu Aug 21 14:59:22 2003
332 \@\@ -10,7 +10,@{[ 7+keys(%boot) ]} \@\@
333   * /boot/kernel.h - initscripts should create it for us
334   */
335  
336 -#include "/boot/kernel.h"
337 +/* lufs: #include "/boot/kernel.h" */
338 $defines
339  #if defined(__BOOT_KERNEL_SMP) && (__BOOT_KERNEL_SMP == 1)
340  #define __module__smp
341 RHCONFIG_H_NOBOOT_EOF
342                                 }
343                         # Mandrake packages have no configs/ and they have just that '.config' file.
344                         my $single_config=-f "$dir/.config";
345                         if (!$single_config) {
346                                 my $spec_config="$dir/configs/kernel-$uname_r_base-$arch".($smp ? "-smp" : "").".config";
347                                 next if ! -f $spec_config;
348                                 _system "ln -s $spec_config $dir/.config";
349                                 }
350                         prebuild_kernel $dir,$vendor,$uname_r.$smp.".".$arch;
351                         _system "rm -f $dir/.config"
352                                         if !$single_config;
353                         }
354                 }
355         _system "rpm -e kernel-source-$uname_r" and confess "Remove of kernel-source-$uname_r";
356         _system "rm -rf $dir";
357 }
358
359 sub prebuild_dir
360 {
361 my($dir)=@_;
362
363         my $vendor;
364         chomp (my $debian=_readfile "/etc/debian_version","optional");
365         $debian=~tr#/#_#;
366         $vendor="Debian".$debian if $debian && $dir=~m#^/usr/src/#;
367         my $uname_r;
368         $uname_r||=($dir=~m#/kernel-headers-([^/]+)/*$#)[0];
369         $uname_r||=($dir=~m#/linux-([^/]+)/*$#)[0];
370         $uname_r||=($dir=~m#^/lib/modules/([^/]+)/build/*$#)[0];
371         prebuild_kernel $dir,$vendor,$uname_r;
372 }
373
374 sub prebuild
375 {
376         $vardir=undef();        # we may not yet have /var/lib/lufs
377         for (@ARGV) {
378                 do { prebuild_rpm $_; next; } if /[.]rpm$/;
379                 do { prebuild_dir $_; next; } if -d $_;
380                 confess "Unrecognized kernel: $_";
381                 }
382         exit 0;
383 }