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>
37 BE_PY_ERR_APPEND
= 6000,
47 * public libbe functions
50 PyObject
*beCreateSnapshot(PyObject
*, PyObject
*);
51 PyObject
*beCopy(PyObject
*, PyObject
*);
52 PyObject
*beList(PyObject
*, PyObject
*);
53 PyObject
*beActivate(PyObject
*, PyObject
*);
54 PyObject
*beDestroy(PyObject
*, PyObject
*);
55 PyObject
*beDestroySnapshot(PyObject
*, PyObject
*);
56 PyObject
*beRename(PyObject
*, PyObject
*);
57 PyObject
*beMount(PyObject
*, PyObject
*);
58 PyObject
*beUnmount(PyObject
*, PyObject
*);
59 PyObject
*bePrintErrors(PyObject
*, PyObject
*);
60 PyObject
*beGetErrDesc(PyObject
*, PyObject
*);
61 char *beMapLibbePyErrorToString(int);
64 static boolean_t
convertBEInfoToDictionary(be_node_list_t
*be
,
66 static boolean_t
convertDatasetInfoToDictionary(be_dataset_list_t
*ds
,
68 static boolean_t
convertSnapshotInfoToDictionary(be_snapshot_list_t
*ss
,
70 static boolean_t
convertPyArgsToNvlist(nvlist_t
**nvList
, int numArgs
, ...);
78 * Function: beCreateSnapshot
79 * Description: Convert Python args to nvlist pairs and
80 * call libbe:be_create_snapshot to create a
81 * snapshot of all the datasets within a BE
83 * args - pointer to a python object containing:
84 * beName - The name of the BE to create a snapshot of
85 * snapName - The name of the snapshot to create (optional)
87 * The following public attribute values. defined by libbe.h,
88 * are used by this function:
90 * Returns a pointer to a python object and an optional snapshot name:
91 * 0, [snapName] - Success
92 * 1, [snapName] - Failure
98 beCreateSnapshot(PyObject
*self
, PyObject
*args
)
101 char *snapName
= NULL
;
102 int ret
= BE_PY_SUCCESS
;
103 nvlist_t
*beAttrs
= NULL
;
104 PyObject
*retVals
= NULL
;
106 if (!PyArg_ParseTuple(args
, "z|z", &beName
, &snapName
)) {
107 return (Py_BuildValue("[is]", BE_PY_ERR_PARSETUPLE
, NULL
));
110 if (!convertPyArgsToNvlist(&beAttrs
, 4,
111 BE_ATTR_ORIG_BE_NAME
, beName
,
112 BE_ATTR_SNAP_NAME
, snapName
)) {
113 nvlist_free(beAttrs
);
114 return (Py_BuildValue("[is]", BE_PY_ERR_NVLIST
, NULL
));
117 if (beAttrs
== NULL
) {
118 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
121 if ((ret
= be_create_snapshot(beAttrs
)) != 0) {
122 nvlist_free(beAttrs
);
123 return (Py_BuildValue("[is]", ret
, NULL
));
125 if (snapName
== NULL
) {
126 if (nvlist_lookup_pairs(beAttrs
, NV_FLAG_NOENTOK
,
127 BE_ATTR_SNAP_NAME
, DATA_TYPE_STRING
, &snapName
,
129 nvlist_free(beAttrs
);
130 return (Py_BuildValue("[is]",
131 BE_PY_ERR_NVLIST
, NULL
));
133 retVals
= Py_BuildValue("[is]", ret
, snapName
);
134 nvlist_free(beAttrs
);
137 nvlist_free(beAttrs
);
139 return (Py_BuildValue("[is]", ret
, NULL
));
144 * Description: Convert Python args to nvlist pairs and call libbe:be_copy
145 * to create a Boot Environment
147 * args - pointer to a python object containing:
148 * trgtBeName - The name of the BE to create
149 * srcBeName - The name of the BE used to create trgtBeName (optional)
150 * rpool - The pool to create the new BE in (optional)
151 * srcSnapName - The snapshot name (optional)
152 * beNameProperties - The properties to use when creating
155 * Returns a pointer to a python object. That Python object will consist of
156 * the return code and optional attributes, trgtBeName and snapshotName
157 * BE_SUCCESS, [trgtBeName], [trgtSnapName] - Success
158 * 1, [trgtBeName], [trgtSnapName] - Failure
164 beCopy(PyObject
*self
, PyObject
*args
)
166 char *trgtBeName
= NULL
;
167 char *srcBeName
= NULL
;
168 char *srcSnapName
= NULL
;
169 char *trgtSnapName
= NULL
;
171 char *beDescription
= NULL
;
173 int ret
= BE_PY_SUCCESS
;
174 nvlist_t
*beAttrs
= NULL
;
175 nvlist_t
*beProps
= NULL
;
176 PyObject
*beNameProperties
= NULL
;
177 PyObject
*pkey
= NULL
;
178 PyObject
*pvalue
= NULL
;
179 PyObject
*retVals
= NULL
;
181 if (!PyArg_ParseTuple(args
, "|zzzzOz", &trgtBeName
, &srcBeName
,
182 &srcSnapName
, &rpool
, &beNameProperties
, &beDescription
)) {
183 return (Py_BuildValue("[iss]", BE_PY_ERR_PARSETUPLE
,
187 if (!convertPyArgsToNvlist(&beAttrs
, 10,
188 BE_ATTR_NEW_BE_NAME
, trgtBeName
,
189 BE_ATTR_ORIG_BE_NAME
, srcBeName
,
190 BE_ATTR_SNAP_NAME
, srcSnapName
,
191 BE_ATTR_NEW_BE_POOL
, rpool
,
192 BE_ATTR_NEW_BE_DESC
, beDescription
)) {
193 nvlist_free(beAttrs
);
194 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST
, NULL
, NULL
));
197 if (beNameProperties
!= NULL
) {
198 if (nvlist_alloc(&beProps
, NV_UNIQUE_NAME
, 0) != 0) {
199 (void) printf("nvlist_alloc failed.\n");
200 nvlist_free(beAttrs
);
201 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST
,
204 while (PyDict_Next(beNameProperties
, &pos
, &pkey
, &pvalue
)) {
205 if (!convertPyArgsToNvlist(&beProps
, 2,
206 PyString_AsString(pkey
),
207 PyString_AsString(pvalue
))) {
208 nvlist_free(beProps
);
209 nvlist_free(beAttrs
);
210 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST
,
216 if (beProps
!= NULL
&& beAttrs
!= NULL
&&
217 nvlist_add_nvlist(beAttrs
, BE_ATTR_ZFS_PROPERTIES
,
219 nvlist_free(beProps
);
220 nvlist_free(beAttrs
);
221 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST
,
225 nvlist_free(beProps
);
227 if (trgtBeName
== NULL
) {
229 * Caller wants to get back the BE_ATTR_NEW_BE_NAME and
232 if ((ret
= be_copy(beAttrs
)) != BE_SUCCESS
) {
233 nvlist_free(beAttrs
);
234 return (Py_BuildValue("[iss]", ret
, NULL
, NULL
));
238 * When no trgtBeName is passed to be_copy, be_copy
239 * returns an auto generated beName and snapshot name.
241 if (nvlist_lookup_string(beAttrs
, BE_ATTR_NEW_BE_NAME
,
243 nvlist_free(beAttrs
);
244 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST
,
247 if (nvlist_lookup_string(beAttrs
, BE_ATTR_SNAP_NAME
,
248 &trgtSnapName
) != 0) {
249 nvlist_free(beAttrs
);
250 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST
,
254 retVals
= Py_BuildValue("[iss]", BE_PY_SUCCESS
,
255 trgtBeName
, trgtSnapName
);
256 nvlist_free(beAttrs
);
260 ret
= be_copy(beAttrs
);
261 nvlist_free(beAttrs
);
262 return (Py_BuildValue("[iss]", ret
, NULL
, NULL
));
268 * Description: Convert Python args to nvlist pairs and call libbe:be_list
269 * to gather information about Boot Environments
271 * args - pointer to a python object containing:
272 * beName - The name of the BE to list (optional)
274 * Returns a pointer to a python object. That Python object will consist of
275 * the return code and a list of Dicts or NULL.
276 * BE_PY_SUCCESS, listOfDicts - Success
277 * bePyErr or be_errno_t, NULL - Failure
283 beList(PyObject
*self
, PyObject
*args
)
286 int ret
= BE_PY_SUCCESS
;
287 be_node_list_t
*list
= NULL
;
288 be_node_list_t
*be
= NULL
;
289 PyObject
*dict
= NULL
;
290 PyObject
*listOfDicts
= NULL
;
292 if ((listOfDicts
= PyList_New(0)) == NULL
) {
293 ret
= BE_PY_ERR_DICT
;
294 listOfDicts
= Py_None
;
298 if (!PyArg_ParseTuple(args
, "|z", &beName
)) {
299 ret
= BE_PY_ERR_PARSETUPLE
;
303 if ((ret
= be_list(beName
, &list
)) != BE_SUCCESS
) {
307 for (be
= list
; be
!= NULL
; be
= be
->be_next_node
) {
308 be_dataset_list_t
*ds
= be
->be_node_datasets
;
309 be_snapshot_list_t
*ss
= be
->be_node_snapshots
;
311 if ((dict
= PyDict_New()) == NULL
) {
312 ret
= BE_PY_ERR_DICT
;
316 if (!convertBEInfoToDictionary(be
, &dict
)) {
319 ret
= BE_PY_ERR_VAR_CONV
;
323 if (PyList_Append(listOfDicts
, dict
) != 0) {
326 ret
= BE_PY_ERR_APPEND
;
334 if ((dict
= PyDict_New()) == NULL
) {
335 ret
= BE_PY_ERR_DICT
;
339 if (!convertDatasetInfoToDictionary(ds
, &dict
)) {
342 ret
= BE_PY_ERR_VAR_CONV
;
346 if (PyList_Append(listOfDicts
, dict
) != 0) {
349 ret
= BE_PY_ERR_APPEND
;
353 ds
= ds
->be_next_dataset
;
361 if ((dict
= PyDict_New()) == NULL
) {
364 ret
= BE_PY_ERR_DICT
;
368 if (!convertSnapshotInfoToDictionary(ss
, &dict
)) {
371 ret
= BE_PY_ERR_VAR_CONV
;
375 if (PyList_Append(listOfDicts
, dict
) != 0) {
378 ret
= BE_PY_ERR_APPEND
;
382 ss
= ss
->be_next_snapshot
;
392 return (Py_BuildValue("[iO]", ret
, listOfDicts
));
396 * Function: beActivate
397 * Description: Convert Python args to nvlist pairs and call libbe:be_activate
398 * to activate a Boot Environment
400 * args - pointer to a python object containing:
401 * beName - The name of the BE to activate
403 * Returns a pointer to a python object:
404 * BE_SUCCESS - Success
405 * bePyErr or be_errno_t - Failure
411 beActivate(PyObject
*self
, PyObject
*args
)
414 int ret
= BE_PY_SUCCESS
;
415 nvlist_t
*beAttrs
= NULL
;
417 if (!PyArg_ParseTuple(args
, "z", &beName
)) {
418 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
421 if (!convertPyArgsToNvlist(&beAttrs
, 2, BE_ATTR_ORIG_BE_NAME
, beName
)) {
422 nvlist_free(beAttrs
);
423 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
426 if (beAttrs
== NULL
) {
427 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
430 ret
= be_activate(beAttrs
);
431 nvlist_free(beAttrs
);
432 return (Py_BuildValue("i", ret
));
436 * Function: beDestroy
437 * Description: Convert Python args to nvlist pairs and call libbe:be_destroy
438 * to destroy a Boot Environment
440 * args - pointer to a python object containing:
441 * beName - The name of the BE to destroy
443 * Returns a pointer to a python object:
444 * BE_SUCCESS - Success
445 * bePyErr or be_errno_t - Failure
451 beDestroy(PyObject
*self
, PyObject
*args
)
454 int destroy_snaps
= 0;
455 int force_unmount
= 0;
456 int destroy_flags
= 0;
457 int ret
= BE_PY_SUCCESS
;
458 nvlist_t
*beAttrs
= NULL
;
460 if (!PyArg_ParseTuple(args
, "z|ii", &beName
, &destroy_snaps
,
462 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
465 if (destroy_snaps
== 1)
466 destroy_flags
|= BE_DESTROY_FLAG_SNAPSHOTS
;
468 if (force_unmount
== 1)
469 destroy_flags
|= BE_DESTROY_FLAG_FORCE_UNMOUNT
;
471 if (!convertPyArgsToNvlist(&beAttrs
, 2, BE_ATTR_ORIG_BE_NAME
, beName
)) {
472 nvlist_free(beAttrs
);
473 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
476 if (nvlist_add_uint16(beAttrs
, BE_ATTR_DESTROY_FLAGS
, destroy_flags
)
478 (void) printf("nvlist_add_uint16 failed for "
479 "BE_ATTR_DESTROY_FLAGS (%d).\n", destroy_flags
);
480 nvlist_free(beAttrs
);
481 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
484 if (beAttrs
== NULL
) {
485 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
488 ret
= be_destroy(beAttrs
);
489 nvlist_free(beAttrs
);
490 return (Py_BuildValue("i", ret
));
494 * Function: beDestroySnapshot
495 * Description: Convert Python args to nvlist pairs and call libbe:be_destroy
496 * to destroy a snapshot of a Boot Environment
498 * args - pointer to a python object containing:
499 * beName - The name of the BE to destroy
500 * snapName - The name of the snapshot to destroy
502 * Returns a pointer to a python object:
503 * BE_SUCCESS - Success
504 * bePyErr or be_errno_t - Failure
510 beDestroySnapshot(PyObject
*self
, PyObject
*args
)
513 char *snapName
= NULL
;
514 int ret
= BE_PY_SUCCESS
;
515 nvlist_t
*beAttrs
= NULL
;
517 if (!PyArg_ParseTuple(args
, "zz", &beName
, &snapName
)) {
518 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
521 if (!convertPyArgsToNvlist(&beAttrs
, 4,
522 BE_ATTR_ORIG_BE_NAME
, beName
,
523 BE_ATTR_SNAP_NAME
, snapName
)) {
524 nvlist_free(beAttrs
);
525 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
528 if (beAttrs
== NULL
) {
529 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
532 ret
= be_destroy_snapshot(beAttrs
);
533 nvlist_free(beAttrs
);
534 return (Py_BuildValue("i", ret
));
539 * Description: Convert Python args to nvlist pairs and call libbe:be_rename
540 * to rename a Boot Environment
542 * args - pointer to a python object containing:
543 * oldBeName - The name of the old Boot Environment
544 * newBeName - The name of the new Boot Environment
546 * Returns a pointer to a python object:
547 * BE_SUCCESS - Success
548 * bePyErr or be_errno_t - Failure
554 beRename(PyObject
*self
, PyObject
*args
)
556 char *oldBeName
= NULL
;
557 char *newBeName
= NULL
;
558 int ret
= BE_PY_SUCCESS
;
559 nvlist_t
*beAttrs
= NULL
;
561 if (!PyArg_ParseTuple(args
, "zz", &oldBeName
, &newBeName
)) {
562 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
565 if (!convertPyArgsToNvlist(&beAttrs
, 4,
566 BE_ATTR_ORIG_BE_NAME
, oldBeName
,
567 BE_ATTR_NEW_BE_NAME
, newBeName
)) {
568 nvlist_free(beAttrs
);
569 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
572 if (beAttrs
== NULL
) {
573 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
576 ret
= be_rename(beAttrs
);
577 nvlist_free(beAttrs
);
578 return (Py_BuildValue("i", ret
));
583 * Description: Convert Python args to nvlist pairs and call libbe:be_mount
584 * to mount a Boot Environment
586 * args - pointer to a python object containing:
587 * beName - The name of the Boot Environment to mount
588 * mountpoint - The path of the mountpoint to mount the
589 * Boot Environment on (optional)
591 * Returns a pointer to a python object:
592 * BE_SUCCESS - Success
593 * bePyErr or be_errno_t - Failure
599 beMount(PyObject
*self
, PyObject
*args
)
602 char *mountpoint
= NULL
;
603 int ret
= BE_PY_SUCCESS
;
604 nvlist_t
*beAttrs
= NULL
;
606 if (!PyArg_ParseTuple(args
, "zz", &beName
, &mountpoint
)) {
607 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
610 if (!convertPyArgsToNvlist(&beAttrs
, 4,
611 BE_ATTR_ORIG_BE_NAME
, beName
,
612 BE_ATTR_MOUNTPOINT
, mountpoint
)) {
613 nvlist_free(beAttrs
);
614 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
617 if (beAttrs
== NULL
) {
618 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
621 ret
= be_mount(beAttrs
);
622 nvlist_free(beAttrs
);
623 return (Py_BuildValue("i", ret
));
627 * Function: beUnmount
628 * Description: Convert Python args to nvlist pairs and call libbe:be_unmount
629 * to unmount a Boot Environment
631 * args - pointer to a python object containing:
632 * beName - The name of the Boot Environment to unmount
634 * Returns a pointer to a python object:
635 * BE_SUCCESS - Success
636 * bePyErr or be_errno_t - Failure
642 beUnmount(PyObject
*self
, PyObject
*args
)
645 int force_unmount
= 0;
646 int unmount_flags
= 0;
647 int ret
= BE_PY_SUCCESS
;
648 nvlist_t
*beAttrs
= NULL
;
650 if (!PyArg_ParseTuple(args
, "z|i", &beName
, &force_unmount
)) {
651 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
654 if (force_unmount
== 1)
655 unmount_flags
|= BE_UNMOUNT_FLAG_FORCE
;
657 if (!convertPyArgsToNvlist(&beAttrs
, 2,
658 BE_ATTR_ORIG_BE_NAME
, beName
)) {
659 nvlist_free(beAttrs
);
660 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
663 if (nvlist_add_uint16(beAttrs
, BE_ATTR_UNMOUNT_FLAGS
, unmount_flags
)
665 (void) printf("nvlist_add_uint16 failed for "
666 "BE_ATTR_UNMOUNT_FLAGS (%d).\n", unmount_flags
);
667 nvlist_free(beAttrs
);
668 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
671 if (beAttrs
== NULL
) {
672 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
675 ret
= be_unmount(beAttrs
);
676 nvlist_free(beAttrs
);
677 return (Py_BuildValue("i", ret
));
681 * Function: beRollback
682 * Description: Convert Python args to nvlist pairs and call libbe:be_rollback
683 * to rollback a Boot Environment to a previously taken
686 * args - pointer to a python object containing:
687 * beName - The name of the Boot Environment to unmount
689 * Returns a pointer to a python object:
690 * BE_SUCCESS - Success
691 * bePyErr or be_errno_t - Failure
697 beRollback(PyObject
*self
, PyObject
*args
)
700 char *snapName
= NULL
;
701 int ret
= BE_PY_SUCCESS
;
702 nvlist_t
*beAttrs
= NULL
;
704 if (!PyArg_ParseTuple(args
, "zz", &beName
, &snapName
)) {
705 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE
));
708 if (!convertPyArgsToNvlist(&beAttrs
, 4,
709 BE_ATTR_ORIG_BE_NAME
, beName
,
710 BE_ATTR_SNAP_NAME
, snapName
)) {
711 nvlist_free(beAttrs
);
712 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
715 if (beAttrs
== NULL
) {
716 return (Py_BuildValue("i", BE_PY_ERR_NVLIST
));
719 ret
= be_rollback(beAttrs
);
720 nvlist_free(beAttrs
);
721 return (Py_BuildValue("i", ret
));
725 * Function: bePrintErrors
726 * Description: Convert Python args to boolean and call libbe_print_errors to
727 * turn on/off error output for the library.
729 * args - pointer to a python object containing:
730 * print_errors - Boolean that turns library error
731 * printing on or off.
733 * args - pointer to a python object containing:
734 * 0 - do not print errors - Python boolean "False"
735 * 1 - print errors - Python boolean "True"
737 * Returns 1 on missing or invalid argument, 0 otherwise
743 bePrintErrors(PyObject
*self
, PyObject
*args
)
747 if (!PyArg_ParseTuple(args
, "i", &print_errors
) ||
748 (print_errors
!= 1 && print_errors
!= 0))
749 return (Py_BuildValue("i", BE_PY_ERR_PRINT_ERR
));
750 libbe_print_errors(print_errors
== 1);
751 return (Py_BuildValue("i", BE_PY_SUCCESS
));
755 * Function: beGetErrDesc
756 * Description: Convert Python args to an int and call be_err_to_str to
757 * map an error code to an error string.
759 * args - pointer to a python object containing:
760 * errCode - value to map to an error string.
762 * Returns: error string or NULL
768 beGetErrDesc(PyObject
*self
, PyObject
*args
)
771 char *beErrStr
= NULL
;
773 if (!PyArg_ParseTuple(args
, "i", &errCode
)) {
774 return (Py_BuildValue("s", NULL
));
778 * First check libbe_py errors. If NULL is returned check error codes
782 if ((beErrStr
= beMapLibbePyErrorToString(errCode
)) == NULL
) {
783 beErrStr
= be_err_to_str(errCode
);
786 return (Py_BuildValue("s", beErrStr
));
790 * Function: beVerifyBEName
791 * Description: Call be_valid_be_name() to verify the BE name.
793 * args - pointer to a python object containing:
794 * string - value to map to a string.
796 * Returns: 0 for success or 1 for failure
802 beVerifyBEName(PyObject
*self
, PyObject
*args
)
806 if (!PyArg_ParseTuple(args
, "s", &string
)) {
807 return (Py_BuildValue("i", 1));
810 if (be_valid_be_name(string
)) {
811 return (Py_BuildValue("i", 0));
813 return (Py_BuildValue("i", 1));
817 /* ~~~~~~~~~~~~~~~~~ */
818 /* Private Functions */
819 /* ~~~~~~~~~~~~~~~~~ */
822 convertBEInfoToDictionary(be_node_list_t
*be
, PyObject
**listDict
)
824 if (be
->be_node_name
!= NULL
) {
825 if (PyDict_SetItemString(*listDict
, BE_ATTR_ORIG_BE_NAME
,
826 PyString_FromString(be
->be_node_name
)) != 0) {
831 if (be
->be_rpool
!= NULL
) {
832 if (PyDict_SetItemString(*listDict
, BE_ATTR_ORIG_BE_POOL
,
833 PyString_FromString(be
->be_rpool
)) != 0) {
838 if (be
->be_mntpt
!= NULL
) {
839 if (PyDict_SetItemString(*listDict
, BE_ATTR_MOUNTPOINT
,
840 PyString_FromString(be
->be_mntpt
)) != 0) {
845 if (PyDict_SetItemString(*listDict
, BE_ATTR_MOUNTED
,
846 (be
->be_mounted
? Py_True
: Py_False
)) != 0) {
850 if (PyDict_SetItemString(*listDict
, BE_ATTR_ACTIVE
,
851 (be
->be_active
? Py_True
: Py_False
)) != 0) {
855 if (PyDict_SetItemString(*listDict
, BE_ATTR_ACTIVE_ON_BOOT
,
856 (be
->be_active_on_boot
? Py_True
: Py_False
)) != 0) {
860 if (PyDict_SetItemString(*listDict
, BE_ATTR_GLOBAL_ACTIVE
,
861 (be
->be_global_active
? Py_True
: Py_False
)) != 0) {
865 if (be
->be_space_used
!= 0) {
866 if (PyDict_SetItemString(*listDict
, BE_ATTR_SPACE
,
867 PyLong_FromUnsignedLongLong(be
->be_space_used
)) != 0) {
872 if (be
->be_root_ds
!= NULL
) {
873 if (PyDict_SetItemString(*listDict
, BE_ATTR_ROOT_DS
,
874 PyString_FromString(be
->be_root_ds
)) != 0) {
879 if (be
->be_node_creation
!= NULL
) {
880 if (PyDict_SetItemString(*listDict
, BE_ATTR_DATE
,
881 PyLong_FromLong(be
->be_node_creation
)) != 0) {
886 if (be
->be_policy_type
!= NULL
) {
887 if (PyDict_SetItemString(*listDict
, BE_ATTR_POLICY
,
888 PyString_FromString(be
->be_policy_type
)) != 0) {
893 if (be
->be_uuid_str
!= NULL
) {
894 if (PyDict_SetItemString(*listDict
, BE_ATTR_UUID_STR
,
895 PyString_FromString(be
->be_uuid_str
)) != 0) {
904 convertDatasetInfoToDictionary(be_dataset_list_t
*ds
, PyObject
**listDict
)
906 if (ds
->be_dataset_name
!= NULL
) {
907 if (PyDict_SetItemString(*listDict
, BE_ATTR_DATASET
,
908 PyString_FromString(ds
->be_dataset_name
)) != 0) {
913 if (PyDict_SetItemString(*listDict
, BE_ATTR_STATUS
,
914 (ds
->be_ds_mounted
? Py_True
: Py_False
)) != 0) {
918 if (ds
->be_ds_mntpt
!= NULL
) {
919 if (PyDict_SetItemString(*listDict
, BE_ATTR_MOUNTPOINT
,
920 PyString_FromString(ds
->be_ds_mntpt
)) != 0) {
925 if (PyDict_SetItemString(*listDict
, BE_ATTR_MOUNTED
,
926 (ds
->be_ds_mounted
? Py_True
: Py_False
)) != 0) {
930 if (ds
->be_ds_space_used
!= 0) {
931 if (PyDict_SetItemString(*listDict
, BE_ATTR_SPACE
,
932 PyLong_FromUnsignedLongLong(ds
->be_ds_space_used
))
938 if (ds
->be_dataset_name
!= 0) {
939 if (PyDict_SetItemString(*listDict
, BE_ATTR_DATASET
,
940 PyString_FromString(ds
->be_dataset_name
)) != 0) {
945 if (ds
->be_ds_plcy_type
!= NULL
) {
946 if (PyDict_SetItemString(*listDict
, BE_ATTR_POLICY
,
947 PyString_FromString(ds
->be_ds_plcy_type
)) != 0) {
952 if (ds
->be_ds_creation
!= NULL
) {
953 if (PyDict_SetItemString(*listDict
, BE_ATTR_DATE
,
954 PyLong_FromLong(ds
->be_ds_creation
)) != 0) {
963 convertSnapshotInfoToDictionary(be_snapshot_list_t
*ss
, PyObject
**listDict
)
965 if (ss
->be_snapshot_name
!= NULL
) {
966 if (PyDict_SetItemString(*listDict
, BE_ATTR_SNAP_NAME
,
967 PyString_FromString(ss
->be_snapshot_name
)) != 0) {
972 if (ss
->be_snapshot_creation
!= NULL
) {
973 if (PyDict_SetItemString(*listDict
, BE_ATTR_DATE
,
974 PyLong_FromLong(ss
->be_snapshot_creation
)) != 0) {
979 if (ss
->be_snapshot_type
!= NULL
) {
980 if (PyDict_SetItemString(*listDict
, BE_ATTR_POLICY
,
981 PyString_FromString(ss
->be_snapshot_type
)) != 0) {
986 if (ss
->be_snapshot_space_used
!= 0) {
987 if (PyDict_SetItemString(*listDict
, BE_ATTR_SPACE
,
988 PyLong_FromUnsignedLongLong(ss
->be_snapshot_space_used
))
998 * Convert string arguments to nvlist attributes
1002 convertPyArgsToNvlist(nvlist_t
**nvList
, int numArgs
, ...)
1008 if (*nvList
== NULL
) {
1009 if (nvlist_alloc(nvList
, NV_UNIQUE_NAME
, 0) != 0) {
1010 (void) printf("nvlist_alloc failed.\n");
1015 va_start(ap
, numArgs
);
1017 for (i
= 0; i
< numArgs
; i
+= 2) {
1018 if ((pt
= va_arg(ap
, char *)) == NULL
||
1019 (pt2
= va_arg(ap
, char *)) == NULL
) {
1022 if (nvlist_add_string(*nvList
, pt
, pt2
) != 0) {
1023 (void) printf("nvlist_add_string failed for %s (%s).\n",
1025 nvlist_free(*nvList
);
1036 * Function: beMapLibbePyErrorToString
1037 * Description: Convert Python args to an int and map an error code to an
1040 * errCode - value to map to an error string.
1042 * Returns error string or NULL
1048 beMapLibbePyErrorToString(int errCode
)
1051 case BE_PY_ERR_APPEND
:
1052 return ("Unable to append a dictionary to a list "
1054 case BE_PY_ERR_DICT
:
1055 return ("Creation of a Python dictionary failed.");
1056 case BE_PY_ERR_LIST
:
1057 return ("beList() failed.");
1058 case BE_PY_ERR_NVLIST
:
1059 return ("An nvlist operation failed.");
1060 case BE_PY_ERR_PARSETUPLE
:
1061 return ("PyArg_ParseTuple() failed to convert variable to C.");
1062 case BE_PY_ERR_PRINT_ERR
:
1063 return ("bePrintErrors() failed.");
1064 case BE_PY_ERR_VAR_CONV
:
1065 return ("Unable to add variables to a Python dictionary.");
1071 /* Private python initialization structure */
1073 static struct PyMethodDef libbeMethods
[] = {
1074 {"beCopy", (PyCFunction
)beCopy
, METH_VARARGS
, "Create/Copy a BE."},
1075 {"beCreateSnapshot", (PyCFunction
)beCreateSnapshot
, METH_VARARGS
,
1076 "Create a snapshot."},
1077 {"beDestroy", (PyCFunction
)beDestroy
, METH_VARARGS
, "Destroy a BE."},
1078 {"beDestroySnapshot", (PyCFunction
)beDestroySnapshot
, METH_VARARGS
,
1079 "Destroy a snapshot."},
1080 {"beMount", (PyCFunction
)beMount
, METH_VARARGS
, "Mount a BE."},
1081 {"beUnmount", (PyCFunction
)beUnmount
, METH_VARARGS
, "Unmount a BE."},
1082 {"beList", (PyCFunction
)beList
, METH_VARARGS
, "List BE info."},
1083 {"beRename", (PyCFunction
)beRename
, METH_VARARGS
, "Rename a BE."},
1084 {"beActivate", (PyCFunction
)beActivate
, METH_VARARGS
, "Activate a BE."},
1085 {"beRollback", (PyCFunction
)beRollback
, METH_VARARGS
, "Rollback a BE."},
1086 {"bePrintErrors", (PyCFunction
)bePrintErrors
, METH_VARARGS
,
1087 "Enable/disable error printing."},
1088 {"beGetErrDesc", (PyCFunction
)beGetErrDesc
, METH_VARARGS
,
1089 "Map Error codes to strings."},
1090 {"beVerifyBEName", (PyCFunction
)beVerifyBEName
, METH_VARARGS
,
1092 {NULL
, NULL
, 0, NULL
}
1098 /* PyMODINIT_FUNC; */
1099 (void) Py_InitModule("libbe_py", libbeMethods
);