Commits

Michał Górny  committed 0c34fcd

Add a separate method for R/W mounting.

  • Participants
  • Parent commits b64059f

Comments (0)

Files changed (1)

File pymountboot.cxx

 static void BootMountpoint_dealloc(PyObject *o);
 static PyObject *BootMountpoint_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
 static PyObject *BootMountpoint_mount(PyObject *args, PyObject *kwds);
+static PyObject *BootMountpoint_rwmount(PyObject *args, PyObject *kwds);
 static PyObject *BootMountpoint_umount(PyObject *args, PyObject *kwds);
 
 static PyMethodDef BootMountpoint_methods[] = {
 	{ "mount", BootMountpoint_mount, METH_NOARGS,
-		"Mount (or remount r/w) /boot if necessary" },
+		"Mount /boot (r/o) if necessary" },
+	{ "rwmount", BootMountpoint_rwmount, METH_NOARGS,
+		"Mount or remount /boot r/w if necessary" },
 	{ "umount", BootMountpoint_umount, METH_NOARGS,
 		"Unmount (or remount r/o) previously mounted /boot" },
 	{ NULL }
 
 			/* try to mount only if in fstab */
 			if (mnt_table_find_target(fstab, "/boot", MNT_ITER_FORWARD)) {
+				if (mnt_context_set_options(ctx, "ro"))
+					throw std::runtime_error("unable to set mount options (to 'ro')");
+				if (mnt_context_mount(ctx))
+					throw std::runtime_error("mount failed");
+				self->status = MOUNTPOINT_MOUNTED;
+			}
+		}
+
+		return Py_None;
+	} catch (std::runtime_error err) {
+		PyErr_SetString(PyExc_RuntimeError, err.what());
+		return NULL;
+	} catch (std::exception e) {
+		PyErr_SetString(PyExc_Exception, e.what());
+		return NULL;
+	}
+}
+
+static PyObject *BootMountpoint_rwmount(PyObject *args, PyObject *kwds) {
+	try {
+		BootMountpoint *self = reinterpret_cast<BootMountpoint*>(args);
+		struct libmnt_context* const ctx = self->mnt_context;
+
+		struct libmnt_table *mtab;
+		if (mnt_context_get_mtab(ctx, &mtab))
+			throw std::runtime_error("unable to get mtab");
+
+		/* backward -> find the top-most mount */
+		struct libmnt_fs *fs = mnt_table_find_target(mtab, "/boot",
+				MNT_ITER_BACKWARD);
+
+		if (!fs) { /* boot not mounted, mount it? */
+			struct libmnt_table *fstab;
+			if (mnt_context_get_fstab(ctx, &fstab))
+				throw std::runtime_error("unable to get fstab");
+
+			/* try to mount only if in fstab */
+			if (mnt_table_find_target(fstab, "/boot", MNT_ITER_FORWARD)) {
 				if (mnt_context_set_options(ctx, "rw"))
 					throw std::runtime_error("unable to set mount options (to 'rw')");
 				if (mnt_context_mount(ctx))
 				throw std::runtime_error("unable to set mount options (to 'remount,rw')");
 			if (mnt_context_mount(ctx))
 				throw std::runtime_error("remount r/w failed");
-			self->status = MOUNTPOINT_REMOUNTED_RW;
+			if (self->status != MOUNTPOINT_MOUNTED)
+				self->status = MOUNTPOINT_REMOUNTED_RW;
 		}
 
 		return Py_None;
 	}
 }
 
+
 static PyObject *BootMountpoint_umount(PyObject *args, PyObject *kwds) {
 	try {
 		BootMountpoint *self = reinterpret_cast<BootMountpoint*>(args);