xref: /titanic_44/usr/src/lib/pyzfs/common/holds.py (revision 842727c2f41f01b380de4f5e787d905702870f23)
1*842727c2SChris Kirby#! /usr/bin/python2.4
2*842727c2SChris Kirby#
3*842727c2SChris Kirby# CDDL HEADER START
4*842727c2SChris Kirby#
5*842727c2SChris Kirby# The contents of this file are subject to the terms of the
6*842727c2SChris Kirby# Common Development and Distribution License (the "License").
7*842727c2SChris Kirby# You may not use this file except in compliance with the License.
8*842727c2SChris Kirby#
9*842727c2SChris Kirby# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*842727c2SChris Kirby# or http://www.opensolaris.org/os/licensing.
11*842727c2SChris Kirby# See the License for the specific language governing permissions
12*842727c2SChris Kirby# and limitations under the License.
13*842727c2SChris Kirby#
14*842727c2SChris Kirby# When distributing Covered Code, include this CDDL HEADER in each
15*842727c2SChris Kirby# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*842727c2SChris Kirby# If applicable, add the following below this CDDL HEADER, with the
17*842727c2SChris Kirby# fields enclosed by brackets "[]" replaced with your own identifying
18*842727c2SChris Kirby# information: Portions Copyright [yyyy] [name of copyright owner]
19*842727c2SChris Kirby#
20*842727c2SChris Kirby# CDDL HEADER END
21*842727c2SChris Kirby#
22*842727c2SChris Kirby# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*842727c2SChris Kirby# Use is subject to license terms.
24*842727c2SChris Kirby#
25*842727c2SChris Kirby
26*842727c2SChris Kirby"""This module implements the "zfs holds" subcommand.
27*842727c2SChris KirbyThe only public interface is the zfs.holds.do_holds() function."""
28*842727c2SChris Kirby
29*842727c2SChris Kirbyimport optparse
30*842727c2SChris Kirbyimport sys
31*842727c2SChris Kirbyimport errno
32*842727c2SChris Kirbyimport time
33*842727c2SChris Kirbyimport zfs.util
34*842727c2SChris Kirbyimport zfs.dataset
35*842727c2SChris Kirbyimport zfs.table
36*842727c2SChris Kirby
37*842727c2SChris Kirby_ = zfs.util._
38*842727c2SChris Kirby
39*842727c2SChris Kirbydef do_holds():
40*842727c2SChris Kirby	"""Implements the "zfs holds" subcommand."""
41*842727c2SChris Kirby	def usage(msg=None):
42*842727c2SChris Kirby		parser.print_help()
43*842727c2SChris Kirby		if msg:
44*842727c2SChris Kirby			print
45*842727c2SChris Kirby			parser.exit("zfs: error: " + msg)
46*842727c2SChris Kirby		else:
47*842727c2SChris Kirby			parser.exit()
48*842727c2SChris Kirby
49*842727c2SChris Kirby	u = _("""holds [-r] <snapshot> ...""")
50*842727c2SChris Kirby
51*842727c2SChris Kirby	parser = optparse.OptionParser(usage=u, prog="zfs")
52*842727c2SChris Kirby
53*842727c2SChris Kirby	parser.add_option("-r", action="store_true", dest="recursive",
54*842727c2SChris Kirby	    help=_("list holds recursively"))
55*842727c2SChris Kirby
56*842727c2SChris Kirby	(options, args) = parser.parse_args(sys.argv[2:])
57*842727c2SChris Kirby
58*842727c2SChris Kirby	if len(args) < 1:
59*842727c2SChris Kirby		usage(_("missing snapshot argument"))
60*842727c2SChris Kirby
61*842727c2SChris Kirby	fields = ("name", "tag", "timestamp")
62*842727c2SChris Kirby	rjustfields = ()
63*842727c2SChris Kirby	printing = False
64*842727c2SChris Kirby	t = zfs.table.Table(fields, rjustfields)
65*842727c2SChris Kirby	for ds in zfs.dataset.snapshots_fromcmdline(args, options.recursive):
66*842727c2SChris Kirby		for tag, tm in ds.get_holds().iteritems():
67*842727c2SChris Kirby			val = {"name": ds.name, "tag": tag,
68*842727c2SChris Kirby			    "timestamp": time.ctime(tm)}
69*842727c2SChris Kirby			t.addline(ds.name, val)
70*842727c2SChris Kirby			printing = True
71*842727c2SChris Kirby	if printing:
72*842727c2SChris Kirby		t.printme()
73