summaryrefslogtreecommitdiff
path: root/usr/src/lib/pylibbe/common/libbe_py.c
blob: b4f6ca0e524091cdc0420b4cbbf75ef51f37ebd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <Python.h>
#include <sys/varargs.h>
#include <stdio.h>
#include <libnvpair.h>

#include <libbe.h>
#include <libbe_priv.h>

enum {
	BE_PY_SUCCESS = 0,
	BE_PY_ERR_APPEND = 6000,
	BE_PY_ERR_DICT,
	BE_PY_ERR_LIST,
	BE_PY_ERR_NVLIST,
	BE_PY_ERR_PARSETUPLE,
	BE_PY_ERR_PRINT_ERR,
	BE_PY_ERR_VAR_CONV,
} bePyErr;

/*
 * public libbe functions
 */

PyObject *beCreateSnapshot(PyObject *, PyObject *);
PyObject *beCopy(PyObject *, PyObject *);
PyObject *beList(PyObject *, PyObject *);
PyObject *beActivate(PyObject *, PyObject *);
PyObject *beDestroy(PyObject *, PyObject *);
PyObject *beDestroySnapshot(PyObject *, PyObject *);
PyObject *beRename(PyObject *, PyObject *);
PyObject *beMount(PyObject *, PyObject *);
PyObject *beUnmount(PyObject *, PyObject *);
PyObject *bePrintErrors(PyObject *, PyObject *);
PyObject *beGetErrDesc(PyObject *, PyObject *);
char *beMapLibbePyErrorToString(int);
void initlibbe_py();

static boolean_t convertBEInfoToDictionary(be_node_list_t *be,
    PyObject **listDict);
static boolean_t convertDatasetInfoToDictionary(be_dataset_list_t *ds,
    PyObject **listDict);
static boolean_t convertSnapshotInfoToDictionary(be_snapshot_list_t *ss,
    PyObject **listDict);
static boolean_t convertPyArgsToNvlist(nvlist_t **nvList, int numArgs, ...);


/* ~~~~~~~~~~~~~~~ */
/* Public Funtions */
/* ~~~~~~~~~~~~~~~ */

