Skip to contentSkip to conversation

Introduce global config and default devices

PATCH In review Anthony Liguori 5 participants 12 messages Jan 21, 2010 – Jan 22, 2010

lore
Share & tools
lore
#0 lore raw

cover Introduce global config and default devices

This series introduces global config files stored in /etc/qemu. There is both a common config (qemu.conf) and a per-target config (target-<TARGET_NAME>.conf).

To demonstrate what can be done with global config, I've also made some enhancements to the default device code that allows many of the builtin qemu defaults to be overridden. For instance, the following config file would always disable the monitor on stdio and use virtio-net with tun/tap instead of slirp.

/etc/qemu/qemu.conf:

[default]
monitor = "stdio"

[net]
type = "nic"
default = "on"
model = "virtio"

[net]
type = "tap"
default = "on"

#1 lore raw

PATCH 1/4 Support --confdir in configure to specify path to configuration files

The default value is ${prefix}/etc/qemu. --confdir can be used to override the default to an absolute path. The expectation is that when installed to /usr,
--confdir=/etc/qemu will be used.

---
 configure |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
configure +10 -0
index 5631bbb..e86b27e 100755
@@ -32,6 +32,7 @@ cpu=""
 prefix=""
 interp_prefix="/usr/gnemul/qemu-%M"
 static="no"
+confdir=""
 sparc_cpu=""
 cross_prefix=""
 cc="gcc"
@@ -453,6 +454,8 @@ for opt do
   ;;
   --static) static="yes"
   ;;
+  --confdir) confdir="$optarg"
+  ;;
   --disable-sdl) sdl="no"
   ;;
   --enable-sdl) sdl="yes"
@@ -686,6 +689,7 @@ echo "  --extra-ldflags=LDFLAGS  append extra linker flags LDFLAGS"
 echo "  --make=MAKE              use specified make [$make]"
 echo "  --install=INSTALL        use specified install [$install]"
 echo "  --static                 enable static build [$static]"
+echo "  --confdir=PATH           install config in PATH"
 echo "  --enable-debug-tcg       enable TCG debugging"
 echo "  --disable-debug-tcg      disable TCG debugging (default)"
 echo "  --enable-debug           enable common debug build options"
@@ -1828,6 +1832,7 @@ if test "$mingw32" = "yes" ; then
   fi
   mansuffix=""
   datasuffix=""
+  confsuffix=""
   docsuffix=""
   binsuffix=""
 else
@@ -1838,6 +1843,9 @@ else
   datasuffix="/share/qemu"
   docsuffix="/share/doc/qemu"
   binsuffix="/bin"
+  if test -z "$confdir" ; then
+      confdir="${prefix}/etc/qemu"
+  fi
 fi
 
 echo "Install prefix    $prefix"
@@ -1914,6 +1922,7 @@ printf " '%s'" "$0" "$@" >> $config_host_mak
 echo >> $config_host_mak
 
 echo "CONFIG_QEMU_SHAREDIR=\"$prefix$datasuffix\"" >> $config_host_mak
+echo "CONFIG_QEMU_CONFDIR=\"$confdir\"" >> $config_host_mak
 
 case "$cpu" in
   i386|x86_64|alpha|cris|hppa|ia64|m68k|microblaze|mips|mips64|ppc|ppc64|s390|s390x|sparc|sparc64)
@@ -2159,6 +2168,7 @@ echo "prefix=$prefix" >> $config_host_mak
 echo "bindir=\${prefix}$binsuffix" >> $config_host_mak
 echo "mandir=\${prefix}$mansuffix" >> $config_host_mak
 echo "datadir=\${prefix}$datasuffix" >> $config_host_mak
+echo "confdir=$confdir" >> $config_host_mak
 echo "docdir=\${prefix}$docsuffix" >> $config_host_mak
 echo "MAKE=$make" >> $config_host_mak
 echo "INSTALL=$install" >> $config_host_mak
Signature · 1 line
1.6.5.2
#2 lore raw

[Qemu-devel] Re: [PATCH 1/4] Support --confdir in configure to specify path to configuration files

Quoted 3 lines · Anthony Liguori

--confdir can be used to override the
default to an absolute path. The expectation is that when installed to /usr,
--confdir=/etc/qemu will be used.

