home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / sbin / mkinitrd_setup < prev    next >
Text File  |  2010-05-10  |  7KB  |  290 lines

  1. #!/usr/bin/perl
  2. #
  3. # Install initrd scripts
  4. #
  5.  
  6. use strict;
  7. use Getopt::Long;
  8.  
  9. my @scripts_boot = ();
  10. my @scripts_setup = ();
  11. my %level_boot = ();
  12. my %level_setup = ();
  13. my %depends_boot = ();
  14. my %depends_setup = ();
  15. my %providedby_boot = ();
  16. my %providedby_setup = ();
  17.  
  18. my $debug = 0;
  19. my $offset = 0;
  20. my $installdir = "/lib/mkinitrd";
  21. my $scriptdir = "$installdir/scripts";
  22. my $dohelp = 0;
  23.  
  24. my %stages = (
  25.     setup => 0,
  26.     filesystem => 1,
  27.     crypto => 2,
  28.     volumemanager => 3,
  29.     softraid => 4,
  30.     partitions => 5,
  31.     devicemapper => 6,
  32.     block => 7,
  33.     device => 8,
  34.     boot => 9,
  35. );
  36.  
  37. sub dprintf
  38. {
  39.     if ($debug > 0) {
  40.         printf @_;
  41.     }
  42. }
  43.  
  44. sub resolve_dependency
  45. {
  46.     my $section = shift(@_);
  47.     my $name = shift(@_);
  48.     my $oldlevel;
  49.     my $level;
  50.     my $depends;
  51.     my $providedby;
  52.  
  53.     if ( $section eq "setup" ) {
  54.         $level = \%level_setup;
  55.         $depends = \%depends_setup;
  56.         $providedby= \%providedby_setup;
  57.     } else {
  58.         $level = \%level_boot;
  59.         $depends = \%depends_boot;
  60.         $providedby= \%providedby_boot;
  61.     }
  62.     $oldlevel = $$level{$name};
  63.  
  64.     foreach my $elem (split(' ',$$depends{$name})) {
  65.         my $newlevel = -1;
  66.  
  67.         foreach my $n (split(' ',$$providedby{$elem})) {
  68.             $newlevel = resolve_dependency($section, $n);
  69.             if ( $oldlevel <= $newlevel) {
  70.                 $oldlevel = $newlevel + 1;
  71.             }
  72.         }
  73.  
  74.         if ($newlevel == -1) {
  75.             dprintf "Unresolved dependency \"%s\" ", $elem;
  76.         }
  77.     }
  78.     dprintf "%s/%s (%s) ", $section, $name, $oldlevel;
  79.     $$level{$name} = $oldlevel;
  80.  
  81.     return $oldlevel;
  82. }
  83.  
  84. sub scan_section
  85. {
  86.     my @scripts = @_;
  87.     my @scrlist = ();
  88.     my $depends;
  89.     my $level;
  90.     my $providedby;
  91.  
  92.     SCAN: foreach $_ (@scripts) {
  93.         my $provides;
  94.         my $section;
  95.         my $scriptname;
  96.         my $level;
  97.         my $depends;
  98.         my $providedby;
  99.         my $scrlist;
  100.  
  101.         if (/(.*)-(.*)\.sh$/) {
  102.             if (($1 ne "setup" ) && ($1 ne "boot")) {
  103.                 next SCAN;
  104.             }
  105.             $section = $1;
  106.             $scriptname = $2;
  107.         } else {
  108.             next SCAN;
  109.         }
  110.  
  111.         if ( $section eq "setup" ) {
  112.             $level = \%level_setup;
  113.             $depends = \%depends_setup;
  114.             $providedby= \%providedby_setup;
  115.             $scrlist = \@scripts_setup;
  116.         } else {
  117.             $level = \%level_boot;
  118.             $depends = \%depends_boot;
  119.             $providedby= \%providedby_boot;
  120.             $scrlist = \@scripts_boot;
  121.         }
  122.  
  123.         dprintf "scanning script %s (name %s)\n", $_, $scriptname;
  124.         dprintf "\tsection: %s\n", $section;
  125.         $provides = $scriptname;
  126.  
  127.         open(SCR, "$scriptdir/$_");
  128.  
  129.         while (<SCR>) {
  130.             chomp;
  131.             if ( /^\#%stage: (.*)/ ) {
  132.                 if (! defined $stages{$1}) {
  133.                     dprintf "%s: Invalid stage \"%s\"\n", $scriptname, $1;
  134.                     close(SCR);
  135.                     next SCAN;
  136.                 }
  137.                 if ($section eq "setup") {
  138.                     $$level{$scriptname} = ($stages{$1} * 10) + 1;
  139.                 } else {
  140.                     $$level{$scriptname} = 91 - ($stages{$1} * 10);
  141.                 }
  142.                 dprintf "\tstage %s: %d\n", $1, $$level{$scriptname};
  143.             }
  144.             if ( /^\#%depends: (.*)/ ) {
  145.                 dprintf "\tdepends on %s\n", $1;
  146.                 $$depends{$scriptname} = $1;
  147.             }
  148.             if ( /\#%provides: (.*)/ ) {
  149.                 $provides = join(' ',$provides,$1);
  150.             }
  151.  
  152.         }
  153.         close SCR;
  154.  
  155.         @$scrlist = (@$scrlist,$scriptname);
  156.  
  157.         dprintf "\tprovides %s\n", $provides;
  158.         foreach my $elem (split(' ',$provides)) {
  159.             $$providedby{$elem} = join(' ', $$providedby{$elem},$scriptname);
  160.         }
  161.     }
  162. }
  163.  
  164. sub usage {
  165.     print <<EOF;
  166. usage:
  167.         mkinitrd_setup [-s|--scriptdir <scriptdir>]
  168.                 [-i|--installdir <installdir>]
  169.                 [-d|--debug] [-o|--offset <offset>] [-h|--help]
  170.  
  171.         Install mkinitrd scripts. Options are:
  172.         -s|--scriptdir  Install initrd scripts from dir <scriptdir>
  173.         -i|--installdir Install initrd scripts in dir <installdir>
  174.         -d|--debug      Enable debug output
  175.         -o|--offset     Use <offset> between script numbers.
  176.  
  177. EOF
  178. }
  179.  
  180. Getopt::Long::Configure('no_ignore_case');
  181.  
  182. if (!GetOptions('scriptdir|s=s' => \$scriptdir,
  183.                 'installdir|i=s' => \$installdir,
  184.                 'debug|d' => \$debug,
  185.                 'offset|o' => \$offset,
  186.                 'help|h' => \$dohelp
  187.                 )) {
  188.     usage();
  189.     exit 1;
  190. }
  191. if ( ! -d $installdir ) {
  192.     print "Installation directory $installdir does not exist!\n";
  193.     $dohelp++;
  194. }
  195. if ( $dohelp == 0 && ! -d "$installdir/setup" ) {
  196.     print "Installation directory $installdir/setup does not exist!\n";
  197.     $dohelp++;
  198. }
  199. if ( $dohelp == 0 && ! -d "$installdir/boot" ) {
  200.     print "Installation directory $installdir/boot does not exist!\n";
  201.     $dohelp++;
  202. }
  203. if ( $dohelp == 0 && ! -d $scriptdir ) {
  204.     print "Script directory $scriptdir does not exist!\n";
  205.     $dohelp++;
  206. }
  207. if ($dohelp > 0) {
  208.     usage();
  209.     exit 0;
  210. }
  211.  
  212. print "Scanning scripts ...\n";
  213. opendir(DIR, $scriptdir);
  214. my @scripts = grep { /.*\.sh$/ && -f "$scriptdir/$_" } readdir(DIR);
  215. closedir DIR;
  216.  
  217. # Scan scripts
  218. scan_section(@scripts);
  219.  
  220. print "Resolve dependencies ...\n";
  221. # Resolve dependencies
  222. foreach my $scr (@scripts_setup) {
  223.     resolve_dependency("setup", $scr);
  224.     dprintf "\n";
  225. }
  226.  
  227. foreach my $scr (@scripts_boot) {
  228.     resolve_dependency("boot", $scr);
  229.     dprintf "\n";
  230. }
  231.  
  232. print "Install symlinks in $installdir/setup ...\n";
  233. chdir "$installdir/setup" || die "Can't chdir to $installdir/setup : $!";
  234.  
  235. opendir(DIR, ".");
  236. my @links = grep { -l "$_" } readdir(DIR);
  237. closedir DIR;
  238.  
  239. foreach $_ (@links) {
  240.     unlink || printf "Can't unlink %s: %s\n", $_, $!;
  241. }
  242.  
  243. foreach (@scripts_setup) {
  244.     my $level = \%level_setup;
  245.     my $lvl = $$level{$_};
  246.     my $linkname;
  247.     my $target;
  248.     my $ret;
  249.  
  250.     $linkname = sprintf "%02d-%s.sh", $lvl, $_;
  251.     $target = sprintf "../scripts/setup-%s.sh", $_;
  252.     # Strictly speaking not required, but ...
  253.     next if -l $linkname;
  254.     dprintf "Linking %s to %s\n", $target, $linkname;
  255.     $ret = symlink($target, $linkname);
  256.     if ( $ret < 1 ) {
  257.         printf "Failed to create symlink %s: %s\n", $linkname, $!;
  258.     }
  259. }
  260.  
  261. print "Install symlinks in $installdir/boot ...\n";
  262. chdir "../boot" || die "Can't chdir to $installdir/boot: $!";
  263.  
  264. opendir(DIR, ".");
  265. @links = grep { -l "$_" } readdir(DIR);
  266. closedir DIR;
  267.  
  268. foreach $_ (@links) {
  269.     unlink || printf "Can't unlink %s: %s\n", $_, $!;
  270. }
  271.  
  272. foreach (@scripts_boot) {
  273.     my $level = \%level_boot;
  274.     my $lvl = $$level{$_};
  275.     my $linkname;
  276.     my $target;
  277.     my $ret;
  278.  
  279.     $linkname = sprintf "%02d-%s.sh", $lvl, $_;
  280.     $target = sprintf "../scripts/boot-%s.sh", $_;
  281.     # Strictly speaking not required, but ...
  282.     next if -l $linkname;
  283.     dprintf "Linking %s to %s\n", $target, $linkname;
  284.     $ret = symlink($target, $linkname);
  285.     if ( $ret < 1 ) {
  286.         printf "Failed to create symlink %s: %s\n", $linkname, $!;
  287.     }
  288. }
  289.  
  290.