/*
 * Function:    beCreateSnapshot
 * Description: Convert Python args to nvlist pairs and
 *              call libbe:be_create_snapshot to create a
 *              snapshot of all the datasets within a BE
 * Parameters:
 *   args -          pointer to a python object containing:
 *        beName -   The name of the BE to create a snapshot of
 *        snapName - The name of the snapshot to create (optional)
 *
 *        The following public attribute values. defined by libbe.h,
 *        are used by this function:
 *
 * Returns a pointer to a python object and an optional snapshot name:
 *      0, [snapName] - Success
 *      1, [snapName] - Failure
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
beCreateSnapshot(PyObject *self, PyObject *args)
{
	char	*beName = NULL;
	char	*snapName = NULL;
	int	ret = BE_PY_SUCCESS;
	nvlist_t	*beAttrs = NULL;
	PyObject	*retVals = NULL;

	if (!PyArg_ParseTuple(args, "z|z", &beName, &snapName)) {
		return (Py_BuildValue("[is]", BE_PY_ERR_PARSETUPLE, NULL));
	}

	if (!convertPyArgsToNvlist(&beAttrs, 4,
	    BE_ATTR_ORIG_BE_NAME, beName,
	    BE_ATTR_SNAP_NAME, snapName)) {
		nvlist_free(beAttrs);
		return (Py_BuildValue("[is]", BE_PY_ERR_NVLIST, NULL));
	}

	if (beAttrs == NULL) {
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	if ((ret = be_create_snapshot(beAttrs)) != 0) {
		nvlist_free(beAttrs);
		return (Py_BuildValue("[is]", ret, NULL));
	}
	if (snapName == NULL) {
		if (nvlist_lookup_pairs(beAttrs, NV_FLAG_NOENTOK,
		    BE_ATTR_SNAP_NAME, DATA_TYPE_STRING, &snapName,
		    NULL) != 0) {
			nvlist_free(beAttrs);
			return (Py_BuildValue("[is]",
			    BE_PY_ERR_NVLIST, NULL));
		}
		retVals = Py_BuildValue("[is]", ret, snapName);
		nvlist_free(beAttrs);
		return (retVals);
	}
	nvlist_free(beAttrs);

	return (Py_BuildValue("[is]", ret, NULL));
}

/*
 * Function:    beCopy
 * Description: Convert Python args to nvlist pairs and call libbe:be_copy
 *              to create a Boot Environment
 * Parameters:
 *   args -     pointer to a python object containing:
 *     trgtBeName - The name of the BE to create
 *     srcBeName - The name of the BE used to create trgtBeName (optional)
 *     rpool - The pool to create the new BE in (optional)
 *     srcSnapName - The snapshot name (optional)
 *     beNameProperties - The properties to use when creating
 *                        the BE (optional)
 *
 * Returns a pointer to a python object. That Python object will consist of
 * the return code and optional attributes, trgtBeName and snapshotName
 *      BE_SUCCESS, [trgtBeName], [trgtSnapName] - Success
 *      1, [trgtBeName], [trgtSnapName] - Failure
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
beCopy(PyObject *self, PyObject *args)
{
	char	*trgtBeName = NULL;
	char	*srcBeName = NULL;
	char	*srcSnapName = NULL;
	char	*trgtSnapName = NULL;
	char	*rpool = NULL;
	char	*beDescription = NULL;
	int		pos = 0;
	int		ret = BE_PY_SUCCESS;
	nvlist_t	*beAttrs = NULL;
	nvlist_t	*beProps = NULL;
	PyObject	*beNameProperties = NULL;
	PyObject	*pkey = NULL;
	PyObject	*pvalue = NULL;
	PyObject	*retVals = NULL;

	if (!PyArg_ParseTuple(args, "|zzzzOz", &trgtBeName, &srcBeName,
	    &srcSnapName, &rpool, &beNameProperties, &beDescription)) {
		return (Py_BuildValue("[iss]", BE_PY_ERR_PARSETUPLE,
		    NULL, NULL));
	}

	if (!convertPyArgsToNvlist(&beAttrs, 10,
	    BE_ATTR_NEW_BE_NAME, trgtBeName,
	    BE_ATTR_ORIG_BE_NAME, srcBeName,
	    BE_ATTR_SNAP_NAME, srcSnapName,
	    BE_ATTR_NEW_BE_POOL, rpool,
	    BE_ATTR_NEW_BE_DESC, beDescription)) {
		nvlist_free(beAttrs);
		return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST, NULL, NULL));
	}

	if (beNameProperties != NULL) {
		if (nvlist_alloc(&beProps, NV_UNIQUE_NAME, 0) != 0) {
			(void) printf("nvlist_alloc failed.\n");
			nvlist_free(beAttrs);
			return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
			    NULL, NULL));
		}
		while (PyDict_Next(beNameProperties, &pos, &pkey, &pvalue)) {
			if (!convertPyArgsToNvlist(&beProps, 2,
			    PyString_AsString(pkey),
			    PyString_AsString(pvalue))) {
				nvlist_free(beProps);
				nvlist_free(beAttrs);
				return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
				    NULL, NULL));
			}
		}
	}

	if (beProps != NULL && beAttrs != NULL &&
	    nvlist_add_nvlist(beAttrs, BE_ATTR_ZFS_PROPERTIES,
	    beProps) != 0) {
		nvlist_free(beProps);
		nvlist_free(beAttrs);
		return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
		    NULL, NULL));
	}

	if (beProps != NULL) nvlist_free(beProps);

	if (trgtBeName == NULL) {
		/*
		 * Caller wants to get back the BE_ATTR_NEW_BE_NAME and
		 * BE_ATTR_SNAP_NAME
		 */
		if ((ret = be_copy(beAttrs)) != BE_SUCCESS) {
			nvlist_free(beAttrs);
			return (Py_BuildValue("[iss]", ret, NULL, NULL));
		}

		/*
		 * When no trgtBeName is passed to be_copy, be_copy
		 * returns an auto generated beName and snapshot name.
		 */
		if (nvlist_lookup_string(beAttrs, BE_ATTR_NEW_BE_NAME,
		    &trgtBeName) != 0) {
			nvlist_free(beAttrs);
			return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
			    NULL, NULL));
		}
		if (nvlist_lookup_string(beAttrs, BE_ATTR_SNAP_NAME,
		    &trgtSnapName) != 0) {
			nvlist_free(beAttrs);
			return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
			    NULL, NULL));
		}

		retVals = Py_BuildValue("[iss]", BE_PY_SUCCESS,
		    trgtBeName, trgtSnapName);
		nvlist_free(beAttrs);
		return (retVals);

	} else {
		ret = be_copy(beAttrs);
		nvlist_free(beAttrs);
		return (Py_BuildValue("[iss]", ret, NULL, NULL));
	}
}

