+ if (add_string (&ret, &size, &alloc, NULL) == -1)
+ return NULL;
+
+ /* Convert /dev/mapper LV paths into canonical paths (RHBZ#646432). */
+ for (i = 0; ret[i] != NULL; i += mp ? 2 : 1) {
+ if (STRPREFIX (ret[i], "/dev/mapper/") || STRPREFIX (ret[i], "/dev/dm-")) {
+ char *canonical;
+ r = lv_canonical (ret[i], &canonical);
+ if (r == -1) {
+ free_strings (ret);
+ return NULL;
+ }
+ if (r == 1) {
+ free (ret[i]);
+ ret[i] = canonical;
+ }
+ /* Ignore the case where r == 0. This might happen where
+ * eg. a LUKS /dev/mapper device is mounted, but that won't
+ * correspond to any LV.
+ */
+ }
+ }
+
+ return ret;
+}
+
+char **
+do_mounts (void)
+{
+ return mounts_or_mountpoints (0);
+}
+
+char **
+do_mountpoints (void)
+{
+ return mounts_or_mountpoints (1);
+}
+
+/* Unmount everything mounted under /sysroot.
+ *
+ * We have to unmount in the correct order, so we sort the paths by
+ * longest first to ensure that child paths are unmounted by parent
+ * paths.
+ *
+ * This call is more important than it appears at first, because it
+ * is widely used by both test and production code in order to
+ * get back to a known state (nothing mounted, everything synchronized).
+ */
+static int
+compare_longest_first (const void *vp1, const void *vp2)
+{
+ char * const *p1 = (char * const *) vp1;
+ char * const *p2 = (char * const *) vp2;
+ int n1 = strlen (*p1);
+ int n2 = strlen (*p2);
+ return n2 - n1;
+}
+
+int
+do_umount_all (void)
+{
+ char *out, *err;
+ int i, r;
+ char **mounts = NULL;
+ int size = 0, alloc = 0;
+ char *p, *p2, *p3, *pend;
+ char matching[5 + sysroot_len];
+
+ r = command (&out, &err, "mount", NULL);
+ if (r == -1) {
+ reply_with_error ("mount: %s", err);
+ free (out);
+ free (err);
+ return -1;
+ }
+
+ free (err);
+
+ /* Lines have the format:
+ * /dev/foo on /mountpoint type ...
+ */
+ snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
+
+ p = out;
+ while (p) {
+ pend = strchr (p, '\n');
+ if (pend) {
+ *pend = '\0';
+ pend++;
+ }
+
+ p2 = strstr (p, matching);
+ if (p2 != NULL) {
+ p2 += 4;
+ p3 = p2 + strcspn (p2, " ");
+ *p3 = '\0';
+ if (add_string (&mounts, &size, &alloc, p2) == -1) {
+ free (out);
+ return -1;
+ }
+ }
+
+ p = pend;
+ }
+ free (out);
+
+ qsort (mounts, size, sizeof (char *), compare_longest_first);
+
+ /* Unmount them. */
+ for (i = 0; i < size; ++i) {
+ r = command (NULL, &err, "umount", mounts[i], NULL);
+ if (r == -1) {
+ reply_with_error ("umount: %s: %s", mounts[i], err);
+ free (err);
+ free_stringslen (mounts, size);
+ return -1;
+ }
+ free (err);
+ }
+
+ free_stringslen (mounts, size);
+
+ return 0;
+}
+
+/* Mount using the loopback device. You can't use the generic
+ * do_mount call for this because the first parameter isn't a
+ * device.
+ */
+int
+do_mount_loop (const char *file, const char *mountpoint)
+{
+ int r;
+ char *buf, *mp;
+ char *error;
+
+ /* We have to prefix /sysroot on both the filename and the mountpoint. */
+ mp = sysroot_path (mountpoint);