Do not create bogus '@localstatedir@' subdir if not yet subst-ed version used.
[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.5"); }
34 my $modbindir=$basedir."/modbin";
35 my @sources=qw(proc.c inode.c dir.c file.c symlink.c);
36 my $tmp_dir="prepmod-tmp-dir";
37
38 my $quiet;
39 my $kernel;
40 my $prebuild;
41 my $kernel_gcc_args="";
42
43 my $lufsmnt_bin;
44 if ($0 eq "lufsmnt" || $0=~m#/lufsmnt$#) {
45         $quiet=1;
46         $lufsmnt_bin='@bindir@/lufsmnt-bin';
47         }
48 else {
49         die if !GetOptions(
50                         "q|quiet",\$quiet,
51                           "kernel",\$kernel,    # it must contain "include" subdir
52                           "modbindir=s",\$modbindir,
53                           "basedir=s",\$basedir,
54                           "prebuild",\&prebuild,
55                           "kernel-gcc-args=s",\$kernel_gcc_args,
56                         );
57         }
58
59
60 sub _readfile
61 {
62 my($filename,$optional)=@_;
63
64         local $/=undef();
65         local *F;
66         open F,$filename or do { cluck "Open \"$filename\": $!" if !$optional; return ""; };
67         my $r=<F>;
68         close F or cluck "Close \"$filename\": $!";
69         return $r;
70 }
71
72 sub _system
73 {
74 my(@args)=@_;
75
76         print STDERR "+ ".join(" ",@args)."\n" if !$quiet;
77         return system @args;
78 }
79
80 sub _pass
81 {
82 my($load)=@_;
83
84         my $modproberc;
85         if ($load) {
86                 do { ($modproberc=_system $_) and cluck "$_ failed - ignoring"; } for ("depmod -aq","modprobe lufs");
87                 }
88         exit ($modproberc ? 1 : 0) if !$lufsmnt_bin;
89         do { exec $lufsmnt_bin,@ARGV; };
90         confess "Failed to exec '$lufsmnt_bin': $!";
91 }
92
93
94 my $filesystems=_readfile "/proc/filesystems";  # 'lufs' may be built-in
95 _pass if $filesystems=~/\blufs$/m;
96 my $modules=_readfile "/proc/modules";  # 'lufs' may be already loaded
97 _pass if $modules=~/^lufs\b/m;
98 _pass if !_system "modprobe lufs 2>/dev/null";
99
100 my $proc_version=_readfile "/proc/version";
101 my $uname_r=($proc_version=~/^Linux version (\S+)/)[0] || _readfile "uname -r|";
102 chomp $uname_r;
103 confess "Failed to detect kernel version" if !$uname_r;
104 my $uname_r_base=($uname_r=~/^([^-]+)/)[0];
105 print STDERR "Running kernel version: $uname_r (base version $uname_r_base)\n" if !$quiet;
106
107 do { $kernel||=$_ if -d $_; } for ("/lib/modules/$uname_r/build");
108 do { $kernel||=$_ if -d $_; } for ("/usr/src/kernel-headers-$uname_r");
109 do { $kernel||=$_ if -d $_; } for ("/usr/src/linux-$uname_r");
110 do { $kernel||=$_ if -d $_; } for ("/usr/src/linux-$uname_r_base");
111 do { $kernel||=$_ if -d $_; } for ("/usr/src/linux");
112 confess "Failed to find kernel headers for $uname_r" if !$kernel;
113
114 my $moduledir="/lib/modules/$uname_r/kernel/fs/lufs";
115 print STDERR "Destination module directory: $moduledir\n" if !$quiet;
116
117 for ("$vardir/lufs.o") {
118         next if !$vardir;
119         # Create the 'lufs.o' in our /var/lib directory and only link it
120         # to prevent using obsolete modules after upgrading 'lufs' package.
121         # depmod(1) will take the larget symlink name - we must create directory for  it.
122         if (build($kernel,$uname_r,$_)) {
123                 do { cluck "Failed to symlink $_"; next; }
124                                 if _system "rm -rf $moduledir; mkdir -p $moduledir; ln -s $_ $moduledir/lufs.o";
125                 _pass 1;
126                 }
127         }
128
129 local $_;
130 for (<$modbindir/*-$uname_r.o>,<$modbindir/*-${uname_r_base}*.o>,<$modbindir/*.o>) {
131         next if _system "rmmod lufs 2>/dev/null; insmod -o lufs -p $_ 2>/dev/null";
132         do { cluck "Failed to symlink $_"; next; } if _system "mkdir -p $moduledir; ln -s $_ $moduledir/lufs.o";
133         _pass 1;
134         }
135 confess "lufs module not loaded: $basedir not compilable and no module of $modbindir/*.o usable";
136
137
138 sub build
139 {
140 my($kernel,$uname_r,$destmodule)=@_;
141
142         print STDERR "Using kernel sources: $kernel\n" if !$quiet;
143         confess "Kernel sources $kernel do not contain 'include' subdirectory" if ! -d $kernel."/include";
144
145         my $kdebug="";
146         do { $kdebug=$_ if !/^@/; } for ('@KDEBUG_FLAGS@');
147         my $cmdline="gcc"
148                         .($quiet ? " 2>/dev/null" : "")
149                         ." -O2 -pipe -fomit-frame-pointer -fno-strict-aliasing -mpreferred-stack-boundary=2 -Wall"
150                         ." -D__KERNEL__ -DMODULE -DLINUX -DKBUILD_MODNAME=lufs"
151                         ." ".$kdebug
152                         ." ".$kernel_gcc_args
153                         ." -I$kernel/include"
154                         ." -I$kernel/include/asm-i386/mach-default";    # gcc should not care if this 2.5 dir does not exist
155
156         my $config=_readfile "$kernel/.config","optional";
157         my $autoconf=_readfile "$kernel/include/linux/autoconf.h","optional";
158         $cmdline.=" -DMODVERSIONS -include $kernel/include/linux/modversions.h"
159                         if 0
160                                         || $config=~/^CONFIG_MODVERSIONS=y\b/m
161                                         || $autoconf=~/^#define CONFIG_MODVERSIONS\b/m
162                                         || (!$config && !$autoconf);    # assume modversions if not known
163
164         my @objects=map({ my $o=$_; $o=~s/[.]c$/.o/; $o; } @sources);
165         return !_system "set -e; mkdir -p `dirname $destmodule`; rm -f $destmodule;"
166                         .(!$vardir ? "" : " cd $vardir;")
167                         ." $cmdline -c ".join(" ",map({ srcdir($uname_r)."/".$_; } @sources)).";"
168                         ." ld -r -o $destmodule ".join(" ",@objects).";"
169                         ." rm -f ".join(" ",@objects);
170 }
171
172
173 sub prebuild_kernel
174 {
175 my($dir,$vendor,$uname_r)=@_;
176
177         confess "Unrecognized vendor for dir: $dir" if !$vendor;
178         confess "Unrecognized uname_r for dir: $dir" if !$uname_r;
179         confess "Failed to build $dir" if !build $dir,$uname_r,$modbindir."/lufs-$vendor-$uname_r.o";
180 }
181
182
183 sub prebuild_rpm
184 {
185 my($rpm)=@_;
186
187         my $vendor=_readfile "rpm 2>/dev/null --qf '%{VENDOR}' -qp '$rpm' |";
188         $vendor=~s/,.*//;
189         $vendor=~tr/ //d;
190         my $uname_r;
191         $uname_r||=($rpm=~m#/kernel-source-([^-]+-[^-]+)[.][^.]+[.]rpm$#)[0];
192         _system "rm -rf $tmp_dir; mkdir $tmp_dir; set -e;"
193                         # Here we will extract even some false 'include's but we filter dirs just for speed/space.
194                         ." rpm2cpio '$rpm' | (cd $tmp_dir; cpio -id --quiet ./usr/src/linux*/{.config,include/*})"
195                         and confess "Extraction of: $rpm";
196         prebuild_kernel $tmp_dir."/usr/src/linux-$uname_r",$vendor,$uname_r;
197         _system "rm -rf $tmp_dir" and confess "Deletion of: $tmp_dir";
198 }
199
200 sub prebuild_dir
201 {
202 my($dir)=@_;
203
204         my $vendor;
205         chomp (my $debian=_readfile "/etc/debian_version","optional");
206         $vendor="Debian".$debian if $debian && $dir=~m#^/usr/src/#;
207         my $uname_r;
208         $uname_r||=($dir=~m#/kernel-headers-([^/]+)/*$#)[0];
209         $uname_r||=($dir=~m#/linux-([^/]+)/*$#)[0];
210         $uname_r||=($dir=~m#^/lib/modules/([^/]+)/build/*$#)[0];
211         prebuild_kernel $dir,$vendor,$uname_r;
212 }
213
214 sub prebuild
215 {
216         $vardir=undef();        # we may not yet have /var/lib/lufs
217         for (@ARGV) {
218                 do { prebuild_rpm $_; next; } if /[.]rpm$/;
219                 do { prebuild_dir $_; next; } if -d $_;
220                 confess "Unrecognized kernel: $_";
221                 }
222         exit 0;
223 }