/*
 * Function:    beList
 * Description: Convert Python args to nvlist pairs and call libbe:be_list
 *              to gather information about Boot Environments
 * Parameters:
 *   args -     pointer to a python object containing:
 *     beName - The name of the BE to list (optional)
 *
 * Returns a pointer to a python object. That Python object will consist of
 * the return code and a list of Dicts or NULL.
 *      BE_PY_SUCCESS, listOfDicts - Success
 *      bePyErr or be_errno_t, NULL - Failure
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
beList(PyObject *self, PyObject *args)
{
	char	*beName = NULL;
	int	ret = BE_PY_SUCCESS;
	be_node_list_t *list = NULL;
	be_node_list_t *be = NULL;
	PyObject *dict = NULL;
	PyObject *listOfDicts = NULL;

	if ((listOfDicts = PyList_New(0)) == NULL) {
		ret = BE_PY_ERR_DICT;
		listOfDicts = Py_None;
		goto done;
	}

	if (!PyArg_ParseTuple(args, "|z", &beName)) {
		ret = BE_PY_ERR_PARSETUPLE;
		goto done;
	}

	if ((ret = be_list(beName, &list)) != BE_SUCCESS) {
		goto done;
	}

	for (be = list; be != NULL; be = be->be_next_node) {
		be_dataset_list_t *ds = be->be_node_datasets;
		be_snapshot_list_t *ss = be->be_node_snapshots;

		if ((dict = PyDict_New()) == NULL) {
			ret = BE_PY_ERR_DICT;
			goto done;
		}

		if (!convertBEInfoToDictionary(be, &dict)) {
			/* LINTED */
			Py_DECREF(dict);
			ret = BE_PY_ERR_VAR_CONV;
			goto done;
		}

		if (PyList_Append(listOfDicts, dict) != 0) {
			/* LINTED */
			Py_DECREF(dict);
			ret = BE_PY_ERR_APPEND;
			goto done;
		}

		/* LINTED */
		Py_DECREF(dict);

		while (ds != NULL) {
			if ((dict = PyDict_New()) == NULL) {
				ret = BE_PY_ERR_DICT;
				goto done;
			}

			if (!convertDatasetInfoToDictionary(ds, &dict)) {
				/* LINTED */
				Py_DECREF(dict);
				ret = BE_PY_ERR_VAR_CONV;
				goto done;
			}

			if (PyList_Append(listOfDicts, dict) != 0) {
				/* LINTED */
				Py_DECREF(dict);
				ret = BE_PY_ERR_APPEND;
				goto done;
			}

			ds = ds->be_next_dataset;

			/* LINTED */
			Py_DECREF(dict);
		}


		while (ss != NULL) {
			if ((dict = PyDict_New()) == NULL) {
				/* LINTED */
				Py_DECREF(dict);
				ret = BE_PY_ERR_DICT;
				goto done;
			}

			if (!convertSnapshotInfoToDictionary(ss, &dict)) {
				/* LINTED */
				Py_DECREF(dict);
				ret = BE_PY_ERR_VAR_CONV;
				goto done;
			}

			if (PyList_Append(listOfDicts, dict) != 0) {
				/* LINTED */
				Py_DECREF(dict);
				ret = BE_PY_ERR_APPEND;
				goto done;
			}

			ss = ss->be_next_snapshot;

			/* LINTED */
			Py_DECREF(dict);
		}
	}

