Print this page
3352 would like 64bit install libraries
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/pylibbe/common/libbe_py.c
+++ new/usr/src/lib/pylibbe/common/libbe_py.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 + * Copyright 2012 OmniTI Computer Consulting, Inc. All rights reserved.
24 25 */
25 26
26 27 #include <Python.h>
27 28 #include <sys/varargs.h>
28 29 #include <stdio.h>
29 30 #include <libnvpair.h>
30 31
31 32 #include <libbe.h>
32 33 #include <libbe_priv.h>
33 34
34 35 enum {
35 36 BE_PY_SUCCESS = 0,
36 37 BE_PY_ERR_APPEND = 6000,
37 38 BE_PY_ERR_DICT,
38 39 BE_PY_ERR_LIST,
39 40 BE_PY_ERR_NVLIST,
40 41 BE_PY_ERR_PARSETUPLE,
41 42 BE_PY_ERR_PRINT_ERR,
42 43 BE_PY_ERR_VAR_CONV,
43 44 } bePyErr;
44 45
45 46 /*
46 47 * public libbe functions
47 48 */
48 49
49 50 PyObject *beCreateSnapshot(PyObject *, PyObject *);
50 51 PyObject *beCopy(PyObject *, PyObject *);
51 52 PyObject *beList(PyObject *, PyObject *);
52 53 PyObject *beActivate(PyObject *, PyObject *);
53 54 PyObject *beDestroy(PyObject *, PyObject *);
54 55 PyObject *beDestroySnapshot(PyObject *, PyObject *);
55 56 PyObject *beRename(PyObject *, PyObject *);
56 57 PyObject *beMount(PyObject *, PyObject *);
57 58 PyObject *beUnmount(PyObject *, PyObject *);
58 59 PyObject *bePrintErrors(PyObject *, PyObject *);
59 60 PyObject *beGetErrDesc(PyObject *, PyObject *);
60 61 char *beMapLibbePyErrorToString(int);
61 62 void initlibbe_py();
62 63
63 64 static boolean_t convertBEInfoToDictionary(be_node_list_t *be,
64 65 PyObject **listDict);
65 66 static boolean_t convertDatasetInfoToDictionary(be_dataset_list_t *ds,
66 67 PyObject **listDict);
67 68 static boolean_t convertSnapshotInfoToDictionary(be_snapshot_list_t *ss,
68 69 PyObject **listDict);
69 70 static boolean_t convertPyArgsToNvlist(nvlist_t **nvList, int numArgs, ...);
70 71
71 72
72 73 /* ~~~~~~~~~~~~~~~ */
73 74 /* Public Funtions */
74 75 /* ~~~~~~~~~~~~~~~ */
75 76
76 77 /*
77 78 * Function: beCreateSnapshot
78 79 * Description: Convert Python args to nvlist pairs and
79 80 * call libbe:be_create_snapshot to create a
80 81 * snapshot of all the datasets within a BE
81 82 * Parameters:
82 83 * args - pointer to a python object containing:
83 84 * beName - The name of the BE to create a snapshot of
84 85 * snapName - The name of the snapshot to create (optional)
85 86 *
86 87 * The following public attribute values. defined by libbe.h,
87 88 * are used by this function:
88 89 *
89 90 * Returns a pointer to a python object and an optional snapshot name:
90 91 * 0, [snapName] - Success
91 92 * 1, [snapName] - Failure
92 93 * Scope:
93 94 * Public
94 95 */
95 96 /* ARGSUSED */
96 97 PyObject *
97 98 beCreateSnapshot(PyObject *self, PyObject *args)
98 99 {
99 100 char *beName = NULL;
100 101 char *snapName = NULL;
101 102 int ret = BE_PY_SUCCESS;
102 103 nvlist_t *beAttrs = NULL;
103 104 PyObject *retVals = NULL;
104 105
105 106 if (!PyArg_ParseTuple(args, "z|z", &beName, &snapName)) {
106 107 return (Py_BuildValue("[is]", BE_PY_ERR_PARSETUPLE, NULL));
107 108 }
108 109
109 110 if (!convertPyArgsToNvlist(&beAttrs, 4,
110 111 BE_ATTR_ORIG_BE_NAME, beName,
111 112 BE_ATTR_SNAP_NAME, snapName)) {
112 113 nvlist_free(beAttrs);
113 114 return (Py_BuildValue("[is]", BE_PY_ERR_NVLIST, NULL));
114 115 }
115 116
116 117 if (beAttrs == NULL) {
117 118 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
118 119 }
119 120
120 121 if ((ret = be_create_snapshot(beAttrs)) != 0) {
121 122 nvlist_free(beAttrs);
122 123 return (Py_BuildValue("[is]", ret, NULL));
123 124 }
124 125 if (snapName == NULL) {
125 126 if (nvlist_lookup_pairs(beAttrs, NV_FLAG_NOENTOK,
126 127 BE_ATTR_SNAP_NAME, DATA_TYPE_STRING, &snapName,
127 128 NULL) != 0) {
128 129 nvlist_free(beAttrs);
129 130 return (Py_BuildValue("[is]",
130 131 BE_PY_ERR_NVLIST, NULL));
131 132 }
132 133 retVals = Py_BuildValue("[is]", ret, snapName);
133 134 nvlist_free(beAttrs);
134 135 return (retVals);
135 136 }
136 137 nvlist_free(beAttrs);
137 138
138 139 return (Py_BuildValue("[is]", ret, NULL));
139 140 }
140 141
141 142 /*
142 143 * Function: beCopy
143 144 * Description: Convert Python args to nvlist pairs and call libbe:be_copy
144 145 * to create a Boot Environment
145 146 * Parameters:
146 147 * args - pointer to a python object containing:
147 148 * trgtBeName - The name of the BE to create
148 149 * srcBeName - The name of the BE used to create trgtBeName (optional)
149 150 * rpool - The pool to create the new BE in (optional)
150 151 * srcSnapName - The snapshot name (optional)
151 152 * beNameProperties - The properties to use when creating
152 153 * the BE (optional)
153 154 *
154 155 * Returns a pointer to a python object. That Python object will consist of
155 156 * the return code and optional attributes, trgtBeName and snapshotName
156 157 * BE_SUCCESS, [trgtBeName], [trgtSnapName] - Success
157 158 * 1, [trgtBeName], [trgtSnapName] - Failure
158 159 * Scope:
159 160 * Public
160 161 */
↓ open down ↓ |
127 lines elided |
↑ open up ↑ |
161 162 /* ARGSUSED */
162 163 PyObject *
163 164 beCopy(PyObject *self, PyObject *args)
164 165 {
165 166 char *trgtBeName = NULL;
166 167 char *srcBeName = NULL;
167 168 char *srcSnapName = NULL;
168 169 char *trgtSnapName = NULL;
169 170 char *rpool = NULL;
170 171 char *beDescription = NULL;
171 - int pos = 0;
172 + Py_ssize_t pos = 0;
172 173 int ret = BE_PY_SUCCESS;
173 174 nvlist_t *beAttrs = NULL;
174 175 nvlist_t *beProps = NULL;
175 176 PyObject *beNameProperties = NULL;
176 177 PyObject *pkey = NULL;
177 178 PyObject *pvalue = NULL;
178 179 PyObject *retVals = NULL;
179 180
180 181 if (!PyArg_ParseTuple(args, "|zzzzOz", &trgtBeName, &srcBeName,
181 182 &srcSnapName, &rpool, &beNameProperties, &beDescription)) {
182 183 return (Py_BuildValue("[iss]", BE_PY_ERR_PARSETUPLE,
183 184 NULL, NULL));
184 185 }
185 186
186 187 if (!convertPyArgsToNvlist(&beAttrs, 10,
187 188 BE_ATTR_NEW_BE_NAME, trgtBeName,
188 189 BE_ATTR_ORIG_BE_NAME, srcBeName,
189 190 BE_ATTR_SNAP_NAME, srcSnapName,
190 191 BE_ATTR_NEW_BE_POOL, rpool,
191 192 BE_ATTR_NEW_BE_DESC, beDescription)) {
192 193 nvlist_free(beAttrs);
193 194 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST, NULL, NULL));
194 195 }
195 196
196 197 if (beNameProperties != NULL) {
197 198 if (nvlist_alloc(&beProps, NV_UNIQUE_NAME, 0) != 0) {
198 199 (void) printf("nvlist_alloc failed.\n");
199 200 nvlist_free(beAttrs);
200 201 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
201 202 NULL, NULL));
202 203 }
203 204 while (PyDict_Next(beNameProperties, &pos, &pkey, &pvalue)) {
204 205 if (!convertPyArgsToNvlist(&beProps, 2,
205 206 PyString_AsString(pkey),
206 207 PyString_AsString(pvalue))) {
207 208 nvlist_free(beProps);
208 209 nvlist_free(beAttrs);
209 210 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
210 211 NULL, NULL));
211 212 }
212 213 }
213 214 }
214 215
215 216 if (beProps != NULL && beAttrs != NULL &&
216 217 nvlist_add_nvlist(beAttrs, BE_ATTR_ZFS_PROPERTIES,
217 218 beProps) != 0) {
218 219 nvlist_free(beProps);
219 220 nvlist_free(beAttrs);
220 221 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
221 222 NULL, NULL));
222 223 }
223 224
224 225 if (beProps != NULL) nvlist_free(beProps);
225 226
226 227 if (trgtBeName == NULL) {
227 228 /*
228 229 * Caller wants to get back the BE_ATTR_NEW_BE_NAME and
229 230 * BE_ATTR_SNAP_NAME
230 231 */
231 232 if ((ret = be_copy(beAttrs)) != BE_SUCCESS) {
232 233 nvlist_free(beAttrs);
233 234 return (Py_BuildValue("[iss]", ret, NULL, NULL));
234 235 }
235 236
236 237 /*
237 238 * When no trgtBeName is passed to be_copy, be_copy
238 239 * returns an auto generated beName and snapshot name.
239 240 */
240 241 if (nvlist_lookup_string(beAttrs, BE_ATTR_NEW_BE_NAME,
241 242 &trgtBeName) != 0) {
242 243 nvlist_free(beAttrs);
243 244 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
244 245 NULL, NULL));
245 246 }
246 247 if (nvlist_lookup_string(beAttrs, BE_ATTR_SNAP_NAME,
247 248 &trgtSnapName) != 0) {
248 249 nvlist_free(beAttrs);
249 250 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
250 251 NULL, NULL));
251 252 }
252 253
253 254 retVals = Py_BuildValue("[iss]", BE_PY_SUCCESS,
254 255 trgtBeName, trgtSnapName);
255 256 nvlist_free(beAttrs);
256 257 return (retVals);
257 258
258 259 } else {
259 260 ret = be_copy(beAttrs);
260 261 nvlist_free(beAttrs);
261 262 return (Py_BuildValue("[iss]", ret, NULL, NULL));
262 263 }
263 264 }
264 265
265 266 /*
266 267 * Function: beList
267 268 * Description: Convert Python args to nvlist pairs and call libbe:be_list
268 269 * to gather information about Boot Environments
269 270 * Parameters:
270 271 * args - pointer to a python object containing:
271 272 * beName - The name of the BE to list (optional)
272 273 *
273 274 * Returns a pointer to a python object. That Python object will consist of
274 275 * the return code and a list of Dicts or NULL.
275 276 * BE_PY_SUCCESS, listOfDicts - Success
276 277 * bePyErr or be_errno_t, NULL - Failure
277 278 * Scope:
278 279 * Public
279 280 */
280 281 /* ARGSUSED */
281 282 PyObject *
282 283 beList(PyObject *self, PyObject *args)
283 284 {
284 285 char *beName = NULL;
285 286 int ret = BE_PY_SUCCESS;
286 287 be_node_list_t *list = NULL;
287 288 be_node_list_t *be = NULL;
288 289 PyObject *dict = NULL;
289 290 PyObject *listOfDicts = NULL;
290 291
291 292 if ((listOfDicts = PyList_New(0)) == NULL) {
292 293 ret = BE_PY_ERR_DICT;
293 294 listOfDicts = Py_None;
294 295 goto done;
295 296 }
296 297
297 298 if (!PyArg_ParseTuple(args, "|z", &beName)) {
298 299 ret = BE_PY_ERR_PARSETUPLE;
299 300 goto done;
300 301 }
301 302
302 303 if ((ret = be_list(beName, &list)) != BE_SUCCESS) {
303 304 goto done;
304 305 }
305 306
306 307 for (be = list; be != NULL; be = be->be_next_node) {
307 308 be_dataset_list_t *ds = be->be_node_datasets;
308 309 be_snapshot_list_t *ss = be->be_node_snapshots;
309 310
310 311 if ((dict = PyDict_New()) == NULL) {
311 312 ret = BE_PY_ERR_DICT;
312 313 goto done;
313 314 }
314 315
315 316 if (!convertBEInfoToDictionary(be, &dict)) {
316 317 /* LINTED */
317 318 Py_DECREF(dict);
318 319 ret = BE_PY_ERR_VAR_CONV;
319 320 goto done;
320 321 }
321 322
322 323 if (PyList_Append(listOfDicts, dict) != 0) {
323 324 /* LINTED */
324 325 Py_DECREF(dict);
325 326 ret = BE_PY_ERR_APPEND;
326 327 goto done;
327 328 }
328 329
329 330 /* LINTED */
330 331 Py_DECREF(dict);
331 332
332 333 while (ds != NULL) {
333 334 if ((dict = PyDict_New()) == NULL) {
334 335 ret = BE_PY_ERR_DICT;
335 336 goto done;
336 337 }
337 338
338 339 if (!convertDatasetInfoToDictionary(ds, &dict)) {
339 340 /* LINTED */
340 341 Py_DECREF(dict);
341 342 ret = BE_PY_ERR_VAR_CONV;
342 343 goto done;
343 344 }
344 345
345 346 if (PyList_Append(listOfDicts, dict) != 0) {
346 347 /* LINTED */
347 348 Py_DECREF(dict);
348 349 ret = BE_PY_ERR_APPEND;
349 350 goto done;
350 351 }
351 352
352 353 ds = ds->be_next_dataset;
353 354
354 355 /* LINTED */
355 356 Py_DECREF(dict);
356 357 }
357 358
358 359
359 360 while (ss != NULL) {
360 361 if ((dict = PyDict_New()) == NULL) {
361 362 /* LINTED */
362 363 Py_DECREF(dict);
363 364 ret = BE_PY_ERR_DICT;
364 365 goto done;
365 366 }
366 367
367 368 if (!convertSnapshotInfoToDictionary(ss, &dict)) {
368 369 /* LINTED */
369 370 Py_DECREF(dict);
370 371 ret = BE_PY_ERR_VAR_CONV;
371 372 goto done;
372 373 }
373 374
374 375 if (PyList_Append(listOfDicts, dict) != 0) {
375 376 /* LINTED */
376 377 Py_DECREF(dict);
377 378 ret = BE_PY_ERR_APPEND;
378 379 goto done;
379 380 }
380 381
381 382 ss = ss->be_next_snapshot;
382 383
383 384 /* LINTED */
384 385 Py_DECREF(dict);
385 386 }
386 387 }
387 388
388 389 done:
389 390 if (list != NULL)
390 391 be_free_list(list);
391 392 return (Py_BuildValue("[iO]", ret, listOfDicts));
392 393 }
393 394
394 395 /*
395 396 * Function: beActivate
396 397 * Description: Convert Python args to nvlist pairs and call libbe:be_activate
397 398 * to activate a Boot Environment
398 399 * Parameters:
399 400 * args - pointer to a python object containing:
400 401 * beName - The name of the BE to activate
401 402 *
402 403 * Returns a pointer to a python object:
403 404 * BE_SUCCESS - Success
404 405 * bePyErr or be_errno_t - Failure
405 406 * Scope:
406 407 * Public
407 408 */
408 409 /* ARGSUSED */
409 410 PyObject *
410 411 beActivate(PyObject *self, PyObject *args)
411 412 {
412 413 char *beName = NULL;
413 414 int ret = BE_PY_SUCCESS;
414 415 nvlist_t *beAttrs = NULL;
415 416
416 417 if (!PyArg_ParseTuple(args, "z", &beName)) {
417 418 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
418 419 }
419 420
420 421 if (!convertPyArgsToNvlist(&beAttrs, 2, BE_ATTR_ORIG_BE_NAME, beName)) {
421 422 nvlist_free(beAttrs);
422 423 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
423 424 }
424 425
425 426 if (beAttrs == NULL) {
426 427 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
427 428 }
428 429
429 430 ret = be_activate(beAttrs);
430 431 nvlist_free(beAttrs);
431 432 return (Py_BuildValue("i", ret));
432 433 }
433 434
434 435 /*
435 436 * Function: beDestroy
436 437 * Description: Convert Python args to nvlist pairs and call libbe:be_destroy
437 438 * to destroy a Boot Environment
438 439 * Parameters:
439 440 * args - pointer to a python object containing:
440 441 * beName - The name of the BE to destroy
441 442 *
442 443 * Returns a pointer to a python object:
443 444 * BE_SUCCESS - Success
444 445 * bePyErr or be_errno_t - Failure
445 446 * Scope:
446 447 * Public
447 448 */
448 449 /* ARGSUSED */
449 450 PyObject *
450 451 beDestroy(PyObject *self, PyObject *args)
451 452 {
452 453 char *beName = NULL;
453 454 int destroy_snaps = 0;
454 455 int force_unmount = 0;
455 456 int destroy_flags = 0;
456 457 int ret = BE_PY_SUCCESS;
457 458 nvlist_t *beAttrs = NULL;
458 459
459 460 if (!PyArg_ParseTuple(args, "z|ii", &beName, &destroy_snaps,
460 461 &force_unmount)) {
461 462 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
462 463 }
463 464
464 465 if (destroy_snaps == 1)
465 466 destroy_flags |= BE_DESTROY_FLAG_SNAPSHOTS;
466 467
467 468 if (force_unmount == 1)
468 469 destroy_flags |= BE_DESTROY_FLAG_FORCE_UNMOUNT;
469 470
470 471 if (!convertPyArgsToNvlist(&beAttrs, 2, BE_ATTR_ORIG_BE_NAME, beName)) {
471 472 nvlist_free(beAttrs);
472 473 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
473 474 }
474 475
475 476 if (nvlist_add_uint16(beAttrs, BE_ATTR_DESTROY_FLAGS, destroy_flags)
476 477 != 0) {
477 478 (void) printf("nvlist_add_uint16 failed for "
478 479 "BE_ATTR_DESTROY_FLAGS (%d).\n", destroy_flags);
479 480 nvlist_free(beAttrs);
480 481 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
481 482 }
482 483
483 484 if (beAttrs == NULL) {
484 485 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
485 486 }
486 487
487 488 ret = be_destroy(beAttrs);
488 489 nvlist_free(beAttrs);
489 490 return (Py_BuildValue("i", ret));
490 491 }
491 492
492 493 /*
493 494 * Function: beDestroySnapshot
494 495 * Description: Convert Python args to nvlist pairs and call libbe:be_destroy
495 496 * to destroy a snapshot of a Boot Environment
496 497 * Parameters:
497 498 * args - pointer to a python object containing:
498 499 * beName - The name of the BE to destroy
499 500 * snapName - The name of the snapshot to destroy
500 501 *
501 502 * Returns a pointer to a python object:
502 503 * BE_SUCCESS - Success
503 504 * bePyErr or be_errno_t - Failure
504 505 * Scope:
505 506 * Public
506 507 */
507 508 /* ARGSUSED */
508 509 PyObject *
509 510 beDestroySnapshot(PyObject *self, PyObject *args)
510 511 {
511 512 char *beName = NULL;
512 513 char *snapName = NULL;
513 514 int ret = BE_PY_SUCCESS;
514 515 nvlist_t *beAttrs = NULL;
515 516
516 517 if (!PyArg_ParseTuple(args, "zz", &beName, &snapName)) {
517 518 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
518 519 }
519 520
520 521 if (!convertPyArgsToNvlist(&beAttrs, 4,
521 522 BE_ATTR_ORIG_BE_NAME, beName,
522 523 BE_ATTR_SNAP_NAME, snapName)) {
523 524 nvlist_free(beAttrs);
524 525 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
525 526 }
526 527
527 528 if (beAttrs == NULL) {
528 529 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
529 530 }
530 531
531 532 ret = be_destroy_snapshot(beAttrs);
532 533 nvlist_free(beAttrs);
533 534 return (Py_BuildValue("i", ret));
534 535 }
535 536
536 537 /*
537 538 * Function: beRename
538 539 * Description: Convert Python args to nvlist pairs and call libbe:be_rename
539 540 * to rename a Boot Environment
540 541 * Parameters:
541 542 * args - pointer to a python object containing:
542 543 * oldBeName - The name of the old Boot Environment
543 544 * newBeName - The name of the new Boot Environment
544 545 *
545 546 * Returns a pointer to a python object:
546 547 * BE_SUCCESS - Success
547 548 * bePyErr or be_errno_t - Failure
548 549 * Scope:
549 550 * Public
550 551 */
551 552 /* ARGSUSED */
552 553 PyObject *
553 554 beRename(PyObject *self, PyObject *args)
554 555 {
555 556 char *oldBeName = NULL;
556 557 char *newBeName = NULL;
557 558 int ret = BE_PY_SUCCESS;
558 559 nvlist_t *beAttrs = NULL;
559 560
560 561 if (!PyArg_ParseTuple(args, "zz", &oldBeName, &newBeName)) {
561 562 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
562 563 }
563 564
564 565 if (!convertPyArgsToNvlist(&beAttrs, 4,
565 566 BE_ATTR_ORIG_BE_NAME, oldBeName,
566 567 BE_ATTR_NEW_BE_NAME, newBeName)) {
567 568 nvlist_free(beAttrs);
568 569 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
569 570 }
570 571
571 572 if (beAttrs == NULL) {
572 573 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
573 574 }
574 575
575 576 ret = be_rename(beAttrs);
576 577 nvlist_free(beAttrs);
577 578 return (Py_BuildValue("i", ret));
578 579 }
579 580
580 581 /*
581 582 * Function: beMount
582 583 * Description: Convert Python args to nvlist pairs and call libbe:be_mount
583 584 * to mount a Boot Environment
584 585 * Parameters:
585 586 * args - pointer to a python object containing:
586 587 * beName - The name of the Boot Environment to mount
587 588 * mountpoint - The path of the mountpoint to mount the
588 589 * Boot Environment on (optional)
589 590 *
590 591 * Returns a pointer to a python object:
591 592 * BE_SUCCESS - Success
592 593 * bePyErr or be_errno_t - Failure
593 594 * Scope:
594 595 * Public
595 596 */
596 597 /* ARGSUSED */
597 598 PyObject *
598 599 beMount(PyObject *self, PyObject *args)
599 600 {
600 601 char *beName = NULL;
601 602 char *mountpoint = NULL;
602 603 int ret = BE_PY_SUCCESS;
603 604 nvlist_t *beAttrs = NULL;
604 605
605 606 if (!PyArg_ParseTuple(args, "zz", &beName, &mountpoint)) {
606 607 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
607 608 }
608 609
609 610 if (!convertPyArgsToNvlist(&beAttrs, 4,
610 611 BE_ATTR_ORIG_BE_NAME, beName,
611 612 BE_ATTR_MOUNTPOINT, mountpoint)) {
612 613 nvlist_free(beAttrs);
613 614 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
614 615 }
615 616
616 617 if (beAttrs == NULL) {
617 618 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
618 619 }
619 620
620 621 ret = be_mount(beAttrs);
621 622 nvlist_free(beAttrs);
622 623 return (Py_BuildValue("i", ret));
623 624 }
624 625
625 626 /*
626 627 * Function: beUnmount
627 628 * Description: Convert Python args to nvlist pairs and call libbe:be_unmount
628 629 * to unmount a Boot Environment
629 630 * Parameters:
630 631 * args - pointer to a python object containing:
631 632 * beName - The name of the Boot Environment to unmount
632 633 *
633 634 * Returns a pointer to a python object:
634 635 * BE_SUCCESS - Success
635 636 * bePyErr or be_errno_t - Failure
636 637 * Scope:
637 638 * Public
638 639 */
639 640 /* ARGSUSED */
640 641 PyObject *
641 642 beUnmount(PyObject *self, PyObject *args)
642 643 {
643 644 char *beName = NULL;
644 645 int force_unmount = 0;
645 646 int unmount_flags = 0;
646 647 int ret = BE_PY_SUCCESS;
647 648 nvlist_t *beAttrs = NULL;
648 649
649 650 if (!PyArg_ParseTuple(args, "z|i", &beName, &force_unmount)) {
650 651 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
651 652 }
652 653
653 654 if (force_unmount == 1)
654 655 unmount_flags |= BE_UNMOUNT_FLAG_FORCE;
655 656
656 657 if (!convertPyArgsToNvlist(&beAttrs, 2,
657 658 BE_ATTR_ORIG_BE_NAME, beName)) {
658 659 nvlist_free(beAttrs);
659 660 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
660 661 }
661 662
662 663 if (nvlist_add_uint16(beAttrs, BE_ATTR_UNMOUNT_FLAGS, unmount_flags)
663 664 != 0) {
664 665 (void) printf("nvlist_add_uint16 failed for "
665 666 "BE_ATTR_UNMOUNT_FLAGS (%d).\n", unmount_flags);
666 667 nvlist_free(beAttrs);
667 668 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
668 669 }
669 670
670 671 if (beAttrs == NULL) {
671 672 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
672 673 }
673 674
674 675 ret = be_unmount(beAttrs);
675 676 nvlist_free(beAttrs);
676 677 return (Py_BuildValue("i", ret));
677 678 }
678 679
679 680 /*
680 681 * Function: beRollback
681 682 * Description: Convert Python args to nvlist pairs and call libbe:be_rollback
682 683 * to rollback a Boot Environment to a previously taken
683 684 * snapshot.
684 685 * Parameters:
685 686 * args - pointer to a python object containing:
686 687 * beName - The name of the Boot Environment to unmount
687 688 *
688 689 * Returns a pointer to a python object:
689 690 * BE_SUCCESS - Success
690 691 * bePyErr or be_errno_t - Failure
691 692 * Scope:
692 693 * Public
693 694 */
694 695 /* ARGSUSED */
695 696 PyObject *
696 697 beRollback(PyObject *self, PyObject *args)
697 698 {
698 699 char *beName = NULL;
699 700 char *snapName = NULL;
700 701 int ret = BE_PY_SUCCESS;
701 702 nvlist_t *beAttrs = NULL;
702 703
703 704 if (!PyArg_ParseTuple(args, "zz", &beName, &snapName)) {
704 705 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
705 706 }
706 707
707 708 if (!convertPyArgsToNvlist(&beAttrs, 4,
708 709 BE_ATTR_ORIG_BE_NAME, beName,
709 710 BE_ATTR_SNAP_NAME, snapName)) {
710 711 nvlist_free(beAttrs);
711 712 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
712 713 }
713 714
714 715 if (beAttrs == NULL) {
715 716 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
716 717 }
717 718
718 719 ret = be_rollback(beAttrs);
719 720 nvlist_free(beAttrs);
720 721 return (Py_BuildValue("i", ret));
721 722 }
722 723
723 724 /*
724 725 * Function: bePrintErrors
725 726 * Description: Convert Python args to boolean and call libbe_print_errors to
726 727 * turn on/off error output for the library.
727 728 * Parameter:
728 729 * args - pointer to a python object containing:
729 730 * print_errors - Boolean that turns library error
730 731 * printing on or off.
731 732 * Parameters:
732 733 * args - pointer to a python object containing:
733 734 * 0 - do not print errors - Python boolean "False"
734 735 * 1 - print errors - Python boolean "True"
735 736 *
736 737 * Returns 1 on missing or invalid argument, 0 otherwise
737 738 * Scope:
738 739 * Public
739 740 */
740 741 /* ARGSUSED */
741 742 PyObject *
742 743 bePrintErrors(PyObject *self, PyObject *args)
743 744 {
744 745 int print_errors;
745 746
746 747 if (!PyArg_ParseTuple(args, "i", &print_errors) ||
747 748 (print_errors != 1 && print_errors != 0))
748 749 return (Py_BuildValue("i", BE_PY_ERR_PRINT_ERR));
749 750 libbe_print_errors(print_errors == 1);
750 751 return (Py_BuildValue("i", BE_PY_SUCCESS));
751 752 }
752 753
753 754 /*
754 755 * Function: beGetErrDesc
755 756 * Description: Convert Python args to an int and call be_err_to_str to
756 757 * map an error code to an error string.
757 758 * Parameter:
758 759 * args - pointer to a python object containing:
759 760 * errCode - value to map to an error string.
760 761 *
761 762 * Returns: error string or NULL
762 763 * Scope:
763 764 * Public
764 765 */
765 766 /* ARGSUSED */
766 767 PyObject *
767 768 beGetErrDesc(PyObject *self, PyObject *args)
768 769 {
769 770 int errCode = 0;
770 771 char *beErrStr = NULL;
771 772
772 773 if (!PyArg_ParseTuple(args, "i", &errCode)) {
773 774 return (Py_BuildValue("s", NULL));
774 775 }
775 776
776 777 /*
777 778 * First check libbe_py errors. If NULL is returned check error codes
778 779 * in libbe.
779 780 */
780 781
781 782 if ((beErrStr = beMapLibbePyErrorToString(errCode)) == NULL) {
782 783 beErrStr = be_err_to_str(errCode);
783 784 }
784 785
785 786 return (Py_BuildValue("s", beErrStr));
786 787 }
787 788
788 789 /*
789 790 * Function: beVerifyBEName
790 791 * Description: Call be_valid_be_name() to verify the BE name.
791 792 * Parameter:
792 793 * args - pointer to a python object containing:
793 794 * string - value to map to a string.
794 795 *
795 796 * Returns: 0 for success or 1 for failure
796 797 * Scope:
797 798 * Public
798 799 */
799 800 /* ARGSUSED */
800 801 PyObject *
801 802 beVerifyBEName(PyObject *self, PyObject *args)
802 803 {
803 804 char *string = NULL;
804 805
805 806 if (!PyArg_ParseTuple(args, "s", &string)) {
806 807 return (Py_BuildValue("i", 1));
807 808 }
808 809
809 810 if (be_valid_be_name(string)) {
810 811 return (Py_BuildValue("i", 0));
811 812 } else {
812 813 return (Py_BuildValue("i", 1));
813 814 }
814 815 }
815 816
816 817 /* ~~~~~~~~~~~~~~~~~ */
817 818 /* Private Functions */
818 819 /* ~~~~~~~~~~~~~~~~~ */
819 820
820 821 static boolean_t
821 822 convertBEInfoToDictionary(be_node_list_t *be, PyObject **listDict)
822 823 {
823 824 if (be->be_node_name != NULL) {
824 825 if (PyDict_SetItemString(*listDict, BE_ATTR_ORIG_BE_NAME,
825 826 PyString_FromString(be->be_node_name)) != 0) {
826 827 return (B_FALSE);
827 828 }
828 829 }
829 830
830 831 if (be->be_rpool != NULL) {
831 832 if (PyDict_SetItemString(*listDict, BE_ATTR_ORIG_BE_POOL,
832 833 PyString_FromString(be->be_rpool)) != 0) {
833 834 return (B_FALSE);
834 835 }
835 836 }
836 837
837 838 if (be->be_mntpt != NULL) {
838 839 if (PyDict_SetItemString(*listDict, BE_ATTR_MOUNTPOINT,
839 840 PyString_FromString(be->be_mntpt)) != 0) {
840 841 return (B_FALSE);
841 842 }
842 843 }
843 844
844 845 if (PyDict_SetItemString(*listDict, BE_ATTR_MOUNTED,
845 846 (be->be_mounted ? Py_True : Py_False)) != 0) {
846 847 return (B_FALSE);
847 848 }
848 849
849 850 if (PyDict_SetItemString(*listDict, BE_ATTR_ACTIVE,
850 851 (be->be_active ? Py_True : Py_False)) != 0) {
851 852 return (B_FALSE);
852 853 }
853 854
854 855 if (PyDict_SetItemString(*listDict, BE_ATTR_ACTIVE_ON_BOOT,
855 856 (be->be_active_on_boot ? Py_True : Py_False)) != 0) {
856 857 return (B_FALSE);
857 858 }
858 859
859 860 if (be->be_space_used != 0) {
860 861 if (PyDict_SetItemString(*listDict, BE_ATTR_SPACE,
861 862 PyLong_FromUnsignedLongLong(be->be_space_used)) != 0) {
862 863 return (B_FALSE);
863 864 }
864 865 }
865 866
866 867 if (be->be_root_ds != NULL) {
867 868 if (PyDict_SetItemString(*listDict, BE_ATTR_ROOT_DS,
868 869 PyString_FromString(be->be_root_ds)) != 0) {
869 870 return (B_FALSE);
870 871 }
871 872 }
872 873
873 874 if (be->be_node_creation != NULL) {
874 875 if (PyDict_SetItemString(*listDict, BE_ATTR_DATE,
875 876 PyLong_FromLong(be->be_node_creation)) != 0) {
876 877 return (B_FALSE);
877 878 }
878 879 }
879 880
880 881 if (be->be_policy_type != NULL) {
881 882 if (PyDict_SetItemString(*listDict, BE_ATTR_POLICY,
882 883 PyString_FromString(be->be_policy_type)) != 0) {
883 884 return (B_FALSE);
884 885 }
885 886 }
886 887
887 888 if (be->be_uuid_str != NULL) {
888 889 if (PyDict_SetItemString(*listDict, BE_ATTR_UUID_STR,
889 890 PyString_FromString(be->be_uuid_str)) != 0) {
890 891 return (B_FALSE);
891 892 }
892 893 }
893 894
894 895 return (B_TRUE);
895 896 }
896 897
897 898 static boolean_t
898 899 convertDatasetInfoToDictionary(be_dataset_list_t *ds, PyObject **listDict)
899 900 {
900 901 if (ds->be_dataset_name != NULL) {
901 902 if (PyDict_SetItemString(*listDict, BE_ATTR_DATASET,
902 903 PyString_FromString(ds->be_dataset_name)) != 0) {
903 904 return (B_FALSE);
904 905 }
905 906 }
906 907
907 908 if (PyDict_SetItemString(*listDict, BE_ATTR_STATUS,
908 909 (ds->be_ds_mounted ? Py_True : Py_False)) != 0) {
909 910 return (B_FALSE);
910 911 }
911 912
912 913 if (ds->be_ds_mntpt != NULL) {
913 914 if (PyDict_SetItemString(*listDict, BE_ATTR_MOUNTPOINT,
914 915 PyString_FromString(ds->be_ds_mntpt)) != 0) {
915 916 return (B_FALSE);
916 917 }
917 918 }
918 919
919 920 if (PyDict_SetItemString(*listDict, BE_ATTR_MOUNTED,
920 921 (ds->be_ds_mounted ? Py_True : Py_False)) != 0) {
921 922 return (B_FALSE);
922 923 }
923 924
924 925 if (ds->be_ds_space_used != 0) {
925 926 if (PyDict_SetItemString(*listDict, BE_ATTR_SPACE,
926 927 PyLong_FromUnsignedLongLong(ds->be_ds_space_used))
927 928 != 0) {
928 929 return (B_FALSE);
929 930 }
930 931 }
931 932
932 933 if (ds->be_dataset_name != 0) {
933 934 if (PyDict_SetItemString(*listDict, BE_ATTR_DATASET,
934 935 PyString_FromString(ds->be_dataset_name)) != 0) {
935 936 return (B_FALSE);
936 937 }
937 938 }
938 939
939 940 if (ds->be_ds_plcy_type != NULL) {
940 941 if (PyDict_SetItemString(*listDict, BE_ATTR_POLICY,
941 942 PyString_FromString(ds->be_ds_plcy_type)) != 0) {
942 943 return (B_FALSE);
943 944 }
944 945 }
945 946
946 947 if (ds->be_ds_creation != NULL) {
947 948 if (PyDict_SetItemString(*listDict, BE_ATTR_DATE,
948 949 PyLong_FromLong(ds->be_ds_creation)) != 0) {
949 950 return (B_FALSE);
950 951 }
951 952 }
952 953
953 954 return (B_TRUE);
954 955 }
955 956
956 957 static boolean_t
957 958 convertSnapshotInfoToDictionary(be_snapshot_list_t *ss, PyObject **listDict)
958 959 {
959 960 if (ss->be_snapshot_name != NULL) {
960 961 if (PyDict_SetItemString(*listDict, BE_ATTR_SNAP_NAME,
961 962 PyString_FromString(ss->be_snapshot_name)) != 0) {
962 963 return (B_FALSE);
963 964 }
964 965 }
965 966
966 967 if (ss->be_snapshot_creation != NULL) {
967 968 if (PyDict_SetItemString(*listDict, BE_ATTR_DATE,
968 969 PyLong_FromLong(ss->be_snapshot_creation)) != 0) {
969 970 return (B_FALSE);
970 971 }
971 972 }
972 973
973 974 if (ss->be_snapshot_type != NULL) {
974 975 if (PyDict_SetItemString(*listDict, BE_ATTR_POLICY,
975 976 PyString_FromString(ss->be_snapshot_type)) != 0) {
976 977 return (B_FALSE);
977 978 }
978 979 }
979 980
980 981 if (ss->be_snapshot_space_used != 0) {
981 982 if (PyDict_SetItemString(*listDict, BE_ATTR_SPACE,
982 983 PyLong_FromUnsignedLongLong(ss->be_snapshot_space_used))
983 984 != 0) {
984 985 return (B_FALSE);
985 986 }
986 987 }
987 988
988 989 return (B_TRUE);
989 990 }
990 991
991 992 /*
992 993 * Convert string arguments to nvlist attributes
993 994 */
994 995
995 996 static boolean_t
996 997 convertPyArgsToNvlist(nvlist_t **nvList, int numArgs, ...)
997 998 {
998 999 char *pt, *pt2;
999 1000 va_list ap;
1000 1001 int i;
1001 1002
1002 1003 if (*nvList == NULL) {
1003 1004 if (nvlist_alloc(nvList, NV_UNIQUE_NAME, 0) != 0) {
1004 1005 (void) printf("nvlist_alloc failed.\n");
1005 1006 return (B_FALSE);
1006 1007 }
1007 1008 }
1008 1009
1009 1010 va_start(ap, numArgs);
1010 1011
1011 1012 for (i = 0; i < numArgs; i += 2) {
1012 1013 if ((pt = va_arg(ap, char *)) == NULL ||
1013 1014 (pt2 = va_arg(ap, char *)) == NULL) {
1014 1015 continue;
1015 1016 }
1016 1017 if (nvlist_add_string(*nvList, pt, pt2) != 0) {
1017 1018 (void) printf("nvlist_add_string failed for %s (%s).\n",
1018 1019 pt, pt2);
1019 1020 nvlist_free(*nvList);
1020 1021 return (B_FALSE);
1021 1022 }
1022 1023 }
1023 1024
1024 1025 va_end(ap);
1025 1026
1026 1027 return (B_TRUE);
1027 1028 }
1028 1029
1029 1030 /*
1030 1031 * Function: beMapLibbePyErrorToString
1031 1032 * Description: Convert Python args to an int and map an error code to an
1032 1033 * error string.
1033 1034 * Parameter:
1034 1035 * errCode - value to map to an error string.
1035 1036 *
1036 1037 * Returns error string or NULL
1037 1038 * Scope:
1038 1039 * Public
1039 1040 */
1040 1041
1041 1042 char *
1042 1043 beMapLibbePyErrorToString(int errCode)
1043 1044 {
1044 1045 switch (errCode) {
1045 1046 case BE_PY_ERR_APPEND:
1046 1047 return ("Unable to append a dictionary to a list "
1047 1048 "of dictinaries.");
1048 1049 case BE_PY_ERR_DICT:
1049 1050 return ("Creation of a Python dictionary failed.");
1050 1051 case BE_PY_ERR_LIST:
1051 1052 return ("beList() failed.");
1052 1053 case BE_PY_ERR_NVLIST:
1053 1054 return ("An nvlist operation failed.");
1054 1055 case BE_PY_ERR_PARSETUPLE:
1055 1056 return ("PyArg_ParseTuple() failed to convert variable to C.");
1056 1057 case BE_PY_ERR_PRINT_ERR:
1057 1058 return ("bePrintErrors() failed.");
1058 1059 case BE_PY_ERR_VAR_CONV:
1059 1060 return ("Unable to add variables to a Python dictionary.");
1060 1061 default:
1061 1062 return (NULL);
1062 1063 }
1063 1064 }
1064 1065
1065 1066 /* Private python initialization structure */
1066 1067
1067 1068 static struct PyMethodDef libbeMethods[] = {
1068 1069 {"beCopy", (PyCFunction)beCopy, METH_VARARGS, "Create/Copy a BE."},
1069 1070 {"beCreateSnapshot", (PyCFunction)beCreateSnapshot, METH_VARARGS,
1070 1071 "Create a snapshot."},
1071 1072 {"beDestroy", (PyCFunction)beDestroy, METH_VARARGS, "Destroy a BE."},
1072 1073 {"beDestroySnapshot", (PyCFunction)beDestroySnapshot, METH_VARARGS,
1073 1074 "Destroy a snapshot."},
1074 1075 {"beMount", (PyCFunction)beMount, METH_VARARGS, "Mount a BE."},
1075 1076 {"beUnmount", (PyCFunction)beUnmount, METH_VARARGS, "Unmount a BE."},
1076 1077 {"beList", (PyCFunction)beList, METH_VARARGS, "List BE info."},
1077 1078 {"beRename", (PyCFunction)beRename, METH_VARARGS, "Rename a BE."},
1078 1079 {"beActivate", (PyCFunction)beActivate, METH_VARARGS, "Activate a BE."},
1079 1080 {"beRollback", (PyCFunction)beRollback, METH_VARARGS, "Rollback a BE."},
1080 1081 {"bePrintErrors", (PyCFunction)bePrintErrors, METH_VARARGS,
1081 1082 "Enable/disable error printing."},
1082 1083 {"beGetErrDesc", (PyCFunction)beGetErrDesc, METH_VARARGS,
1083 1084 "Map Error codes to strings."},
1084 1085 {"beVerifyBEName", (PyCFunction)beVerifyBEName, METH_VARARGS,
1085 1086 "Verify BE name."},
1086 1087 {NULL, NULL, 0, NULL}
1087 1088 };
1088 1089
1089 1090 void
1090 1091 initlibbe_py()
1091 1092 {
1092 1093 /* PyMODINIT_FUNC; */
1093 1094 (void) Py_InitModule("libbe_py", libbeMethods);
1094 1095 }
↓ open down ↓ |
913 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX