bscontrol.proxy.pl 11.7 KB
Newer Older
Mike Hibler's avatar
Mike Hibler committed
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2013-2018 University of Utah and the Flux Group.
Mike Hibler's avatar
Mike Hibler committed
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 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 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
# 
# {{{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;

#
# Proxy for the blockstore server control program on boss.
#

sub usage()
{
    print STDERR "Usage: bscontrol [-hd] command args\n";
    print STDERR "   -h       This message\n";
    print STDERR "   -d       Print additional debug info\n";
    print STDERR "commands:\n";
    print STDERR "   pools    Print size info about pools\n";
    print STDERR "   volumes  Print info about volumes\n";
    print STDERR "   create <pool> <vol> <size> [ <fstype> ]\n";
    print STDERR "            Create <vol> in <pool> with <size> in MiB; optionally create a filesystem of type <fstype> on it\n";
    print STDERR "   snapshot <pool> <vol> <tstamp>\n";
    print STDERR "            Create a snapshot of <pool>/<vol> with timestamp <tstamp>\n";
    print STDERR "   clone    <pool> <ovol> <nvol> [ <tstamp> ]\n";
    print STDERR "            Create a clone of <pool>/<vol> called <nvol> from the snapshot at <tstamp> (most recent if not specified)\n";
    print STDERR "   destroy <pool> <vol>\n";
    print STDERR "            Destroy <vol> in <pool>\n";
    print STDERR "   desnapshot <pool> <vol> [ <tstamp> ]\n";
    print STDERR "            Destroy snapshot <vol>/<pool>@<tstamp>; if <tstamp> is not given, destroy all snapshots\n";
    print STDERR "   desnapshotall <pool> <vol>\n";
    print STDERR "            Like desnapshot, but removes non-blockstore related snapshots as well\n";
    print STDERR "   declone <pool> <vol>\n";
    print STDERR "            Destroy clone <vol> in <pool>; also destroys associated snapshot if this is the last clone\n";
    print STDERR "iSCSI-related debugging commands:\n";
    print STDERR "   slices    Print info about Emulab slices\n";
    print STDERR "   targets   Print info about iSCSI targets\n";
    print STDERR "   extents   Print info about iSCSI extents\n";
    print STDERR "   assocs    Print info about iSCSI target/extent assocs\n";
    print STDERR "   authinit  Print info about iSCSI authorized initiators\n";
    print STDERR "   nextaitag Print next available initiator tag\n";
    exit(-1);
}
my $optlist  = "hd";
my $debug = 0;

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

# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

# Libraries
use libfreenas;

# Protos
sub fatal($);

# Commands
my %cmds = (
    "pools"      => \&pools,
    "volumes"    => \&volumes,
    "create"     => \&create,
    "snapshot"   => \&snapshot,
    "clone"      => \&clone,
    "destroy"    => \&destroy,
    "desnapshot" => \&desnapshot,
    "declone"    => \&declone,
    "slices"     => \&slices,
    "extents"    => \&extents,
    "authinit"   => \&authinit,
    "nextaitag"  => \&nexttag,
    "targets"    => \&targets,
    "assocs"     => \&assocs,
    "desnapshotall" => \&desnapshotall,
);

#
# 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 (@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();
}

exit(&{$cmds{$cmd}}(@ARGV));

#
# Print all the available pools from which blockstores can be allocated
# along with size info.
#
sub pools()
{
    my $pref = freenasPoolList();
    foreach my $pool (keys %{$pref}) {
	my $size = int($pref->{$pool}->{'size'});
	my $avail = int($pref->{$pool}->{'avail'});
	print "pool=$pool size=$size avail=$avail\n";
    }

    return 0;
}

#
# Print uninterpreted volume info.
#
sub volumes()
{
    my $vref = freenasVolumeList(1,1);
    foreach my $vol (keys %{$vref}) {
	my $pool = $vref->{$vol}->{'pool'};
	my $iname = $vref->{$vol}->{'iname'};
	my $size = int($vref->{$vol}->{'size'});
	my $snapshots = $vref->{$vol}->{'snapshots'};
	my $cloneof = $vref->{$vol}->{'cloneof'};

	print "volume=$vol pool=$pool size=$size";
	if ($iname) {
	    print " iname=$iname";
	}
	if ($cloneof) {
	    print " cloneof=$cloneof";
	}
	if ($snapshots) {
	    print " snapshots=$snapshots";
	}
	print "\n";
    }

    return 0;
}

