~ubuntu-branches/ubuntu/breezy/gnome-system-tools/breezy

« back to all changes in this revision

Viewing changes to backends/boot.pl.in

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-10-14 18:49:22 UTC
  • Revision ID: james.westby@ubuntu.com-20041014184922-efvh7u8kpyy67a3z
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env perl
 
2
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 
3
 
 
4
# Boot manager configurator. Designed to be architecture and distribution independent.
 
5
#
 
6
# Copyright (C) 2000-2001 Ximian, Inc.
 
7
#
 
8
# Authors: Tambet Ingo     <tambet@ximian.com>
 
9
#          Arturo Espinosa <arturo@ximian.com>
 
10
#
 
11
# This program is free software; you can redistribute it and/or modify
 
12
# it under the terms of the GNU Library General Public License as published
 
13
# by the Free Software Foundation; either version 2 of the License, or
 
14
# (at your option) any later version.
 
15
#
 
16
# This program is distributed in the hope that it will be useful,
 
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
# GNU Library General Public License for more details.
 
20
#
 
21
# You should have received a copy of the GNU Library General Public License
 
22
# along with this program; if not, write to the Free Software
 
23
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 
24
 
 
25
 
 
26
$SCRIPTSDIR = "@scriptsdir@";
 
27
if ($SCRIPTSDIR =~ /^@scriptsdir[@]/)
 
28
{
 
29
    $SCRIPTSDIR = ".";
 
30
    $DOTIN = ".in";
 
31
}
 
32
 
 
33
require "$SCRIPTSDIR/general.pl$DOTIN";
 
34
require "$SCRIPTSDIR/util.pl$DOTIN";
 
35
require "$SCRIPTSDIR/file.pl$DOTIN";
 
36
require "$SCRIPTSDIR/xml.pl$DOTIN";
 
37
require "$SCRIPTSDIR/util.pl$DOTIN";
 
38
require "$SCRIPTSDIR/parse.pl$DOTIN";
 
39
require "$SCRIPTSDIR/replace.pl$DOTIN";
 
40
require "$SCRIPTSDIR/boot-grub.pl$DOTIN";
 
41
require "$SCRIPTSDIR/boot-lilo.pl$DOTIN";
 
42
require "$SCRIPTSDIR/boot-yaboot.pl$DOTIN";
 
43
 
 
44
# ----------------------------------------------------------------------------
 
45
# Fix functions.
 
46
#
 
47
# The main reason for these functions is to add default values to xml so the
 
48
# frontend doesn't have to know anything about lilo or its defaults.
 
49
 
 
50
sub gst_boot_fix_default
 
51
{
 
52
  my ($hash) = @_;
 
53
  my ($entries, $found, $entry);
 
54
  
 
55
  $entries = $$hash{"entry"};
 
56
  return unless $entries;
 
57
  
 
58
  # 'default'
 
59
  if (exists ($$hash{"default"}))
 
60
  {
 
61
    # Check if valid
 
62
    foreach $entry (@$entries)
 
63
    {
 
64
      if ($$hash{"default"} eq $$entry{"label"})
 
65
      {
 
66
        $found = 1;
 
67
        last;
 
68
      }
 
69
    }
 
70
    delete $$hash{"default"} unless $found;
 
71
  }
 
72
  
 
73
  if (not exists ($$hash{"default"}))
 
74
  {
 
75
    # No 'default', let's add
 
76
    foreach $entry (@$entries)
 
77
    {
 
78
      if ($$hash{"key"} == 0)
 
79
      {
 
80
        $$hash{"default"} = $$entry{"label"};
 
81
        last;
 
82
      }
 
83
    }
 
84
  }
 
85
}
 
86
 
 
87
# Internal. Should be run after fix_entries.
 
88
sub gst_boot_fix_globals
 
89
{
 
90
  my ($hash) = @_;
 
91
  
 
92
  return unless $hash;
 
93
  &gst_boot_fix_default ($hash);
 
94
}
 
95
 
 
96
# Internal
 
97
sub gst_boot_fix_entry
 
98
{
 
99
  my ($entry) = @_;
 
100
  my ($name);
 
101
  
 
102
  return unless $entry;
 
103
 
 
104
  $name = $$entry{"image"} if exists $$entry{"image"};
 
105
  $name = $$entry{"other"} if (!$name && exists $$entry{"other"});
 
106
    
 
107
  # Remove entries without entry identifier 'entry' or 'other'.
 
108
  unless ($name) {
 
109
    undef $entry;
 
110
    return;
 
111
  }
 
112
 
 
113
  # Add 'label' field which is optional in lilo.conf
 
114
  unless (exists $$entry{"label"})
 
115
  {
 
116
    $$entry{"label"} = $1 if ($name =~ /\S+\/(\S+)$/);
 
117
  }
 
118
}
 
