home *** CD-ROM | disk | FTP | other *** search
/ Caldera Network Desktop 1.0 / caldera-network-desktop-1.0.bin / images / mkfloppies.pl next >
Perl Script  |  1995-10-11  |  7KB  |  238 lines

  1. #!/usr/bin/perl
  2. #
  3. # Make a Red Hat installation floppy set.  Parses the Red Hat image.idx file
  4. # to generate a dialogue to pick a disk image, then makes the disk.  Send
  5. # flames and tossed roses to Eric Raymond, <esr@snark.thyrsus.com>.
  6. #
  7. # Hacked up by Erik Troan, <ewt@redhat.com>. I'll take any flames but keep
  8. # the roses going to esr.
  9. #
  10. # I guess this script i just "Eri[ck]'s thing"
  11.  
  12. # There should probably be some options to override these
  13. $cddir="..";                 # Can be overwritten by $ARGV[0]
  14.  
  15. # Note: we skip the verify because (on my Linux, anyway) it's prone
  16. # to issuing "unknown read error" messages on floppies that seem OK.
  17. $format="fdformat -n /dev/fd0H1440";    # How to format a floppy
  18.  
  19. sub pick_one {
  20. # Pick one choice from a list of alternatives
  21.     local ($legend, @alternatives) = @_;
  22.  
  23.     while (1)
  24.     {
  25.     print "$legend:\n";
  26.     $num = 0;
  27.     foreach $x (@alternatives)
  28.     {
  29.         print "    ", $num++, ") $x\n";
  30.     }
  31.  
  32.     print "Pick one: ";
  33.     $response = <STDIN>;
  34.     chop $response;
  35.  
  36.     if ($response =~ /[0-9]+/ && $response >= 0 && $response <= $num)
  37.     {
  38.         print "\n";
  39.         return $alternatives[$response]
  40.     }
  41.  
  42.     print "Sorry, that didn't look like a valid response.\n\n"
  43.     }
  44. }
  45.  
  46. sub copy_image {
  47. # Copy a given image to the boot device, offering to do a format first
  48.     local($name, $imagefile) = @_;
  49.  
  50.     print "\n";
  51.  
  52.     # Verify that the desired image is accessible
  53.     if (! -r "$imagefile" && ! -r "$imagefile.gz")
  54.     {
  55.     print "I can't find $imagefile to read it.\n";
  56.     print "This may mean that your CD-ROM is not mounted,\n";
  57.     print "or that you lack the necessary read permissions,\n";
  58.     print "or that your Linux uses a different mount point for\n";
  59.     print "the CD-ROM than I am expecting.\n";
  60.     return;
  61.     }
  62.  
  63.     # Offer to format the disk.
  64.     print "About to make $name disk.\n";
  65.  
  66.     dformat: {
  67.     print "Please insert your disk in your boot floppy drive.\n";
  68.     print "If you answer `y' to this prompt, it will be formatted first [yn]: ";
  69.     if (<STDIN> =~ '^[yY]')
  70.     {
  71.         system($format);
  72.  
  73.         print "\nIf the format issued any suspicious messages,\n";
  74.         print "you can try formatting another disk.\n\n";
  75.         print "Did the format look OK [yn]? ";
  76.         redo dformat if (<STDIN> !~ '^[yY]');
  77.     }
  78.     else
  79.     {
  80.         print "Format skipped.\n";
  81.     }
  82.  
  83.  
  84.     print "\n";
  85.     }
  86.  
  87.     # Now dd the chosen image to it
  88.     print "Copying the image...\n";
  89.     if ( -r $imagefile ){
  90.     $status = system("dd if=$imagefile of=$bootdev bs=64k");
  91.     } elsif ( -r "$imagefile.gz" ) {
  92.     $status = system("gunzip < $imagefile.gz | dd of=$bootdev");
  93.     }
  94.     if ($status == 0) {
  95.     print "Copy succeeded (status 0).\n";
  96.     } else {
  97.     print "Copy failed (status $status).\n";
  98.     }
  99. }
  100.  
  101. sub make_bootdisk {
  102. # Ask the user for his/her configuration and make an appropriate boot disk
  103.     open(IMAGES, "$imagedir/image.idx") || die("Can't find images file.\n");
  104.  
  105.     # Build lists of the possible alternatives
  106.     while (<IMAGES>)
  107.     {
  108.     chop;
  109.     ($image, $scsi, $ethernet, $cdrom) = split(/; /);
  110.  
  111.     foreach $x (split(/, /, $scsi)) {
  112.         push(@scsi_types, $x) if $scsi_seen{$x}++ == 0;
  113.     }
  114.  
  115.     foreach $x (split(/, /, $ethernet)) {
  116.         push(@ethernet_types, $x) if $ethernet_seen{$x}++ == 0;
  117.     }
  118.  
  119.     foreach $x (split(/, /, $cdrom)) {
  120.         push(@cdrom_types, $x) if $cdrom_seen{$x}++ == 0;
  121.     }
  122.    }
  123.    seek(IMAGES, 0, 0);
  124.  
  125.    print "\n";
  126.  
  127.    # Get the user's responses on his or her configuration
  128.    hardware: {
  129.     print "Please pick the menu choices describing your hardware.\n\n";
  130.  
  131.     $scsi_choice     = &pick_one("SCSI support", @scsi_types);
  132.     $ethernet_choice = &pick_one("Ethernet support", @ethernet_types);
  133.     $cdrom_choice    = &pick_one("CD-ROM support", @cdrom_types);
  134.  
  135.     # Feedback
  136.     print "Here's what you selected:\n";
  137.     print "    SCSI: $scsi_choice\n";
  138.     print "    Ethernet: $ethernet_choice\n";
  139.     print "    CD-ROM: $cdrom_choice\n";
  140.     print "\n";
  141.  
  142.     print "Is this correct [yn]? ";
  143.     redo hardware if (<STDIN> !~ '^[yY]');
  144.     print "\n";
  145.     }
  146.  
  147.     # OK, now build the set of image alternatives
  148.     while (<IMAGES>)
  149.     {
  150.     chop;
  151.     ($image, $scsi, $ethernet, $cdrom) = split(/; /);
  152.  
  153.     if ((index($scsi, $scsi_choice) != -1)
  154.         && (index($ethernet, $ethernet_choice) != -1)
  155.         && (index($cdrom, $cdrom_choice) != -1))
  156.     {
  157.         push(@imagelist, "$image: $scsi; $ethernet; $cdrom");
  158.     }
  159.     }
  160.  
  161.     # Find the images that match the spec the user set up
  162.     image: {
  163.     $image = &pick_one("Here are the images that match", @imagelist);
  164.  
  165.     # Feedback
  166.     ($imageno, $rest) = split(/:/, $image);
  167.     print "You selected image $imageno\n";
  168.  
  169.     $imagefile = "$imagedir/boot${imageno}.img";
  170.     print "Make a boot disk from $imagefile [yn]? ";
  171.     redo image if (<STDIN> !~ '^[yY]');
  172.     }
  173.     close(IMAGES);
  174.  
  175.     # Actually make the disk
  176.     ©_image("boot", $imagefile);
  177.  
  178.     # Now mount it and run our "savesetup" script
  179.     system("mkdir -p /tmp/floppy");
  180.     system("mount -t ext2 /dev/fd0 /tmp/floppy");
  181.     print "Saving system configuration info...";
  182.     system("./savesetup.pl /tmp/floppy");
  183.     system("umount /tmp/floppy");
  184.     print "\n";
  185. }
  186.  
  187. if ( @ARGV == 1 ) {
  188.     $cddir = $ARGV[0];
  189. }
  190.  
  191. $sysdir="$cddir/images";               # Root/rescue image directory
  192. $imagedir="${sysdir}/1213";        # Boot images directory
  193. $bootdev="/dev/fd0";            # Where to make the floppy
  194.  
  195. if ( @ARGV > 1 ) {
  196.     print STDERR "usage:\n";
  197.     print STDERR "\t$0 <path to Red Hat CD>\n";
  198.     print STDERR;
  199.     exit 1;
  200. }
  201.  
  202. # Check $sysdir for correctness
  203. if ( ! -e "$sysdir/version.idx" ) {
  204.     print STDERR "$cddir isn't the path to your Red Hat CD. Run this script\n";
  205.     print STDERR "with the path to your Red Hat CD as it's sole argument\n";
  206.     print STDERR;
  207.     exit 1;
  208. }
  209.  
  210. # Main sequence
  211. print "mkfloppies.pl 1.1 - Copyright 1995 Eric Raymond, Red Hat Software\n";
  212. print "This may be freely redistributed under the terms of the GNU Public License\n\n";
  213.  
  214. print "This script will help you create an installation set for your\n";
  215. print "Red Hat distribution: boot, root, and rescue floppies.  To use it,\n";
  216. print "you should have a 3.5-inch 1.44K floppy drive as your boot device\n";
  217. print "(which is normal) and have at least two 1.44K floppies ready (three\n";
  218. print "if you want to make a rescue disk, which we recommend).  This script\n";
  219. print "can format the floppies for you.\n\n";
  220.  
  221. print 'Do you want to make a boot disk [yn]? ';
  222. &make_bootdisk if (<STDIN> =~ '[yY]');
  223. print "\n";
  224.  
  225. print 'Do you want to make your two ramdisks [yn]? ';
  226. $answer = <STDIN>;
  227. ©_image("root 1", "${sysdir}/ramdisk1.img") if ($answer =~ '[yY]');
  228. ©_image("root 2", "${sysdir}/ramdisk2.img") if ($answer =~ '[yY]');
  229. print "\n";
  230.  
  231. print 'Do you want to make a rescue disk [yn]? ';
  232. ©_image("rescue", "${sysdir}/rescue.img") if (<STDIN> =~ '[yY]');
  233. print "\n";
  234.  
  235. print "Your floppies should now be ready to be used for system installation.\n"
  236.  
  237. # script ends here
  238.