bscontrol.in 20 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2013-2014 University of Utah and the Flux Group.
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
# 
# {{{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/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;

#
# Block storage server control utility.
#
# Contacts one or all available block storage servers to perform actions.
# Commands:
#
# bscontrol list
#	List all configured blockstore servers.
#
# bscontrol [ -S server [ -P pool ] ] avail
#	For the named servers (or all if none specified), print out
#	how much storage is available.
#
# bscontrol [ -S server [ -P pool ] ] info
#	Return detailed info the listed block servers (or all) including the
#	name of all existant blockstores and their sizes, attributes, etc.
#
# The following commands are for persistent blockstores.
# For these, a blockstore name, "bsname", had better be unique.
#
48
# bscontrol [ -S server [ -P pool ] ] [-l leaseidx] [-f fstype] -s size -t type create bsname
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#	Create a blockstore of the given size with the given name.
#	If the server is not specified, we pick the "best" server,
#	based on availability of space.
#
# bscontrol destroy [ -S server -P pool ] bsname
#	Destroy the named blockstore freeing up the space.
#	Here bsname needs to be unique across all servers
#	or the server and pool need to be explicitly specified.
#
# bscontrol [ -S server [ -P pool ] ] copy from-bsname to-bsname
#	Do an efficient copy of one blockstore to another. Use the
#	server/pool arguments to force a specific placement of to-bsname.
#
sub usage()
{
    print STDERR "Usage: bscontrol command args\n";
    print STDERR "   -h       This message\n";
    print STDERR "   -d       Print additional debug info\n";
67 68 69 70
    print STDERR "commands:\n";
    print STDERR "   list     List all blockstore servers\n";
    print STDERR "   avail    For each server, show available storage\n";
    print STDERR "   info     For each server, show all extant volumes\n";
71 72
    exit(-1);
}
73
my $optlist  = "hds:t:l:S:P:Ff:";
74 75 76 77 78 79
my $debug = 0;
my $server;
my $pool;
my $size;
my $type = "stdataset";
my $leaseidx = 0;
80
my $fstype = "";
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
my $fakeit = 0;

# Protos
sub fatal($);
sub bs_list($$$@);
sub bs_avail($$$@);
sub bs_info($$$@);
sub bs_create($$$@);
sub bs_destory($$$@);

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

96
my $PROXYCMD	 = "/usr/local/etc/emulab/bscontrol.proxy";
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
my $SSH		 = "ssh -n -o ConnectTimeout=2 -o Protocol=2 -o BatchMode=yes -o StrictHostKeyChecking=no";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use emutil;
use Lease;
use Blockstore;
use Experiment;
use User;
use Project;

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

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";

#
# We don't want to run this script unless its the real version.
# That is, it must be setuid root. 
#
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
}

# Commands
my %cmds = (
   "list"   => \&bs_list,
   "avail"  => \&bs_avail,
   "info"   => \&bs_info,
   "create" => \&bs_create,
   "destroy" => \&bs_destroy,
);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{h})) {
    usage();
}
if (defined($options{d})) {
    $debug = 1;
}
if (defined($options{S})) {
    if ($options{S} =~ /^([-\w]+)$/) {
	$server = $1;
    }
}
if (defined($options{P})) {
    $pool = $options{P};
    if ($options{P} =~ /^([-\w]+)$/) {
	$pool = $1;
    }
}
if (defined($options{s})) {
Mike Hibler's avatar
Mike Hibler committed
165 166 167
    if ($options{s} =~ /^(\d+)$/) {
	$size = $1;
    }
168 169 170 171 172 173 174
}
if (defined($options{t})) {
    $type = $options{t};
}
if (defined($options{l})) {
    $leaseidx = $options{l};
}
175 176 177
if (defined($options{f})) {
    $fstype = $options{f};
}
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
if (defined($options{F})) {
    $fakeit = 1;
}
if (@ARGV < 1) {
    usage();
}

my $cmd = shift;
if (!exists($cmds{$cmd})) {
    print STDERR "Unrecognized command '$cmd', should be one of:\n";
    print STDERR "  ", join(", ", keys %cmds), "\n";
    usage();
}

