You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

extractdach.pl 6.6KB

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