Why not --sysconfdir? There are already several differences between Autoconf options and QEMU configure options, however unlike the others this one would be used by basically all distros.

Paolo

#3 lore raw

[Qemu-devel] Re: [PATCH 1/4] Support --confdir in configure to specify path to configuration files

Quoted 6 lines · Paolo Bonzini
Quoted 3 lines · Anthony Liguori

--confdir can be used to override the
default to an absolute path. The expectation is that when installed to /usr,
--confdir=/etc/qemu will be used.

Why not --sysconfdir? There are already several differences between Autoconf options and QEMU configure options, however unlike the others this one would be used by basically all distros.

Wasn't 100% sure on the proper naming. sysconfdir works for me.

Regards,

Anthony Liguori

Quoted 1 line

Paolo

#4 lore raw

PATCH 2/4 Load global config files by default

In reply to Anthony Liguori

A new option, -nodefconfig is introduced to prevent loading from the default config location. Otherwise, two configuration files will be searched for, qemu.conf and target-<TARGET_NAME>.conf.

The ordering is a little troubling. Command line options are parsed before loading the default configs which means that the command line configs will be loaded before the default config. The effect is that the default config will override -readconfig directives.

It's unclear the best way to handle this.

---
 qemu-options.hx |    7 +++++++
 vl.c            |   23 +++++++++++++++++++++++
 2 files changed, 30 insertions(+), 0 deletions(-)
qemu-options.hx +7 -0
index ee60d8a..57f453d 100644
@@ -1965,6 +1965,13 @@ STEXI
 @item -writeconfig @var{file}
 Write device configuration to @var{file}.
 ETEXI
+DEF("nodefconfig", 0, QEMU_OPTION_nodefconfig,
+    "-nodefconfig\n"
+    "                do not load default config file\n")
+STEXI
+@item -nodefconfig
+Do not load default config files.
+ETEXI
 
 HXCOMM This is the last statement. Insert new options before this line!
 STEXI
vl.c +23 -0
index e070ec9..cf12ab0 100644
@@ -4690,6 +4690,7 @@ int main(int argc, char **argv, char **envp)
 #endif
     CPUState *env;
     int show_vnc_port = 0;
+    int defconfig = 1;
 
     init_clocks();
 
@@ -5457,6 +5458,9 @@ int main(int argc, char **argv, char **envp)
                     fclose(fp);
                     break;
                 }
+            case QEMU_OPTION_nodefconfig:
+                defconfig=0;
+                break;
             }
         }
     }
@@ -5471,6 +5475,25 @@ int main(int argc, char **argv, char **envp)
         data_dir = CONFIG_QEMU_SHAREDIR;
     }
 