$fakeit = 1
    if (-e "$TB/etc/bs-servers.txt");
if ($fakeit) {
    print STDERR "WARNING: bscontrol operating in fake mode!\n";
}

exit(&{$cmds{$cmd}}($server, $pool, $size, @ARGV));

#
# Look in the database to find the storage servers
#
sub bsservers()
{
    my @bs = ();

    if ($fakeit) {
	if (-e "$TB/etc/bs-servers.txt") {
	    foreach my $bs (`cat $TB/etc/bs-servers.txt`) {
		# keep taint happy
		if ($bs =~ /^([-\w]+)$/) {
		    push @bs, $1;
		}
	    }
	}
	return @bs;
    }

    my $result =
	DBQueryFatal("SELECT node_id FROM reserved WHERE erole='storagehost'".
		     " order by node_id");
    while (my ($node) = $result->fetchrow_array) {
	push @bs, $node;
    }
    return @bs;
}

sub fake_change($$$)
{
    my ($file,$pool,$used) = @_;

    my $line = `grep 'pool=$pool ' $file`;
    if ($? == 0 && $line =~ /size=(\d+) avail=(\d+)/) {
	my $size = $1;
	my $oavail = $2;
	my $navail = $oavail - $used;
	if (system("sed -i '' -e 's/pool=$pool size=$size avail=$oavail/pool=$pool size=$size avail=$navail/' $file") == 0) {
	    return 1;
	}
    }
    return 0;
}

sub fake_cmd($$$)
{
    my ($host, $cmdstr, $outref) = @_;
    my @output = ();

    my ($fcmd,$fargs);
    if ($cmdstr =~ /^$PROXYCMD\s+(\S+)(?:\s+(.*))?$/) {
	$fcmd = $1;
	$fargs = $2;
    } else {
	push @output, "Unrecognized command string '$cmdstr'";
	$$outref = \@output;
	return -1;
    }
    if ($fcmd =~ /^(volumes|pools)$/) {
	if (-e "$TB/etc/bs-$fcmd-$host.txt") {
	    @output = `cat $TB/etc/bs-$fcmd-$host.txt`;
	    chomp @output;
	}
	$$outref = \@output;
	return 0;
    } elsif ($fcmd eq "create") {
	# in format: create pool volume size
	# out format: volume=lease-2 pool=rz-1 size=95
	if ($fargs =~ /^(\S+)\s+(\S+)\s+(\S+)/) {
	    my $pool = $1;
	    my $vol = $2;
	    my $size = $3;

	    my $vfile = "$TB/etc/bs-volumes-$host.txt";
	    my $pfile = "$TB/etc/bs-pools-$host.txt";
	    system("cp -f $vfile $vfile.new");
	    system("cp -f $pfile $pfile.new");

	    system("echo 'volume=$vol pool=$pool size=$size' >> $vfile.new");

	    if (fake_change("$pfile.new", $pool, $size) &&
		rename($pfile, "$pfile.old") &&
		rename("$pfile.new", $pfile) &&
		rename($vfile, "$vfile.old") &&
		rename("$vfile.new", $vfile)) {
		$$outref = \@output;
		return 0;
	    }
	}
	push @output, "create $fargs failed!";
	$$outref = \@output;
	return -1;
    } elsif ($fcmd eq "destroy") {
	# in format: destroy pool volume
	if ($fargs =~ /^(\S+)\s+(\S+)/) {
	    my $pool = $1;
	    my $volume = $2;
	    my $size;

	    my $vfile = "$TB/etc/bs-volumes-$host.txt";
	    my $pfile = "$TB/etc/bs-pools-$host.txt";
	    system("cp -f $vfile $vfile.new");
	    system("cp -f $pfile $pfile.new");

	    my $line = `grep 'volume=$volume pool=$pool' $vfile`;
	    if ($line =~ /size=(\d+)/) {
		$size = $1;
		if (fake_change("$pfile.new", $pool, -$size) &&
		    system("sed -i '' -e '/volume=$volume pool=$pool /d' $vfile.new") == 0 &&
		rename($pfile, "$pfile.old") &&
		rename("$pfile.new", $pfile) &&
		rename($vfile, "$vfile.old") &&
		rename("$vfile.new", $vfile)) {
		    $$outref = \@output;
		    return 0;
		}
	    }
	}
	push @output, "destroy $fargs failed!";
	$$outref = \@output;
	return -1;
    } else {
	push @output, "Cannot fake '$fcmd' right now";
	$$outref = \@output;
	return -1;
    }
}

