setzfsquotas.in 9.94 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
#!/usr/bin/perl -w
#
# Copyright (c) 2017 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/>.
# 
# }}}
#

#
# Ensure that individual /users, /proj, /groups and /scratch filesystem
# quotas and refquotas are set correctly.
#
# If refquota is not set but quota is, we set refquota to the current
# quota value and apply the mulitplier to set a new value for quota.
# If there was no current quota, it stays that way.
#
# Currently, if refquota is set but not to the target value, we just
# warn and leave it as is. We might want an option in the future to
# override that.
#
# If the new calculated (from refquota) value for quota is smaller than
# the current quota value, we just warn and leave it along. Again, we
# might want a force option. If the calculated value is larger than the
# current quota, we warn but also increase the value.
#
# The net effect is that every filesystem will have a quota that is at
# least N (where N is the multiplication factor) times larger than the
# refquota value. However, right now either or both of the refquota and
# quota values might be different than the target values.
#
use English;
use Getopt::Std;

sub usage()
{
    print STDERR "Usage: setzfsquotas [-adhqnr] [-Q value] [-F factor] FS ...\n";
    print STDERR "\nSet ZFS quota/refquota as indicated for each specified\n";
    print STDERR "standard Emulab filesystem\n";
    print STDERR "   -h         This message\n";
    print STDERR "   -a         Set quotas for all standard Emulab filesystems\n";
    print STDERR "              Implies '-r'\n";
    print STDERR "   -d         Enabled additional debugging messages\n";
    print STDERR "   -q         Run quietly\n";
    print STDERR "   -n         Don't change quotas, just say what would happen\n";
    print STDERR "   -r         Recursive; assume arg is a root and apply to all FSes underneath\n";
    print STDERR "   -Q value   Set the quota to the indicated value.\n";
    print STDERR "              Value should be a valid string for the ZFS refquota attribute.\n";
    print STDERR "              Default comes from the defs- file ZFS_QUOTA_* variables\n";
    print STDERR "   -F factor  Multiplier for setting the quota attribute.\n";
    print STDERR "              1.0 will set quota==refquota. Larger values will permit space\n";
    print STDERR "              in excess of what the user can allocate (i.e., for backup purposes)\n";
    print STDERR "              Default comes from the defs- file ZFS_QUOTA_*_X variables\n";
    print STDERR "\nFS should be one of more of 'users', 'proj', 'groups'.\n";
    print STDERR "Not required if '-a' is specified.\n";
}

my $optlist = "adhnqrF:Q:";
my $doall = 0;
my $debug = 0;
my $quiet = 0;
my $quotaval;
my $factor;
my $recursive = 0;
my $impotent = 0;

#
# Configure variables
#
my $TB		= "@prefix@";
my $WITHZFS	= "@WITHZFS@";
my $ZFS_ROOT	= "@ZFS_ROOT@";
my $U_QUOTA	= "@ZFS_QUOTA_USER@";
my $P_QUOTA	= "@ZFS_QUOTA_PROJECT@";
my $G_QUOTA	= "@ZFS_QUOTA_GROUP@";
my $U_MULT	= "@ZFS_QUOTA_USER_X@";
my $P_MULT	= "@ZFS_QUOTA_PROJECT_X@";
my $G_MULT	= "@ZFS_QUOTA_GROUP_X@";

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

# Protos
sub setquotas($$$);
sub getquotas($);
sub getsubfses($$);
sub parsenumstr($);

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

# Globals
my @fses = ();
my %fsquotas = (
    "users" => parsenumstr($U_QUOTA),
    "proj" => parsenumstr($P_QUOTA),
    "groups" => parsenumstr($G_QUOTA)
);
my %fsmults = (
    "users" => $U_MULT,
    "proj" => $P_MULT,
    "groups" => $G_MULT
);
my %fsinfo = ();