done:
	if (list != NULL)
		be_free_list(list);
	return (Py_BuildValue("[iO]", ret, listOfDicts));
}

/*
 * Function:    beActivate
 * Description: Convert Python args to nvlist pairs and call libbe:be_activate
 *              to activate a Boot Environment
 * Parameters:
 *   args -     pointer to a python object containing:
 *     beName - The name of the BE to activate
 *
 * Returns a pointer to a python object:
 *      BE_SUCCESS - Success
 *      bePyErr or be_errno_t - Failure
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
beActivate(PyObject *self, PyObject *args)
{
	char		*beName = NULL;
	int		ret = BE_PY_SUCCESS;
	nvlist_t	*beAttrs = NULL;

	if (!PyArg_ParseTuple(args, "z", &beName)) {
		return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
	}

	if (!convertPyArgsToNvlist(&beAttrs, 2, BE_ATTR_ORIG_BE_NAME, beName)) {
		nvlist_free(beAttrs);
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	if (beAttrs == NULL) {
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	ret = be_activate(beAttrs);
	nvlist_free(beAttrs);
	return (Py_BuildValue("i", ret));
}

/*
 * Function:    beDestroy
 * Description: Convert Python args to nvlist pairs and call libbe:be_destroy
 *              to destroy a Boot Environment
 * Parameters:
 *   args -     pointer to a python object containing:
 *     beName - The name of the BE to destroy
 *
 * Returns a pointer to a python object:
 *      BE_SUCCESS - Success
 *      bePyErr or be_errno_t - Failure
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
beDestroy(PyObject *self, PyObject *args)
{
	char		*beName = NULL;
	int		destroy_snaps = 0;
	int		force_unmount = 0;
	int		destroy_flags = 0;
	int		ret = BE_PY_SUCCESS;
	nvlist_t	*beAttrs = NULL;

	if (!PyArg_ParseTuple(args, "z|ii", &beName, &destroy_snaps,
	    &force_unmount)) {
		return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
	}

	if (destroy_snaps == 1)
		destroy_flags |= BE_DESTROY_FLAG_SNAPSHOTS;

	if (force_unmount == 1)
		destroy_flags |= BE_DESTROY_FLAG_FORCE_UNMOUNT;

	if (!convertPyArgsToNvlist(&beAttrs, 2, BE_ATTR_ORIG_BE_NAME, beName)) {
		nvlist_free(beAttrs);
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	if (nvlist_add_uint16(beAttrs, BE_ATTR_DESTROY_FLAGS, destroy_flags)
	    != 0) {
		(void) printf("nvlist_add_uint16 failed for "
		    "BE_ATTR_DESTROY_FLAGS (%d).\n", destroy_flags);
		nvlist_free(beAttrs);
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	if (beAttrs == NULL) {
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	ret = be_destroy(beAttrs);
	nvlist_free(beAttrs);
	return (Py_BuildValue("i", ret));
}

/*
 * Function:    beDestroySnapshot
 * Description: Convert Python args to nvlist pairs and call libbe:be_destroy
 *              to destroy a snapshot of a Boot Environment
 * Parameters:
 *   args -     pointer to a python object containing:
 *     beName - The name of the BE to destroy
 *     snapName - The name of the snapshot to destroy
 *
 * Returns a pointer to a python object:
 *      BE_SUCCESS - Success
 *      bePyErr or be_errno_t - Failure
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
beDestroySnapshot(PyObject *self, PyObject *args)
{
	char		*beName = NULL;
	char		*snapName = NULL;
	int		ret = BE_PY_SUCCESS;
	nvlist_t	*beAttrs = NULL;

	if (!PyArg_ParseTuple(args, "zz", &beName, &snapName)) {
		return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
	}

	if (!convertPyArgsToNvlist(&beAttrs, 4,
	    BE_ATTR_ORIG_BE_NAME, beName,
	    BE_ATTR_SNAP_NAME, snapName)) {
		nvlist_free(beAttrs);
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	if (beAttrs == NULL) {
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	ret = be_destroy_snapshot(beAttrs);
	nvlist_free(beAttrs);
	return (Py_BuildValue("i", ret));
}

/*
 * Function:    beRename
 * Description: Convert Python args to nvlist pairs and call libbe:be_rename
 *              to rename a Boot Environment
 * Parameters:
 *   args -     pointer to a python object containing:
 *     oldBeName - The name of the old Boot Environment
 *     newBeName - The name of the new Boot Environment
 *
 * Returns a pointer to a python object:
 *      BE_SUCCESS - Success
 *      bePyErr or be_errno_t - Failure
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
beRename(PyObject *self, PyObject *args)
{
	char		*oldBeName = NULL;
	char		*newBeName = NULL;
	int		ret = BE_PY_SUCCESS;
	nvlist_t	*beAttrs = NULL;

	if (!PyArg_ParseTuple(args, "zz", &oldBeName, &newBeName)) {
		return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
	}

	if (!convertPyArgsToNvlist(&beAttrs, 4,
	    BE_ATTR_ORIG_BE_NAME, oldBeName,
	    BE_ATTR_NEW_BE_NAME, newBeName)) {
		nvlist_free(beAttrs);
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	if (beAttrs == NULL) {
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	ret = be_rename(beAttrs);
	nvlist_free(beAttrs);
	return (Py_BuildValue("i", ret));
}

/*
 * Function:    beMount
 * Description: Convert Python args to nvlist pairs and call libbe:be_mount
 *              to mount a Boot Environment
 * Parameters:
 *   args -     pointer to a python object containing:
 *     beName - The name of the Boot Environment to mount
 *     mountpoint - The path of the mountpoint to mount the
 *                  Boot Environment on (optional)
 *
 * Returns a pointer to a python object:
 *      BE_SUCCESS - Success
 *      bePyErr or be_errno_t - Failure
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
beMount(PyObject *self, PyObject *args)
{
	char		*beName = NULL;
	char		*mountpoint = NULL;
	int		ret = BE_PY_SUCCESS;
	nvlist_t	*beAttrs = NULL;

	if (!PyArg_ParseTuple(args, "zz", &beName, &mountpoint)) {
		return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
	}

	if (!convertPyArgsToNvlist(&beAttrs, 4,
	    BE_ATTR_ORIG_BE_NAME, beName,
	    BE_ATTR_MOUNTPOINT, mountpoint)) {
		nvlist_free(beAttrs);
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	if (beAttrs == NULL) {
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	ret = be_mount(beAttrs);
	nvlist_free(beAttrs);
	return (Py_BuildValue("i", ret));
}

/*
 * Function:    beUnmount
 * Description: Convert Python args to nvlist pairs and call libbe:be_unmount
 *              to unmount a Boot Environment
 * Parameters:
 *   args -     pointer to a python object containing:
 *     beName - The name of the Boot Environment to unmount
 *
 * Returns a pointer to a python object:
 *      BE_SUCCESS - Success
 *      bePyErr or be_errno_t - Failure
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
beUnmount(PyObject *self, PyObject *args)
{
	char 		*beName = NULL;
	int		force_unmount = 0;
	int		unmount_flags = 0;
	int		ret = BE_PY_SUCCESS;
	nvlist_t	*beAttrs = NULL;

	if (!PyArg_ParseTuple(args, "z|i", &beName, &force_unmount)) {
		return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
	}

	if (force_unmount == 1)
		unmount_flags |= BE_UNMOUNT_FLAG_FORCE;

	if (!convertPyArgsToNvlist(&beAttrs, 2,
	    BE_ATTR_ORIG_BE_NAME, beName)) {
		nvlist_free(beAttrs);
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	if (nvlist_add_uint16(beAttrs, BE_ATTR_UNMOUNT_FLAGS, unmount_flags)
	    != 0) {
		(void) printf("nvlist_add_uint16 failed for "
		    "BE_ATTR_UNMOUNT_FLAGS (%d).\n", unmount_flags);
		nvlist_free(beAttrs);
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	if (beAttrs == NULL) {
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	ret = be_unmount(beAttrs);
	nvlist_free(beAttrs);
	return (Py_BuildValue("i", ret));
}

/*
 * Function:    beRollback
 * Description: Convert Python args to nvlist pairs and call libbe:be_rollback
 *              to rollback a Boot Environment to a previously taken
 *               snapshot.
 * Parameters:
 *   args -     pointer to a python object containing:
 *     beName - The name of the Boot Environment to unmount
 *
 * Returns a pointer to a python object:
 *      BE_SUCCESS - Success
 *      bePyErr or be_errno_t - Failure
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
beRollback(PyObject *self, PyObject *args)
{
	char		*beName = NULL;
	char		*snapName = NULL;
	int		ret = BE_PY_SUCCESS;
	nvlist_t	*beAttrs = NULL;

	if (!PyArg_ParseTuple(args, "zz", &beName, &snapName)) {
		return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
	}

	if (!convertPyArgsToNvlist(&beAttrs, 4,
	    BE_ATTR_ORIG_BE_NAME, beName,
	    BE_ATTR_SNAP_NAME, snapName)) {
		nvlist_free(beAttrs);
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	if (beAttrs == NULL) {
		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
	}

	ret = be_rollback(beAttrs);
	nvlist_free(beAttrs);
	return (Py_BuildValue("i", ret));
}

/*
 * Function:    bePrintErrors
 * Description: Convert Python args to boolean and call libbe_print_errors to
 *			turn on/off error output for the library.
 * Parameter:
 *   args -     pointer to a python object containing:
 *		print_errors - Boolean that turns library error
 *			       printing on or off.
 * Parameters:
 *   args -     pointer to a python object containing:
 *     0 - do not print errors - Python boolean "False"
 *     1 - print errors - Python boolean "True"
 *
 * Returns 1 on missing or invalid argument, 0 otherwise
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
bePrintErrors(PyObject *self, PyObject *args)
{
	int		print_errors;

	if (!PyArg_ParseTuple(args, "i", &print_errors) ||
	    (print_errors != 1 && print_errors != 0))
		return (Py_BuildValue("i", BE_PY_ERR_PRINT_ERR));
	libbe_print_errors(print_errors == 1);
	return (Py_BuildValue("i", BE_PY_SUCCESS));
}

/*
 * Function:    beGetErrDesc
 * Description: Convert Python args to an int and call be_err_to_str to
 *			map an error code to an error string.
 * Parameter:
 *   args -     pointer to a python object containing:
 *		errCode - value to map to an error string.
 *
 * Returns: error string or NULL
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
beGetErrDesc(PyObject *self, PyObject *args)
{
	int	errCode = 0;
	char	*beErrStr = NULL;

	if (!PyArg_ParseTuple(args, "i", &errCode)) {
		return (Py_BuildValue("s", NULL));
	}

	/*
	 * First check libbe_py errors. If NULL is returned check error codes
	 * in libbe.
	 */

	if ((beErrStr = beMapLibbePyErrorToString(errCode)) == NULL) {
		beErrStr = be_err_to_str(errCode);
	}

	return (Py_BuildValue("s", beErrStr));
}