119
 
 
120
# Internal
 
121
sub gst_boot_fix_entries
 
122
{
 
123
  my ($hash) = @_;
 
124
  my ($entries, $entry);
 
125
 
 
126
  return unless $hash;
 
127
 
 
128
  $entries = $$hash{"entry"};
 
129
  return unless $entries;
 
130
 
 
131
  foreach $entry (@$entries)
 
132
  {
 
133
    &gst_boot_fix_entry ($entry);
 
134
  }
 
135
}
 
136
 
 
137
sub gst_boot_fix
 
138
{
 
139
  my ($hash) = @_;
 
140
  
 
141
  return unless $hash;
 
142
  
 
143
  &gst_boot_fix_entries ($hash);
 
144
  &gst_boot_fix_globals ($hash);
 
145
}
 
146
 
 
147
# --------------------------------------------------------
 
148
 
 
149
sub gst_boot_bootloader_list
 
150
{
 
151
  my (@list);
 
152
 
 
153
  if (gst_file_locate_tool ("lilo"))
 
154
  {
 
155
    push @list, { "name" => "LILO",
 
156
                  "exec" => "lilo" };
 
157
  }
 
158
 
 
159
  if (gst_file_locate_tool ("grub-install"))
 
160
  {
 
161
    push @list, { "name" => "Grub",
 
162
                  "exec" => "grub" };
 
163
  }
 
164
 
 
165
  if (gst_file_locate_tool ("ybin"))
 
166
  {
 
167
    push @list, { "name" => "YaBoot",
 
168
                  "exec" => "yaboot" };
 
169
  }
 
170
 
 
171
  return (scalar @list, { "bootloaders" => { "bootloader" => \@list }});
 
172
}
 
173
 
 
174
sub gst_boot_bootloader_get
 
175
{
 
176
  my ($current_tool, $current_date, $bootl, $date);
 
177
 
 
178
  $current_date = 0;
 
179
  $bootl = gst_file_locate_tool ("lilo");
 
180
  if ($bootl ne "")
 
181
  {
 
182
    $date = (stat ($bootl)) [8];
 
183
    if ($date > $current_date)
 
184
    {
 
185
      $current_date = $date;
 
186
      $current_tool = "lilo";
 
187
    }
 
188
  }
 
189
  
 
190
  $bootl = gst_file_locate_tool ("grub-install");
 
191
  if ($bootl ne "")
 
192
  {
 
193
    $date = (stat ($bootl)) [8];
 
194
    if ($date > $current_date)
 
195
    {
 
196
      $current_date = $date;
 
197
      $current_tool = "grub";
 
198
    }
 
199
  }
 
200
 
 
201
  $bootl = gst_file_locate_tool ("ybin");
 
202
  if ($bootl ne "")
 
203
  {
 
204
    $date = (stat ($bootl)) [8];
 
205
    if ($date > $current_date)
 
206
    {
 
207
      $current_date = $date;
 
208
      $current_tool = "yaboot";
 
209
    }
 
210
  }
 
211
  
 
212
  if ($current_date eq 0)
 
213
  {
 
214
    &gst_report ("platform_no_bootloader", $main::gst_dist);
 
215
    return undef;
 
216
  }
 
217
  return $current_tool;
 
218
}
 
219
 
 
220
 
 
221
sub gst_boot_entries_get
 
