/* The "help" command. This used to just list all commands, but * that's not very useful. Instead display some useful * context-sensitive help. This could be improved if we knew how many * drives had been added already, and whether anything was mounted. */ void display_help (void) { if (guestfs_is_config (g)) printf (_( "Add disk images to examine using the -a or -d options, or the 'add' command.\n" "Or create a new disk image using -N, or the 'alloc' or 'sparse' commands.\n" "Once you have done this, use the 'run' command.\n" )); else printf (_( "Find out what filesystems are available using 'list-filesystems' and then\n" "mount them to examine or modify the contents using 'mount-ro' or\n" "'mount'.\n" )); printf ("\n"); printf (_( "For more information about a command, use 'help cmd'.\n" "\n" "To read the manual, type 'man'.\n" )); printf ("\n"); }
/* This is the underlying allocation function. It's called from * a few other places in guestfish. */ int alloc_disk (const char *filename, const char *size_str, int add, int sparse) { off_t size; int fd; char c = 0; if (parse_size (size_str, &size) == -1) return -1; if (!guestfs_is_config (g)) { fprintf (stderr, _("can't allocate or add disks after launching\n")); return -1; } fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666); if (fd == -1) { perror (filename); return -1; } if (!sparse) { /* Not sparse */ #ifdef HAVE_POSIX_FALLOCATE int err = posix_fallocate (fd, 0, size); if (err != 0) { errno = err; perror ("fallocate"); close (fd); unlink (filename); return -1; } #else /* Slow emulation of posix_fallocate on platforms which don't have it. */ char buffer[BUFSIZ]; memset (buffer, 0, sizeof buffer); size_t remaining = size; while (remaining > 0) { size_t n = remaining > sizeof buffer ? sizeof buffer : remaining; ssize_t r = write (fd, buffer, n); if (r == -1) { perror ("write"); close (fd); unlink (filename); return -1; } remaining -= r; } #endif } else { /* Sparse */ if (lseek (fd, size-1, SEEK_SET) == (off_t) -1) { perror ("lseek"); close (fd); unlink (filename); return -1; } if (write (fd, &c, 1) != 1) { perror ("write"); close (fd); unlink (filename); return -1; } } if (close (fd) == -1) { perror (filename); unlink (filename); return -1; } if (add) { if (guestfs_add_drive_opts (g, filename, GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", -1) == -1) { unlink (filename); return -1; } } return 0; }