my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
    exit(1);
}
if (defined($options{'h'})) {
    usage();
    exit(0);
}
if (defined($options{'d'})) {
    $debug++;
}
if (defined($options{"a"})) {
    $doall = 1;
    $recursive = 1;
}
if (defined($options{"r"})) {
    $recursive = 1;
}
if (defined($options{"n"})) {
    $impotent = 1;
}
if (defined($options{"Q"})) {
    $quotaval = parsenumstr($options{"Q"});
148 149 150
    foreach my $fs (keys %fsquotas) {
	$fsquotas{$fs} = $quotaval;
    }
151 152 153 154 155 156 157 158 159
}
if (defined($options{'F'})) {
    if ($options{'F'} =~ /^(\d+(\.\d+)?)$/) {
	$factor = $1;
	if ($factor < 1.0) {
	    print STDERR "*** Factor must be at least 1\n";
	    usage();
	    exit(1);
	}
160 161 162
	foreach my $fs (keys %fsmults) {
	    $fsmults{$fs} = $factor;
	}
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
    } else {
	print STDERR "*** Factor must be a number.\n";
	usage();
	exit(1);
    }
}
if ($doall) {
    @fses = ("users", "proj", "groups");
} else {
    if (@ARGV == 0) {
	print STDERR "*** Must specify one or more FSes\n";
	usage();
	exit(1);
    }
    @fses = @ARGV;
}

# Set this to turn off tblog in libraries.
$ENV{'TBLOG_OFF'} = "yep";

# Load the Testbed support stuff.
#use lib "@prefix@/lib";
use lib "/usr/testbed/lib";
use libtestbed;

if (!$impotent && $UID) {
    print STDERR "Must run as root.\n";
    exit(1);
}

#
# Handle recursive traversal
#
my $rv = 0;
if ($recursive) {
    my @allfs = ();
    foreach my $fs (@fses) {
	print "Finding sub FSes of $fs ...\n"
	    if (!$quiet);
	if (!exists($fsquotas{$fs})) {
	    print STDERR "$fs: invalid root filesystem!\n";
	    $rv++;
	    next;
	}
	push @allfs, getsubfses($fs, "$ZFS_ROOT/$fs");
    }
    @fses = @allfs;
}

foreach my $fs (sort @fses) {
    print "Processing $fs ...\n"
	if (!$quiet);
    my ($rq, $q, $desq, $mult) = getquotas($fs);
    if (!defined($rq)) {
	print STDERR "$fs: not a ZFS filesystem?\n";
	$rv++;
	next;
    }
    print STDERR "$fs: refquota='$rq', quota='$q', desquota='$desq', mult='$mult'\n"
	if ($debug);

    # if no quotas, nothing changes
    if ($rq == 0 && $q == 0) {
	print "$fs: no quotas set, not modified.\n"
	    if (!$quiet);
	next;
    }

    my ($newrq, $newq);

    # refquota is zero, set to quota value and calculate new quota value
    if ($rq == 0) {
	$newrq = $q;
	$newq = $newrq * $mult;
    }
    # refquota set to correct value, make sure quota is big enough
    elsif ($rq == $desq) {
	$newrq = $rq;
	$newq = $rq * $mult;
	if ($q == 0 || $newq == $q) {
	    print "$fs: refquota and quota correct, not modified\n"
		if (!$quiet);
	    next;
	}
	if ($newq < $q) {
	    print "$fs: WARNING: quota is larger than desired ($q > $newq), not modified\n"
		if (!$quiet);
	    next;
	}
    }
    # refquota set to a different value, warn and make sure quota is big enough
    else {
	$newrq = $rq;
	$newq = $rq * $mult;
	if ($q == 0 || $newq <= $q) {
	    print "$fs: WARNING: refquota not at desired value ($rq != $desq), not modified\n"
		if (!$quiet);
	    next;
	}
    }

    # if we get here, we need to change something
    my $msg = "";
    if ($rq != $newrq) {
	$msg .= "fixing refquota ($rq->$newrq)";
    } else {
	$newrq = 0;
    }
    if ($q != $newq) {
	if ($msg) {
	    $msg .= " and quota ($q->$newq)";
	} else {
	    $msg .= "fixing quota ($q->$newq)";
	}
    } else {
	$newq = 0;
    }

    print "$fs: $msg\n"
	if ($impotent || !$quiet);
    $rv += setquotas($fs, $newrq, $newq);
}