+    if (defconfig) {
+        FILE *fp;
+        fp = fopen(CONFIG_QEMU_CONFDIR "/qemu.conf", "r");
+        if (fp) {
+            if (qemu_config_parse(fp) != 0) {
+                exit(1);
+            }
+            fclose(fp);
+        }
+
+        fp = fopen(CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", "r");
+        if (fp) {
+            if (qemu_config_parse(fp) != 0) {
+                exit(1);
+            }
+            fclose(fp);
+        }
+    }
+
     /*
      * Default to max_cpus = smp_cpus, in case the user doesn't
      * specify a max_cpus value.
Signature · 1 line
1.6.5.2
#5 lore raw

[Qemu-devel] Re: [PATCH 2/4] Load global config files by default

Quoted 6 lines · Anthony Liguori

The ordering is a little troubling. Command line options are parsed before loading the default configs which means that the command line configs will be loaded before the default config. The effect is that the default config will override -readconfig directives.

It's unclear the best way to handle this.

Maybe we need two passes over the command line options?

-confdir and -nodefconfig switches would be handled in the first pass, then read the global config files (unless disabled), then the second pass.

I think we could also make the default device handling less messy that way by checking -nographic and -nodefaults in the first pass too. Having the machine type (-M) read in the first pass could allow some cleanups too.

cheers,
Gerd

#6 lore raw

PATCH 3/4 Add -defaults option to allow default devices to be overridden

In reply to Anthony Liguori

This option can be used to toggle whether each default device is enabled or disabled. For character devices, the default backend can also be overridden.

For devices, we'll have to take a different approach to changing the defaults which will be covered in the next patch.

N.B. I took special care with -nographic. Now -nographic pretty clearly acts as a mechanism to override the default backend devices.

---
 qemu-config.c   |   45 +++++++++++++++++++++++++++++++++
 qemu-config.h   |    1 +
 qemu-options.hx |    7 +++++
 vl.c            |   75 +++++++++++++++++++++++++++++++++++++++++--------------
 4 files changed, 109 insertions(+), 19 deletions(-)
qemu-config.c +45 -0
index c3203c8..82ca399 100644
@@ -242,6 +242,50 @@ QemuOptsList qemu_mon_opts = {
     },
 };
 
+QemuOptsList qemu_default_opts = {
+    .name = "default",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_default_opts.head),
+    .desc = {
+        {
+            .name = "serial",
+            .type = QEMU_OPT_STRING,
+        },
+        {
+            .name = "parallel",
+            .type = QEMU_OPT_STRING,
+        },
+        {
+            .name = "virtcon",
+            .type = QEMU_OPT_STRING,
+        },
+        {
+            .name = "monitor",
+            .type = QEMU_OPT_STRING,
+        },
+        {
+            .name = "vga",
+            .type = QEMU_OPT_BOOL,
+        },
+        {
+            .name = "net",
+            .type = QEMU_OPT_BOOL,
+        },
+        {
+            .name = "floppy",
+            .type = QEMU_OPT_BOOL,
+        },
+        {
+            .name = "cdrom",
+            .type = QEMU_OPT_BOOL,
+        },
+        {
+            .name = "sdcard",
+            .type = QEMU_OPT_BOOL,
+        },
+        { /* end if list */ }
+    },
+};
+
 static QemuOptsList *lists[] = {
     &qemu_drive_opts,
     &qemu_chardev_opts,
@@ -250,6 +294,7 @@ static QemuOptsList *lists[] = {
     &qemu_net_opts,
     &qemu_rtc_opts,
     &qemu_global_opts,
+    &qemu_default_opts,
     &qemu_mon_opts,
     NULL,
 };
qemu-config.h +1 -0
index dd89ae4..14ed67b 100644
@@ -9,6 +9,7 @@ extern QemuOptsList qemu_net_opts;
 extern QemuOptsList qemu_rtc_opts;
 extern QemuOptsList qemu_global_opts;
 extern QemuOptsList qemu_mon_opts;
+extern QemuOptsList qemu_default_opts;
 
 int qemu_set_option(const char *str);
 int qemu_global_option(const char *str);
qemu-options.hx +7 -0
index 57f453d..e81ecb5 100644
@@ -1919,6 +1919,13 @@ STEXI
 Don't create default devices.
 ETEXI
 
+DEF("default", HAS_ARG, QEMU_OPTION_default, \
+    "-default arg    specify default devices\n")
+STEXI
+@item -defaults
+Override builtin default devices
+ETEXI
+
 #ifndef _WIN32
 DEF("chroot", HAS_ARG, QEMU_OPTION_chroot, \
     "-chroot dir     chroot to dir just before starting the VM\n")
vl.c +56 -19

110 lines not on this page · raw message

Signature · 1 line
1.6.5.2
#7 lore raw

Re: [Qemu-devel] [PATCH 3/4] Add -defaults option to allow default devices to be overridden

Quoted 33 lines · Anthony Liguori

This option can be used to toggle whether each default device is enabled or disabled. For character devices, the default backend can also be overridden.

For devices, we'll have to take a different approach to changing the defaults which will be covered in the next patch.

N.B. I took special care with -nographic. Now -nographic pretty clearly acts as a mechanism to override the default backend devices.

---
 qemu-config.c   |   45 +++++++++++++++++++++++++++++++++
 qemu-config.h   |    1 +
 qemu-options.hx |    7 +++++
 vl.c            |   75 +++++++++++++++++++++++++++++++++++++++++--------------
 4 files changed, 109 insertions(+), 19 deletions(-)
qemu-config.c +8 -0
index c3203c8..82ca399 100644
@@ -242,6 +242,50 @@ QemuOptsList qemu_mon_opts = {
     },
 };
 
