libfreenas.pm 12.4 KB
Newer Older
1 2 3 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 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 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 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
#!/usr/bin/perl -wT
#
# Copyright (c) 2013-2014 University of Utah and the Flux Group.
# 
# {{{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/>.
# 
# }}}
#
# Support functions for the libvnode API and also for bscontrol which is
# a proxy for the blockstore server control program on boss.
#

package libfreenas;
use Exporter;
@ISA    = "Exporter";
@EXPORT =
    qw( 
	freenasPoolList freenasVolumeList
	freenasVolumeCreate freenasVolumeDestroy freenasFSCreate
	freenasRunCmd freenasParseListing
	$FREENAS_CLI_VERB_IFACE $FREENAS_CLI_VERB_IST_EXTENT
	$FREENAS_CLI_VERB_IST_AUTHI $FREENAS_CLI_VERB_IST_TARGET
	$FREENAS_CLI_VERB_IST_ASSOC $FREENAS_CLI_VERB_VLAN
	$FREENAS_CLI_VERB_VOLUME $FREENAS_CLI_VERB_POOL
    );

use strict;
use English;
use Data::Dumper;
use Socket;
use File::Basename;
use File::Path;
use File::Copy;

# Pull in libvnode and other Emulab stuff
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }
use libutil;
use libtestbed;
use libsetup;


#
# Exported CLI constants
#
our $FREENAS_CLI_VERB_IFACE       = "interface";
our $FREENAS_CLI_VERB_IST_EXTENT  = "ist_extent";
our $FREENAS_CLI_VERB_IST_AUTHI   = "ist_authinit";
our $FREENAS_CLI_VERB_IST_TARGET  = "ist";
our $FREENAS_CLI_VERB_IST_ASSOC   = "ist_assoc";
our $FREENAS_CLI_VERB_VLAN        = "vlan";
our $FREENAS_CLI_VERB_VOLUME      = "volume";
our $FREENAS_CLI_VERB_POOL        = "pool";

#
# Constants
#
my $GLOBAL_CONF_LOCK     = "blkconf";
my $ZPOOL_CMD            = "/sbin/zpool";
my $ZFS_CMD              = "/sbin/zfs";
my $ZPOOL_STATUS_UNKNOWN = "unknown";
my $ZPOOL_STATUS_ONLINE  = "online";
my $ZPOOL_LOW_WATERMARK  = 2 * 2**10; # 2GiB, expressed in MiB
my $FREENAS_MNT_PREFIX   = "/mnt";
my $ISCSI_GLOBAL_PORTAL  = 1;
my $SER_PREFIX           = "d0d0";
my $VLAN_IFACE_PREFIX    = "vlan";
my $MAX_RETRY_COUNT      = 5;
my $VOLUME_BUSY_WAIT      = 10;
my $VOLUME_GONE_WAIT      = 5;
my $IFCONFIG             = "/sbin/ifconfig";
my $ALIASMASK            = "255.255.255.255";
my $LINUX_MKFS		 = "/usr/local/sbin/mke2fs";
my $FBSD_MKFS		 = "/sbin/newfs";

# storageconfig constants
# XXX: should go somewhere more general
my $BS_CLASS_SAN         = "SAN";
my $BS_PROTO_ISCSI       = "iSCSI";
my $BS_UUID_TYPE_IQN     = "iqn";

# CLI stuff
my $FREENAS_CLI          = "$BINDIR/freenas-config";

my %cliverbs = (
    $FREENAS_CLI_VERB_IFACE      => 1,
    $FREENAS_CLI_VERB_IST_EXTENT => 1,
    $FREENAS_CLI_VERB_IST_AUTHI  => 1,
    $FREENAS_CLI_VERB_IST_TARGET => 1,
    $FREENAS_CLI_VERB_IST_ASSOC  => 1,
    $FREENAS_CLI_VERB_VLAN       => 1,
    $FREENAS_CLI_VERB_VOLUME     => 1,
    $FREENAS_CLI_VERB_POOL       => 1,
    );

#
# Global variables
#
my $debug  = 0;

sub freenasPoolList();
sub freenasVolumeList($);
sub freenasVolumeCreate($$$);
sub freenasVolumeDestroy($$);
sub freenasFSCreate($$$);
sub freenasRunCmd($$);
sub freenasParseListing($);

#
# Local Functions
#
sub listPools();
sub convertZfsToMebi($);

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

sub setDebug($)
{
    $debug = shift;
    print "libfreenas: debug=$debug\n"
	if ($debug);
}

