[Libguestfs] [PATCH] umount-all: Use /proc/mounts instead of output of 'mount' command.

Richard W.M. Jones rjones at redhat.com
Tue Mar 22 11:22:30 UTC 2011


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://et.redhat.com/~rjones/virt-top
-------------- next part --------------
>From 490560439061398d47873484a55fb823be29b285 Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones at redhat.com>
Date: Tue, 22 Mar 2011 11:15:21 +0000
Subject: [PATCH 1/3] umount-all: Use /proc/mounts instead of output of 'mount' command.

The particular issue is that ntfs-3g (or FUSE?) no longer appears
to update /etc/mtab, which meant that umount-all was not unmounting
these partitions.  But parsing /proc/mounts is simpler and more
robust in any case.
---
 daemon/mount.c |   58 +++++++++++++++++++++++--------------------------------
 1 files changed, 24 insertions(+), 34 deletions(-)

diff --git a/daemon/mount.c b/daemon/mount.c
index a915b44..fd0f6b2 100644
--- a/daemon/mount.c
+++ b/daemon/mount.c
@@ -298,50 +298,40 @@ compare_longest_first (const void *vp1, const void *vp2)
 int
 do_umount_all (void)
 {
-  char *out, *err;
-  int i, r;
+  FILE *fp;
+  struct mntent *m;
   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);
+  char *err;
+  int i, r;
 
-  /* Lines have the format:
-   *   /dev/foo on /mountpoint type ...
+  /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
+   * that requires custom parsing code.
    */
-  snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
+  fp = setmntent ("/proc/mounts", "r");
+  if (fp == NULL) {
+    perror ("/proc/mounts");
+    exit (EXIT_FAILURE);
+  }
 
-  p = out;
-  while (p) {
-    pend = strchr (p, '\n');
-    if (pend) {
-      *pend = '\0';
-      pend++;
+  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;
+      }
     }
-
-    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);
+    /* 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;
       }
     }
-
-    p = pend;
   }
-  free (out);
+
+  endmntent (fp);
 
   qsort (mounts, size, sizeof (char *), compare_longest_first);
 
-- 
1.7.4.1



More information about the Libguestfs mailing list