222
{
 
223
  my ($partition) = @_;
 
224
  my (%dist_attrib, @res, %fn, @entries, $entry);
 
225
  my ($dist, $value, $file, $proc);
 
226
  my ($entry, $j, $key, $tmp);
 
227
 
 
228
  %dist_attrib = &gst_boot_get_entry_parse_table ();
 
229
  %fn = %{$dist_attrib{"fn"}};
 
230
  $proc = $dist_attrib{"entries_get"};
 
231
  @entries = &$proc (\%fn);
 
232
 
 
233
  &gst_boot_grub_check_device_map ();
 
234
 
 
235
  ${$dist_attrib{"fn"}}{"PARTITION"} = $partition;
 
236
 
 
237
  $key = 0;
 
238
  foreach $entry (@entries)
 
239
  {
 
240
    foreach $j (keys (%fn))
 
241
    {
 
242
      $tmp = &gst_parse_expand ($fn{$j}, "key", $key);
 
243
      $ {$dist_attrib{"fn"}}{$j} = &gst_parse_expand ($tmp, "label", $entry);
 
244
    }
 
245
 
 
246
    $entry = &gst_parse_from_table ($dist_attrib{"fn"},
 
247
                                    $dist_attrib{"table"});
 
248
 
 
249
    if (exists ($$entry {"image"}))
 
250
    {
 
251
      # this is filled even when it's a linux style entry, delete it
 
252
      delete $$entry {"other"};
 
253
      push @res, $entry;
 
254
    }
 
255
    elsif (exists ($$entry {"other"}))
 
256
    {
 
257
      # this may be filled, but it's not necessary
 
258
      delete $$entry {"root"};
 
259
      push @res, $entry;
 
260
    }
 
261
 
 
262
    $key++;
 
263
  }
 
264
 
 
265
  return \@res;
 
266
}
 
267
 
 
268
 
 
269
sub gst_boot_conf_get
 
270
{
 
271
  my ($bootl) = @_;
 
272
  my $config_file = &gst_boot_grub_get_config_file ();
 
273
  my %tool_tables =
 
274
  (
 
275
       "lilo" =>
 
276
       {
 
277
         fn =>
 
278
         {
 
279
           LILO_CONF => "/etc/lilo.conf"
 
280
         },
 
281
         table =>
 
282
             [
 
283
              [ "partitions", \&gst_partition_scan_info ],
 
284
#              [ "pixmapsup", \&gst_parse_trivial,             0 ],
 
285
              [ "timeout",   \&gst_boot_lilo_parse_global,    LILO_CONF, "delay" ],
 
286
              [ "prompt",    \&gst_boot_lilo_parse_global_kw, LILO_CONF, "prompt"  ],
 
287
              [ "default",   \&gst_boot_lilo_parse_global,    LILO_CONF, "default" ],
 
288
              [ "boot",      \&gst_boot_lilo_parse_global,    LILO_CONF, "boot"    ],
 
289
#             [ "root",      \&gst_boot_lilo_parse_global,    LILO_CONF, "root"    ],
 
290
              [ "entry",     \&gst_boot_lilo_parse_entries,   LILO_CONF, "%partitions%" ],
 
291
              ] 
 
292
       },
 
293
 
 
294
       "grub" =>
 
295
       {
 
296
         fn =>
 
297
         {
 
298
           GRUB_CONF  => $config_file,
 
299
           DEVICE_MAP => "/boot/grub/device.map",
 
300
#           MTAB       => "/etc/mtab"
 
301
         },
 
302
         table =>
 
303
             [
 
304
              [ "partitions", \&gst_partition_scan_info ],
 
305
#              [ "pixmapsup", \&gst_parse_trivial,           1 ],
 
306
#              [ "pixmap",    \&gst_boot_grub_parse_pixmap,  [GRUB_CONF, DEVICE_MAP, MTAB] ],
 
307
              [ "timeout",   \&gst_boot_grub_parse_timeout, GRUB_CONF ],
 
308
              [ "prompt",    \&gst_boot_grub_parse_prompt,  GRUB_CONF ],
 
309
              [ "default",   \&gst_boot_grub_parse_default, GRUB_CONF ],
 
310
              [ "entry",     \&gst_boot_entries_get,        "%partitions%" ],
 
311
              ] 
 
312
       },
 
313
 
 
314
       "yaboot" =>
 
315
       {
 
316
         fn =>
 
317
         {
 
318
             YABOOT_CONF => "/etc/yaboot.conf",
 
319
         },
 
320
         table =>
 
321
             [
 
322
              [ "partitions", \&gst_partition_scan_info ],
 
323
              [ "timeout",    \&gst_boot_lilo_parse_global, YABOOT_CONF, "timeout" ],
 
324
              [ "default",    \&gst_boot_lilo_parse_global, YABOOT_CONF, "default" ],
 
325
              [ "boot",       \&gst_boot_lilo_parse_global, YABOOT_CONF, "boot" ],
 
326
              [ "root",       \&gst_boot_lilo_parse_global, YABOOT_CONF, "root" ],
 
327
              [ "entry",      \&gst_boot_yaboot_parse_entries, YABOOT_CONF, "%partitions%", "%root%" ]
 
328
             ]
 
329
       }
 
330
  );
 
331
       
 
332
  return &gst_parse_from_table ($ {$tool_tables{$bootl}}{"fn"},
 
333
                                $ {$tool_tables{$bootl}}{"table"});
 
334
}
 
