pf-tools commit: r743 [ccaillet-guest] - /trunk/sbin/mk_grub2opt

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Fri May 14 13:05:54 UTC 2010


Author: ccaillet-guest
Date: Fri May 14 13:05:48 2010
New Revision: 743

URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=743
Log:
Better when file is added

Added:
    trunk/sbin/mk_grub2opt

Added: trunk/sbin/mk_grub2opt
URL: http://svn.debian.org/wsvn/pf-tools/trunk/sbin/mk_grub2opt?rev=743&op=file
==============================================================================
--- trunk/sbin/mk_grub2opt (added)
+++ trunk/sbin/mk_grub2opt Fri May 14 13:05:48 2010
@@ -1,0 +1,137 @@
+#!/usr/bin/perl
+##
+##  $Id: mk_grub2opt 682 2008-11-18 11:50:45Z ccaillet-guest $
+##
+##  Copyright (C) 2007-2008 Christophe Caillet <quadchris at free.fr>
+##  Copyright (C) 2003-2005 Damien Clermonte <damien at sitadelle.com>
+##  Copyright (C) 2001-2003 Olivier Molteni <olivier at molteni.net>
+##
+##  This program is free software; you can redistribute it and/or
+##  modify it under the terms of the GNU General Public License
+##  as published by the Free Software Foundation; either version 2
+##  of the License, or (at your option) any later version.
+##
+##  This program is distributed in the hope that it will be useful,
+##  but WITHOUT ANY WARRANTY; without even the implied warranty of
+##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+##  GNU General Public License for more details.
+##
+##  You should have received a copy of the GNU General Public License
+##  along with this program; if not, write to the Free Software
+##  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+##
+
+use strict;
+use warnings;
+
+use Sys::Hostname ;
+use Digest::MD5;
+use PFTools::Net;
+use PFTools::Update;
+use File::Compare ;
+use File::Copy ;
+use Getopt::Long qw( :config ignore_case_always bundling );
+
+my $program = $0;
+$program =~ s%.*/%%; # cheap basename
+
+my $version = sprintf( "svn-r%s", q$Revision: 682 $ =~ /([\d.]+)/ );
+
+my $MENU_GRUB	= "/etc/default/grub" ;
+my $HOSTNAME	= hostname ;
+my $DEST	= "-" ;
+my $NET		= "GLOBAL:private-network" ;
+my $HELP	= 0 ;
+my $DEBUG	= 0 ;
+my $VERBOSE	= 0 ;
+
+sub Display_usage () {
+print STDERR << "# ENDHELP";
+    $program - version $version
+
+Usage:	$0 [options]
+	-h --help:	print help and exit
+	-v --verbose:	be more verbose
+	-s --src	source for GRUB configuration (default: /boot/grub/menu.lst)
+	-d --dst	destination for modified GRUB configuration (default: /boot/grub/menu.lst)
+	--host		hostname on which GRUB configuration applied
+	-n --net	Possiblitiy for defining an alternate PATH for private-network file (default GLOBAL:private-network)
+# ENDHELP
+}
+
+
+sub Change_kopt ($$$) {
+	my ( $cmd_line, $menulst, $dst ) = @_ ;
+
+	unless ( open ( MENU, $menulst ) ) {
+		warn "Unable top open ".$menulst."\n" ;
+		return 0 ;
+	}
+	my @tmp_grub = <MENU> ;
+	foreach ( @tmp_grub ) {
+		chomp ;
+		next if ( ! /^GRUB_CMDLINE_LINUX_DEFAULT=".*"$/ ) ;
+		s/\"$/ $cmd_line\"/ if ( defined $cmd_line && ! /$cmd_line$/ ) ;
+	}
+	
+	if ( $dst eq '-' ) {
+		foreach ( @tmp_grub ) {
+			print $_."\n" ;
+		}
+		return 1 ;
+	}
+	else {
+		unless ( open ( TMPDST, ">/tmp/menulst" ) ) {
+			warn "Unable to open temporary destination file /tmp/menulst\n" ;
+			return 0 ;
+		}
+		foreach ( @tmp_grub ) {
+			print TMPDST $_."\n" ;
+		}
+		close ( TMPDST ) ;
+		if ( compare ( '/tmp/menulst', $dst ) ) {
+			return move ( '/tmp/menulst', $dst) ;
+		} else {
+			if ( $DEBUG ) {
+				warn "No need to move /tmp/menulst to ".$dst." they are equals\n" ;
+				warn "Unlinking source file /tmp/menulst\n" ;
+			}
+			if ( ! unlink ( '/tmp/menulst' ) ) {
+				warn "Unable to unlink source file /tmp/menulst\n" ;
+				warn "Please clean it manually\n" ;
+			}
+		}
+		return 1 ;
+	}
+}
+
+##### MAIN
+GetOptions(
+    'src|s=s'	=> \$MENU_GRUB,
+    'dst|d=s'	=> \$DEST,
+    'host=s'	=> \$HOSTNAME,
+    'net|n=s'	=> \$NET,
+    'debug'	=> \$DEBUG,
+    'help|h'	=> \$HELP,
+    'verbose|v'	=> \$VERBOSE
+) or die "GetOptions error, try --help: $!\n";
+
+if ( $HELP ) {
+	Display_usage () ;
+	exit 0 ;
+}
+elsif ( ! -e $MENU_GRUB ) {
+	die "Unexistant GRUB configuration ".$MENU_GRUB."\n" ;
+}
+elsif ( -z $MENU_GRUB ) {
+	die "Empty configuration file for GRUB ".$MENU_GRUB."\n" ;
+}
+
+$DEST = "-" if ( $DEBUG ) ;
+$VERBOSE = 1 if ( $DEBUG ) ;
+
+my $NETWORK = Init_lib_net ( Get_source ( $NET ) ) ;
+if ( ! Change_kopt ( Get_Cmdline ( $NETWORK, $HOSTNAME ), $MENU_GRUB, $DEST ) ) {
+	die "Unable to change kernel options(s) into file ".$MENU_GRUB."\n" ;
+}
+




More information about the pf-tools-commits mailing list