summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose M. Guisado <jguisado@soleta.eu>2022-10-06 16:51:02 +0200
committerJose M. Guisado <jguisado@soleta.eu>2022-12-15 12:16:49 +0100
commit3d9db0b93b45d2751009a010fef14003cca5797c (patch)
tree70cf8f35e144a35dfda5eda08fee8a85c5899193
parente58c21bdc837fa5d351dbcccdfcf6e3a0c020294 (diff)
context: add disklabel creation and writing
Adds wrappers for following label related functions from libfdisk: - fdisk_create_disklabel - fdisk_write_disklabel These functions are declared as methods of a Context python object.
-rw-r--r--context.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/context.c b/context.c
index 28c0b7e..a2fe869 100644
--- a/context.c
+++ b/context.c
@@ -126,9 +126,45 @@ static PyObject *Context_partition_to_string(ContextObject *self, PyObject *args
return ret;
}
+#define Context_create_disklabel_HELP "create_disklabel(label)\n\n" \
+ "Creates a new disk label of type name . If name is NULL, " \
+ "then it will create a default system label type, either SUN or DOS."
+static PyObject *Context_create_disklabel(ContextObject *self, PyObject *args, PyObject *kwds)
+{
+ char *label_name = NULL;
+
+ if (!PyArg_ParseTuple(args, "|s", &label_name)) {
+ PyErr_SetString(PyExc_TypeError, ARG_ERR);
+ return NULL;
+ }
+
+ if (fdisk_create_disklabel(self->cxt, label_name)) {
+ PyErr_Format(PyExc_RuntimeError, "Error creating label %s", label_name);
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
+}
+#define Context_write_disklabel_HELP "write_disklabel()\n\n" \
+ "This function wipes the device (if enabled by fdisk_enable_wipe()) " \
+ "and then it writes in-memory changes to disk. Be careful!"
+static PyObject *Context_write_disklabel(ContextObject *self, PyObject *args, PyObject *kwds)
+{
+ int ret;
+
+ ret = fdisk_write_disklabel(self->cxt);
+ if (ret < 0) {
+ PyErr_Format(PyExc_RuntimeError, "Error writing label to disk: %s", strerror(-ret));
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
+}
static PyMethodDef Context_methods[] = {
{"assign_device", (PyCFunction)Context_assign_device, METH_VARARGS, Context_assign_device_HELP},
{"partition_to_string", (PyCFunction)Context_partition_to_string, METH_VARARGS, Context_partition_to_string_HELP},
+ {"create_disklabel", (PyCFunction)Context_create_disklabel, METH_VARARGS, Context_create_disklabel_HELP},
+ {"write_disklabel", (PyCFunction)Context_write_disklabel, METH_NOARGS, Context_write_disklabel_HELP},
{NULL}
};