+QemuOptsList qemu_default_opts = {
+    .name = "default",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_default_opts.head),
+    .desc = {
+        {
+            .name = "serial",
+            .type = QEMU_OPT_STRING,
+        },

[...]

qemu-options.hx STEXI · line 1923

Quoted 10 lines
qemu-options.hx +2 -0
index 57f453d..e81ecb5 100644
@@ -1919,6 +1919,13 @@ STEXI
 Don't create default devices.
 ETEXI
 
+DEF("default", HAS_ARG, QEMU_OPTION_default, \
+    "-default arg    specify default devices\n")

Isn't this too terse?

qemu-options.hx STEXI · line 1927

Quoted 4 lines
qemu-options.hx +4 -0
+STEXI
+@item -defaults
+Override builtin default devices
+ETEXI

This *is* too terse :)

Oh, and it's -default (sans 's'). Same typo in subject.

While we're talking about naming: isn't -default a bit too generic a name for something that manipulates devices? Not sure we care, as -nodefaults is much worse, already.

[...]

#8 lore raw

Re: [Qemu-devel] [PATCH 3/4] Add -defaults option to allow default devices to be overridden

Quoted 47 lines · Markus Armbruster
Quoted 34 lines · Anthony Liguori

This option can be used to toggle whether each default device is enabled or disabled. For character devices, the default backend can also be overridden.

For devices, we'll have to take a different approach to changing the defaults which will be covered in the next patch.

N.B. I took special care with -nographic. Now -nographic pretty clearly acts as a mechanism to override the default backend devices.

---
  qemu-config.c   |   45 +++++++++++++++++++++++++++++++++
  qemu-config.h   |    1 +
  qemu-options.hx |    7 +++++
  vl.c            |   75 +++++++++++++++++++++++++++++++++++++++++--------------
  4 files changed, 109 insertions(+), 19 deletions(-)
qemu-config.c +8 -0
index c3203c8..82ca399 100644
@@ -242,6 +242,50 @@ QemuOptsList qemu_mon_opts = {
      },
  };
 
+QemuOptsList qemu_default_opts = {
+    .name = "default",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_default_opts.head),
+    .desc = {
+        {
+            .name = "serial",
+            .type = QEMU_OPT_STRING,
+        },
 

[...]

Quoted 8 lines
qemu-options.hx +0 -0
index 57f453d..e81ecb5 100644
@@ -1919,6 +1919,13 @@ STEXI
  Don't create default devices.
  ETEXI
 
Quoted 5 lines

qemu-options.hx STEXI

Quoted 3 lines
qemu-options.hx +2 -0
+DEF("default", HAS_ARG, QEMU_OPTION_default, \
+    "-default arg    specify default devices\n")
 

Isn't this too terse?

Yes. Thanks for pointing that out.

Quoted 7 lines

qemu-options.hx STEXI

Quoted 5 lines
qemu-options.hx +4 -0
+STEXI
+@item -defaults
+Override builtin default devices
+ETEXI
 

This *is* too terse :)

Quoted 6 lines

Oh, and it's -default (sans 's'). Same typo in subject.

While we're talking about naming: isn't -default a bit too generic a name for something that manipulates devices? Not sure we care, as -nodefaults is much worse, already.

Absolutely. I'm going to split out the config loading bits because they should be easy to commit. How to best handle defaults could use some more thought. I think this is a key bit of functionality to get right though because it solves a whole class of problems relating to upstream policy vs. downstream policy.

I very much like the idea of having a qdev property 'default' to signify that this is a default device. It means we could easily allow a user to universally enable usb devices, etc without baking a default_usb concept into qemu. Ideally, we could eliminate the whole default mess we have today by doing this, provided that we can implement the right semantics.

Today, those semantics are, if we specify any device of the same "class", do not load default devices of that class. It's unclear how to specify the concept of a "class" though. I know Gerd brought this up ages ago and both Paul and I were very opposed to it but I certainly appreciate the fact that it would simplify default device handling.

It's definitely clear that each device needs to be able to be part of multiple default-class's. We could potentially introduce a qdev property and label every device with a class array but that's a lot of ugliness just to support defaults. I still would prefer that there was a more discoverable way to determine the class of a device as opposed to explicitly labelling it.

If we need to do class tagging, I guess it's not the end of the world...

Regards,

Anthony Liguori

Quoted 2 lines

[...]

#9 lore raw

PATCH 4/4 Allow default network type to be overridden

In reply to Anthony Liguori

Introduce a default option to the network device which specifies that this is a default network device. This approach should generalize to any other device.