sub freenasVolumeList($)
{
    my ($inameinfo) = @_;
    my $vollist = {};

    #
    # Extract blockstores from the freenas volume info and augment
    # with slice info where it exists.
    # 
    my %inames = ();
    if ($inameinfo) {
	my @slist = freenasParseListing($FREENAS_CLI_VERB_IST_EXTENT);
	foreach my $slice (@slist) {
	    if ($slice->{'path'} =~ /^zvol\/([-\w]+\/[-\w+]+)$/) {
		$inames{$1} = $slice->{'name'};
	    }
	}
    }

    my @zvols = freenasParseListing($FREENAS_CLI_VERB_VOLUME);
    foreach my $zvol (@zvols) {
	my $vol = {};
	if ($zvol->{'vol_name'} =~ /^([-\w]+)\/([-\w+]+)$/) {
	    $vol->{'pool'} = $1;
	    $vol->{'volume'} = $2;
	    $vol->{'size'} = convertZfsToMebi($zvol->{'vol_size'});
	    if ($inameinfo && exists($inames{$zvol->{'vol_name'}})) {
		$vol->{'iname'} = $inames{$zvol->{'vol_name'}};
	    }
	    $vollist->{$vol->{'volume'}} = $vol;
	}
    }

    return $vollist;
}

sub freenasPoolList() {
    return listPools();
}

sub freenasVolumeCreate($$$)
{
    my ($pool, $volname, $size) = @_;

    # Untaint arguments since they are passed to a command execution
    $pool = untaintHostname($pool);
    $volname = untaintHostname($volname);
    $size = untaintNumber($size);
    if (!$pool || !$volname || !$size) {
	warn("*** ERROR: freenasVolumeCreate: ".
	     "Invalid arguments");
	return -1;
    }

    # Does the requested pool exist?
    my $pools = listPools();
    my $destpool;
    if (exists($pools->{$pool})) {
	$destpool = $pools->{$pool};
    } else {
	warn("*** ERROR: freenasVolumeCreate: ".
	     "Requested pool not found: $pool!");
	return -1;
    }

    # Is there enough space in the requested pool?
    # If not, there is a discrepancy between reality and the Emulab database.
    if ($size + $ZPOOL_LOW_WATERMARK > $destpool->{'avail'}) {
	warn("*** ERROR: freenasVolumeCreate: ". 
	     "Not enough space remaining in requested pool: $pool");
	return -1;
    }

    # Allocate volume in zpool
    eval { freenasRunCmd($FREENAS_CLI_VERB_VOLUME, 
			 "add $pool $volname ${size}MB off") };
    if ($@) {
	warn("*** ERROR: freenasVolumeCreate: ".
	     "volume allocation failed: $@");
	return -1;
    }

    return 0;
}

sub freenasVolumeDestroy($$)
{
    my ($pool, $volname) = @_;

    # Untaint arguments since they are passed to a command execution
    $pool = untaintHostname($pool);
    $volname = untaintHostname($volname);
    if (!$pool || !$volname) {
	warn("*** ERROR: freenasVolumeDestroy: ".
	     "Invalid arguments");
	return -1;
    }

    # Deallocate volume.  Wrap in loop to enable retries.
    my $count;
    for ($count = 1; $count <= $MAX_RETRY_COUNT; $count++) {
	eval { freenasRunCmd($FREENAS_CLI_VERB_VOLUME, 
			     "del $pool $volname") };
	# Process exceptions thrown during deletion attempt.  Retry on
	# some errors.
	if ($@) { 
	    if ($@ =~ /dataset is busy/) {
		warn("*** WARNING: freenasVolumeDestroy: ".
		     "Volume is busy. ".
		     "Waiting $VOLUME_BUSY_WAIT seconds before trying again ".
		     "(count=$count).");
		sleep $VOLUME_BUSY_WAIT;
	    }
	    elsif ($@ =~ /does not exist/) {
		if ($count < $MAX_RETRY_COUNT) {
		    warn("*** WARNING: freenasVolumeDestroy: ".
			 "Volume seems to be gone, retrying.");
		    # Bump counter to just under termination to try once more.
		    $count = $MAX_RETRY_COUNT-1;
		    sleep $VOLUME_GONE_WAIT;
		} else {
		    warn("*** WARNING: freenasVolumeDestroy: ".
			 "Volume still seems to be gone.");
		    # Bail now because we don't want to report this as an
		    # error to the caller.
		    return 0;
		}
	    } 
	    else {
		warn("*** ERROR: freenasVolumeDestroy: ".
		     "Volume removal failed: $@");
		return -1;
	    }
	} else {
	    # No error condition - jump out of loop.
	    last;
	}
    }

    # Note: Checks for lingering volumes will be performed separately in
    # consistency checking routines.

    if ($count > $MAX_RETRY_COUNT) {
	warn("*** WARNING: freenasVolumeDestroy: ".
	     "Could not free volume after several attempts!");
	return -1;
    }

    return 0;
}

#
# Run a FreeNAS CLI command, checking for a return error and other
# such things.  We check that the incoming verb is valid.  Command line
# argument string needs to be untainted or this will fail.
#
# Throws exceptions (dies), passing along errors in $@.
#
sub freenasRunCmd($$) {
    my ($verb, $argstr) = @_;

    my $errstate = 0;
    my $message;

    die "Invalid FreeNAS CLI verb: $verb"
	unless exists($cliverbs{$verb});

    print "DEBUG: blockstore_freenasRunCmd:\n".
	"\trunning: $verb $argstr\n" if $debug;

    my $output = `$FREENAS_CLI $verb $argstr 2>&1`;

    if ($? != 0) {
	$errstate = 1;
	$output =~ /^(.+Error: .+)$/;
	$message = defined($1) ? $1 : "Error code: $?";
    } elsif ($output =~ /"error": true/) {
	$errstate = 1;
	$output =~ /"message": "([^"]+)"/;
	$message = defined($1) ? $1 : "Unknown error";
    }

    if ($errstate) {
	print STDERR $output if $debug;
	die $message;
    }

    return 0;
}

