userspace.py revision 148434217c040ea38dc844384f6ba68d9b325906
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# CDDL HEADER START
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# The contents of this file are subject to the terms of the
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# Common Development and Distribution License (the "License").
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# You may not use this file except in compliance with the License.
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# See the License for the specific language governing permissions
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# and limitations under the License.
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# When distributing Covered Code, include this CDDL HEADER in each
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# If applicable, add the following below this CDDL HEADER, with the
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# fields enclosed by brackets "[]" replaced with your own identifying
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# information: Portions Copyright [yyyy] [name of copyright owner]
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# CDDL HEADER END
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# Use is subject to license terms.
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens"""This module implements the "zfs userspace" and "zfs groupspace" subcommands.
148434217c040ea38dc844384f6ba68d9b325906Matthew AhrensThe only public interface is the zfs.userspace.do_userspace() function."""
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# map from property name prefix -> (field name, isgroup)
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens """Return True if this property (eg "userquota@") should be skipped."""
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens if isgroup and "posixgroup" not in options.types and \
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens if not isgroup and "posixuser" not in options.types and \
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens """Return a dict("field": value) for this domain (string) + rid (int)"""
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens (1, 1): ("SMB Group", lambda id: zfs.ioctl.sid_to_name(id, 0)),
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens (1, 0): ("POSIX Group", lambda id: grp.getgrgid(int(id)).gr_name),
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens (0, 1): ("SMB User", lambda id: zfs.ioctl.sid_to_name(id, 1)),
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens (0, 0): ("POSIX User", lambda id: pwd.getpwuid(int(id)).pw_name)
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens if typename.lower().replace(" ", "") not in options.types:
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # python's getpwuid/getgrgid is confused by ephemeral uids
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens if "name" not in v:
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # it's just a number, so pad it with spaces so
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # that it will sort numerically
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # fill in default values
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrensdef process_one_raw(acct, maxfieldlen, options, prop, elem):
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens """Update the acct and maxfieldlen dicts to incorporate the
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens information from this elem from Dataset.userspace(prop)."""
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens rid = zfs.ioctl.sid_to_id("%s-%u" % (domain, rid),
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # Add our value to an existing value, which may be present if
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # options.translate is set.
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens value = v[field + ".sort"] = value + v[field + ".sort"]
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens for k in v.keys():
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # some of the .sort fields are integers, so have no len()
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens """Implements the "zfs userspace" and "zfs groupspace" subcommands."""
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens types = ("all", "posixuser", "smbuser", "posixgroup", "smbgroup")
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens u = _("%s [-niHp] [-o field[,...]] [-sS field] ... \n") % sys.argv[1]
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens u += _(" [-t type[,...]] <filesystem|snapshot>")
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens parser = optparse.OptionParser(usage=u, prog="zfs")
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens parser.add_option("-n", action="store_true", dest="noname",
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens help=_("Print numeric ID instead of user/group name"))
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens parser.add_option("-i", action="store_true", dest="translate",
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens help=_("translate SID to posix (possibly ephemeral) ID"))
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens parser.add_option("-H", action="store_true", dest="noheaders",
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens parser.add_option("-p", action="store_true", dest="parsable",
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens parser.add_option("-o", dest="fields", metavar="field[,...]",
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens help=_("print only these fields (eg type,name,used,quota)"))
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens parser.add_option("-s", dest="sortfields", metavar="field",
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens type="choice", choices=fields, default=list(),
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens action="callback", callback=zfs.util.append_with_opt,
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens parser.add_option("-S", dest="sortfields", metavar="field",
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens type="choice", choices=fields, #-s sets the default
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens action="callback", callback=zfs.util.append_with_opt,
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens parser.add_option("-t", dest="types", metavar="type[,...]",
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens help=_("print only these types (eg posixuser,smbuser,posixgroup,smbgroup,all)"))
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens (options, args) = parser.parse_args(sys.argv[2:])
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens options.sortfields = [("-s", "type"), ("-s", "name")]
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens ds = zfs.dataset.Dataset(dsname, types=("filesystem"))
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens if ds.getprop("zoned") and zfs.ioctl.isglobalzone():
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens print(_("Initializing accounting information on old filesystem, please wait..."))
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # gather and process accounting information
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens process_one_raw(acct, maxfieldlen, options, prop, elem)
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # print out headers
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # make sure the field header will fit
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens line += fmt % (maxfieldlen[field], field.upper())
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # custom sorting func
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # reverse sorting
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # it's a string; decompose it
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # into an array of integers,
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # each one the negative of that
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens n = [-ord(c) for c in n]
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # print out data lines
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens for val in sorted(acct.itervalues(), key=cmpkey):