4 # Check and possibly rebuild lufs kernel module for the current kernel.
5 # Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
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
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.
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
22 use Carp qw(cluck confess);
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 ?)
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);
40 use vars qw($kernel_gcc_args);
43 sub modext { my($uname_r)=@_; ($uname_r lt "2.5" ? "o" : "ko"); }
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"} || ()));
49 my $lufsd_base='lufsd';
50 do { eval '$lufsd_base=~'.$_.';' if !/^@/; } for ('@program_transform_name@');
52 if ($0 eq $lufsd_base || $0=~m#/\Q$lufsd_base\E$#) {
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;
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,
72 my($filename,$optional)=@_;
76 open F,$filename or do { cluck "Open \"$filename\": $!" if !$optional; return ""; };
78 close F or cluck "Close \"$filename\": $!";
84 my($filename,@content)=@_;
87 open F,($filename=~/^[|]/ ? "" : ">").$filename or confess "rewrite \"$filename\": $!";
89 close F or confess "close \"$filename\": $!"; # Do not &cluck as it may be pipe result
96 print STDERR "+ ".join(" ",@args)."\n" if !$quiet;
106 do { ($modproberc=_system $_) and cluck "$_ failed - ignoring"; }
107 for ("/sbin/depmod -aq","/sbin/modprobe lufs");
109 exit ($modproberc ? 1 : 0) if !$lufsd_bin;
110 do { exec $lufsd_bin,@ARGV; };
111 confess "Failed to exec '$lufsd_bin': $!";
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";
121 print STDERR "Preparing LUFS kernel module - this may take several minutes...\n"; # if !$quiet;
123 my $proc_version=_readfile "/proc/version";
124 my $uname_r=($proc_version=~/^Linux version (\S+)/)[0] || _readfile "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;
131 my $lufs_o="lufs.".modext($uname_r);
133 my $moduledir="/lib/modules/$uname_r$uname_smp/kernel/fs/lufs";
134 print STDERR "Destination module directory: $moduledir\n" if !$quiet;
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",
142 "/usr/src/kernel-source-$uname_r", # no .config?
144 do { $kernel||=$_ if -d $_; } for (@kernel_paths);
146 print STDERR "Failed to find kernel headers for $uname_r\n" if !$kernel && !$quiet;
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") {
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";
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.
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));
185 my($kernel,$uname_r,$destmodule)=@_;
187 print STDERR "Using kernel sources: $kernel\n" if !$quiet;
188 confess "Kernel sources $kernel do not contain 'include' subdirectory" if ! -d $kernel."/include";
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"
197 ." -D__KERNEL__ -DMODULE -DLINUX -DKBUILD_MODNAME=lufs"
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
203 my $config=_readfile "$kernel/.config","optional";
204 my $autoconf=_readfile "$kernel/include/linux/autoconf.h","optional";
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";
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);
226 my($kernel,$uname_r,$destmodule,%args)=@_;
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");
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 $_;
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;"
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;
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;
264 my($kernel,$uname_r,$destmodule,%args)=@_;
266 # Debian uname(1) does not support '-p'.
267 my $arch=_readfile "uname -p 2>/dev/null || true|"
268 || _readfile "uname -m|";
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"
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; }
281 { $r=build_gcc $kernel,$uname_r,$destmodule,%args; }
282 _system "rm -f $kernel/.config"
290 my($dir,$vendor,$uname_r,%args)=@_;
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;
302 my $vendor=_readfile "rpm 2>/dev/null --qf '%{VENDOR}' -qp '$rpm' |";
306 $uname_r||=($rpm=~m#/kernel-source-([^-]+-[^-]+)[.][^.]+[.]rpm$#)[0];
307 my $uname_r_base=($uname_r=~/^([^-]+)/)[0];
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";
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") {
326 for my $arch_d (glob "$dir/*") {
327 (my $arch=$arch_d)=~s#^.*/##;
328 prebuild_kernel $arch_d,$vendor,$uname_r.".".$arch;
333 # RedHat/Mandrake/old-SuSE kernels
334 for my $smp ("","smp") {
335 my @archs=qw(i386 i586 i686 athlon);
336 for my $arch (@archs) {
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,
346 if (-f "$dir/include/linux/rhconfig.h") {
349 $defines.="+#define $_ ".($boot{$_} ? 1 : 0)."\n";
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
359 -#include "/boot/kernel.h"
360 +/* lufs: #include "/boot/kernel.h" */
362 #if defined(__BOOT_KERNEL_SMP) && (__BOOT_KERNEL_SMP == 1)
363 #define __module__smp
364 RHCONFIG_H_NOBOOT_EOF
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";
373 prebuild_kernel $dir,$vendor,$uname_r.$smp.".".$arch;
375 _system "rm -f $dir/.config"
379 # Old SuSE kernels have no '.config' or 'configs/'.
380 confess "Not a buildable kernel: $rpm" if !$built_total;
382 _system "rm -rf modbin-tmp" and confess "Remove of extracted kernel-source-$uname_r";
390 chomp (my $debian=_readfile "/etc/debian_version","optional");
392 $vendor="Debian".$debian if $debian && $dir=~m#^/usr/src/#;
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;
402 $vardir=undef(); # we may not yet have /var/lib/lufs
404 do { prebuild_rpm $_; next; } if /[.]rpm$/;
405 do { prebuild_dir $_; next; } if -d $_;
406 confess "Unrecognized kernel: $_";