/*
 * Function:    beVerifyBEName
 * Description: Call be_valid_be_name() to verify the BE name.
 * Parameter:
 *   args -     pointer to a python object containing:
 *		string - value to map to a string.
 *
 * Returns:  0 for success or 1 for failure
 * Scope:
 *      Public
 */
/* ARGSUSED */
PyObject *
beVerifyBEName(PyObject *self, PyObject *args)
{
	char	*string = NULL;

	if (!PyArg_ParseTuple(args, "s", &string)) {
		return (Py_BuildValue("i", 1));
	}

	if (be_valid_be_name(string)) {
		return (Py_BuildValue("i", 0));
	} else {
		return (Py_BuildValue("i", 1));
	}
}

/* ~~~~~~~~~~~~~~~~~ */
/* Private Functions */
/* ~~~~~~~~~~~~~~~~~ */

static boolean_t
convertBEInfoToDictionary(be_node_list_t *be, PyObject **listDict)
{
	if (be->be_node_name != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_ORIG_BE_NAME,
		    PyString_FromString(be->be_node_name)) != 0) {
			return (B_FALSE);
		}
	}

	if (be->be_rpool != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_ORIG_BE_POOL,
		    PyString_FromString(be->be_rpool)) != 0) {
			return (B_FALSE);
		}
	}

	if (be->be_mntpt != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_MOUNTPOINT,
		    PyString_FromString(be->be_mntpt)) != 0) {
			return (B_FALSE);
		}
	}

	if (PyDict_SetItemString(*listDict, BE_ATTR_MOUNTED,
	    (be->be_mounted ? Py_True : Py_False)) != 0) {
		return (B_FALSE);
	}

	if (PyDict_SetItemString(*listDict, BE_ATTR_ACTIVE,
	    (be->be_active ? Py_True : Py_False)) != 0) {
		return (B_FALSE);
	}

	if (PyDict_SetItemString(*listDict, BE_ATTR_ACTIVE_ON_BOOT,
	    (be->be_active_on_boot ? Py_True : Py_False)) != 0) {
		return (B_FALSE);
	}

	if (be->be_space_used != 0) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_SPACE,
		    PyLong_FromUnsignedLongLong(be->be_space_used)) != 0) {
			return (B_FALSE);
		}
	}

	if (be->be_root_ds != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_ROOT_DS,
		    PyString_FromString(be->be_root_ds)) != 0) {
			return (B_FALSE);
		}
	}

	if (be->be_node_creation != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_DATE,
		    PyLong_FromLong(be->be_node_creation)) != 0) {
			return (B_FALSE);
		}
	}

	if (be->be_policy_type != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_POLICY,
		    PyString_FromString(be->be_policy_type)) != 0) {
			return (B_FALSE);
		}
	}

	if (be->be_uuid_str != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_UUID_STR,
		    PyString_FromString(be->be_uuid_str)) != 0) {
			return (B_FALSE);
		}
	}

	return (B_TRUE);
}