exit($rv);

sub setquotas($$$)
{
    my ($fs,$refq,$q) = @_;
    my $cmd;

    if ($impotent) {
	$cmd = "echo ";
    }
    $cmd .= "/sbin/zfs";

    my $rv = 0;
    if ($refq && system("$cmd set refquota=$refq $fs")) {
	print STDERR "*** $fs: could not set refquota!\n";
	$rv++;
    }
    if ($q && system("$cmd set quota=$q $fs")) {
	print STDERR "*** $fs: could not set quota!\n";
	$rv++;
    }

    return $rv;
}

sub getquotas($)
{
    my ($fs) = @_;
    my ($refq,$q,$tgtrefq,$tgtmult);

    if (exists($fsinfo{$fs})) {
	$refq = $fsinfo{$fs}{'refquota'};
	$q =$fsinfo{$fs}{'quota'};
	$tgtrefq = $fsquotas{$fsinfo{$fs}{'root'}};
	$tgtmult = $fsmults{$fsinfo{$fs}{'root'}};
    } else {
	my $output = `/sbin/zfs list -Hpo refquota,quota $fs 2>/dev/null`;
	if ($?) {
	    print STDERR "*** $fs: could not get attributes\n";
	    return undef;
	}
	chomp $output;
	($refq, $q) = split '\s+', $output;

	my $root;
	if ($fs =~ /^$ZFS_ROOT\/([^\/]+)\//) {
	    $root = $1;
	} else {
	    $root = "unknown";
	}
336
	if (exists($fsquotas{$root})) {
337 338 339 340 341
	    $tgtrefq = $fsquotas{$root};
	} else {
	    print STDERR "*** $fs: unknown root FS '$root'\n";
	    return undef;
	}
342
	if (exists($fsmults{$root})) {
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
	    $tgtmult = $fsmults{$root};
	} else {
	    print STDERR "*** $fs: unknown root FS '$root'\n";
	    return undef;
	}
    }
    return ($refq, $q, $tgtrefq, $tgtmult);
}

#
# Get sub filesystems of one of the main mountpoints (e.g., "z/users").
#
sub getsubfses($$)
{
    my ($root,$fs) = @_;
    my @fslist = ();

    my @output = `/sbin/zfs list -r -Hpo name,refquota,quota $fs 2>/dev/null`;
    if ($?) {
	print STDERR "*** $fs: could not find ZFS\n";
	return ();
    }
    foreach my $line (@output) {
	chomp $line;
	my ($name,$refq,$q) = split '\s+', $line;
	if ($name eq $fs) {
	    next;
	}
	push @fslist, $name;
	$fsinfo{$name}{'refquota'} = $refq;
	$fsinfo{$name}{'quota'} = $q;
	$fsinfo{$name}{'root'} = $root;
    }
    return @fslist;
}

sub parsenumstr($)
{
    my ($str) = @_;
    my ($num,$unit);

    if ($str =~ /^(\d+(?:\.\d+)?)([MGT]?)$/) {
	($num,$unit) = ($1,$2);
	if ($unit eq "M") {
	    $num *= (1024*1024);
	} elsif ($unit eq "G") {
	    $num *= (1024*1024*1024);
	} elsif ($unit eq "T") {
	    $num *= (1024*1024*1024*1024);
	} else {
	    die "*** could not parse '$str' as a number\n";
	}
    } elsif ($str eq "none") {
	$num = 0;
    } else {
	die "*** could not parse '$str' as a number\n";
    }

    return $num;
}