From 46551d9c51193a4bca2e1b249b8c5f111e1dc7b5 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Wed, 24 Jun 2009 09:59:39 +0100 Subject: [PATCH] Implement guestfish -f option to allow guestfish scripts. New '-f' option allows scripts to be written using: #!/usr/bin/guestfish -f --- TODO | 4 ---- fish/fish.c | 23 +++++++++++++++++++++-- guestfish.pod | 18 ++++++++++++++++-- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/TODO b/TODO index b3860b1..233ac3c 100644 --- a/TODO +++ b/TODO @@ -115,7 +115,3 @@ Supermin appliance should be moved into febootstrap. ---------------------------------------------------------------------- guestfish -i (runs inspector) - ----------------------------------------------------------------------- - -guestfish -f diff --git a/fish/fish.c b/fish/fish.c index bf82b8a..cba0343 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -102,6 +102,7 @@ usage (void) " -h|--cmd-help cmd Display detailed help on 'cmd'\n" " -a|--add image Add image\n" " -D|--no-dest-paths Don't tab-complete paths from guest fs\n" + " -f|--file file Read commands from file\n" " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n" " -n|--no-sync Don't autosync\n" " -r|--ro Mount read-only\n" @@ -113,10 +114,11 @@ usage (void) int main (int argc, char *argv[]) { - static const char *options = "a:h::m:nrv?V"; + static const char *options = "a:f:h::m:nrv?V"; static struct option long_options[] = { { "add", 1, 0, 'a' }, { "cmd-help", 2, 0, 'h' }, + { "file", 1, 0, 'f' }, { "help", 0, 0, '?' }, { "mount", 1, 0, 'm' }, { "no-dest-paths", 0, 0, 'D' }, @@ -130,7 +132,7 @@ main (int argc, char *argv[]) struct drv *drv; struct mp *mps = NULL; struct mp *mp; - char *p; + char *p, *file = NULL; int c; initialize_readline (); @@ -183,6 +185,14 @@ main (int argc, char *argv[]) complete_dest_paths = 0; break; + case 'f': + if (file) { + fprintf (stderr, _("guestfish: only one -f parameter can be given\n")); + exit (1); + } + file = optarg; + break; + case 'h': if (optarg) display_command (optarg); @@ -246,6 +256,15 @@ main (int argc, char *argv[]) mount_mps (mps); } + /* -f (file) parameter? */ + if (file) { + close (0); + if (open (file, O_RDONLY) == -1) { + perror (file); + exit (1); + } + } + /* Interactive, shell script, or command(s) on the command line? */ if (optind >= argc) { if (isatty (0)) diff --git a/guestfish.pod b/guestfish.pod index d83a61c..2d39cbf 100644 --- a/guestfish.pod +++ b/guestfish.pod @@ -40,8 +40,7 @@ Remove C (in reality not such a great idea): guestfish --add disk.img \ --mount /dev/VolGroup00/LogVol00 \ --mount /dev/sda1:/boot \ - rm /boot/grub/menu.lst : \ - sync : exit + rm /boot/grub/menu.lst =head2 As an interactive shell @@ -55,6 +54,14 @@ Remove C (in reality not such a great idea): > help +=head2 As a script interpreter + + #!/usr/bin/guestfish -f + alloc /tmp/output.img 10M + run + sfdisk /dev/sda 0 0 0 , + mkfs ext2 /dev/sda1 + =head1 DESCRIPTION Guestfish is a shell and command-line tool for examining and modifying @@ -81,6 +88,13 @@ Displays detailed help on a single command C. Add a block device or virtual machine image to the shell. +=item B<-f file> | B<--file file> + +Read commands from C. To write pure guestfish +scripts, use: + + #!/usr/bin/guestfish -f + =item B<-m dev[:mountpoint]> | B<--mount dev[:mountpoint]> Mount the named partition or logical volume on the given mountpoint. -- 1.8.3.1