The meaning of a default device is as follows: a default device is added to a machine IIF defaults aren't disable (via -default or -nodefaults) and a non-default device of this type hasn't been added.

---
 net.c |   42 +++++++++++++++++++++++++++++++++++++-----
 1 files changed, 37 insertions(+), 5 deletions(-)
net.c +37 -5
index 6ef93e6..1aa3c77 100644
@@ -833,6 +833,10 @@ static int net_init_nic(QemuOpts *opts,
         .name = "name",                            \
         .type = QEMU_OPT_STRING,                   \
         .help = "identifier for monitor commands", \
+     }, {                                          \
+        .name = "default",                         \
+        .type = QEMU_OPT_BOOL,                     \
+        .help = "act as default network device",   \
      }
 
 typedef int (*net_client_init_func)(QemuOpts *opts,
@@ -1056,6 +1060,15 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
     const char *type;
     int i;
 
+    /* Do not create default network devices if no defaults */
+    if (!default_net) {
+        const char *opt = qemu_opt_get(opts, "default");
+
+        if (opt && strcmp(opt, "on") == 0) {
+            return 0;
+        }
+    }
+
     type = qemu_opt_get(opts, "type");
 
     if (!is_netdev) {
@@ -1320,10 +1333,22 @@ static int net_init_netdev(QemuOpts *opts, void *dummy)
 int net_init_clients(void)
 {
     if (default_net) {
-        /* if no clients, we use a default config */
-        qemu_opts_set(&qemu_net_opts, NULL, "type", "nic");
+        QemuOpts *opts;
+
+        opts = qemu_opts_create(&qemu_net_opts, NULL, 1);
+        if (opts == NULL) {
+            return -1;
+        }
+        qemu_opt_set(opts, "type", "nic");
+        qemu_opt_set(opts, "default", "on");
+
 #ifdef CONFIG_SLIRP
-        qemu_opts_set(&qemu_net_opts, NULL, "type", "user");
+        opts = qemu_opts_create(&qemu_net_opts, NULL, 1);
+        if (opts == NULL) {
+            return -1;
+        }
+        qemu_opt_set(opts, "type", "user");
+        qemu_opt_set(opts, "default", "on");
 #endif
     }
 
@@ -1344,6 +1369,8 @@ int net_init_clients(void)
 
 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
 {
+    QemuOpts *opts;
+    const char *opt;
 #if defined(CONFIG_SLIRP)
     int ret;
     if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
@@ -1351,10 +1378,15 @@ int net_client_parse(QemuOptsList *opts_list, const char *optarg)
     }
 #endif
 
-    if (!qemu_opts_parse(opts_list, optarg, "type")) {
+    opts = qemu_opts_parse(opts_list, optarg, "type");
+    if (opts == NULL) {
         return -1;
     }
 
-    default_net = 0;
+    opt = qemu_opt_get(opts, "default");
+    if (!opt || strcmp(opt, "off") == 0) {
+        default_net = 0;
+    }
+
     return 0;
 }
Signature · 1 line
1.6.5.2
#10 lore raw

[Qemu-devel] Re: [PATCH 4/4] Allow default network type to be overridden

Quoted 6 lines · Anthony Liguori

Introduce a default option to the network device which specifies that this is a default network device. This approach should generalize to any other device.

The meaning of a default device is as follows: a default device is added to a machine IIF defaults aren't disable (via -default or -nodefaults) and a non-default device of this type hasn't been added.

I don't like the idea to have two different mechanisms for configuring defaults ([default] section + default = "on"). I'd suggest to pick one and apply it everythere.

cheers,
Gerd

#11 lore raw

[Qemu-devel] Re: [PATCH 4/4] Allow default network type to be overridden

Quoted 9 lines · Gerd Hoffmann
Quoted 6 lines · Anthony Liguori

Introduce a default option to the network device which specifies that this is a default network device. This approach should generalize to any other device.

The meaning of a default device is as follows: a default device is added to a machine IIF defaults aren't disable (via -default or -nodefaults) and a non-default device of this type hasn't been added.

I don't like the idea to have two different mechanisms for configuring defaults ([default] section + default = "on"). I'd suggest to pick one and apply it everythere.

I agree that it's awkward. Need to think a bit about it.

Regards,

Anthony Liguori

Quoted 3 lines

cheers,
Gerd