sched_reload.in 3.95 KB
Newer Older
1 2
#!/usr/bin/perl -wT
use English;
3
use Getopt::Std;
4 5 6 7 8 9 10
   
#
# Schedule the reloading of a disk partition on a node. If the node is
# currently not reserved, start the loading now after reserving it to 
# testbed:reloading. Otherwise, put the right info into the database, and 
# nfree will do it when the node gets freed.
# 
11
# usage: sched_reload [-f | -p] <imageid> <node> [node ...]
12 13 14
#
sub usage()
{
15 16 17
    print STDOUT "Usage: sched_reload [-f | -p] <imageid> <node> [node ...]\n".
	"Use the -f to force reload. Fail if node cannot be reserved.\n".
	"Use the -p to pend reload for the reload daemon.\n";
18 19
    exit(-1);
}
20
my  $optlist = "fp";
21 22 23 24

#
# Configure variables
#
25
my $TB     = "@prefix@";
26

27 28 29
#
# Load the Testbed support stuff. 
#
30 31
use lib "@prefix@/lib";
use libdb;
32

33
#
34 35
# XXX This stuff should not be hardwired in. If you change these, be sure
# to update the test in doreboot() in tmcd/tmcd.c.
36 37 38
# 
my $RELOADPID	= "emulab-ops";
my $RELOADEID	= "reloading";
39
my $PENDINGEID	= "reloadpending";
40

41 42
my $osload      = "$TB/bin/os_load";
my $nalloc      = "$TB/bin/nalloc";
43
my $name        = "";
44 45 46
my $error       = 0;
my $debug       = 0;
my $force	= 0;
47
my $pend	= 0;
48
my @nodes       = ();
49
my @row;
50 51 52 53 54 55 56 57

# 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

#
58 59 60 61 62 63 64
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
65
if (@ARGV < 2) {
66
    usage();
67
}
68 69 70
if (defined($options{"f"})) {
    $force = $options{"f"};
}
71 72 73 74 75 76
if (defined($options{"p"})) {
    $pend = $options{"p"};
}
if ($pend and $force) {
    usage();
}
77
my $imageid   = shift;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

#
# Untaint args.
#
if ($imageid =~ /^([-\@\w.\+]+)$/) {
    $imageid = $1;
}
else {
    die("Bad data in $imageid.");
}

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

100
#
101 102
# Root and admin types can do whatever they want.
# Mere users cannot schedule reloads.
103
#
104
if ($UID && !TBAdmin($UID)) {
105 106 107 108
    die("Only root or TB administrators can schedule disk reloads.\n");
}

#
109
# A loop.
110 111 112 113
# 
my @load_list=();
foreach my $node (@nodes) {
    my $pc = $node;
114
    my $allocated = 0;
115

116
    $sth = DBQueryFatal("select * from nodes where node_id='$pc'");
117 118 119 120 121
    if ($sth->num_rows() != 1) {
      print STDERR "Node $pc doesn't exist. Skipping $pc.\n";
      next;
    }
    
122
    print STDERR "Checking if $pc is reserved...";
123
    $sth = DBQueryFatal("select * from reserved where node_id='$pc'");
124
   
125 126
    if ( ($sth->num_rows()) < 1) {
        print STDERR "Available.\nReserving and adding to list.\n";
127 128 129 130 131 132 133 134 135
	my $eid;
	if ($pend) {
	    $eid = $PENDINGEID;
	}
	else {
	    $eid = $RELOADEID;
	}
	my $cmd = "$nalloc $RELOADPID $eid $pc";

136 137 138
        if ( system($cmd) != 0 ) {
	    print STDERR "WARNING: Could not reserve $pc!\n";
	} else {
139 140 141 142
	    #
	    # Kill the last_reservation so that whoever gets the node next
	    # won't be fooled into thinking a reload is required.
	    #
143
	    DBQueryFatal("delete from last_reservation where node_id='$pc'");
144
	    push (@load_list,$pc);
145
	    $allocated = 1;
146 147 148 149
	}
    } else {
        print STDERR "Reserved.\n";      
    }
150 151 152 153 154 155 156 157 158 159

    #
    # If force and not able to reserve, do not pend a reload.
    # 
    if ($force && !$allocated) {
	$error++;
	next;
    }

    # Put it in the reloads table so TMCD knows to free it.
160
    print STDERR "Scheduling reload of $imageid for $pc:\n";
161 162
    DBQueryFatal("replace into reloads ".
		 "(node_id, image_id) values ('$pc', '$imageid')");
163 164
}

165 166 167 168 169
if ($pend) {
    print STDOUT "Reload Scheduling Done!\n";
    exit $error;
}

170 171
if (@load_list > 0) {
  print STDERR "Running os_load on ",join(", ",@load_list),":\n";
172
  $cmd = "$osload $imageid @load_list";
173 174 175 176 177 178 179
  print STDERR "Calling '$cmd'\n";
  if ( system($cmd) != 0 ) {
    print STDERR "WARNING: OS_LOAD FAILED ON @load_list!\n";
  }
}

print STDOUT "Reload Scheduling Done!\n";
180
exit $error;