[sanboot] Prevent leaking a stack reference for "keep-san" AoE
[gpxe.git] / contrib / mklrpnb / extractdach.pl
blob6c81da38723ca3a733474e5a2c2a2b2cc80ea2f1
1 #!/usr/bin/perl -w
3 # A program to make a netbootable image from a LRP firewall floppy
5 # Tested on a Dachstein Linux floppy image available from
6 # http://lrp1.steinkuehler.net/ or via http://leaf.sourceforge.net/
8 # The most recent version of this script and a companion HowTo is available at
9 # http://members.optushome.com.au/graybeard/linux/netboot.html
11 # Modified from the mklrpnb file found in the contrib/mklrpnb directory of the
12 # Etherboot source at http://etherboot.sourceforge.net/
14 # Modifications by Glenn McK <graybeard@users.sourceforge.net>
15 # $Id$
16 #####################################
18 # this entry will need changing
19 $image = "/home/graybeard/etherboot/dachstein-v1.0.2-1680.bin";
21 # these can remain, but change them if desired
23 # the next argument defaults to firewall if no other name is passed via the
24 # command line, this will be the directory where distribution will be expanded
25 # under $base and also the directory in /tftpboot for lrp.nb
27 my $uniqdir = shift || 'firewall';
29 $mntdir = "/mnt/floppy"; # where the above image file can be mounted
30 $tftpbase = "/tftpboot";
31 $tftpboot = "$tftpbase/$uniqdir"; # where the netboot images will be available
32 $base = "/usr/src/LRP";
33 $dachorg = "$base/dach-org-$uniqdir"; # a copy required to make the distribution
34 $dachnew = "$base/lrp-$uniqdir"; # the base files for the new distribution
35 $packages = "$dachnew/var/lib/lrpkg"; # list to allow lrcfg to display Packages
37 # everything below should be okay
38 ######################################
40 if ( !-e $image ) {
41 print
42 "\n\tA valid LRP file and directory are required\n\tdownload one then edit $0\n\n";
43 exit 1;
45 if ( !-d $base ) {
46 mkdir( $base, 0700 );
49 if ( !-d $dachorg ) {
50 mkdir( $dachorg, 0700 );
53 if ( !-d $dachnew ) {
54 mkdir( $dachnew, 0700 );
55 `umount $mntdir`;
56 `mount -o ro,loop $image $mntdir`;
58 `cp -vr $mntdir/* $dachorg/`;
60 @cfg = `cat $mntdir/syslinux.cfg`;
62 unless ( defined(@cfg) ) {
63 print "Cannot find syslinux.cfg on $mntdir\n";
64 exit 1;
66 print "cfg = @cfg\n";
67 ($append) = grep( /append/, @cfg ); # find the append= line
68 print "append = \n$append\n";
69 chomp($append); # remove trailing newline
70 $append =~ s/append=//; # remove the append= at beginning
71 print "strip append = \n$append\n\n";
72 @args = split ( / /, $append ); # split into arguments at whitespace
73 ($root) = grep( /^initrd=/, @args ); # find the initrd= argument
74 $root =~ s/^initrd=//; # remove the initrd= at beginning
75 $root =~ s/\.lrp$//; # cleanup for paclages list
76 print "strip initrd = \n$root\n\n";
77 ($lrp) = grep( /^LRP=/, @args ); # find the LRP= argument
78 $lrp =~ s/^LRP=//; # remove the LRP= at beginning
79 print "strip LRP =\n$lrp\n\n";
80 @lrp = split ( /,/, $lrp ); # split into filenames at ,
81 unshift ( @lrp, $root ); # prepend the root LRP filename
82 @pack = @lrp;
83 print "LRP =\n@lrp\n\n";
84 $append = '';
86 foreach $i (@args) { # rebuild the append string
87 next if ( $i =~ /^initrd=/ ); # minus the unneeded parameters
88 next if ( $i =~ /^LRP=/ );
89 next if ( $i =~ /^boot=/ );
90 next if ( $i =~ /^PKGPATH=/ );
91 print "$i = i\n";
92 $append .= "$i ";
95 print "final append = \n$append\n";
97 chdir($dachnew) or die "$dachnew: $!\n";
98 foreach $i (@lrp) {
99 $i .= '.lrp' if $i !~ /\.lrp$/;
100 print "\n\n\nUnpacking $i\n";
101 system("ln -svf $dachorg/$i ${dachorg}/${i}.tar.gz");
102 chmod 0600, "$dachorg/$i";
103 system("cat $mntdir/$i | tar zxvf -");
106 # create file for lrcfg to display packages
107 open( PACKAGES, ">$packages/packages" )
108 || print "unable to modify $packages:$!\n";
109 foreach $line (@pack) {
110 print PACKAGES "$line\n";
112 close PACKAGES;
114 # prevent previous file from being overwritten during installation
115 # and also mess with some values in /linuxrc to hide non errors
116 open( LINUXRC, "$packages/root.linuxrc" );
117 @text = <LINUXRC>;
118 close LINUXRC;
119 open( LINUXRC, ">$packages/root.linuxrc" );
120 foreach $line (@text) {
121 $line =~ s/PFX\/packages/PFX\/packages-old \
122 \t\t\t\t# packages changed to packages-old for netboot setup/;
123 $line =~
124 s/^rc=1/# rc=1 changed to rc=0 to suppress error messages for netboot setup \
125 rc=0/;
126 $line =~
127 s/echo -n \" \(nf\!\)\"/#echo -n \" \(nf\!\)\" changed to reflect ToDo list \
128 \t\t\techo -n \" netboot setup - No backups possible from this machine - ToFix ?"/;
129 print LINUXRC $line;
131 close LINUXRC;
133 # swap interfaces around in network config file
134 # eth1 is the new external eth0 is OUR internal server access
135 open( NETWORK, "$dachnew/etc/network.conf" )
136 || print "Unable to modify NETWORK:$!\n";
137 @text = <NETWORK>;
138 close NETWORK;
139 open( NETWORK, ">$dachnew/etc/network.conf" )
140 || print "Unable to modify NETWORK:$!\n";
141 foreach $line (@text) {
142 $line =~ s/eth0/eth00/;
143 $line =~ s/eth1/eth0/;
144 $line =~ s/eth00/eth1/;
145 print NETWORK $line;
147 close NETWORK;
149 `echo $append > $dachorg/appendstr`;
151 `umount /mnt/floppy`;
152 print "\nThe files have been extracted to $dachnew\n";
153 system("ls -al $dachnew");
155 else {
156 print "\n\n\t$image \n \thas already been extracted to $dachnew \
157 \tNow skipping to the next step where the netboot file\
158 \twill be created.\n";
160 $append = `cat $dachorg/appendstr`;
161 print "\nThe new append string will be...\n$append\n";
163 chdir($dachnew);
164 if ( !-d $tftpbase ) {
165 mkdir( $tftpbase, 0710 );
166 system("chgrp nobody $tftpbase");
169 unlink($tftpboot);
171 # these permissions really need changing to something secure
172 mkdir( $tftpboot, 0710 );
173 system("chgrp nobody $tftpboot");
174 print "\tRepacking to $tftpboot/lrp.lrp\n";
175 system("tar zcf $tftpboot/lrp.lrp *");
176 print "\tExtracting kernel image from $dachorg\n";
177 system("cat $dachorg/linux > $tftpboot/lrp.ker");
178 print "\tCreating netboot image $tftpboot/lrp.nb\n";
179 system(
180 "mknbi-linux --append='$append' --output=$tftpboot/lrp.nb $tftpboot/lrp.ker $tftpboot/lrp.lrp"
182 chmod 0604, "$tftpboot/lrp.nb", "$tftpboot/lrp.ker", "$tftpboot/lrp.lrp";
183 print "\nThese netboot files are in $tftpboot\n";
184 system("ls -al $tftpboot");
185 print "\n The owner and permissions for $tftpboot \
186 and files should be checked for security. The above\
187 permissions assume that tftp is running chroot (nobody)
188 drwx--r--- root:nobody /tftpboot\n\n";
191 exit 0;