#
# Print uninterpreted Emulab slice info.
#
sub slices()
{
    my $eref = freenasSliceList();
    foreach my $ext (keys %{$eref}) {
	foreach my $key ("pid", "eid", "volname", "bsid", "vnode_id", "size", "type") {
	    my $val = $eref->{$ext}->{$key};
	    $val = lc($val)
		if ($key eq "type");
	    print "$key=$val "
		if (defined($val));
	}
	print "\n";
    }

    return 0;
}

#
# Print uninterpreted iSCSI extent info.
#
sub extents()
{
    my $eref = freenasExtentList(0);
    foreach my $ext (keys %{$eref}) {
	foreach my $key ("id", "name", "path", "type", "blocksize", "filesize", "naa") {
	    my $val = $eref->{$ext}->{$key};
	    $val = lc($val)
		if ($key eq "type");
	    print "$key=$val "
		if (defined($val));
	}
	print "\n";
    }

    return 0;
}

#
# Print semi-interpreted iSCSI target info.
#
# XXX At some point between 9.3 stable releases, they changed most of the
# target info to be associated with a target group instead. We just recouple
# them here.
#
sub targets()
{
    my $tref = freenasTargetList(0);
    my $tgref = freenasTargetGroupList(1);
    foreach my $t (keys %{$tref}) {
	foreach my $key ("id", "name", "alias") {
	    my $val = $tref->{$t}->{$key};
	    print "$key=$val "
		if (defined($val));
	}
	if (exists($tgref->{$t})) {
	    my $tg = $tgref->{$t};
	    foreach my $key ("portalgroup", "initiatorgroup", "authgroup", "authtype", "initialdigest") {
		my $val = $tg->{$key};
		print "$key=$val "
		    if (defined($val));
	    }
	}
	print "\n";
    }

    return 0;
}

#
# Print uninterpreted iSCSI target/extent association info.
#
sub assocs()
{
    my $aref = freenasAssocList();
    foreach my $a (keys %{$aref}) {
	foreach my $key ("id", "target", "target_name", "target_group", "extent", "extent_name") {
	    my $val = $aref->{$a}->{$key};
	    print "$key=$val "
		if (defined($val));
	}
	print "\n";
    }

    return 0;
}

#
# Print uninterpreted iSCSI authorized initiator info.
#
sub authinit()
{
    my $airef = freenasAuthInitList();
    foreach my $ai (keys %{$airef}) {
	foreach my $key ("id", "tag", "auth_network", "comment") {
	    my $val = $airef->{$ai}->{$key};
	    print "$key=$val "
		if (defined($val));
	}
	print "\n";
    }

    return 0;
}

#
# Print next available authinit tag
#
sub nexttag() {
    my $aiinfo = freenasAuthInitList();

    my @taglist = ();
    foreach my $ai (keys %{$aiinfo}) {
	my $tag = $aiinfo->{$ai}->{'tag'};
	if (defined($tag) && $tag =~ /^(\d+)$/) {
	    push(@taglist, $1);
	}
    }

    my $freetag = 1;
    foreach my $curtag (sort {$a <=> $b} @taglist) {
	last
	    if ($freetag < $curtag);
	$freetag++;
    }

    print "nexttag=$freetag\n";
}

sub create($$$;$)
{
    my ($pool,$vol,$size,$fstype) = @_;

    # XXX create non-sparse (pre-allocated) volumes
    my $sparse = 0;

    if (defined($pool) && $pool =~ /^([-\w]+)$/) {
	$pool = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus pool arg\n";
	return 1;
    }
    if (defined($vol) && $vol =~ /^([-\w]+)$/) {
	$vol = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus volume arg\n";
	return 1;
    }
    if (defined($size) && $size =~ /^(\d+)$/) {
	$size = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus size arg\n";
	return 1;
    }
    if (!defined($fstype)) {
	$fstype = "none";
    } elsif ($fstype =~ /^(ext2|ext3|ext4|ufs)$/) {
	$fstype = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus fstype arg\n";
	return 1;
    }

336
    my $rv = freenasVolumeCreate($pool, $vol, $size, $sparse, 1);
Mike Hibler's avatar
Mike Hibler committed
337
    if ($rv == 0 && $fstype ne "none") {
338 339
	$rv = freenasFSCreate($pool, $vol, $fstype, 1);
	if ($rv && freenasVolumeDestroy($pool, $vol, 1)) {
340 341 342
	    print STDERR "bscontrol_proxy: could not destroy new volume ".
		"after FS creation failure.\n";
	}
Mike Hibler's avatar
Mike Hibler committed
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
    }

    return $rv;
}

