xref: /illumos-gate/usr/src/lib/pyzfs/common/holds.py (revision e8921a52)
19f923083SAlexander Pyhalov#!@PYTHON@
2842727c2SChris Kirby#
3842727c2SChris Kirby# CDDL HEADER START
4842727c2SChris Kirby#
5842727c2SChris Kirby# The contents of this file are subject to the terms of the
6842727c2SChris Kirby# Common Development and Distribution License (the "License").
7842727c2SChris Kirby# You may not use this file except in compliance with the License.
8842727c2SChris Kirby#
9842727c2SChris Kirby# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10842727c2SChris Kirby# or http://www.opensolaris.org/os/licensing.
11842727c2SChris Kirby# See the License for the specific language governing permissions
12842727c2SChris Kirby# and limitations under the License.
13842727c2SChris Kirby#
14842727c2SChris Kirby# When distributing Covered Code, include this CDDL HEADER in each
15842727c2SChris Kirby# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16842727c2SChris Kirby# If applicable, add the following below this CDDL HEADER, with the
17842727c2SChris Kirby# fields enclosed by brackets "[]" replaced with your own identifying
18842727c2SChris Kirby# information: Portions Copyright [yyyy] [name of copyright owner]
19842727c2SChris Kirby#
20842727c2SChris Kirby# CDDL HEADER END
21842727c2SChris Kirby#
226d52f363SLori Alt# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23*e8921a52SAndy Fiddaman# Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
24842727c2SChris Kirby#
25842727c2SChris Kirby
26842727c2SChris Kirby"""This module implements the "zfs holds" subcommand.
27842727c2SChris KirbyThe only public interface is the zfs.holds.do_holds() function."""
28842727c2SChris Kirby
29842727c2SChris Kirbyimport optparse
30842727c2SChris Kirbyimport sys
31842727c2SChris Kirbyimport errno
32842727c2SChris Kirbyimport time
33842727c2SChris Kirbyimport zfs.util
34842727c2SChris Kirbyimport zfs.dataset
35842727c2SChris Kirbyimport zfs.table
36842727c2SChris Kirby
37842727c2SChris Kirby_ = zfs.util._
38842727c2SChris Kirby
39842727c2SChris Kirbydef do_holds():
40842727c2SChris Kirby	"""Implements the "zfs holds" subcommand."""
41842727c2SChris Kirby	def usage(msg=None):
42842727c2SChris Kirby		parser.print_help()
43842727c2SChris Kirby		if msg:
44842727c2SChris Kirby			print
45842727c2SChris Kirby			parser.exit("zfs: error: " + msg)
46842727c2SChris Kirby		else:
47842727c2SChris Kirby			parser.exit()
48842727c2SChris Kirby
49842727c2SChris Kirby	u = _("""holds [-r] <snapshot> ...""")
50842727c2SChris Kirby
51842727c2SChris Kirby	parser = optparse.OptionParser(usage=u, prog="zfs")
52842727c2SChris Kirby
53842727c2SChris Kirby	parser.add_option("-r", action="store_true", dest="recursive",
54842727c2SChris Kirby	    help=_("list holds recursively"))
55842727c2SChris Kirby
56842727c2SChris Kirby	(options, args) = parser.parse_args(sys.argv[2:])
57842727c2SChris Kirby
58842727c2SChris Kirby	if len(args) < 1:
59842727c2SChris Kirby		usage(_("missing snapshot argument"))
60842727c2SChris Kirby
61842727c2SChris Kirby	fields = ("name", "tag", "timestamp")
62842727c2SChris Kirby	rjustfields = ()
63*e8921a52SAndy Fiddaman	printing = False
64d7747cbcSChris Kirby	gotone = False
65*e8921a52SAndy Fiddaman	t = zfs.table.Table(fields, rjustfields)
66842727c2SChris Kirby	for ds in zfs.dataset.snapshots_fromcmdline(args, options.recursive):
67d7747cbcSChris Kirby		gotone = True
68*e8921a52SAndy Fiddaman		for tag, tm in ds.get_holds().items():
69842727c2SChris Kirby			val = {"name": ds.name, "tag": tag,
70842727c2SChris Kirby			    "timestamp": time.ctime(tm)}
71842727c2SChris Kirby			t.addline(ds.name, val)
72842727c2SChris Kirby			printing = True
73842727c2SChris Kirby	if printing:
74842727c2SChris Kirby		t.printme()
75d7747cbcSChris Kirby	elif not gotone:
76d7747cbcSChris Kirby		raise zfs.util.ZFSError(errno.ENOENT, _("no matching datasets"))
77