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