lib/SI/grub.pm
changeset 37 a61b92c60367
child 39 06bffc9d8977
equal deleted inserted replaced
36:bdc967bf50d2 37:a61b92c60367
       
     1 package SI::grub;
       
     2 
       
     3 use if $ENV{DEBUG} ~~ /grub|all/ => "Smart::Comments";
       
     4 use strict;
       
     5 use warnings;
       
     6 use File::Temp qw(tempdir);
       
     7 use IO::File;
       
     8 
       
     9 use SI::tools;
       
    10 
       
    11 sub restore(\%) {
       
    12     my $devs = shift;
       
    13 
       
    14     # assume the first ext is the boot device (should have
       
    15     # been labeled
       
    16 
       
    17     my ($boot, undef) = grep { $devs->{volume}{$_}{type} =~ /^ext/ }
       
    18     grep { $devs->{volume}{$_}{origin} eq "ptable" }
       
    19     grep { defined $devs->{volume}{$_}{type} } 
       
    20     sort keys %{$devs->{volume}};
       
    21 
       
    22     # now find the partition containing the fstab, it should
       
    23     # be the root file system
       
    24     my $tmpdir = tempdir(CLEANUP => 0);
       
    25 
       
    26     my @fs = 
       
    27 	grep { $devs->{volume}{$_}{type} =~ /^ext/ }
       
    28 	grep { defined $devs->{volume}{$_}{type} }
       
    29 	keys %{$devs->{volume}};
       
    30 
       
    31 
       
    32     my @mounted;
       
    33     eval {
       
    34     
       
    35 	foreach (@fs) {
       
    36 	    run("mount -oro -t $devs->{volume}{$_}{type} $_ $tmpdir");
       
    37 	    last if -f "$tmpdir/etc/fstab";
       
    38 	    run("umount $tmpdir");
       
    39 	    push @mounted, $tmpdir;
       
    40 	}
       
    41 
       
    42 	# and now try to mount the rest
       
    43 
       
    44 
       
    45 	my $fstab = new IO::File "$tmpdir/etc/fstab";
       
    46 	foreach (grep { /^\// } <$fstab>) {
       
    47 	    my ($dev, $mp, $type, $options, undef) = split;
       
    48 	    next if $options =~ /noauto/
       
    49 		or $mp eq "/"
       
    50 		or $mp !~ /^\//;
       
    51 	    run("mount -t $type $dev $tmpdir/$mp");
       
    52 	    push @mounted, "$tmpdir/$mp";
       
    53 	}
       
    54 	close($fstab);
       
    55 
       
    56 	run("mount -o bind /dev $tmpdir/dev");
       
    57 	push @mounted, "$tmpdir/dev";
       
    58 
       
    59 	run("chroot $tmpdir grub-mkdevicemap");
       
    60 	run("chroot $tmpdir /usr/sbin/grub-install '(hd0)'");
       
    61 	#system("/bin/bash --login");
       
    62     };
       
    63     if ($@) {
       
    64 	warn "** EVAL: $@\n";
       
    65     }
       
    66     map { system("umount $_") } reverse @mounted;
       
    67     rmdir($tmpdir);
       
    68 
       
    69 }
       
    70 
       
    71 1;
       
    72 # vim:sts=4 sw=4 aw ai sm: