+ /* 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)
+{
+ FILE *fp;
+ struct mntent *m;
+ char **mounts = NULL;
+ int size = 0, alloc = 0;
+ char *err;
+ int i, r;
+
+ /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
+ * that requires custom parsing code.
+ */
+ fp = setmntent ("/proc/mounts", "r");
+ if (fp == NULL) {
+ perror ("/proc/mounts");
+ exit (EXIT_FAILURE);
+ }
+
+ while ((m = getmntent (fp)) != NULL) {
+ /* Allow a mount directory like "/sysroot". */
+ if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
+ if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) {
+ endmntent (fp);
+ return -1;
+ }
+ }
+ /* Or allow a mount directory like "/sysroot/...". */
+ if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') {
+ if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) {
+ endmntent (fp);
+ return -1;
+ }
+ }
+ }
+
+ endmntent (fp);
+
+ 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);