# Run our custom FreeNAS CLI to extract info.  
#
# Returns an array of hash references.  Each hash contains info from
# one line of output.  The hash keys are the field names from the
# header (first line of output).  The hash values are the
# corresponding pieces of data at each field location in a line.
sub freenasParseListing($) {
    my $verb = shift;
    my @retlist = ();

    die "Invalid FreeNAS CLI verb: $verb"
	unless exists($cliverbs{$verb});

    open(CLI, "$FREENAS_CLI $verb list |") or
	die "Can't run FreeNAS CLI: $!";

    my $header = <CLI>;

    return @retlist
	if !defined($header) or !$header;

    chomp $header;
    my @fields = split(/\t/, $header);

    while (my $line = <CLI>) {
	chomp $line;
	my @lparts = split(/\t/, $line);
	if (scalar(@lparts) != scalar(@fields)) {
	    warn("*** WARNING: blockstore_freenasParseListing: ".
		 "Bad output from CLI ($verb): $line");
	    next;
	}
	my %lineh = ();
	for (my $i = 0; $i < scalar(@fields); $i++) {
	    $lineh{$fields[$i]} = $lparts[$i];
	}
	push @retlist, \%lineh;
    }
    close(CLI);
    return @retlist;
}

sub freenasFSCreate($$$) {
    my ($pool,$vol,$fstype) = @_;
    my $cmd;

    if ($fstype =~ /^ext[234]$/) {
	$cmd = "$LINUX_MKFS -t $fstype -o Linux";
    } elsif ($fstype eq "ufs") {
	$cmd = "$FBSD_MKFS";
    } else {
	warn("*** WARNING: freenasFSCreate: unknown fs type '$fstype'");
	return -1;
    }
    my $redir = ">/dev/null 2>&1";
    if (system("$cmd /dev/zvol/$pool/$vol $redir") != 0) {
	warn("*** WARNING: freenasFSCreate: '$cmd /dev/zvol/$pool/$vol' failed");
	return -1;
    }

    return 0;
}

#######################################################################
# package-local functions
#

#
# Return information on all of the volume pools available on this host.
# Note: to get exact sizes, we execute our own zfs command, e.g:
#
#   zfs get -o name,property,value -Hp available,used rz-1
#
# where "rz-1" is the "root" of the pool. We need -p so that zfs doesn't
# return "human readable" sizes. Zfs rounds up those sizes to the appropriate
# number of significant digits. However, rounding up available space makes
# it seem like we have more space than we actually do! E.g., 39.35T to 39.4T
# will be off by 50GB.
#
sub listPools() {
    my $poolh = {};
    my @pools = freenasParseListing($FREENAS_CLI_VERB_POOL);
    
    # Create hash with pool name as key.  Stuff in some sentinel values
    # in case we don't get a match from 'zpool list' below.
    foreach my $pool (@pools) {
	$pool->{'size'} = 0;
	$pool->{'avail'} = 0;
	$poolh->{$pool->{'volume_name'}} = $pool;
    }

    # Yuck - have to go after capacity and status info by calling the
    # 'zfs' command line utility since the CLI doesn't return this info.
    open(ZFS, "$ZFS_CMD get -o name,property,value -Hp used,avail |") or
	die "Can't run 'zfs get'!";

    while (my $line = <ZFS>) {
	chomp $line;
	my ($pname, $prop, $val) = split(/\s+/, $line);
	next if $pname =~ /\//;  # filter out zvols.
	if (exists($poolh->{$pname})) {
	    my $pool = $poolh->{$pname};
	    if ($prop eq "available") {
		$pool->{'avail'} = convertZfsToMebi($val);
	    } elsif ($prop eq "used") {
		$pool->{'used'} = convertZfsToMebi($val);
	    }
	} else {
	    warn("*** WARNING: blockstore_getPoolInfo: ".
		 "No FreeNAS entry for zpool: $pname");
	}
    }

    # calculates sizes for each pool based on used, avail
    foreach my $pname (keys %$poolh) {
	my $pool = $poolh->{$pname};
	if (!exists($pool->{'used'}) ||
	    !exists($pool->{'avail'})) {
	    warn("*** WARNING: blockstore_getPoolInfo: ".
		 "incomplete size info for zpool: $pname");
	} else {
	    $pool->{'size'} = $pool->{'used'} + $pool->{'avail'};
	}
    }

    return $poolh;
}

#
# ZFS uses "KB", "MB", etc. when it really means "KiB", "MiB", etc.
#
sub convertZfsToMebi($) {
    my ($zsize) = @_;

    if ($zsize =~ /([\d\.]+[KMGT])B?$/) {
	$zsize = $1 . "iB";
    }
    return convertToMebi($zsize);
}

# Required perl foo
1;