summaryrefslogtreecommitdiffstats
path: root/perl-install/standalone/bootloader-config
blob: 2f1aed444c5a2d087a11f4ebd38dee59980cb102 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/perl

use lib qw(/usr/lib/libDrakX);
use Getopt::Long;

use common;
use bootloader;

$::isStandalone = 1; #- not using standalone which messes with @ARGV and usage

my %options = (
	       'action=s' => \ (my $action),

	       'label=s' => \ (my $label),

	       'chainload=s' => \ (my $chainload),

	       'image=s' => \ (my $image), 
	       'kernel-version=s' => \ (my $kernel_version), 

	       'initrd-options=s' => \ (my $initrd_options),
	       'no-short-name' => \ (my $no_short_name),
	       'no-entry' => \ (my $no_entry),
	       'no-launch' => \ (my $no_launch),
	       'no-initrd' => \ (my $no_initrd),
	      );

GetOptions(%options) or die <<'EOF';
usage: Releve --action add-kernel (--image <vmlinuz> | --kernel-version <2.6.xxx>) [--label <...>] [--no-short-name] [--initrd-options <options>]
       Releve --action remove-kernel (--image <vmlinuz> | --kernel-version <2.6.xxx>)

       Releve --action add-entry (--image <image> | --chainload <device>) --label <...>
       Releve --action remove-entry (--image <image> | --chainload <device> | --label <...>)

       Releve --action update-splash
       Releve --action remove-splash
EOF

my @known_actions = qw(add-kernel remove-kernel update-splash remove-splash detectloader add-entry remove-entry);
$action && member($action, @known_actions) or die "<action> must be one of " . join(' ', @known_actions) . "\n";

if ($image) {
    if (my $ver = bootloader::vmlinuz2version($image)) {
	if ($kernel_version) {
	    $kernel_version eq $ver or die "$kernel_version and $ver don't match (hint: don't pass both --image and --kernel-version)\n";
	} else {
	    $kernel_version = $ver;
	}
    }
    $image = "/boot/$image" if $image !~ m!^/!;
} elsif ($kernel_version) {
    $image = "/boot/vmlinuz-$kernel_version";
}


my $all_hds = fsedit::get_hds();
fs::get_info_from_fstab($all_hds, '');
my $hds = $all_hds->{hds};

my $bootloader = bootloader::read([ fsedit::get_fstab(@$hds) ]) or die "Cannot find a boot loader installed\n";


$action =~ s/-/_/g;
$::{$action}->();


#-###############################################################################
sub remove_now_broken_boot_symlinks() {
    foreach (glob("/boot/vmlinuz*"), glob("/boot/initrd*")) {
	-l $_ && !-e $_ or next;
	log::l("removing now broken symlink $_");
	unlink $_;
    }
}
sub remove_kernel() {
    unlink "/lib/modules/$kernel_version/build";
    remove_now_broken_boot_symlinks();


    my ($to_remove, $to_keep) = partition {
	$_->{kernel_or_dev} && $_->{kernel_or_dev} eq $image;
    } @{$bootloader->{entries}};

    $_->{initrd} && unlink $_->{initrd} foreach @$to_remove;

#    foreach (@$to_keep) {
#	$_->{kernel_or_dev} && bootloader::expand_vmlinuz_symlink($_->{kernel_or_dev}) eq $image or next;
#    }

    @{$bootloader->{entries}} = @$to_keep;


    modify_bootloader();
}    


#-###############################################################################
sub add_kernel() {
    bootloader::create_link_source();

    my $kernel_str = bootloader::vmlinuz2kernel_str($image);
    $kernel_str->{use_long_name} = $no_short_name;

    my $root_part = fsedit::get_root([ fsedit::get_fstab(@$hds) ]);
    bootloader::add_kernel($bootloader, $kernel_str, 
			   { 
			    root => "/dev/$root_part->{device}", 
			    initrd_options => $initrd_options,
			    if_($label, label => $label),
			    if_($bootloader->{default_vga}, vga => $bootloader->{default_vga}),
			   }, $no_short_name, $no_initrd);

    modify_bootloader();
}

sub modify_bootloader() {
    !$no_entry or return;

    bootloader::action($bootloader, 'write', $hds);
    bootloader::action($bootloader, 'when_config_changed', $hds) if !$no_launch;
}

#-###############################################################################
sub add_entry() {
    $label or die "you must give a label\n";
    ($image xor $chainload) or die "you must give an image or a chainload\n";

    my $entry = {
		  type => $image ? 'image' : 'other',
		  label => $label,
		  kernel_or_dev => $image || $other,
		 };
    bootloader::add_entry($bootloader, $entry);
    modify_bootloader();
}

sub remove_entry() {
    listlength(grep { $_ } $label, $image, $chainload) == 1 or die "you must give one of --label, --image and --chainload\n";

    my $e = $label ? bootloader::get_label($label, $bootloader) : 
                     find { $_->{kernel_or_dev} && $_->{kernel_or_dev} eq ($image || $chainload) } @{$bootloader->{entries}};

    if ($e) {
	@{$bootloader->{entries}} = grep { $_ != $e } @{$bootloader->{entries}};
    }

    modify_bootloader();
}

#-###############################################################################
sub update_splash() {
    foreach (@{$bootloader->{entries}}) {
	bootloader::make_boot_splash($_->{initrd}, $_->{vga}) if $_->{initrd};
    }
    bootloader::action($bootloader, 'when_config_changed', $hds) if !$no_launch;
}

sub remove_splash() {
    foreach (@{$bootloader->{entries}}) {
	bootloader::remove_boot_splash($_->{initrd}) if $_->{initrd};
    }
    bootloader::action($bootloader, 'when_config_changed', $hds) if !$no_launch;
}

sub detectloader() {
    print uc(bootloader::main_method($bootloader->{method})), "\n" if $bootloader;
}