summaryrefslogtreecommitdiff
path: root/scripts/vyatta-boot-image.pl
blob: 23dd7a061788703193a2a1abf50ebcd8dcaa484b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;

my $UNION_GRUB_CFG = '/live/image/boot/grub/grub.cfg';

# this function parses the grub config file and returns a hash reference
# of the parsed data.
sub parseGrubCfg {
  my $fd = undef;
  return undef if (!open($fd, '<', $UNION_GRUB_CFG));

  my %ghash = ();
  my @entries = ();
  my $in_entry = 0;
  my $idx = 0;
  while (<$fd>) {
    if ($in_entry) {
      if (/^}/) {
        $in_entry = 0;
        ++$idx;
      } elsif (/^\s+linux /) {
        my %ehash = (
          'idx' => $idx,
          'ver' => undef,
          'term' => undef,
          'reset' => undef
        );
        # kernel line
        if (/^\s+linux \/boot\/([^\/ ]+)\/.* boot=live /) {
          # union install
          $ehash{'ver'} = $1;
        } else {
          # old install
          $ehash{'ver'} = 'Old non-image installation';
        }
        if (/console=tty0.*console=ttyS0/) {
          $ehash{'term'} = 'serial';
        } else {
          $ehash{'term'} = 'kvm';
        }
        if (/standalone_root_pw_reset/) {
          $ehash{'reset'} = 1;
        } else {
          $ehash{'reset'} = 0;
        }
        push @entries, \%ehash;
      }
    } elsif (/^set default=(\d+)$/) {
      $ghash{'default'} = $1;
    } elsif (/^menuentry /) {
      $in_entry = 1;
    } 
  }
  close($fd);
  $ghash{'entries'} = \@entries;
  return \%ghash;
}

# this function takes the default terminal type and a list of all entries
# and returns the "boot list", i.e., the list for user to select which one
# to boot.
sub getBootList {
  my ($dterm, $entries) = @_;
  my %vhash = ();
  my @list = ();
  foreach (@{$entries}) {
    my ($ver, $term) = ($_->{'ver'}, $_->{'term'});
    next if ($_->{'reset'}); # skip password reset entry
    next if ($term ne $dterm); # not the default terminal
    next if (defined($vhash{$ver})); # version already in list
    $vhash{$ver} = 1;
    push @list, $_;
  }
  return \@list;
}

sub displayBootList {
  my ($didx, $entries) = @_;
  for my $i (0 .. $#{$entries}) {
    my $di = $i + 1; 
    my $ver = ${$entries}[$i]->{'ver'};
    my $m = '';
    if ($didx == ${$entries}[$i]->{'idx'}) {
      $m = ' (default boot)';
    }
    printf "  %2d: %s%s\n", $di, $ver, $m;
  }
}

my ($show, $sel) = (undef, undef);
GetOptions(
  'show' => \$show,
  'select' => \$sel
);

my $gref = parseGrubCfg();
if (!defined($gref)) {
  print "Cannot find GRUB configuration file. Exiting...\n";
  exit 1;
}
my $def_idx = $gref->{'default'};
my $entries = $gref->{'entries'};
if (!defined($def_idx) || !defined($entries)
    || !defined(${$entries}[$def_idx])) {
  print "Error parsing GRUB configuration file. Exiting...\n";
  exit 1;
}
my $def_term = ${$entries}[$def_idx]->{'term'};

my $bentries = getBootList($def_term, $entries);

print "The system currently has the following images installed:\n\n";
displayBootList($def_idx, $bentries);
print "\n";

exit 0 if ($show); # show-only. done.

print 'Select the default boot image: ';
my $resp = <STDIN>;
if (!defined($resp) || !($resp =~ /^\d+$/) || ($resp < 1)
    || ($resp > ($#{$bentries} + 1))) {
  print "Invalid selection. Default is not changed. Exiting...\n";
  exit 1;
}

print "\n";
$resp -= 1;
my $new_idx = ${$bentries}[$resp]->{'idx'};
my $new_ver = ${$bentries}[$resp]->{'ver'};
system("sed -i 's/^set default=.*\$/set default=$new_idx/' $UNION_GRUB_CFG");
if ($? >> 8) {
  print "Failed to set the default boot image. Exiting...\n";
  exit 1;
}
print <<EOF;
Default boot image has been set to "$new_ver".
You need to reboot the system to start the new default image.

EOF
exit 0;