qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Anthony Liguori <aliguori@us.ibm.com>
To: qemu-devel@nongnu.org
Cc: kvm-devel@lists.sourceforge.net,
	Anthony Liguori <aliguori@us.ibm.com>,
	Paul Brook <paul@codesourcery.com>
Subject: [Qemu-devel] [PATCH] Add support for a configuration file
Date: Tue, 13 May 2008 16:19:05 -0500	[thread overview]
Message-ID: <1210713545-11916-1-git-send-email-aliguori@us.ibm.com> (raw)

There has been an awful lot of discussion about a configuration file with
almost no general agreement except that one is strongly desired.

I thought about it a bit, and I think a nice step would be to simply allow
the current configuration parameters to be stored in a file using a pretty
familiar format.

I think this is pretty useful as-is.  I think it also gives us a reasonable
way to move forward that will keep everyone pretty happy.

Here's a short example:

qemu-system-x86_64 -hda ~/images/linux.img -snapshot -vnc :2

Would become `foo.qemu':

 # Main disk image
 hda=/home/anthony/images/linux.img

 # Redirect disk writes to a temporary image
 snapshot

 # Make the graphical display available on port 5902
 vnc=:2

With:

qemu-system-x86_64 -config foo.qemu

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

diff --git a/qemu-doc.texi b/qemu-doc.texi
index cca483c..4861fc0 100644
--- a/qemu-doc.texi
+++ b/qemu-doc.texi
@@ -395,6 +395,12 @@ Sets the @var{name} of the guest.
 This name will be display in the SDL window caption.
 The @var{name} will also be used for the VNC server.
 
+@item -config @var{file}
+Reads configuration options from @var{file}.  The format of @var{file} is the
+same as the command line options, except no dash ``-'' is required.  Options
+that take an argument are in the format @var{option=value}.  A pound ``#''
+character can be used as a comment.
+
 @end table
 
 Display options:
diff --git a/vl.c b/vl.c
index 67712f0..2eb39dd 100644
--- a/vl.c
+++ b/vl.c
@@ -7276,6 +7276,7 @@ static void help(int exitcode)
            "-clock          force the use of the given methods for timer alarm.\n"
            "                To see what timers are available use -clock ?\n"
            "-startdate      select initial date of the clock\n"
+	   "-config FILE    read command line options from FILE\n"
            "\n"
            "During emulation, the following keys are useful:\n"
            "ctrl-alt-f      toggle full screen\n"
@@ -7379,6 +7380,7 @@ enum {
     QEMU_OPTION_old_param,
     QEMU_OPTION_clock,
     QEMU_OPTION_startdate,
+    QEMU_OPTION_config,
 };
 
 typedef struct QEMUOption {
@@ -7490,6 +7492,7 @@ const QEMUOption qemu_options[] = {
 #endif
     { "clock", HAS_ARG, QEMU_OPTION_clock },
     { "startdate", HAS_ARG, QEMU_OPTION_startdate },
+    { "config", HAS_ARG, QEMU_OPTION_config },
     { NULL },
 };
 
@@ -7665,9 +7668,106 @@ static BOOL WINAPI qemu_ctrl_handler(DWORD type)
 }
 #endif
 
+static char **insert_opts(char **old_argv, int *old_argc, int index,
+			  char **argv, int argc)
+{
+    char **new_argv;
+
+    /* Allocate larger array */
+    new_argv = realloc(old_argv, (*old_argc + argc) * sizeof(old_argv[0]));
+    if (new_argv == NULL) {
+	fprintf(stderr, "allocate failed in insert_opts\n");
+	exit(1);
+    }
+
+    /* move elements after insertion point to end of array */
+    memmove(new_argv+index + argc, new_argv + index,
+	    (*old_argc - index) * sizeof(argv[0]));
+
+    /* copy in new elements */
+    memcpy(new_argv + index, argv, argc * sizeof(argv[0]));
+
+    *old_argc += argc;
+
+    if (0) { /* for debugging */
+	int i;
+	printf("argv[] = {");
+	for (i = 0; i < *old_argc; i++) {
+	    if (i)
+		printf(", ");
+	    printf("\"%s\"", new_argv[i]);
+	}
+	printf("}\n");
+    }
+
+    return new_argv;
+}
+
+static char **parse_config_file(const char *file, int *pargc)
+{
+    FILE *f;
+    char buffer[4096];
+    char **argv = NULL;
+    int argc = 0;
+
+    f = fopen(file, "r");
+    if (f == NULL)
+	return NULL;
+
+    while (fgets(buffer, sizeof(buffer), f)) {
+	char *ptr = buffer;
+	char *tok, *key, *val;
+	char *targv[2];
+	int targc = 0;
+
+	/* skip whitespace */
+	while (isspace(*ptr)) ptr++;
+
+	/* skip comments or empty lines */
+	if (*ptr == '#' || *ptr == 0)
+	    continue;
+
+	/* trim new line and carriage return if necessary */
+	tok = strchr(ptr, '\n');
+	if (tok)
+	    *tok = 0;
+	tok = strchr(ptr, '\r');
+	if (tok)
+	    *tok = 0;
+
+	/* check if it has an argument */
+	tok = strchr(ptr, '=');
+	if (tok)
+	    *tok = 0;
+
+	/* add key */
+	if (asprintf(&key, "--%s", ptr) == -1)
+	    return NULL;
+	targv[targc++] = key;
+
+	/* add argument (optionally) */
+	if (tok) {
+	    if (asprintf(&val, "%s", tok + 1) == -1)
+		return NULL;
+	    targv[targc++] = val;
+	}
+
+	/* insert new arguments */
+	argv = insert_opts(argv, &argc, argc, targv, targc);
+	if (argv == NULL)
+	    return NULL;
+    }
+
+    fclose(f);
+
+    *pargc = argc;
+
+    return argv;
+}
+
 #define MAX_NET_CLIENTS 32
 
-int main(int argc, char **argv)
+int main(int orig_argc, char **orig_argv)
 {
 #ifdef CONFIG_GDBSTUB
     int use_gdbstub;
@@ -7700,6 +7800,10 @@ int main(int argc, char **argv)
     int fds[2];
     const char *pid_file = NULL;
     VLANState *vlan;
+    char **argv = NULL;
+    int argc = 0;
+
+    argv = insert_opts(argv, &argc, 0, orig_argv, orig_argc);
 
     LIST_INIT (&vm_change_state_head);
 #ifndef _WIN32
@@ -8297,6 +8401,20 @@ int main(int argc, char **argv)
                     }
                 }
                 break;
+	    case QEMU_OPTION_config: {
+		char **config_argv;
+		int config_argc;
+
+		config_argv = parse_config_file(optarg, &config_argc);
+		if (config_argv == NULL) {
+		    fprintf(stderr, "failed to parse config file `%s'\n", optarg);
+		    exit(1);
+		}
+
+		argv = insert_opts(argv, &argc, optind,
+				   config_argv, config_argc);
+		free(config_argv);
+		} break;
             }
         }
     }

             reply	other threads:[~2008-05-13 21:19 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-13 21:19 Anthony Liguori [this message]
2008-05-13 23:07 ` [Qemu-devel] Re: [PATCH] Add support for a configuration file Anthony Liguori
2008-05-13 23:20   ` [Qemu-devel] Re: [kvm-devel] " Daniel P. Berrange
2008-05-14  6:35     ` Colin Adams
2008-05-14 14:41     ` Avi Kivity
2008-05-14 14:52       ` Dor Laor
2008-05-14 15:02         ` Daniel P. Berrange
2008-05-14 15:18         ` [kvm-devel] [Qemu-devel] " Anthony Liguori
2008-05-14 14:59       ` [Qemu-devel] Re: [kvm-devel] " Anthony Liguori
2008-05-14 15:10       ` Andreas Färber
2008-05-15 14:50         ` Ian Jackson
2008-05-14  8:27   ` [Qemu-devel] " Fabrice Bellard
2008-05-14 10:31     ` Avi Kivity
2008-05-14 12:26       ` Fabrice Bellard
2008-05-14 13:31         ` [kvm-devel] " Daniel P. Berrange
2008-05-15  8:04           ` Avi Kivity
2008-05-15 11:52             ` Daniel P. Berrange
2008-05-15 12:04               ` Avi Kivity
2008-05-15 12:16                 ` Andreas Färber
2008-05-15 12:20                 ` Laurent Vivier
2008-05-14 14:06         ` Anthony Liguori
2008-05-14 14:26         ` Paul Brook
2008-05-14 14:45           ` [kvm-devel] " Javier Guerra
2008-05-14 15:13             ` Johannes Schindelin
2008-05-14 15:30               ` Javier Guerra
2008-05-14 15:37                 ` Johannes Schindelin
2008-05-14 15:42                   ` Javier Guerra
2008-05-14 16:07                   ` Kelly French
2008-05-15 14:59                     ` Paul Brook
2008-05-14 16:01                 ` Andreas Färber
2008-05-14 16:21                   ` Daniel P. Berrange
2008-05-14 16:37                     ` Andreas Färber
2008-05-14 16:29               ` Fabrice Bellard
2008-05-17 21:49                 ` Luca Barbato
2008-05-14 15:22             ` Daniel P. Berrange
2008-05-14 15:09           ` Anthony Liguori
2008-05-14 15:18             ` Paul Brook
2008-05-14 16:25           ` Fabrice Bellard
2008-05-14 14:36   ` andrzej zaborowski
2008-05-14 15:10     ` Johannes Schindelin
2008-05-15 14:58     ` Ian Jackson
2008-05-15 12:03 ` [Qemu-devel] " Erik de Castro Lopo
2008-05-15 12:36   ` andrzej zaborowski
2008-05-15 13:22     ` Daniel P. Berrange
2008-05-15 14:46       ` andrzej zaborowski
2008-05-15 15:05   ` Anthony Liguori
2008-05-15 15:26     ` Avi Kivity
2008-05-15 18:50       ` Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1210713545-11916-1-git-send-email-aliguori@us.ibm.com \
    --to=aliguori@us.ibm.com \
    --cc=kvm-devel@lists.sourceforge.net \
    --cc=paul@codesourcery.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).