9f9230833b50b8271840dc2c12bd1e94d9df7d12Alexander Pyhalov#!@PYTHON@ -S
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens#
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# CDDL HEADER START
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens#
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#
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# or http://www.opensolaris.org/os/licensing.
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# See the License for the specific language governing permissions
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# and limitations under the License.
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens#
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#
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# CDDL HEADER END
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens#
6d52f363e3b2c0c5da672c5b8c8adec99d345f38Lori Alt# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens#
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# Note, we want SIGINT (control-c) to exit the process quietly, to mimic
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# the standard behavior of C programs. The best we can do with pure
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# Python is to run with -S (to disable "import site"), and start our
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# program with a "try" statement. Hopefully nobody hits ^C before our
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens# try statement is executed.
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrenstry:
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens import site
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens import gettext
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens import zfs.util
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens import zfs.ioctl
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens import sys
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens import errno
e4d060fb4c00d44cd578713eb9a921f594b733b8Sam Falkner import solaris.misc
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens """This is the main script for doing zfs subcommands. It doesn't know
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens what subcommands there are, it just looks for a module zfs.<subcommand>
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens that implements that subcommand."""
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
e4d060fb4c00d44cd578713eb9a921f594b733b8Sam Falkner try:
e4d060fb4c00d44cd578713eb9a921f594b733b8Sam Falkner _ = gettext.translation("SUNW_OST_OSCMD", "/usr/lib/locale",
e4d060fb4c00d44cd578713eb9a921f594b733b8Sam Falkner fallback=True).gettext
e4d060fb4c00d44cd578713eb9a921f594b733b8Sam Falkner except:
e4d060fb4c00d44cd578713eb9a921f594b733b8Sam Falkner _ = solaris.misc.gettext
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens if len(sys.argv) < 2:
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens sys.exit(_("missing subcommand argument"))
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens zfs.ioctl.set_cmdstr(" ".join(["zfs"] + sys.argv[1:]))
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens try:
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # import zfs.<subcommand>
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens # subfunc = zfs.<subcommand>.do_<subcommand>
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens subcmd = sys.argv[1]
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens __import__("zfs." + subcmd)
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens submod = getattr(zfs, subcmd)
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens subfunc = getattr(submod, "do_" + subcmd)
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens except (ImportError, AttributeError):
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens sys.exit(_("invalid subcommand"))
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens try:
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens subfunc()
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens except zfs.util.ZFSError, e:
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens print(e)
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens sys.exit(1)
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrensexcept IOError, e:
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens import errno
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens import sys
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens if e.errno == errno.EPIPE:
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens sys.exit(1)
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens raise
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrensexcept KeyboardInterrupt:
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens import sys
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens sys.exit(1)