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