sub snapshot($$$)
{
    my ($pool,$vol,$tstamp) = @_;

    if (defined($pool) && $pool =~ /^([-\w]+)$/) {
	$pool = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus pool arg\n";
	return 1;
    }
    if (defined($vol) && $vol =~ /^([-\w]+)$/) {
	$vol = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus volume arg\n";
	return 1;
    }
    if (defined($tstamp) && $tstamp =~ /^(\d+)$/) {
	$tstamp = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus tstamp arg\n";
	return 1;
    }

371
    return freenasVolumeSnapshot($pool, $vol, $tstamp, 1);
Mike Hibler's avatar
Mike Hibler committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
}

sub desnapshot($$$)
{
    my ($pool,$vol,$tstamp) = @_;

    if (defined($pool) && $pool =~ /^([-\w]+)$/) {
	$pool = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus pool arg\n";
	return 1;
    }
    if (defined($vol) && $vol =~ /^([-\w]+)$/) {
	$vol = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus volume arg\n";
	return 1;
    }
    if (defined($tstamp)) {
	if ($tstamp =~ /^(\d+)$/) {
	    $tstamp = $1;
	} else {
	    print STDERR "bscontrol_proxy: bogus tstamp arg\n";
	    return 1;
	}
    }

399
    return freenasVolumeDesnapshot($pool, $vol, $tstamp, 0, 1);
Mike Hibler's avatar
Mike Hibler committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
}

sub desnapshotall($$)
{
    my ($pool,$vol) = @_;

    if (defined($pool) && $pool =~ /^([-\w]+)$/) {
	$pool = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus pool arg\n";
	return 1;
    }
    if (defined($vol) && $vol =~ /^([-\w]+)$/) {
	$vol = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus volume arg\n";
	return 1;
    }

419
    return freenasVolumeDesnapshot($pool, $vol, undef, 1, 1);
Mike Hibler's avatar
Mike Hibler committed
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
}

sub clone($$$;$)
{
    my ($pool,$ovol,$nvol,$tstamp) = @_;

    if (defined($pool) && $pool =~ /^([-\w]+)$/) {
	$pool = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus pool arg\n";
	return 1;
    }
    if (defined($ovol) && $ovol =~ /^([-\w]+)$/) {
	$ovol = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus origin volume arg\n";
	return 1;
    }
    if (defined($nvol) && $nvol =~ /^([-\w]+)$/) {
	$nvol = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus clone volume arg\n";
	return 1;
    }
    if (defined($tstamp)) {
	if ($tstamp =~ /^(\d+)$/) {
	    $tstamp = $1;
	} else {
	    print STDERR "bscontrol_proxy: bogus tstamp arg\n";
	    return 1;
	}
    } else {
	# zero means most recent
	$tstamp = 0;
    }

456
    return freenasVolumeClone($pool, $ovol, $nvol, $tstamp, 1);
Mike Hibler's avatar
Mike Hibler committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
}

sub destroy($$$)
{
    my ($pool,$vol) = @_;

    if (defined($pool) && $pool =~ /^([-\w]+)$/) {
	$pool = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus pool arg\n";
	return 1;
    }
    if (defined($vol) && $vol =~ /^([-\w]+)$/) {
	$vol = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus volume arg\n";
	return 1;
    }

476
    return freenasVolumeDestroy($pool, $vol, 1);
Mike Hibler's avatar
Mike Hibler committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
}

sub declone($$$)
{
    my ($pool,$vol) = @_;

    if (defined($pool) && $pool =~ /^([-\w]+)$/) {
	$pool = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus pool arg\n";
	return 1;
    }
    if (defined($vol) && $vol =~ /^([-\w]+)$/) {
	$vol = $1;
    } else {
	print STDERR "bscontrol_proxy: bogus volume arg\n";
	return 1;
    }

496
    return freenasVolumeDeclone($pool, $vol, 1);
Mike Hibler's avatar
Mike Hibler committed
497
}