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)
{
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
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) */
"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"
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...