1*3227e6cfSchs#! /usr/bin/python2.6 2*3227e6cfSchs# 3*3227e6cfSchs# CDDL HEADER START 4*3227e6cfSchs# 5*3227e6cfSchs# The contents of this file are subject to the terms of the 6*3227e6cfSchs# Common Development and Distribution License (the "License"). 7*3227e6cfSchs# You may not use this file except in compliance with the License. 8*3227e6cfSchs# 9*3227e6cfSchs# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*3227e6cfSchs# or http://www.opensolaris.org/os/licensing. 11*3227e6cfSchs# See the License for the specific language governing permissions 12*3227e6cfSchs# and limitations under the License. 13*3227e6cfSchs# 14*3227e6cfSchs# When distributing Covered Code, include this CDDL HEADER in each 15*3227e6cfSchs# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*3227e6cfSchs# If applicable, add the following below this CDDL HEADER, with the 17*3227e6cfSchs# fields enclosed by brackets "[]" replaced with your own identifying 18*3227e6cfSchs# information: Portions Copyright [yyyy] [name of copyright owner] 19*3227e6cfSchs# 20*3227e6cfSchs# CDDL HEADER END 21*3227e6cfSchs# 22*3227e6cfSchs# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 23*3227e6cfSchs# 24*3227e6cfSchs 25*3227e6cfSchs"""This module provides utility functions for ZFS. 26*3227e6cfSchszfs.util.dev -- a file object of /dev/zfs """ 27*3227e6cfSchs 28*3227e6cfSchsimport gettext 29*3227e6cfSchsimport errno 30*3227e6cfSchsimport os 31*3227e6cfSchsimport solaris.misc 32*3227e6cfSchs# Note: this module (zfs.util) should not import zfs.ioctl, because that 33*3227e6cfSchs# would introduce a circular dependency 34*3227e6cfSchs 35*3227e6cfSchserrno.ECANCELED = 47 36*3227e6cfSchserrno.ENOTSUP = 48 37*3227e6cfSchs 38*3227e6cfSchsdev = open("/dev/zfs", "w") 39*3227e6cfSchs 40*3227e6cfSchstry: 41*3227e6cfSchs _ = gettext.translation("SUNW_OST_OSLIB", "/usr/lib/locale", 42*3227e6cfSchs fallback=True).gettext 43*3227e6cfSchsexcept: 44*3227e6cfSchs _ = solaris.misc.gettext 45*3227e6cfSchs 46*3227e6cfSchsdef default_repr(self): 47*3227e6cfSchs """A simple __repr__ function.""" 48*3227e6cfSchs if self.__slots__: 49*3227e6cfSchs str = "<" + self.__class__.__name__ 50*3227e6cfSchs for v in self.__slots__: 51*3227e6cfSchs str += " %s: %r" % (v, getattr(self, v)) 52*3227e6cfSchs return str + ">" 53*3227e6cfSchs else: 54*3227e6cfSchs return "<%s %s>" % \ 55*3227e6cfSchs (self.__class__.__name__, repr(self.__dict__)) 56*3227e6cfSchs 57*3227e6cfSchsclass ZFSError(StandardError): 58*3227e6cfSchs """This exception class represents a potentially user-visible 59*3227e6cfSchs ZFS error. If uncaught, it will be printed and the process will 60*3227e6cfSchs exit with exit code 1. 61*3227e6cfSchs 62*3227e6cfSchs errno -- the error number (eg, from ioctl(2)).""" 63*3227e6cfSchs 64*3227e6cfSchs __slots__ = "why", "task", "errno" 65*3227e6cfSchs __repr__ = default_repr 66*3227e6cfSchs 67*3227e6cfSchs def __init__(self, eno, task=None, why=None): 68*3227e6cfSchs """Create a ZFS exception. 69*3227e6cfSchs eno -- the error number (errno) 70*3227e6cfSchs task -- a string describing the task that failed 71*3227e6cfSchs why -- a string describing why it failed (defaults to 72*3227e6cfSchs strerror(eno))""" 73*3227e6cfSchs 74*3227e6cfSchs self.errno = eno 75*3227e6cfSchs self.task = task 76*3227e6cfSchs self.why = why 77*3227e6cfSchs 78*3227e6cfSchs def __str__(self): 79*3227e6cfSchs s = "" 80*3227e6cfSchs if self.task: 81*3227e6cfSchs s += self.task + ": " 82*3227e6cfSchs if self.why: 83*3227e6cfSchs s += self.why 84*3227e6cfSchs else: 85*3227e6cfSchs s += self.strerror 86*3227e6cfSchs return s 87*3227e6cfSchs 88*3227e6cfSchs __strs = { 89*3227e6cfSchs errno.EPERM: _("permission denied"), 90*3227e6cfSchs errno.ECANCELED: 91*3227e6cfSchs _("delegated administration is disabled on pool"), 92*3227e6cfSchs errno.EINTR: _("signal received"), 93*3227e6cfSchs errno.EIO: _("I/O error"), 94*3227e6cfSchs errno.ENOENT: _("dataset does not exist"), 95*3227e6cfSchs errno.ENOSPC: _("out of space"), 96*3227e6cfSchs errno.EEXIST: _("dataset already exists"), 97*3227e6cfSchs errno.EBUSY: _("dataset is busy"), 98*3227e6cfSchs errno.EROFS: 99*3227e6cfSchs _("snapshot permissions cannot be modified"), 100*3227e6cfSchs errno.ENAMETOOLONG: _("dataset name is too long"), 101*3227e6cfSchs errno.ENOTSUP: _("unsupported version"), 102*3227e6cfSchs errno.EAGAIN: _("pool I/O is currently suspended"), 103*3227e6cfSchs } 104*3227e6cfSchs 105*3227e6cfSchs __strs[errno.EACCES] = __strs[errno.EPERM] 106*3227e6cfSchs __strs[errno.ENXIO] = __strs[errno.EIO] 107*3227e6cfSchs __strs[errno.ENODEV] = __strs[errno.EIO] 108*3227e6cfSchs __strs[errno.EDQUOT] = __strs[errno.ENOSPC] 109*3227e6cfSchs 110*3227e6cfSchs @property 111*3227e6cfSchs def strerror(self): 112*3227e6cfSchs return ZFSError.__strs.get(self.errno, os.strerror(self.errno)) 113*3227e6cfSchs 114*3227e6cfSchsdef nicenum(num): 115*3227e6cfSchs """Return a nice string (eg "1.23M") for this integer.""" 116*3227e6cfSchs index = 0; 117*3227e6cfSchs n = num; 118*3227e6cfSchs 119*3227e6cfSchs while n >= 1024: 120*3227e6cfSchs n /= 1024 121*3227e6cfSchs index += 1 122*3227e6cfSchs 123*3227e6cfSchs u = " KMGTPE"[index] 124*3227e6cfSchs if index == 0: 125*3227e6cfSchs return "%u" % n; 126*3227e6cfSchs elif n >= 100 or num & ((1024*index)-1) == 0: 127*3227e6cfSchs # it's an exact multiple of its index, or it wouldn't 128*3227e6cfSchs # fit as floating point, so print as an integer 129*3227e6cfSchs return "%u%c" % (n, u) 130*3227e6cfSchs else: 131*3227e6cfSchs # due to rounding, it's tricky to tell what precision to 132*3227e6cfSchs # use; try each precision and see which one fits 133*3227e6cfSchs for i in (2, 1, 0): 134*3227e6cfSchs s = "%.*f%c" % (i, float(num) / (1<<(10*index)), u) 135*3227e6cfSchs if len(s) <= 5: 136*3227e6cfSchs return s 137*3227e6cfSchs 138*3227e6cfSchsdef append_with_opt(option, opt, value, parser): 139*3227e6cfSchs """A function for OptionParser which appends a tuple (opt, value).""" 140*3227e6cfSchs getattr(parser.values, option.dest).append((opt, value)) 141*3227e6cfSchs 142