[Pkg-libvirt-commits] [libguestfs] 139/165: rescue: Use guestfs_add_drive_scratch to implement the --scratch option.

Hilko Bengen bengen at moszumanska.debian.org
Sat Aug 30 08:25:23 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to branch experimental
in repository libguestfs.

commit c3c659e996b18a8b7ccdf8e2df673bbaa99aa7a7
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Mon Jun 23 21:38:33 2014 +0100

    rescue: Use guestfs_add_drive_scratch to implement the --scratch option.
---
 fish/options.c     | 16 +++++++++++++
 fish/options.h     |  8 +++++++
 rescue/Makefile.am |  1 +
 rescue/rescue.c    | 67 ++----------------------------------------------------
 4 files changed, 27 insertions(+), 65 deletions(-)

diff --git a/fish/options.c b/fish/options.c
index 5e6eb73..f7870a4 100644
--- a/fish/options.c
+++ b/fish/options.c
@@ -204,6 +204,17 @@ add_drives_handle (guestfs_h *g, struct drv *drv, char next_drive)
       break;
 #endif
 
+#if COMPILING_VIRT_RESCUE
+    case drv_scratch:
+      r = guestfs_add_drive_scratch (g, drv->scratch.size, -1);
+      if (r == -1)
+        exit (EXIT_FAILURE);
+
+      drv->nr_drives = 1;
+      next_drive++;
+      break;
+#endif
+
     default: /* keep GCC happy */
       abort ();
     }
@@ -306,6 +317,11 @@ free_drives (struct drv *drv)
     drv->N.data_free (drv->N.data);
     break;
 #endif
+#if COMPILING_VIRT_RESCUE
+  case drv_scratch:
+    /* nothing */
+    break;
+#endif
   default: ;                    /* keep GCC happy */
   }
   free (drv);
diff --git a/fish/options.h b/fish/options.h
index 7df20f5..ce092a0 100644
--- a/fish/options.h
+++ b/fish/options.h
@@ -55,6 +55,9 @@ struct drv {
 #if COMPILING_GUESTFISH
     drv_N,                      /* -N option (guestfish only) */
 #endif
+#if COMPILING_VIRT_RESCUE
+    drv_scratch,                /* --scratch option (virt-rescue only) */
+#endif
   } type;
   union {
     struct {
@@ -82,6 +85,11 @@ struct drv {
       void (*data_free)(void*); /* function to free 'data' */
     } N;
 #endif
+#if COMPILING_VIRT_RESCUE
+    struct {
+      int64_t size;         /* size of the disk in bytes */
+    } scratch;
+#endif
   };
 
   /* Opaque pointer.  Not used by the options-parsing code, and so
diff --git a/rescue/Makefile.am b/rescue/Makefile.am
index 6a5e175..505b327 100644
--- a/rescue/Makefile.am
+++ b/rescue/Makefile.am
@@ -41,6 +41,7 @@ virt_rescue_SOURCES = \
 	rescue.c
 
 virt_rescue_CPPFLAGS = \
+	-DCOMPILING_VIRT_RESCUE=1 \
 	-DGUESTFS_WARN_DEPRECATED=1 \
 	-DLOCALEBASEDIR=\""$(datadir)/locale"\" \
 	-I$(top_srcdir)/src -I$(top_builddir)/src \
diff --git a/rescue/rescue.c b/rescue/rescue.c
index d0c6207..dc56d4b 100644
--- a/rescue/rescue.c
+++ b/rescue/rescue.c
@@ -508,13 +508,6 @@ suggest_filesystems (void)
 #undef TEST_MOUNTABLE
 }
 
-struct scratch_disk {
-  struct scratch_disk *next;
-  char *filename;
-};
-static struct scratch_disk *scratch_disks = NULL;
-
-static void unlink_scratch_disks (void);
 static void add_scratch_disk (struct drv **drvs);
 
 static void
@@ -529,73 +522,17 @@ add_scratch_disks (int n, struct drv **drvs)
 static void
 add_scratch_disk (struct drv **drvs)
 {
-  char filename_s[] = "/var/tmp/rescueXXXXXX";
-  int fd;
-  char *filename;
-  struct scratch_disk *sd;
   struct drv *drv;
 
-  /* XXX Is there a reason we're not using guestfs_add_drive_scratch here? */
-
-  /* Create a temporary file, raw sparse format. */
-  fd = mkstemp (filename_s);
-  if (fd == -1) {
-    perror ("mkstemp: scratch disk");
-    exit (EXIT_FAILURE);
-  }
-  if (ftruncate (fd, 10737418240ULL) == -1) {
-    perror ("ftruncate: scratch disk");
-    exit (EXIT_FAILURE);
-  }
-  if (close (fd) == -1) {
-    perror ("close: scratch disk");
-    exit (EXIT_FAILURE);
-  }
-
-  filename = strdup (filename_s);
-  if (filename == NULL) {
-    perror ("malloc");
-    exit (EXIT_FAILURE);
-  }
-
-  /* Remember this scratch disk, so we can clean it up at exit. */
-  if (scratch_disks == NULL)
-    atexit (unlink_scratch_disks);
-  sd = malloc (sizeof (struct scratch_disk));
-  if (!sd) {
-    perror ("malloc");
-    exit (EXIT_FAILURE);
-  }
-  sd->filename = filename;
-  sd->next = scratch_disks;
-  scratch_disks = sd;
-
   /* Add the scratch disk to the drives list. */
   drv = calloc (1, sizeof (struct drv));
   if (!drv) {
     perror ("malloc");
     exit (EXIT_FAILURE);
   }
-  drv->type = drv_a;
+  drv->type = drv_scratch;
   drv->nr_drives = -1;
-  drv->a.filename = strdup (filename);
-  if (!drv->a.filename) {
-    perror ("strdup");
-    exit (EXIT_FAILURE);
-  }
-  drv->a.format = "raw";
-  drv->a.cachemode = "unsafe"; /* because it's a scratch disk */
+  drv->scratch.size = INT64_C (10737418240);
   drv->next = *drvs;
   *drvs = drv;
 }
-
-/* Called atexit to unlink the scratch disks. */
-static void
-unlink_scratch_disks (void)
-{
-  while (scratch_disks) {
-    unlink (scratch_disks->filename);
-    free (scratch_disks->filename);
-    scratch_disks = scratch_disks->next;
-  }
-}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list