#
# Execute a command on a remote blockstore server and return the output.
#
sub bsserver_cmd($$$)
{
    my ($host, $cmdstr, $outref) = @_;
    my @output = ();
    my $stat = 0;

    if ($fakeit) {
	print STDERR "Faking '$SSH $host $cmdstr'\n"
	    if ($debug);
	return fake_cmd($host, $cmdstr, $outref);
    }

    print STDERR "Doing '$SSH $host $cmdstr'\n"
	if ($debug);

    my $SAVEUID = $UID;
    $UID = 0;
    @output = `$SSH $host $cmdstr 2>&1`;
    $UID = $SAVEUID;
    if ($?) {
	$stat = $? >> 8;
	print STDERR "*** WARNING: ssh to $host failed ($stat)!\n";
    }

    print STDERR "Got output:\n", @output
	if ($debug > 1);
    chomp(@output);
    $$outref = \@output;
    return $stat;
}

sub parseattrs($)
{
    my ($line) = @_;
    my %attrs = ();

    foreach my $pair (split(/\s+/, $line)) {
	if ($pair =~ /^(\S+)=(\S+)$/) {
	    $attrs{$1} = $2;
	}
    }

    return \%attrs;
}

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
#
# Augment pool atrributes with Emulab blockstore attributes.
#
sub get_poolattrs($)
{
    my ($attrs) = @_;

    my $server = $attrs->{'server'};
    my $pool = $attrs->{'pool'};
    my $bstore = Blockstore->Lookup($server, $pool);
    if ($bstore) {
	my $bsidx = $bstore->bsidx();
	my $result =
	    DBQueryFatal("SELECT attrvalue FROM blockstore_attributes ".
			 "WHERE bsidx='$bsidx' and attrkey='allowed_bstores'");
	if ($result && $result->numrows) {
	    my ($allows) = $result->fetchrow_array;
	    $attrs->{'allowed'} = $allows;
	} else {
	    # XXX if nothing specified, assume non-persistent blockstores
	    $attrs->{'allowed'} = "volatile";
	}
    } else {
	print STDERR "WARNING: no blockstore object for $server/$pool\n";
    }

    return $attrs;
}

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
#
# Augment volume attributes with Emulab blockstore attributes gleaned
# from the volume attributes.
#
sub get_bsattrs($)
{
    my ($attrs) = @_;

    #
    # iname info implies that the volume is exported via iSCSI
    # Parse out good stuff like the iqn, pid, eid, and vname.
    # Get even more good stuff from the experiment.
    #
    my $iname = $attrs->{'iname'};
    if ($iname) {
	my ($iqn,$pid,$eid,$vname) = split(':', $iname);

	$attrs->{'iqn'} = $iqn;
	$attrs->{'pid'} = $pid;
	$attrs->{'eid'} = $eid;
	$attrs->{'vname'} = $vname;
	$attrs->{'uname'} = "$pid/$eid/$vname";
	if (defined($pid) && defined($eid)) {
	    my $expt;

	    if (!($expt = Experiment->Lookup("$pid/$eid"))) {
		print STDERR "WARNING: no experiment info associated with $pid/$eid\n";
		$attrs->{'swapper'} = $attrs->{'owner'} = "??";
	    } else {
		$attrs->{'swapper'} = $expt->swapper();
		$attrs->{'owner'} = $expt->creator();
	    }
	}
	$attrs->{'active'} = 1;
    } else {
	$attrs->{'active'} = 0;
    }

    #
    # If the volume name is of the form 'lease-<id>' then this
    # is a persistent dataset. Extract more info from the lease.
    # Note that attributes from the lease override those from a
    # swapped in experiment using the lease (e.g., owner and uname).
    #
    my $volume = $attrs->{'volume'};
    if ($volume =~ /^lease-(\d+)$/) {
	my $lidx = $1;

	$attrs->{'lidx'} = $lidx;
	my $lease = Lease->Lookup($lidx);
	if (!$lease) {
	    print STDERR "WARNING: no lease info associated with persistent lease $lidx\n";
	    $attrs->{'lid'} = $attrs->{'owner'} = $attrs->{'lpid'} =
		$attrs->{'type'} = $attrs->{'expiration'} = "??";
	    $attrs->{'uname'} = $volume;
	} else {
	    $attrs->{'lid'} = $lease->lease_id();
	    $attrs->{'owner'} = $lease->owner();
	    $attrs->{'lpid'} = $lease->pid();
	    $attrs->{'type'} = $lease->type();
	    $attrs->{'expiration'} = $lease->expiration();
	    $attrs->{'uname'} = $attrs->{'lpid'} . "/" . $attrs->{'lid'};
	}
	$attrs->{'persist'} = 1;

    } else {
	$attrs->{'type'} = "volatile";
	$attrs->{'persist'} = 0;
    }

    return $attrs;
}

