#!/usr/bin/perl # $Id: lilo,v 1.1.1.1 2003/11/26 19:11:38 at Exp $ #-------------------------------------------------------------------- # Copyright (C) 2000, 2001 by MandrakeSoft. # Chmouel Boudjnah . # # Redistribution of this file is permitted under the terms of the GNU # Public License (GPL) #-------------------------------------------------------------------- # Copyright (C) 2003 by ALT Linux Team, # Alexey Tourbin . #-------------------------------------------------------------------- # description: Add/remove entry for lilo bootloader. use strict; use Getopt::Long qw(GetOptions); GetOptions "n|nolaunch" => \my $nolaunch, "r|remove" => \my $remove, "m|memtest" => \my $memtest and (my $version = shift) and (@ARGV == 0) or die "usage: $0 [-n|--nolaunch] [-r|--remove] [-m|--memtest] version\n"; use bootloader_utils qw(getroot); sub add_memtest { local $_ = shift; my $txt = "image=/boot/memtest-$version\n\tlabel=memtest86-$version"; /^image=\/boot\/memtest-\Q$version\E\b/m or $_ .= $txt; return $_; } sub remove_memtest { local $_ = shift; my @sections = split /^(?=image=)/m; @sections = grep { !/^image=\/boot\/memtest-\Q$version\E\b/ } @sections; return join "" => @sections; } sub kernel_label { local $_ = shift; s/\.//g; s/mdk|alt//; s/secure/sec/; s/enterprise/ent/; s/vanilla/vnl/; return $_; } sub add_kernel { local $_ = shift; my $label = kernel_label($version); my $root = getroot(); my $txt = "image=/boot/vmlinuz-$version\n\tlabel=$label\n\troot=$root\n\tread-only\n\toptional\n"; /^image=\/boot\/vmlinuz-\Q$version\E\b/ or $_ .= $txt; return $_; } sub remove_kernel { local $_ = shift; my @sections = split /^(?=image=)/m; @sections = grep { !/^image=\/boot\/vmlinuz-\Q$version\E\b/ } @sections; return join "" => @sections; } { # open lilo.conf for update my $lilo_conf = $ENV{LILO_CONF} || "/etc/lilo.conf"; open my $fh, "+<", $lilo_conf or die "Cannot open $lilo_conf\n"; local $/ = undef; my $content = <$fh>; # modify the content if ($memtest && $remove) { $content = remove_memtest($content); } elsif ($memtest) { $content = add_memtest($content); } elsif ($remove) { $content = remove_kernel($content); } else { $content = add_kernel($content); } # debug print $content; exit 0; # do the update seek $fh, 0, 0; print $fh $content; truncate $fh, tell $fh; # autoclosed }