rescue: Use guestfs_add_drive_scratch to implement the --scratch option.

This commit is contained in:
Richard W.M. Jones
2014-06-23 21:38:33 +01:00
parent bb3ba0c43a
commit c3c659e996
4 changed files with 27 additions and 65 deletions

View File

@@ -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 ();
}
@@ -305,6 +316,11 @@ free_drives (struct drv *drv)
free (drv->N.filename);
drv->N.data_free (drv->N.data);
break;
#endif
#if COMPILING_VIRT_RESCUE
case drv_scratch:
/* nothing */
break;
#endif
default: ; /* keep GCC happy */
}

View File

@@ -54,6 +54,9 @@ struct drv {
drv_d, /* -d option */
#if COMPILING_GUESTFISH
drv_N, /* -N option (guestfish only) */
#endif
#if COMPILING_VIRT_RESCUE
drv_scratch, /* --scratch option (virt-rescue only) */
#endif
} type;
union {
@@ -81,6 +84,11 @@ struct drv {
void *data; /* prepared type */
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
};

View File

@@ -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 \

View File

@@ -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;
}
}