static boolean_t
convertDatasetInfoToDictionary(be_dataset_list_t *ds, PyObject **listDict)
{
	if (ds->be_dataset_name != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_DATASET,
		    PyString_FromString(ds->be_dataset_name)) != 0) {
			return (B_FALSE);
		}
	}

	if (PyDict_SetItemString(*listDict, BE_ATTR_STATUS,
	    (ds->be_ds_mounted ? Py_True : Py_False)) != 0) {
			return (B_FALSE);
	}

	if (ds->be_ds_mntpt != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_MOUNTPOINT,
		    PyString_FromString(ds->be_ds_mntpt)) != 0) {
			return (B_FALSE);
		}
	}

	if (PyDict_SetItemString(*listDict, BE_ATTR_MOUNTED,
	    (ds->be_ds_mounted ? Py_True : Py_False)) != 0) {
		return (B_FALSE);
	}

	if (ds->be_ds_space_used != 0) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_SPACE,
		    PyLong_FromUnsignedLongLong(ds->be_ds_space_used))
		    != 0) {
			return (B_FALSE);
		}
	}

	if (ds->be_dataset_name != 0) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_DATASET,
		    PyString_FromString(ds->be_dataset_name)) != 0) {
			return (B_FALSE);
		}
	}

	if (ds->be_ds_plcy_type != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_POLICY,
		    PyString_FromString(ds->be_ds_plcy_type)) != 0) {
			return (B_FALSE);
		}
	}

	if (ds->be_ds_creation != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_DATE,
		    PyLong_FromLong(ds->be_ds_creation)) != 0) {
			return (B_FALSE);
		}
	}

	return (B_TRUE);
}