335
 
 
336
sub gst_boot_get_entry_parse_table
 
337
{
 
338
  my $config_file = &gst_boot_grub_get_config_file ();
 
339
  my %tool_tables =
 
340
      (
 
341
         entries_get => \&gst_boot_grub_get_entries_fn,
 
342
         fn =>
 
343
         {
 
344
           KEY        => "#key#",
 
345
           LABEL      => "#label#",
 
346
           GRUB_CONF  => $config_file,
 
347
           DEVICE_MAP => "/boot/grub/device.map",
 
348
           MTAB       => "/etc/mtab"
 
349
         },
 
350
         table =>
 
351
             [
 
352
              ["key",    \&gst_parse_trivial,          [KEY]],
 
353
              ["label",  \&gst_parse_trivial,          [LABEL]],
 
354
              ["root",   \&gst_boot_grub_parse_root,   [GRUB_CONF, DEVICE_MAP, KEY]],
 
355
              ["type",   \&gst_boot_grub_parse_type,   [GRUB_CONF, KEY, PARTITION], "%root%"],
 
356
              ["image",  \&gst_boot_grub_parse_image,  [GRUB_CONF, DEVICE_MAP, MTAB, KEY]],
 
357
              ["other",  \&gst_boot_grub_parse_other,  [GRUB_CONF, DEVICE_MAP, MTAB, KEY]],
 
358
              ["append", \&gst_boot_grub_parse_append, [GRUB_CONF, KEY]],
 
359
              ["initrd", \&gst_boot_grub_parse_initrd, [GRUB_CONF, DEVICE_MAP, MTAB, KEY]],
 
360
              ["module", \&gst_boot_grub_parse_module, [GRUB_CONF, DEVICE_MAP, MTAB, KEY]],
 
361
              ["password", \&gst_boot_grub_parse_password, [GRUB_CONF, KEY]]
 
362
             ]
 
363
       );
 
364
 
 
365
  return %tool_tables;
 
366
 
 
367
  &gst_report ("platform_no_table", $gst_dist);
 
368
  return undef;
 
369
}
 
370
 
 
371
sub gst_boot_entry_set
 
372
{
 
373
  my ($dist_attrib, $values_hash) = @_;
 
374
  my ($j, %fn, $res);
 
375
 
 
376
  %fn = %{$$dist_attrib{"fn"}};
 
377
  foreach $j (keys (%fn))
 
378
  {
 
379
    $fn{$j} = &gst_parse_expand ($ {$$dist_attrib{"fn"}}{$j}, "key", $$values_hash{"key"});
 
380
  }
 
381
  
 
382
  return &gst_replace_from_table (\%fn, $$dist_attrib{"table"}, $values_hash);
 
383
}
 
384
 
 
385
sub gst_boot_entries_set
 
386
{
 
387
  my ($entries) = @_;
 
388
  my (%dist_attrib, %fn, @old, @new, @del);
 
389
  my ($i, $proc, $entry);
 
390
  my ($tmp, $res);
 
391
  %dist_attrib = &gst_boot_get_entry_replace_table ();
 
392
  %fn = %{$dist_attrib{"fn"}};
 
393
 
 
394
  foreach $entry (@$entries)
 
395
  {
 
396
    $new[$$entry{"key"}] = $entry;
 
397
  }
 
398
  $proc = $dist_attrib{"entries_get"};
 
399
  @old = &$proc (\%fn);
 
400
  
 
401
  for ($i = 0; $i < &gst_max (scalar @old, scalar @$entries); $i++)
 
402
  {
 
403
    if ($new[$i] ne undef)
 
404
    {
 
405
      $tmp = &gst_boot_entry_set (\%dist_attrib, $new[$i]);
 
406
      $res = $tmp if !$res;
 
407
    }
 
408
    else
 
409
    {
 
410
      push @del, $i;
 
411
    }
 
412
  }
 
413
 
 
414
  # Delete all old entries that are not in the hash.
 
415
  $proc = $dist_attrib{"entries_del"};
 
416
  $tmp = &$proc (\%fn, \@del);
 
417
  $res = $tmp if !$res;
 
418
 
 
419
  return $res;
 
420
}
 
421
    
 
422
sub gst_boot_conf_set
 