sub getpools($$)
{
    my ($dsrv,$dpool) = @_;
    my %pools = ();

    foreach my $srv (bsservers()) {
	next
	    if (defined($dsrv) && $dsrv ne $srv);

	my $outref;
	if (bsserver_cmd($srv, "$PROXYCMD pools", \$outref) == 0) {
	    foreach my $pstr (@$outref) {
		my $attrs = parseattrs($pstr);
		my $pool = $attrs->{'pool'};
		if (!defined($dpool) || $dpool eq $pool) {
		    $attrs->{'server'} = $srv;
494
		    $attrs = get_poolattrs($attrs);
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
		    $pools{"$srv/$pool"} = $attrs;
		}
	    }
	} else {
	    print STDERR "*** could not get pools from $srv, error:\n";
	    foreach my $str (@$outref) {
		print STDERR "  $str\n";
	    }
	}
    }

    return \%pools;
}

sub getvolumes($$)
{
    my ($dsrv,$dpool) = @_;
    my %volumes = ();

    foreach my $srv (bsservers()) {
	next
	    if (defined($dsrv) && $dsrv ne $srv);

	my $outref;
	if (bsserver_cmd($srv, "$PROXYCMD volumes", \$outref) == 0) {
	    foreach my $vstr (@$outref) {
		my $attrs = parseattrs($vstr);
		my $pool = $attrs->{'pool'};
		if (!defined($dpool) || $dpool eq $pool) {
		    $attrs->{'server'} = $srv;
		    my $vol = $attrs->{'volume'};
		    $volumes{"$srv/$pool/$vol"} = $attrs;
		}
	    }
	} else {
	    print STDERR "*** could not get volumes from $srv, error:\n";
	    foreach my $str (@$outref) {
		print STDERR "  $str\n";
	    }
	}
    }

    return \%volumes;
}

sub getblockstores($$)
{
    my ($dsrv,$dpool) = @_;
    my %bstores = ();

    foreach my $srv (bsservers()) {
	next
	    if (defined($dsrv) && $dsrv ne $srv);

	my $outref;
	if (bsserver_cmd($srv, "$PROXYCMD volumes", \$outref) == 0) {
	    foreach my $vstr (@$outref) {
		my $attrs = get_bsattrs(parseattrs($vstr));
		if (!defined($dpool) || $dpool eq $attrs->{'pool'}) {
		    $attrs->{'server'} = $srv;
555 556 557 558 559 560 561

		    if (!exists($attrs->{'uname'})) {
			$attrs->{'uname'} = "NON-EMULAB/$srv-" .
			    $attrs->{'pool'} . "-" . $attrs->{'volume'};
			$attrs->{'type'} = "--";
		    }

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
		    $bstores{$attrs->{'uname'}} = $attrs;
		}
	    }
	} else {
	    print STDERR "*** could not get blockstores from $srv, error:\n";
	    foreach my $str (@$outref) {
		print STDERR "  $str\n";
	    }
	}
    }

    return \%bstores;
}

sub bs_list($$$@)
{
    foreach my $srv (bsservers()) {
	print "$srv\n";
    }
}