static boolean_t
convertSnapshotInfoToDictionary(be_snapshot_list_t *ss, PyObject **listDict)
{
	if (ss->be_snapshot_name != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_SNAP_NAME,
		    PyString_FromString(ss->be_snapshot_name)) != 0) {
			return (B_FALSE);
		}
	}

	if (ss->be_snapshot_creation != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_DATE,
		    PyLong_FromLong(ss->be_snapshot_creation)) != 0) {
			return (B_FALSE);
		}
	}

	if (ss->be_snapshot_type != NULL) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_POLICY,
		    PyString_FromString(ss->be_snapshot_type)) != 0) {
			return (B_FALSE);
		}
	}

	if (ss->be_snapshot_space_used != 0) {
		if (PyDict_SetItemString(*listDict, BE_ATTR_SPACE,
		    PyLong_FromUnsignedLongLong(ss->be_snapshot_space_used))
		    != 0) {
			return (B_FALSE);
		}
	}

	return (B_TRUE);
}

/*
 * Convert string arguments to nvlist attributes
 */

static boolean_t
convertPyArgsToNvlist(nvlist_t **nvList, int numArgs, ...)
{
	char *pt, *pt2;
	va_list ap;
	int i;

	if (*nvList == NULL) {
		if (nvlist_alloc(nvList, NV_UNIQUE_NAME, 0) != 0) {
			(void) printf("nvlist_alloc failed.\n");
			return (B_FALSE);
		}
	}

	va_start(ap, numArgs);

	for (i = 0; i < numArgs; i += 2) {
		if ((pt = va_arg(ap, char *)) == NULL ||
		    (pt2 = va_arg(ap, char *)) == NULL) {
			continue;
		}
		if (nvlist_add_string(*nvList, pt, pt2) != 0) {
			(void) printf("nvlist_add_string failed for %s (%s).\n",
			    pt, pt2);
			nvlist_free(*nvList);
			return (B_FALSE);
		}
	}

	va_end(ap);

	return (B_TRUE);
}

