pxe_select.in 6.01 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT

#
4
# Copyright (c) 2000-2012 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
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
#

# pxe_select sets the PXE boot program that should be loaded on a node,
# either one time or permanently.

sub usage() {
    print <<"EOF";
Usage: pxe_select -P [-c] [-1] [<path>] <node> [<node> ...]
 -h    Display this help message
 -d    Debug mode
 -c    Clear the PXE boot path for nodes. Do not provide a path.
       Either clears the one-time field (for -1) or resets to the default PXE boot.
 -1    Apply change to one-time PXE boot field
 -N    Do not restart dhcpd on a change
 path  Path to PXE boot program (must be in /tftpboot)
 node  Node identifiers (ie pcXX)
EOF
    exit(-1);
}
my $optlist = "hdc1lN";

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

$| = 1; #Turn off line buffering on output

# Configure variables
my $TB          = "@prefix@";
my $TBOPS       = "@TBSTATEDEMAIL@";
my $TBLOG	= "@TBLOGFACIL@";

# Testbed Support libraries
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use OSinfo;
use Node;
use English;
use Getopt::Std;
use Sys::Syslog;

# Constants
my $makeconf = "$TB/sbin/dhcpd_makeconf";
my $MBKERNEL = TB_OSID_MBKERNEL;
my %osidmap = # Map some magic OSIDs to op_modes
    ( $MBKERNEL => "MINIMAL");

# Functions

sub set_nextmode($;$);
sub set_boot_osid($);
sub node_opmode($);
sub debug($;$);
sub notify($);
sub info($);
sub fatal($);
sub warning($);

# Global vars
my $debug	= 0; # debug/verbose
my $oneshot	= 0; # apply change to next_boot_osid.
my $clear       = 0; # Clear the selected boot (def,temp,next).
my $list	= 0; # Just list the current settings.
my $nochange	= 0; # Do not restart dhcpd on a change
my @nodes       = ();
my $pxebootpath = "";

# Set up syslog
openlog("pxeselect", "pid", $TBLOG);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) { usage(); }
if (defined($options{"h"})) { usage(); }
if (defined($options{"d"})) { $debug=1; }
if (defined($options{"1"})) { $oneshot=1; }
if (defined($options{"c"})) { $clear=1; }
if (defined($options{"l"})) { $list=1; }
if (defined($options{"N"})) { $nochange=1; }

if (! ($clear || $list)) {
    usage()
	if (@ARGV < 2);
    $pxebootpath = shift();

    # Untaint args.
    if ($pxebootpath !~ /\.\./ &&
	$pxebootpath =~ /^(\/tftpboot\/[-\w\+\.\/]+)$/) {
	$pxebootpath = $1;
    }
    else {
	fatal("Bad path: '$pxebootpath'");
    }
}
else {
    usage()
	if (@ARGV < 1);
}

# Untaint the nodes.
foreach my $node ( @ARGV ) {
    if ($node =~ /^([-\@\w]+)$/) {
	$node = $1;
    }
    else {
	fatal("Bad node name: $node");
    }
    push(@nodes, $node);
}

#
# Figure out who called us. Only root, people with admin status
# in the DB, or members of the right project can do this.
#
if ($UID && !TBAdmin($UID)) {
    if ($list && !TBNodeAccessCheck($UID, TB_NODEACCESS_READINFO, @nodes)) {
	fatal("pxe_select: You do not have permission to access ".
	      "one or more of the nodes.\n");
    }
    if (!$list && !TBNodeAccessCheck($UID, TB_NODEACCESS_MODIFYINFO, @nodes)) {
	fatal("pxe_select: You do not have permission to modify ".
	      "one or more of the nodes.\n");
    }
}

153 154 155 156 157 158 159 160 161 162
#
# Warn them if the path doesn't actually exist.
#
# XXX this is not fatal because we overload the pathname to encode "special"
# boot information like pxelinux configs in some cases.
#
if ($pxebootpath && ! -e "$pxebootpath") {
    print STDERR "WARNING: '$pxebootpath' does not exist\n";
}

163 164 165 166 167 168 169 170 171
my $changed = 0;
foreach my $node (@nodes) {
    my $nodeobject = Node->Lookup($node);

    # In list mode, show all the IDs
    if ($list) {
	my $npb = $nodeobject->next_pxe_boot_path();
	my $pb = $nodeobject->pxe_boot_path();
	if (!$pb) {
172 173 174 175 176
	    if (!$nodeobject->NodeAttribute("pxe_boot_path", \$pb) &&
		defined($pb)) {
		$pb = "$pb (NODE DEFAULT)";
	    } elsif (!$nodeobject->NodeTypeAttribute("pxe_boot_path", \$pb) &&
		     defined($pb)) {
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
		$pb = "$pb (NODE TYPE DEFAULT)";
	    } else {
		$pb = "/tftpboot/pxeboot.emu (SYSTEM DEFAULT)";
	    }
	}
	print "$node: ";
	if ($npb) {
	    print "next_pxeboot=$npb";
	    print ", ";
	}
	if ($pb) {
	    print "pxeboot=$pb";
	}
	print "\n";
	next;
    }

    # The field to change in the DB. 
    my $field  = "pxe_boot_path";
    $field = "next_pxe_boot_path"
	if ($oneshot);
    my $did;
    if ($nodeobject->PXESelect($pxebootpath, $field, $debug, \$did) != 0) {
	fatal("PXESelect(): " . ($pxebootpath ? "$pxebootpath " : "") ."failed on $node");
    }
    $changed += $did;
}

#
# Need to remake the dhcpd.conf file if we changed a
# pxeboot path.
#
if (!$nochange && $changed) {
    system("$makeconf -i -r") == 0 ||
	notify("$makeconf failed!\n");
}

exit(0);

sub debug($;$)
{
    my $msg = shift;
    my $notice = shift || 0;
    my $prio="info";
    
    if ($notice) { $prio = "notice"; }
    
    syslog($prio, $msg);
    if ($debug) { print $msg; }
}

sub notify($)
{
    my $msg = shift;
    
    if (!$debug) {
	SENDMAIL($TBOPS, "pxe_select error", $msg);
    } 
    debug($msg, 1);
}

sub info($)
{
    my $msg = shift;
    
    debug($msg);
}

sub fatal($)
{
    my $msg = shift;
    
    notify("FATAL: $msg\n");
    exit(1);
}

sub warning($)
{
    my $msg = shift;
    
    info("WARNING: $msg\n");
}

# This is called when we exit with exit() or die()
END {
  closelog();
}