diff options
-rw-r--r-- | fish/alloc.c | 54 | ||||
-rw-r--r-- | fish/fish.c | 28 | ||||
-rw-r--r-- | fish/fish.h | 1 | ||||
-rw-r--r-- | guestfish.pod | 37 |
4 files changed, 120 insertions, 0 deletions
diff --git a/fish/alloc.c b/fish/alloc.c index a7d115a9..e35c02f7 100644 --- a/fish/alloc.c +++ b/fish/alloc.c @@ -75,6 +75,60 @@ do_alloc (const char *cmd, int argc, char *argv[]) return 0; } +int +do_sparse (const char *cmd, int argc, char *argv[]) +{ + off_t size; + int fd; + char c = 0; + + if (argc != 2) { + fprintf (stderr, _("use 'sparse file size' to create a sparse image\n")); + return -1; + } + + if (parse_size (argv[1], &size) == -1) + return -1; + + if (!guestfs_is_config (g)) { + fprintf (stderr, _("can't allocate or add disks after launching\n")); + return -1; + } + + fd = open (argv[0], O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666); + if (fd == -1) { + perror (argv[0]); + return -1; + } + + if (lseek (fd, size-1, SEEK_SET) == (off_t) -1) { + perror ("lseek"); + close (fd); + unlink (argv[0]); + return -1; + } + + if (write (fd, &c, 1) != 1) { + perror ("write"); + close (fd); + unlink (argv[0]); + return -1; + } + + if (close (fd) == -1) { + perror (argv[0]); + unlink (argv[0]); + return -1; + } + + if (guestfs_add_drive (g, argv[0]) == -1) { + unlink (argv[0]); + return -1; + } + + return 0; +} + static int parse_size (const char *str, off_t *size_rtn) { diff --git a/fish/fish.c b/fish/fish.c index f6996032..2dfdfb92 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -874,6 +874,8 @@ issue_command (const char *cmd, char *argv[], const char *pipecmd) r = do_more (cmd, argc, argv); else if (strcasecmp (cmd, "reopen") == 0) r = do_reopen (cmd, argc, argv); + else if (strcasecmp (cmd, "sparse") == 0) + r = do_sparse (cmd, argc, argv); else if (strcasecmp (cmd, "time") == 0) r = do_time (cmd, argc, argv); else @@ -927,6 +929,8 @@ list_builtin_commands (void) printf ("%-20s %s\n", "reopen", _("close and reopen libguestfs handle")); printf ("%-20s %s\n", + "sparse", _("allocate a sparse image file")); + printf ("%-20s %s\n", "time", _("measure time taken to run command")); /* actions are printed after this (see list_commands) */ @@ -1020,6 +1024,30 @@ display_builtin_command (const char *cmd) "Close and reopen the libguestfs handle. It is not necessary to use\n" "this normally, because the handle is closed properly when guestfish\n" "exits. However this is occasionally useful for testing.\n")); + else if (strcasecmp (cmd, "sparse") == 0) + printf (_("sparse - allocate a sparse image file\n" + " sparse <filename> <size>\n" + "\n" + " This creates an empty sparse file of the given size,\n" + " and then adds so it can be further examined.\n" + "\n" + " In all respects it works the same as the 'alloc'\n" + " command, except that the image file is allocated\n" + " sparsely, which means that disk blocks are not assigned\n" + " to the file until they are needed. Sparse disk files\n" + " only use space when written to, but they are slower\n" + " and there is a danger you could run out of real disk\n" + " space during a write operation.\n" + "\n" + " For more advanced image creation, see qemu-img utility.\n" + "\n" + " Size can be specified (where <nn> means a number):\n" + " <nn> number of kilobytes\n" + " eg: 1440 standard 3.5\" floppy\n" + " <nn>K or <nn>KB number of kilobytes\n" + " <nn>M or <nn>MB number of megabytes\n" + " <nn>G or <nn>GB number of gigabytes\n" + " <nn>sects number of 512 byte sectors\n")); else if (strcasecmp (cmd, "time") == 0) printf (_("time - measure time taken to run command\n" " time <command> [<args> ...]\n" diff --git a/fish/fish.h b/fish/fish.h index 8c5dba16..cdb3d634 100644 --- a/fish/fish.h +++ b/fish/fish.h @@ -62,6 +62,7 @@ extern char *complete_dest_paths_generator (const char *text, int state); /* in alloc.c */ extern int do_alloc (const char *cmd, int argc, char *argv[]); +extern int do_sparse (const char *cmd, int argc, char *argv[]); /* in echo.c */ extern int do_echo (const char *cmd, int argc, char *argv[]); diff --git a/guestfish.pod b/guestfish.pod index b6354192..7755cc62 100644 --- a/guestfish.pod +++ b/guestfish.pod @@ -531,6 +531,43 @@ Close and reopen the libguestfs handle. It is not necessary to use this normally, because the handle is closed properly when guestfish exits. However this is occasionally useful for testing. +=head2 sparse + + sparse filename size + +This creates an empty sparse file of the given size, and then adds +so it can be further examined. + +In all respects it works the same as the C<alloc> command, except that +the image file is allocated sparsely, which means that disk blocks are +not assigned to the file until they are needed. Sparse disk files +only use space when written to, but they are slower and there is a +danger you could run out of real disk space during a write operation. + +For more advanced image creation, see L<qemu-img(1)> utility. + +Size can be specified (where C<nn> means a number): + +=over 4 + +=item C<nn> or C<nn>K or C<nn>KB + +number of kilobytes, eg: C<1440> = standard 3.5in floppy + +=item C<nn>M or C<nn>MB + +number of megabytes + +=item C<nn>G or C<nn>GB + +number of gigabytes + +=item C<nn>sects + +number of 512 byte sectors + +=back + =head2 time time command args... |