423
{
 
424
  my $values_hash = $_[0];
 
425
  my $config_file = &gst_boot_grub_get_config_file ();
 
426
  $bootl = &gst_boot_bootloader_get ();
 
427
 
 
428
  my %tool_tables =
 
429
      (
 
430
       "lilo" => {
 
431
         fn => { LILO_CONF => "/etc/lilo.conf"},
 
432
         table => [
 
433
                   [ "boot",    \&gst_boot_lilo_replace_global,    LILO_CONF, "boot"    ],
 
434
#                  [ "root",    \&gst_boot_lilo_replace_global,    LILO_CONF, "root"    ],
 
435
             [ "timeout", \&gst_boot_lilo_replace_global,    LILO_CONF, "timeout" ],
 
436
             [ "timeout", \&gst_boot_lilo_replace_global,    LILO_CONF, "delay" ],
 
437
             [ "prompt",  \&gst_boot_lilo_replace_global_kw, LILO_CONF, "prompt"  ],
 
438
             [ "default", \&gst_boot_lilo_replace_global,    LILO_CONF, "default" ],
 
439
             [ "entry",   \&gst_boot_lilo_entries_set,       LILO_CONF ],
 
440
         ] 
 
441
       },
 
442
       
 
443
       "grub" =>
 
444
       {
 
445
         fn =>
 
446
         {
 
447
           GRUB_CONF  => $config_file,
 
448
           DEVICE_MAP => "/boot/grub/device.map",
 
449
           MTAB       => "/etc/mtab"
 
450
         },
 
451
         table =>
 
452
             [
 
453
 #             [ "pixmap",  \&gst_boot_grub_replace_pixmap,  [GRUB_CONF, DEVICE_MAP, MTAB] ],
 
454
              [ "timeout", \&gst_boot_grub_replace_timeout,  GRUB_CONF ],
 
455
              [ "prompt",  \&gst_boot_grub_replace_prompt,   GRUB_CONF ],
 
456
              [ "default", \&gst_boot_grub_replace_default,  GRUB_CONF , "%entry%"],
 
457
              [ "entry",   \&gst_boot_grub_entries_set, [GRUB_CONF, DEVICE_MAP, MTAB] ],
 
458
              ] 
 
459
       },
 
460
 
 
461
       "yaboot" =>
 
462
       {
 
463
         fn => { YABOOT_CONF => "/etc/yaboot.conf" },
 
464
         table =>
 
465
             [
 
466
              [ "boot",    \&gst_boot_lilo_replace_global, YABOOT_CONF, "boot"    ],
 
467
              [ "timeout", \&gst_boot_lilo_replace_global, YABOOT_CONF, "timeout" ],
 
468
              [ "default", \&gst_boot_lilo_replace_global, YABOOT_CONF, "default" ],
 
469
              [ "entry",   \&gst_boot_yaboot_entries_set,  YABOOT_CONF ],
 
470
             ]
 
471
         }
 
472
       );
 
473
 
 
474
  return &gst_replace_from_table ($ {$tool_tables{$bootl}}{"fn"},
 
475
                                  $ {$tool_tables{$bootl}}{"table"}, $values_hash);
 
476
}
 
477
 
 
478
sub gst_boot_get_entry_replace_table
 
479
{
 
480
  my $config_file = &gst_boot_grub_get_config_file ();
 
481
  my %tool_tables =
 
482
      (
 
483
         entries_get => \&gst_boot_grub_get_entries_fn,
 
484
         entries_del => \&gst_boot_grub_remove_entries,
 
485
         fn =>
 
486
         {
 
487
           KEY        => "#key#",
 
488
           GRUB_CONF  => $config_file,
 
489
           DEVICE_MAP => "/boot/grub/device.map",
 
490
           MTAB       => "/etc/mtab"
 
491
         },
 
492
         table =>
 
493
             [
 
494
              # label has to go first, because it creates the entry if non-existent.
 
495
              ["label",  \&gst_boot_grub_replace_label,  [GRUB_CONF, KEY]],
 
496
#              ["type",   \&gst_boot_grub_replace_type,   [GRUB_CONF, KEY]],
 
497
              ["root",   \&gst_boot_grub_replace_root,   [GRUB_CONF, DEVICE_MAP, KEY, "%type%"]],
 
498
              ["image",  \&gst_boot_grub_replace_image,  [GRUB_CONF, DEVICE_MAP, MTAB, KEY]],
 
499
              ["other",  \&gst_boot_grub_replace_other,  [GRUB_CONF, DEVICE_MAP, KEY]],
 
500
              ["append", \&gst_boot_grub_replace_append, [GRUB_CONF, DEVICE_MAP, KEY]],
 
501
              ]
 
502
       );
 
503
  return %tool_tables;
 
504
}
 
505
 
 
506