/*
 * Function:    beMapLibbePyErrorToString
 * Description: Convert Python args to an int and map an error code to an
 *			error string.
 * Parameter:
 *		errCode - value to map to an error string.
 *
 * Returns error string or NULL
 * Scope:
 *      Public
 */

char *
beMapLibbePyErrorToString(int errCode)
{
	switch (errCode) {
	case BE_PY_ERR_APPEND:
		return ("Unable to append a dictionary to a list "
		    "of dictinaries.");
	case BE_PY_ERR_DICT:
		return ("Creation of a Python dictionary failed.");
	case BE_PY_ERR_LIST:
		return ("beList() failed.");
	case BE_PY_ERR_NVLIST:
		return ("An nvlist operation failed.");
	case BE_PY_ERR_PARSETUPLE:
		return ("PyArg_ParseTuple() failed to convert variable to C.");
	case BE_PY_ERR_PRINT_ERR:
		return ("bePrintErrors() failed.");
	case BE_PY_ERR_VAR_CONV:
		return ("Unable to add variables to a Python dictionary.");
	default:
		return (NULL);
	}
}

/* Private python initialization structure */

static struct PyMethodDef libbeMethods[] = {
	{"beCopy", (PyCFunction)beCopy, METH_VARARGS, "Create/Copy a BE."},
	{"beCreateSnapshot", (PyCFunction)beCreateSnapshot, METH_VARARGS,
	    "Create a snapshot."},
	{"beDestroy", (PyCFunction)beDestroy, METH_VARARGS, "Destroy a BE."},
	{"beDestroySnapshot", (PyCFunction)beDestroySnapshot, METH_VARARGS,
	    "Destroy a snapshot."},
	{"beMount", (PyCFunction)beMount, METH_VARARGS, "Mount a BE."},
	{"beUnmount", (PyCFunction)beUnmount, METH_VARARGS, "Unmount a BE."},
	{"beList", (PyCFunction)beList, METH_VARARGS, "List BE info."},
	{"beRename", (PyCFunction)beRename, METH_VARARGS, "Rename a BE."},
	{"beActivate", (PyCFunction)beActivate, METH_VARARGS, "Activate a BE."},
	{"beRollback", (PyCFunction)beRollback, METH_VARARGS, "Rollback a BE."},
	{"bePrintErrors", (PyCFunction)bePrintErrors, METH_VARARGS,
	    "Enable/disable error printing."},
	{"beGetErrDesc", (PyCFunction)beGetErrDesc, METH_VARARGS,
	    "Map Error codes to strings."},
	{"beVerifyBEName", (PyCFunction)beVerifyBEName, METH_VARARGS,
	    "Verify BE name."},
	{NULL, NULL, 0, NULL}
};

void
initlibbe_py()
{
	/* PyMODINIT_FUNC; */
	(void) Py_InitModule("libbe_py", libbeMethods);
}