sub bs_avail($$$@)
{
    my ($dsrv,$dpool,undef) = @_;

    my $poolref = getpools($dsrv, $dpool);

    if (keys(%{$poolref}) > 0) {
590 591
	printf("%-12s %-12s %10s %10s %-5s %-s\n",
	       "Server", "Pool", "Size", "Avail", "Free %", "Allows");
592 593 594 595 596

	foreach my $pstr (sort keys(%{$poolref})) {
	    my $attrs = $poolref->{$pstr};
	    my $size = ($attrs->{'size'} ? $attrs->{'size'} : 1);
	    my $pct = $attrs->{'avail'} / $size * 100;
597
	    printf("%-12s %-12s %10s %10s %5.1f %-s\n",
598
		   $attrs->{'server'}, $attrs->{'pool'},
599
		   $size, $attrs->{'avail'}, $pct, $attrs->{'allowed'});
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	}
    }

    return 0;
}

sub bs_info($$$@)
{
    my ($dsrv,$dpool,undef) = @_;

    my $bsref = getblockstores($dsrv, $dpool);

    if (keys(%{$bsref}) > 0) {
	printf("%-32s %-24s %-10s %10s %-s\n",
	       "Unique ID", "Server/Pool/Volume", "Type", "Size", "Exported as");

	foreach my $bs (sort keys(%{$bsref})) {
	    my $attrs = $bsref->{$bs};
	    printf("%-32s %-24s %-10s %10s %s\n", $bs,
		   $attrs->{'server'} . "/" . $attrs->{'pool'} . "/" . $attrs->{'volume'},
		   $attrs->{'type'}, $attrs->{'size'},
		   ($attrs->{'active'} ? $attrs->{'iname'} : ""));
	}
    }

    return 0;
}

sub bs_create($$$@)
{
    my ($srv,$pool,$size,$name) = @_;

    if (!defined($size)) {
	fatal("create: must specify a size in MiB (-s)");
    }
Mike Hibler's avatar
Mike Hibler committed
635 636 637 638
    if (defined($name) && $name =~ /^([-\w]+)$/) {
	$name = $1;
    } else {
	fatal("create: must specify a valid volume name");
639 640 641 642 643
    }

    if ($leaseidx !~ /^\d+$/) {
	fatal("create: lease index must be an integer");
    }
644 645 646 647 648 649 650
    if ($fstype) {
	if ($fstype !~ /^(ext2|ext3|ext4|ufs)$/) {
	    fatal("create: fstype must be one of ext2|ext3|ext4|ufs");
	} else {
	    $fstype = $1;
	}
    }
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
    if ($type !~ /^(st|lt)dataset$/) {
	fatal("create: type must be either 'stdataset' or 'ltdataset'");
    }

    # get all qualified pools
    my $poolref = getpools($srv, $pool);

    if ($debug) {
	print STDERR
	    "create(", ($srv ? $srv : "-"), "/", ($pool ? $pool : "-"),
	    ", $size, $name): found candidate pools:\n";
	foreach my $pstr (sort keys(%{$poolref})) {
	    my $attrs = $poolref->{$pstr};
	    print STDERR "  ", $attrs->{'server'}, "/", $attrs->{'pool'},
		": avail=", $attrs->{'avail'}, "\n";
	}
    }

669
    # eliminate those without enough space or that don't allow the type
670 671 672 673 674 675 676 677 678
    my %servercount = ();
    my @pools = ();
    foreach my $pstr (sort keys(%{$poolref})) {
	my $attrs = $poolref->{$pstr};
	if (!exists($servercount{$attrs->{'server'}})) {
	    $servercount{$attrs->{'server'}} = 1;
	} else {
	    $servercount{$attrs->{'server'}}++;
	}
679 680 681 682
	if ($attrs->{'size'} >= $size &&
	    $attrs->{'allowed'} =~ /$type/) {
	    push(@pools, $attrs);
	}
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    }

    # if none qualify, quit
    if (@pools == 0) {
	if ($srv) {
	    print STDERR "$srv: not enough space in ",
	    ($pool ? "pool $pool" : "any pool"), "\n";
	} else {
	    print STDERR "No storage server has enough space\n";
	}
	exit(1);
    }

    #
    # Pick the best pool if more than one:
    #  - prefer pool with most available space
    # XXX should take number of blockstores in each pool into account.
    #
    my $bpoolattrs;
    foreach my $attrs (@pools) {
	if (!$bpoolattrs || $attrs->{'avail'} > $bpoolattrs->{'avail'}) {
	    $bpoolattrs = $attrs;
	    next;
	}
    }

    # we should have one, do some sanity checks
    if (!$bpoolattrs) {
	fatal("create: did not find a pool!?");
    }
    if ($srv) {
	if ($srv ne $bpoolattrs->{'server'}) {
	    fatal("create: found server is not the specified server!?");
	}
    } else {
	$srv = $bpoolattrs->{'server'};
    }

    if ($pool) {
	if ($pool ne $bpoolattrs->{'pool'}) {
	    fatal("create: found pool is not the specified pool!?");
	}
    } else {
	$pool = $bpoolattrs->{'pool'};
    }

    #
    # Now we can begin the task of actually creating the blockstore!
    # First, lookup the blockstore object for the underlying pool.
    #
733 734
    print STDERR "$name: size=$size, server=$srv, pool=$pool\n"
	if ($debug);
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    my $bstore = Blockstore->Lookup($srv, $pool);
    if (!$bstore) {
	fatal("create: could not find blockstore for $srv:$pool");
    }

    #
    # Create the new partition blockstore in the DB.
    #
    my $pbstore = $bstore->Partition($leaseidx, $name, $type, $size);
    if (!$pbstore) {
	fatal("create: could not create '$name' in DB");
    }

    # Call out to the server to allocate the actual storage.
    my $outref;
750
    if (bsserver_cmd($srv, "$PROXYCMD create $pool $name $size $fstype", \$outref)) {
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
	$pbstore->Delete();
	print STDERR "*** create: could not allocate storage for '$name' on $srv/$pool:";
	foreach my $str (@$outref) {
	    print STDERR "  $str\n";
	}
	exit(-1);
    }

    return 0;
}

