4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2012 OmniTI Computer Consulting, Inc. All rights reserved.
28 #include <sys/varargs.h>
30 #include <libnvpair.h>
33 //#include <libbe_priv.h>
34 boolean_t
be_valid_be_name(const char *);
38 BE_PY_ERR_APPEND
= 6000,
48 * public libbe functions
51 PyObject
*beCreateSnapshot(PyObject
*, PyObject
*);
52 PyObject
*beCopy(PyObject
*, PyObject
*);
53 PyObject
*beList(PyObject
*, PyObject
*);
54 PyObject
*beActivate(PyObject
*, PyObject
*);
55 PyObject
*beDestroy(PyObject
*, PyObject
*);
56 PyObject
*beDestroySnapshot(PyObject
*, PyObject
*);
57 PyObject
*beRename(PyObject
*, PyObject
*);
58 PyObject
*beMount(PyObject
*, PyObject
*);
59 PyObject
*beUnmount(PyObject
*, PyObject
*);
60 PyObject
*bePrintErrors(PyObject
*, PyObject
*);
61 PyObject
*beGetErrDesc(PyObject
*, PyObject
*);
62 char *beMapLibbePyErrorToString(int);
65 static boolean_t
convertBEInfoToDictionary(be_node_list_t
*be
,
67 static boolean_t
convertDatasetInfoToDictionary(be_dataset_list_t
*ds
,
69 static boolean_t
convertSnapshotInfoToDictionary(be_snapshot_list_t
*ss
,
71 static boolean_t
convertPyArgsToNvlist(nvlist_t
**nvList
, int numArgs
, ...);
79 * Function: beCreateSnapshot
80 * Description: Convert Python args to nvlist pairs and
81 * call libbe:be_create_snapshot to create a
82 * snapshot of all the datasets within a BE
84 * args - pointer to a python object containing:
85 * beName - The name of the BE to create a snapshot of
86 * snapName - The name of the snapshot to create (optional)
88 * The following public attribute values. defined by libbe.h,
89 * are used by this function:
91 * Returns a pointer to a python object and an optional snapshot name:
92 * 0, [snapName] - Success
93 * 1, [snapName] - Failure
99 beCreateSnapshot(PyObject
*self
, PyObject
*args
)
102 char *snapName
= NULL
;
103 int ret
= BE_PY_SUCCESS
;
104 nvlist_t
*beAttrs
= NULL
;
105 PyObject
*retVals
= NULL
;
107 if (!PyArg_ParseTuple(args
, "z|z", &beName
, &snapName
)) {
108 return (Py_BuildValue("[is]", BE_PY_ERR_PARSETUPLE
, NULL
));
111 if (!convertPyArgsToNvlist(&beAttrs
, 4,
112 BE_ATTR_ORIG_BE_NAME
, beName
,
113 BE_ATTR_SNAP_NAME
, snapName
)) {
114 nvlist_free(beAttrs
);
115 return (Py_BuildValue("[is]", BE_PY_ERR_NVLIST
, NULL
));
118 if (beAttrs
== NULL
) {
119 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
122 if ((ret
= be_create_snapshot(beAttrs
)) != 0) {
123 nvlist_free(beAttrs
);
124 return (Py_BuildValue("[is]", ret
, NULL
));
126 if (snapName
== NULL
) {
127 if (nvlist_lookup_pairs(beAttrs
, NV_FLAG_NOENTOK
,
128 BE_ATTR_SNAP_NAME
, DATA_TYPE_STRING
, &snapName
,
130 nvlist_free(beAttrs
);
131 return (Py_BuildValue("[is]",
132 BE_PY_ERR_NVLIST
, NULL
));
134 retVals
= Py_BuildValue("[is]", ret
, snapName
);
135 nvlist_free(beAttrs
);
138 nvlist_free(beAttrs
);
140 return (Py_BuildValue("[is]", ret
, NULL
));
145 * Description: Convert Python args to nvlist pairs and call libbe:be_copy
146 * to create a Boot Environment
148 * args - pointer to a python object containing:
149 * trgtBeName - The name of the BE to create
150 * srcBeName - The name of the BE used to create trgtBeName (optional)
151 * rpool - The pool to create the new BE in (optional)
152 * srcSnapName - The snapshot name (optional)
153 * beNameProperties - The properties to use when creating
156 * Returns a pointer to a python object. That Python object will consist of
157 * the return code and optional attributes, trgtBeName and snapshotName
158 * BE_SUCCESS, [trgtBeName], [trgtSnapName] - Success
159 * 1, [trgtBeName], [trgtSnapName] - Failure
165 beCopy(PyObject
*self
, PyObject
*args
)
167 char *trgtBeName
= NULL
;
168 char *srcBeName
= NULL
;
169 char *srcSnapName
= NULL
;
170 char *trgtSnapName
= NULL
;
172 char *beDescription
= NULL
;
174 int ret
= BE_PY_SUCCESS
;
175 nvlist_t
*beAttrs
= NULL
;
176 nvlist_t
*beProps
= NULL
;
177 PyObject
*beNameProperties
= NULL
;
178 PyObject
*pkey
= NULL
;
179 PyObject
*pvalue
= NULL
;
180 PyObject
*retVals
= NULL
;
182 if (!PyArg_ParseTuple(args
, "|zzzzOz", &trgtBeName
, &srcBeName
,
183 &srcSnapName
, &rpool
, &beNameProperties
, &beDescription
)) {
184 return (Py_BuildValue("[iss]", BE_PY_ERR_PARSETUPLE
,
188 if (!convertPyArgsToNvlist(&beAttrs
, 10,
189 BE_ATTR_NEW_BE_NAME
, trgtBeName
,
190 BE_ATTR_ORIG_BE_NAME
, srcBeName
,
191 BE_ATTR_SNAP_NAME
, srcSnapName
,
192 BE_ATTR_NEW_BE_POOL
, rpool
,
193 BE_ATTR_NEW_BE_DESC
, beDescription
)) {
194 nvlist_free(beAttrs
);
195 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST
, NULL
, NULL
));
198 if (beNameProperties
!= NULL
) {
199 if (nvlist_alloc(&beProps
, NV_UNIQUE_NAME
, 0) != 0) {
200 (void) printf("nvlist_alloc failed.\n");
201 nvlist_free(beAttrs
);
202 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST
,
205 while (PyDict_Next(beNameProperties
, &pos
, &pkey
, &pvalue
)) {
206 if (!convertPyArgsToNvlist(&beProps
, 2,
207 PyString_AsString(pkey
),
208 PyString_AsString(pvalue
))) {
209 nvlist_free(beProps
);
210 nvlist_free(beAttrs
);
211 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST
,
217 if (beProps
!= NULL
&& beAttrs
!= NULL
&&
218 nvlist_add_nvlist(beAttrs
, BE_ATTR_ZFS_PROPERTIES
,
220 nvlist_free(beProps
);
221 nvlist_free(beAttrs
);
222 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST
,
226 if (beProps
!= NULL
) nvlist_free(beProps
);
228 if (trgtBeName
== NULL
) {
230 * Caller wants to get back the BE_ATTR_NEW_BE_NAME and
233 if ((ret
= be_copy(beAttrs
)) != BE_SUCCESS
) {
234 nvlist_free(beAttrs
);
235 return (Py_BuildValue("[iss]", ret
, NULL
, NULL
));
239 * When no trgtBeName is passed to be_copy, be_copy
240 * returns an auto generated beName and snapshot name.
242 if (nvlist_lookup_string(beAttrs
, BE_ATTR_NEW_BE_NAME
,
244 nvlist_free(beAttrs
);
245 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST
,
248 if (nvlist_lookup_string(beAttrs
, BE_ATTR_SNAP_NAME
,
249 &trgtSnapName
) != 0) {
250 nvlist_free(beAttrs
);
251 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST
,
255 retVals
= Py_BuildValue("[iss]", BE_PY_SUCCESS
,
256 trgtBeName
, trgtSnapName
);
257 nvlist_free(beAttrs
);
261 ret
= be_copy(beAttrs
);
262 nvlist_free(beAttrs
);
263 return (Py_BuildValue("[iss]", ret
, NULL
, NULL
));
269 * Description: Convert Python args to nvlist pairs and call libbe:be_list
270 * to gather information about Boot Environments
272 * args - pointer to a python object containing:
273 * beName - The name of the BE to list (optional)
275 * Returns a pointer to a python object. That Python object will consist of
276 * the return code and a list of Dicts or NULL.
277 * BE_PY_SUCCESS, listOfDicts - Success
278 * bePyErr or be_errno_t, NULL - Failure
284 beList(PyObject
*self
, PyObject
*args
)
287 int ret
= BE_PY_SUCCESS
;
288 be_node_list_t
*list
= NULL
;
289 be_node_list_t
*be
= NULL
;
290 PyObject
*dict
= NULL
;
291 PyObject
*listOfDicts
= NULL
;
293 if ((listOfDicts
= PyList_New(0)) == NULL
) {
294 ret
= BE_PY_ERR_DICT
;
295 listOfDicts
= Py_None
;
299 if (!PyArg_ParseTuple(args
, "|z", &beName
)) {
300 ret
= BE_PY_ERR_PARSETUPLE
;
304 if ((ret
= be_list(beName
, &list
)) != BE_SUCCESS
) {
308 for (be
= list
; be
!= NULL
; be
= be
->be_next_node
) {
309 be_dataset_list_t
*ds
= be
->be_node_datasets
;
310 be_snapshot_list_t
*ss
= be
->be_node_snapshots
;
312 if ((dict
= PyDict_New()) == NULL
) {
313 ret
= BE_PY_ERR_DICT
;
317 if (!convertBEInfoToDictionary(be
, &dict
)) {
320 ret
= BE_PY_ERR_VAR_CONV
;
324 if (PyList_Append(listOfDicts
, dict
) != 0) {
327 ret
= BE_PY_ERR_APPEND
;
335 if ((dict
= PyDict_New()) == NULL
) {
336 ret
= BE_PY_ERR_DICT
;
340 if (!convertDatasetInfoToDictionary(ds
, &dict
)) {
343 ret
= BE_PY_ERR_VAR_CONV
;
347 if (PyList_Append(listOfDicts
, dict
) != 0) {
350 ret
= BE_PY_ERR_APPEND
;
354 ds
= ds
->be_next_dataset
;
362 if ((dict
= PyDict_New()) == NULL
) {
365 ret
= BE_PY_ERR_DICT
;
369 if (!convertSnapshotInfoToDictionary(ss
, &dict
)) {
372 ret
= BE_PY_ERR_VAR_CONV
;
376 if (PyList_Append(listOfDicts
, dict
) != 0) {
379 ret
= BE_PY_ERR_APPEND
;
383 ss
= ss
->be_next_snapshot
;
393 return (Py_BuildValue("[iO]", ret
, listOfDicts
));
397 * Function: beActivate
398 * Description: Convert Python args to nvlist pairs and call libbe:be_activate
399 * to activate a Boot Environment
401 * args - pointer to a python object containing:
402 * beName - The name of the BE to activate
404 * Returns a pointer to a python object:
405 * BE_SUCCESS - Success
406 * bePyErr or be_errno_t - Failure
412 beActivate(PyObject
*self
, PyObject
*args
)
415 int ret
= BE_PY_SUCCESS
;
416 nvlist_t
*beAttrs
= NULL
;
418 if (!PyArg_ParseTuple(args
, "z", &beName
)) {
419 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
422 if (!convertPyArgsToNvlist(&beAttrs
, 2, BE_ATTR_ORIG_BE_NAME
, beName
)) {
423 nvlist_free(beAttrs
);
424 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
427 if (beAttrs
== NULL
) {
428 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
431 ret
= be_activate(beAttrs
);
432 nvlist_free(beAttrs
);
433 return (Py_BuildValue("i", ret
));
437 * Function: beDestroy
438 * Description: Convert Python args to nvlist pairs and call libbe:be_destroy
439 * to destroy a Boot Environment
441 * args - pointer to a python object containing:
442 * beName - The name of the BE to destroy
444 * Returns a pointer to a python object:
445 * BE_SUCCESS - Success
446 * bePyErr or be_errno_t - Failure
452 beDestroy(PyObject
*self
, PyObject
*args
)
455 int destroy_snaps
= 0;
456 int force_unmount
= 0;
457 int destroy_flags
= 0;
458 int ret
= BE_PY_SUCCESS
;
459 nvlist_t
*beAttrs
= NULL
;
461 if (!PyArg_ParseTuple(args
, "z|ii", &beName
, &destroy_snaps
,
463 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
466 if (destroy_snaps
== 1)
467 destroy_flags
|= BE_DESTROY_FLAG_SNAPSHOTS
;
469 if (force_unmount
== 1)
470 destroy_flags
|= BE_DESTROY_FLAG_FORCE_UNMOUNT
;
472 if (!convertPyArgsToNvlist(&beAttrs
, 2, BE_ATTR_ORIG_BE_NAME
, beName
)) {
473 nvlist_free(beAttrs
);
474 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
477 if (nvlist_add_uint16(beAttrs
, BE_ATTR_DESTROY_FLAGS
, destroy_flags
)
479 (void) printf("nvlist_add_uint16 failed for "
480 "BE_ATTR_DESTROY_FLAGS (%d).\n", destroy_flags
);
481 nvlist_free(beAttrs
);
482 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
485 if (beAttrs
== NULL
) {
486 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
489 ret
= be_destroy(beAttrs
);
490 nvlist_free(beAttrs
);
491 return (Py_BuildValue("i", ret
));
495 * Function: beDestroySnapshot
496 * Description: Convert Python args to nvlist pairs and call libbe:be_destroy
497 * to destroy a snapshot of a Boot Environment
499 * args - pointer to a python object containing:
500 * beName - The name of the BE to destroy
501 * snapName - The name of the snapshot to destroy
503 * Returns a pointer to a python object:
504 * BE_SUCCESS - Success
505 * bePyErr or be_errno_t - Failure
511 beDestroySnapshot(PyObject
*self
, PyObject
*args
)
514 char *snapName
= NULL
;
515 int ret
= BE_PY_SUCCESS
;
516 nvlist_t
*beAttrs
= NULL
;
518 if (!PyArg_ParseTuple(args
, "zz", &beName
, &snapName
)) {
519 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
522 if (!convertPyArgsToNvlist(&beAttrs
, 4,
523 BE_ATTR_ORIG_BE_NAME
, beName
,
524 BE_ATTR_SNAP_NAME
, snapName
)) {
525 nvlist_free(beAttrs
);
526 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
529 if (beAttrs
== NULL
) {
530 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
533 ret
= be_destroy_snapshot(beAttrs
);
534 nvlist_free(beAttrs
);
535 return (Py_BuildValue("i", ret
));
540 * Description: Convert Python args to nvlist pairs and call libbe:be_rename
541 * to rename a Boot Environment
543 * args - pointer to a python object containing:
544 * oldBeName - The name of the old Boot Environment
545 * newBeName - The name of the new Boot Environment
547 * Returns a pointer to a python object:
548 * BE_SUCCESS - Success
549 * bePyErr or be_errno_t - Failure
555 beRename(PyObject
*self
, PyObject
*args
)
557 char *oldBeName
= NULL
;
558 char *newBeName
= NULL
;
559 int ret
= BE_PY_SUCCESS
;
560 nvlist_t
*beAttrs
= NULL
;
562 if (!PyArg_ParseTuple(args
, "zz", &oldBeName
, &newBeName
)) {
563 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
566 if (!convertPyArgsToNvlist(&beAttrs
, 4,
567 BE_ATTR_ORIG_BE_NAME
, oldBeName
,
568 BE_ATTR_NEW_BE_NAME
, newBeName
)) {
569 nvlist_free(beAttrs
);
570 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
573 if (beAttrs
== NULL
) {
574 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
577 ret
= be_rename(beAttrs
);
578 nvlist_free(beAttrs
);
579 return (Py_BuildValue("i", ret
));
584 * Description: Convert Python args to nvlist pairs and call libbe:be_mount
585 * to mount a Boot Environment
587 * args - pointer to a python object containing:
588 * beName - The name of the Boot Environment to mount
589 * mountpoint - The path of the mountpoint to mount the
590 * Boot Environment on (optional)
592 * Returns a pointer to a python object:
593 * BE_SUCCESS - Success
594 * bePyErr or be_errno_t - Failure
600 beMount(PyObject
*self
, PyObject
*args
)
603 char *mountpoint
= NULL
;
604 int ret
= BE_PY_SUCCESS
;
605 nvlist_t
*beAttrs
= NULL
;
607 if (!PyArg_ParseTuple(args
, "zz", &beName
, &mountpoint
)) {
608 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
611 if (!convertPyArgsToNvlist(&beAttrs
, 4,
612 BE_ATTR_ORIG_BE_NAME
, beName
,
613 BE_ATTR_MOUNTPOINT
, mountpoint
)) {
614 nvlist_free(beAttrs
);
615 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
618 if (beAttrs
== NULL
) {
619 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
622 ret
= be_mount(beAttrs
);
623 nvlist_free(beAttrs
);
624 return (Py_BuildValue("i", ret
));
628 * Function: beUnmount
629 * Description: Convert Python args to nvlist pairs and call libbe:be_unmount
630 * to unmount a Boot Environment
632 * args - pointer to a python object containing:
633 * beName - The name of the Boot Environment to unmount
635 * Returns a pointer to a python object:
636 * BE_SUCCESS - Success
637 * bePyErr or be_errno_t - Failure
643 beUnmount(PyObject
*self
, PyObject
*args
)
646 int force_unmount
= 0;
647 int unmount_flags
= 0;
648 int ret
= BE_PY_SUCCESS
;
649 nvlist_t
*beAttrs
= NULL
;
651 if (!PyArg_ParseTuple(args
, "z|i", &beName
, &force_unmount
)) {
652 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
655 if (force_unmount
== 1)
656 unmount_flags
|= BE_UNMOUNT_FLAG_FORCE
;
658 if (!convertPyArgsToNvlist(&beAttrs
, 2,
659 BE_ATTR_ORIG_BE_NAME
, beName
)) {
660 nvlist_free(beAttrs
);
661 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
664 if (nvlist_add_uint16(beAttrs
, BE_ATTR_UNMOUNT_FLAGS
, unmount_flags
)
666 (void) printf("nvlist_add_uint16 failed for "
667 "BE_ATTR_UNMOUNT_FLAGS (%d).\n", unmount_flags
);
668 nvlist_free(beAttrs
);
669 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
672 if (beAttrs
== NULL
) {
673 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
676 ret
= be_unmount(beAttrs
);
677 nvlist_free(beAttrs
);
678 return (Py_BuildValue("i", ret
));
682 * Function: beRollback
683 * Description: Convert Python args to nvlist pairs and call libbe:be_rollback
684 * to rollback a Boot Environment to a previously taken
687 * args - pointer to a python object containing:
688 * beName - The name of the Boot Environment to unmount
690 * Returns a pointer to a python object:
691 * BE_SUCCESS - Success
692 * bePyErr or be_errno_t - Failure
698 beRollback(PyObject
*self
, PyObject
*args
)
701 char *snapName
= NULL
;
702 int ret
= BE_PY_SUCCESS
;
703 nvlist_t
*beAttrs
= NULL
;
705 if (!PyArg_ParseTuple(args
, "zz", &beName
, &snapName
)) {
706 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
709 if (!convertPyArgsToNvlist(&beAttrs
, 4,
710 BE_ATTR_ORIG_BE_NAME
, beName
,
711 BE_ATTR_SNAP_NAME
, snapName
)) {
712 nvlist_free(beAttrs
);
713 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
716 if (beAttrs
== NULL
) {
717 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
720 ret
= be_rollback(beAttrs
);
721 nvlist_free(beAttrs
);
722 return (Py_BuildValue("i", ret
));
726 * Function: bePrintErrors
727 * Description: Convert Python args to boolean and call libbe_print_errors to
728 * turn on/off error output for the library.
730 * args - pointer to a python object containing:
731 * print_errors - Boolean that turns library error
732 * printing on or off.
734 * args - pointer to a python object containing:
735 * 0 - do not print errors - Python boolean "False"
736 * 1 - print errors - Python boolean "True"
738 * Returns 1 on missing or invalid argument, 0 otherwise
744 bePrintErrors(PyObject
*self
, PyObject
*args
)
748 if (!PyArg_ParseTuple(args
, "i", &print_errors
) ||
749 (print_errors
!= 1 && print_errors
!= 0))
750 return (Py_BuildValue("i", BE_PY_ERR_PRINT_ERR
));
751 libbe_print_errors(print_errors
== 1);
752 return (Py_BuildValue("i", BE_PY_SUCCESS
));
756 * Function: beGetErrDesc
757 * Description: Convert Python args to an int and call be_err_to_str to
758 * map an error code to an error string.
760 * args - pointer to a python object containing:
761 * errCode - value to map to an error string.
763 * Returns: error string or NULL
769 beGetErrDesc(PyObject
*self
, PyObject
*args
)
772 char *beErrStr
= NULL
;
774 if (!PyArg_ParseTuple(args
, "i", &errCode
)) {
775 return (Py_BuildValue("s", NULL
));
779 * First check libbe_py errors. If NULL is returned check error codes
783 if ((beErrStr
= beMapLibbePyErrorToString(errCode
)) == NULL
) {
784 beErrStr
= be_err_to_str(errCode
);
787 return (Py_BuildValue("s", beErrStr
));
791 * Function: beVerifyBEName
792 * Description: Call be_valid_be_name() to verify the BE name.
794 * args - pointer to a python object containing:
795 * string - value to map to a string.
797 * Returns: 0 for success or 1 for failure
803 beVerifyBEName(PyObject
*self
, PyObject
*args
)
807 if (!PyArg_ParseTuple(args
, "s", &string
)) {
808 return (Py_BuildValue("i", 1));
811 if (be_valid_be_name(string
)) {
812 return (Py_BuildValue("i", 0));
814 return (Py_BuildValue("i", 1));
818 /* ~~~~~~~~~~~~~~~~~ */
819 /* Private Functions */
820 /* ~~~~~~~~~~~~~~~~~ */
823 convertBEInfoToDictionary(be_node_list_t
*be
, PyObject
**listDict
)
825 if (be
->be_node_name
!= NULL
) {
826 if (PyDict_SetItemString(*listDict
, BE_ATTR_ORIG_BE_NAME
,
827 PyString_FromString(be
->be_node_name
)) != 0) {
832 if (be
->be_rpool
!= NULL
) {
833 if (PyDict_SetItemString(*listDict
, BE_ATTR_ORIG_BE_POOL
,
834 PyString_FromString(be
->be_rpool
)) != 0) {
839 if (be
->be_mntpt
!= NULL
) {
840 if (PyDict_SetItemString(*listDict
, BE_ATTR_MOUNTPOINT
,
841 PyString_FromString(be
->be_mntpt
)) != 0) {
846 if (PyDict_SetItemString(*listDict
, BE_ATTR_MOUNTED
,
847 (be
->be_mounted
? Py_True
: Py_False
)) != 0) {
851 if (PyDict_SetItemString(*listDict
, BE_ATTR_ACTIVE
,
852 (be
->be_active
? Py_True
: Py_False
)) != 0) {
856 if (PyDict_SetItemString(*listDict
, BE_ATTR_ACTIVE_ON_BOOT
,
857 (be
->be_active_on_boot
? Py_True
: Py_False
)) != 0) {
861 if (PyDict_SetItemString(*listDict
, BE_ATTR_GLOBAL_ACTIVE
,
862 (be
->be_global_active
? Py_True
: Py_False
)) != 0) {
866 if (be
->be_space_used
!= 0) {
867 if (PyDict_SetItemString(*listDict
, BE_ATTR_SPACE
,
868 PyLong_FromUnsignedLongLong(be
->be_space_used
)) != 0) {
873 if (be
->be_root_ds
!= NULL
) {
874 if (PyDict_SetItemString(*listDict
, BE_ATTR_ROOT_DS
,
875 PyString_FromString(be
->be_root_ds
)) != 0) {
880 if (be
->be_node_creation
!= NULL
) {
881 if (PyDict_SetItemString(*listDict
, BE_ATTR_DATE
,
882 PyLong_FromLong(be
->be_node_creation
)) != 0) {
887 if (be
->be_policy_type
!= NULL
) {
888 if (PyDict_SetItemString(*listDict
, BE_ATTR_POLICY
,
889 PyString_FromString(be
->be_policy_type
)) != 0) {
894 if (be
->be_uuid_str
!= NULL
) {
895 if (PyDict_SetItemString(*listDict
, BE_ATTR_UUID_STR
,
896 PyString_FromString(be
->be_uuid_str
)) != 0) {
905 convertDatasetInfoToDictionary(be_dataset_list_t
*ds
, PyObject
**listDict
)
907 if (ds
->be_dataset_name
!= NULL
) {
908 if (PyDict_SetItemString(*listDict
, BE_ATTR_DATASET
,
909 PyString_FromString(ds
->be_dataset_name
)) != 0) {
914 if (PyDict_SetItemString(*listDict
, BE_ATTR_STATUS
,
915 (ds
->be_ds_mounted
? Py_True
: Py_False
)) != 0) {
919 if (ds
->be_ds_mntpt
!= NULL
) {
920 if (PyDict_SetItemString(*listDict
, BE_ATTR_MOUNTPOINT
,
921 PyString_FromString(ds
->be_ds_mntpt
)) != 0) {
926 if (PyDict_SetItemString(*listDict
, BE_ATTR_MOUNTED
,
927 (ds
->be_ds_mounted
? Py_True
: Py_False
)) != 0) {
931 if (ds
->be_ds_space_used
!= 0) {
932 if (PyDict_SetItemString(*listDict
, BE_ATTR_SPACE
,
933 PyLong_FromUnsignedLongLong(ds
->be_ds_space_used
))
939 if (ds
->be_dataset_name
!= 0) {
940 if (PyDict_SetItemString(*listDict
, BE_ATTR_DATASET
,
941 PyString_FromString(ds
->be_dataset_name
)) != 0) {
946 if (ds
->be_ds_plcy_type
!= NULL
) {
947 if (PyDict_SetItemString(*listDict
, BE_ATTR_POLICY
,
948 PyString_FromString(ds
->be_ds_plcy_type
)) != 0) {
953 if (ds
->be_ds_creation
!= NULL
) {
954 if (PyDict_SetItemString(*listDict
, BE_ATTR_DATE
,
955 PyLong_FromLong(ds
->be_ds_creation
)) != 0) {
964 convertSnapshotInfoToDictionary(be_snapshot_list_t
*ss
, PyObject
**listDict
)
966 if (ss
->be_snapshot_name
!= NULL
) {
967 if (PyDict_SetItemString(*listDict
, BE_ATTR_SNAP_NAME
,
968 PyString_FromString(ss
->be_snapshot_name
)) != 0) {
973 if (ss
->be_snapshot_creation
!= NULL
) {
974 if (PyDict_SetItemString(*listDict
, BE_ATTR_DATE
,
975 PyLong_FromLong(ss
->be_snapshot_creation
)) != 0) {
980 if (ss
->be_snapshot_type
!= NULL
) {
981 if (PyDict_SetItemString(*listDict
, BE_ATTR_POLICY
,
982 PyString_FromString(ss
->be_snapshot_type
)) != 0) {
987 if (ss
->be_snapshot_space_used
!= 0) {
988 if (PyDict_SetItemString(*listDict
, BE_ATTR_SPACE
,
989 PyLong_FromUnsignedLongLong(ss
->be_snapshot_space_used
))
999 * Convert string arguments to nvlist attributes
1003 convertPyArgsToNvlist(nvlist_t
**nvList
, int numArgs
, ...)
1009 if (*nvList
== NULL
) {
1010 if (nvlist_alloc(nvList
, NV_UNIQUE_NAME
, 0) != 0) {
1011 (void) printf("nvlist_alloc failed.\n");
1016 va_start(ap
, numArgs
);
1018 for (i
= 0; i
< numArgs
; i
+= 2) {
1019 if ((pt
= va_arg(ap
, char *)) == NULL
||
1020 (pt2
= va_arg(ap
, char *)) == NULL
) {
1023 if (nvlist_add_string(*nvList
, pt
, pt2
) != 0) {
1024 (void) printf("nvlist_add_string failed for %s (%s).\n",
1026 nvlist_free(*nvList
);
1037 * Function: beMapLibbePyErrorToString
1038 * Description: Convert Python args to an int and map an error code to an
1041 * errCode - value to map to an error string.
1043 * Returns error string or NULL
1049 beMapLibbePyErrorToString(int errCode
)
1052 case BE_PY_ERR_APPEND
:
1053 return ("Unable to append a dictionary to a list "
1055 case BE_PY_ERR_DICT
:
1056 return ("Creation of a Python dictionary failed.");
1057 case BE_PY_ERR_LIST
:
1058 return ("beList() failed.");
1059 case BE_PY_ERR_NVLIST
:
1060 return ("An nvlist operation failed.");
1061 case BE_PY_ERR_PARSETUPLE
:
1062 return ("PyArg_ParseTuple() failed to convert variable to C.");
1063 case BE_PY_ERR_PRINT_ERR
:
1064 return ("bePrintErrors() failed.");
1065 case BE_PY_ERR_VAR_CONV
:
1066 return ("Unable to add variables to a Python dictionary.");
1072 /* Private python initialization structure */
1074 static struct PyMethodDef libbeMethods
[] = {
1075 {"beCopy", (PyCFunction
)beCopy
, METH_VARARGS
, "Create/Copy a BE."},
1076 {"beCreateSnapshot", (PyCFunction
)beCreateSnapshot
, METH_VARARGS
,
1077 "Create a snapshot."},
1078 {"beDestroy", (PyCFunction
)beDestroy
, METH_VARARGS
, "Destroy a BE."},
1079 {"beDestroySnapshot", (PyCFunction
)beDestroySnapshot
, METH_VARARGS
,
1080 "Destroy a snapshot."},
1081 {"beMount", (PyCFunction
)beMount
, METH_VARARGS
, "Mount a BE."},
1082 {"beUnmount", (PyCFunction
)beUnmount
, METH_VARARGS
, "Unmount a BE."},
1083 {"beList", (PyCFunction
)beList
, METH_VARARGS
, "List BE info."},
1084 {"beRename", (PyCFunction
)beRename
, METH_VARARGS
, "Rename a BE."},
1085 {"beActivate", (PyCFunction
)beActivate
, METH_VARARGS
, "Activate a BE."},
1086 {"beRollback", (PyCFunction
)beRollback
, METH_VARARGS
, "Rollback a BE."},
1087 {"bePrintErrors", (PyCFunction
)bePrintErrors
, METH_VARARGS
,
1088 "Enable/disable error printing."},
1089 {"beGetErrDesc", (PyCFunction
)beGetErrDesc
, METH_VARARGS
,
1090 "Map Error codes to strings."},
1091 {"beVerifyBEName", (PyCFunction
)beVerifyBEName
, METH_VARARGS
,
1093 {NULL
, NULL
, 0, NULL
}
1099 /* PyMODINIT_FUNC; */
1100 (void) Py_InitModule("libbe_py", libbeMethods
);