sub bs_destroy($$$@)
{
    my ($srv,$pool,$size,$name) = @_;

Mike Hibler's avatar
Mike Hibler committed
766 767 768 769
    if (defined($name) && $name =~ /^([-\w]+)$/) {
	$name = $1;
    } else {
	fatal("create: must specify a valid volume name");
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
    }

    #
    # Find the blockstore based on info from the server(s).
    #
    my $volattrs;
    my $volref = getvolumes($srv, $pool);
    foreach my $vol (sort keys(%{$volref})) {
	my $attrs = $volref->{$vol};
	if ($name eq $attrs->{'volume'}) {
	    if ($volattrs) {
		fatal("destroy: multiple volumes match '$name', specify a server and pool");
	    }
	    $volattrs = $attrs;
	}
    }
    if (!$volattrs) {
	fatal("destroy: no such volume '$name'");
    }
    if ($srv) {
	if ($srv ne $volattrs->{'server'}) {
	    fatal("destroy: found server is not the specified server!?");
	}
    } else {
	$srv = $volattrs->{'server'};
    }
    if ($pool) {
	if ($pool ne $volattrs->{'pool'}) {
	    fatal("destroy: found pool is not the specified pool!?");
	}
    } else {
	$pool = $volattrs->{'pool'};
    }

    #
    # Delete the blockstore object
    #
    my $bstore = Blockstore->Lookup($srv, $name);
    if (!$bstore) {
	fatal("destroy: no blockstore '$srv/$name'");
    }
    if ($bstore->role() ne "partition") {
	fatal("destroy: wrong type of blockstore '$srv/$name'");
    }
    if ($bstore->Delete()) {
	fatal("destroy: could not delete blockstore '$srv/$name'");
    }

    #
    # Call out to the server to delete the storage.
    #
    my $outref;
    if (bsserver_cmd($srv, "$PROXYCMD destroy $pool $name", \$outref)) {
	print STDERR "*** create: could not deallocate storage for '$name' on $srv/$pool:";
	foreach my $str (@$outref) {
	    print STDERR "  $str\n";
	}
	exit(-1);
    }

    return 0;
}

sub fatal($)
{
    my ($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}