Skip to contentSkip to conversation

Rework -boot option

PATCH New Jan Kiszka 2 participants 8 messages Jul 1, 2009 – Jul 10, 2009

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

cover Rework -boot option

This is another round of patches for a -boot rework. This version splits the previous patch up and renames the 'interactive' option to 'menu' as suggested during review. Moreover, it adds support for 'once', a way to specify a boot order only for the first guest startup. The latter required some more refactoring, thus more patches.

Find this series also at git://git.kiszka.org/qemu.git queues/boot

Jan Kiszka (7):
Rework -boot option
Move boot_set callback backend
Rework reset handler management
Add qemu_unregister_reset
Add boot-once support
Add boot menu control via command line switch Update boot option documentation

 hw/fw_cfg.c     |    1 +
 hw/fw_cfg.h     |    1 +
 hw/hw.h         |    6 ++-
 monitor.c       |   23 ++------
 qemu-options.hx |   27 ++++++++--
 sysemu.h        |    1 +
 vl.c            |  160 +++++++++++++++++++++++++++++++++++++++++--------------
 7 files changed, 155 insertions(+), 64 deletions(-)
#1 lore raw

PATCH 1/7 Rework -boot option

This patch changes the boot command line option to the canonical format

-boot [order=drives][,...]

where 'drives' is using the same format as the old -boot. The format switch allows to add the 'menu' and 'once' options in later patches. The old format is still understood and will be processed at least for a transition time.

---

 vl.c |   81 +++++++++++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 50 insertions(+), 31 deletions(-)
vl.c +50 -31
index 7b7489c..057037a 100644
@@ -2569,6 +2569,35 @@ int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
     return drives_table_idx;
 }
 
+static int parse_bootdevices(char *devices)
+{
+    /* We just do some generic consistency checks */
+    const char *p;
+    int bitmap = 0;
+
+    for (p = devices; *p != '\0'; p++) {
+        /* Allowed boot devices are:
+         * a-b: floppy disk drives
+         * c-f: IDE disk drives
+         * g-m: machine implementation dependant drives
+         * n-p: network devices
+         * It's up to each machine implementation to check if the given boot
+         * devices match the actual hardware implementation and firmware
+         * features.
+         */
+        if (*p < 'a' || *p > 'p') {
+            fprintf(stderr, "Invalid boot device '%c'\n", *p);
+            exit(1);
+        }
+        if (bitmap & (1 << (*p - 'a'))) {
+            fprintf(stderr, "Boot device '%c' was given twice\n", *p);
+            exit(1);
+        }
+        bitmap |= 1 << (*p - 'a');
+    }
+    return bitmap;
+}
+
 static void numa_add(const char *optarg)
 {
     char option[128];
@@ -4978,7 +5007,7 @@ int main(int argc, char **argv, char **envp)
     int snapshot, linux_boot, net_boot;
     const char *initrd_filename;
     const char *kernel_filename, *kernel_cmdline;
-    const char *boot_devices = "";
+    char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
     DisplayState *ds;
     DisplayChangeListener *dcl;
     int cyls, heads, secs, translation;
@@ -5267,33 +5296,27 @@ int main(int argc, char **argv, char **envp)
                 drive_add(optarg, CDROM_ALIAS);
                 break;
             case QEMU_OPTION_boot:
-                boot_devices = optarg;
-                /* We just do some generic consistency checks */
                 {
-                    /* Could easily be extended to 64 devices if needed */
-                    const char *p;
-                    
-                    boot_devices_bitmap = 0;
-                    for (p = boot_devices; *p != '\0'; p++) {
-                        /* Allowed boot devices are:
-                         * a b     : floppy disk drives
-                         * c ... f : IDE disk drives
-                         * g ... m : machine implementation dependant drives
-                         * n ... p : network devices
-                         * It's up to each machine implementation to check
-                         * if the given boot devices match the actual hardware
-                         * implementation and firmware features.
-                         */
-                        if (*p < 'a' || *p > 'q') {
-                            fprintf(stderr, "Invalid boot device '%c'\n", *p);
-                            exit(1);
-                        }
-                        if (boot_devices_bitmap & (1 << (*p - 'a'))) {
-                            fprintf(stderr,
-                                    "Boot device '%c' was given twice\n",*p);
-                            exit(1);
-                        }
-                        boot_devices_bitmap |= 1 << (*p - 'a');
+                    static const char * const params[] = {
+                        "order", NULL
+                    };
+                    char buf[sizeof(boot_devices)];
+                    int legacy = 0;
+
+                    if (!strchr(optarg, '=')) {
+                        legacy = 1;
+                        pstrcpy(buf, sizeof(buf), optarg);
+                    } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
+                        fprintf(stderr,
+                                "qemu: unknown boot parameter '%s' in '%s'\n",
+                                buf, optarg);
+                        exit(1);
+                    }
+
+                    if (legacy ||
+                        get_param_value(buf, sizeof(buf), "order", optarg)) {
+                        boot_devices_bitmap = parse_bootdevices(buf);
+                        pstrcpy(boot_devices, sizeof(boot_devices), buf);
                     }
                 }
                 break;
@@ -5850,10 +5873,6 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
-    /* boot to floppy or the default cd if no hard disk defined yet */
-    if (!boot_devices[0]) {
-        boot_devices = "cad";
-    }
     setvbuf(stdout, NULL, _IOLBF, 0);
 
     init_timers();
#2 lore raw

PATCH 2/7 Move boot_set callback backend

In reply to Jan Kiszka

Move registration function for the boot_set callback handler and provide qemu_boot_set so that it can also be used outside the monitor code.

---

 hw/hw.h   |    5 +++--
 monitor.c |   23 +++++------------------
 vl.c      |   17 +++++++++++++++++
 3 files changed, 25 insertions(+), 20 deletions(-)
hw/hw.h +3 -2
index 2e43c1f..0f1ef98 100644
@@ -261,10 +261,11 @@ typedef void QEMUResetHandler(void *opaque);
 
 void qemu_register_reset(QEMUResetHandler *func, void *opaque);
 
-/* handler to set the boot_device for a specific type of QEMUMachine */
+/* handler to set the boot_device order for a specific type of QEMUMachine */
 /* return 0 if success */
-typedef int QEMUBootSetHandler(void *opaque, const char *boot_device);
+typedef int QEMUBootSetHandler(void *opaque, const char *boot_devices);
 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque);
+int qemu_boot_set(const char *boot_devices);
 
 /* These should really be in isa.h, but are here to make pc.h happy.  */
 typedef void (IOPortWriteFunc)(void *opaque, uint32_t address, uint32_t data);
monitor.c +5 -18
index bad79fe..fedc04e 100644
@@ -1185,28 +1185,15 @@ static void do_ioport_read(Monitor *mon, int count, int format, int size,
                    suffix, addr, size * 2, val);
 }
 
-/* boot_set handler */
-static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
-static void *boot_opaque;
-
-void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
-{
-    qemu_boot_set_handler = func;
-    boot_opaque = opaque;
-}
-
 static void do_boot_set(Monitor *mon, const char *bootdevice)
 {
     int res;
 
-    if (qemu_boot_set_handler)  {
-        res = qemu_boot_set_handler(boot_opaque, bootdevice);
-        if (res == 0)
-            monitor_printf(mon, "boot device list now set to %s\n",
-                           bootdevice);
-        else
-            monitor_printf(mon, "setting boot device list failed with "
-                           "error %i\n", res);
+    res = qemu_boot_set(bootdevice);
+    if (res == 0) {
+        monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
+    } else if (res > 0) {
+        monitor_printf(mon, "setting boot device list failed\n");
     } else {
         monitor_printf(mon, "no function defined to set boot device list for "
                        "this architecture\n");
vl.c +17 -0
index 057037a..9f1c9db 100644
@@ -288,6 +288,9 @@ static QEMUTimer *nographic_timer;
 
 uint8_t qemu_uuid[16];
 
+static QEMUBootSetHandler *boot_set_handler;
+static void *boot_set_opaque;
+
 /***********************************************************/
 /* x86 ISA bus support */
 
@@ -2569,6 +2572,20 @@ int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
     return drives_table_idx;
 }
 
+void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
+{
+    boot_set_handler = func;
+    boot_set_opaque = opaque;
+}
+
+int qemu_boot_set(const char *boot_devices)
+{
+    if (!boot_set_handler) {
+        return -EINVAL;
+    }
+    return boot_set_handler(boot_set_opaque, boot_devices);
+}
+
 static int parse_bootdevices(char *devices)
 {
     /* We just do some generic consistency checks */
#3 lore raw

PATCH 3/7 Rework reset handler management

In reply to Jan Kiszka

Convert the reset handler maintenance code to TAILQ services.

---

 vl.c |   16 ++++++----------
 1 files changed, 6 insertions(+), 10 deletions(-)
vl.c +6 -10
index 9f1c9db..9d0e298 100644
@@ -3655,12 +3655,13 @@ void vm_start(void)
 /* reset/shutdown handler */
 
 typedef struct QEMUResetEntry {
+    TAILQ_ENTRY(QEMUResetEntry) entry;
     QEMUResetHandler *func;
     void *opaque;
-    struct QEMUResetEntry *next;
 } QEMUResetEntry;
 
-static QEMUResetEntry *first_reset_entry;
+static TAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
+    TAILQ_HEAD_INITIALIZER(reset_handlers);
 static int reset_requested;
 static int shutdown_requested;
 static int powerdown_requested;
@@ -3714,16 +3715,11 @@ static void do_vm_stop(int reason)
 
 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
 {
-    QEMUResetEntry **pre, *re;
+    QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
 
-    pre = &first_reset_entry;
-    while (*pre != NULL)
-        pre = &(*pre)->next;
-    re = qemu_mallocz(sizeof(QEMUResetEntry));
     re->func = func;
     re->opaque = opaque;
-    re->next = NULL;
-    *pre = re;
+    TAILQ_INSERT_TAIL(&reset_handlers, re, entry);
 }
 
 void qemu_system_reset(void)
@@ -3731,7 +3727,7 @@ void qemu_system_reset(void)
     QEMUResetEntry *re;
 
     /* reset all devices */
-    for(re = first_reset_entry; re != NULL; re = re->next) {
+    TAILQ_FOREACH(re, &reset_handlers, entry) {
         re->func(re->opaque);
     }
 }
#4 lore raw

PATCH 4/7 Add qemu_unregister_reset

In reply to Jan Kiszka

Will be used by '-boot once=...', and should also help in other use cases.

---

 hw/hw.h |    1 +
 vl.c    |   17 +++++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)
hw/hw.h +1 -0
index 0f1ef98..cfd6a4e 100644
@@ -260,6 +260,7 @@ void unregister_savevm(const char *idstr, void *opaque);
 typedef void QEMUResetHandler(void *opaque);
 
 void qemu_register_reset(QEMUResetHandler *func, void *opaque);
+void qemu_unregister_reset(QEMUResetHandler *func, void *opaque);
 
 /* handler to set the boot_device order for a specific type of QEMUMachine */
 /* return 0 if success */
vl.c +15 -2
index 9d0e298..0586a8a 100644
@@ -3722,12 +3722,25 @@ void qemu_register_reset(QEMUResetHandler *func, void *opaque)
     TAILQ_INSERT_TAIL(&reset_handlers, re, entry);
 }
 
-void qemu_system_reset(void)
+void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
 {
     QEMUResetEntry *re;
 
-    /* reset all devices */
     TAILQ_FOREACH(re, &reset_handlers, entry) {
+        if (re->func == func && re->opaque == opaque) {
+            TAILQ_REMOVE(&reset_handlers, re, entry);
+            qemu_free(re);
+            return;
+        }
+    }
+}
+
+void qemu_system_reset(void)
+{
+    QEMUResetEntry *re, *nre;
+
+    /* reset all devices */
+    TAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
         re->func(re->opaque);
     }
 }
#5 lore raw

PATCH 5/7 Add boot-once support

In reply to Jan Kiszka

This allows to specify an exceptional boot order only for the first startup of the guest. After reboot, qemu will switch back to the default order (or what was specified via 'order='). Makes installing from CD images and then booting the freshly set up harddisk more handy.

---

 vl.c |   23 ++++++++++++++++++++++-
 1 files changed, 22 insertions(+), 1 deletions(-)
vl.c +22 -1
index 0586a8a..cd71710 100644
@@ -2615,6 +2615,16 @@ static int parse_bootdevices(char *devices)
     return bitmap;
 }
 
+static void restore_boot_devices(void *opaque)
+{
+    char *standard_boot_devices = opaque;
+
+    qemu_boot_set(standard_boot_devices);
+
+    qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
+    qemu_free(standard_boot_devices);
+}
+
 static void numa_add(const char *optarg)
 {
     char option[128];
@@ -5324,9 +5334,10 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_boot:
                 {
                     static const char * const params[] = {
-                        "order", NULL
+                        "order", "once", NULL
                     };
                     char buf[sizeof(boot_devices)];
+                    char *standard_boot_devices;
                     int legacy = 0;
 
                     if (!strchr(optarg, '=')) {
@@ -5344,6 +5355,16 @@ int main(int argc, char **argv, char **envp)
                         boot_devices_bitmap = parse_bootdevices(buf);
                         pstrcpy(boot_devices, sizeof(boot_devices), buf);
                     }
+                    if (!legacy) {
+                        if (get_param_value(buf, sizeof(buf),
+                                            "once", optarg)) {
+                            boot_devices_bitmap |= parse_bootdevices(buf);
+                            standard_boot_devices = qemu_strdup(boot_devices);
+                            pstrcpy(boot_devices, sizeof(boot_devices), buf);
+                            qemu_register_reset(restore_boot_devices,
+                                                standard_boot_devices);
+                        }
+                    }
                 }
                 break;
             case QEMU_OPTION_fda:
#6 lore raw

PATCH 6/7 Add boot menu control via command line switch

In reply to Jan Kiszka

Disable the lengthy BIOS prompt for selecting a boot device by default, but let the user reenable it via '-boot menu=on'.

---

 hw/fw_cfg.c |    1 +
 hw/fw_cfg.h |    1 +
 sysemu.h    |    1 +
 vl.c        |   16 +++++++++++++++-
 4 files changed, 18 insertions(+), 1 deletions(-)
hw/fw_cfg.c +1 -0
index ce57de7..d75cce4 100644
@@ -279,6 +279,7 @@ void *fw_cfg_init(uint32_t ctl_port, uint32_t data_port,
     fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
     fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)(display_type == DT_NOGRAPHIC));
     fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
+    fw_cfg_add_i16(s, FW_CFG_BOOT_MENU, (uint16_t)boot_menu);
 
     register_savevm("fw_cfg", -1, 1, fw_cfg_save, fw_cfg_load, s);
     qemu_register_reset(fw_cfg_reset, s);
hw/fw_cfg.h +1 -0
index f616ed2..9fee181 100644
@@ -15,6 +15,7 @@
 #define FW_CFG_INITRD_SIZE      0x0b
 #define FW_CFG_BOOT_DEVICE      0x0c
 #define FW_CFG_NUMA             0x0d
+#define FW_CFG_BOOT_MENU        0x0e
 #define FW_CFG_MAX_ENTRY        0x10
 
 #define FW_CFG_WRITE_CHANNEL    0x4000
sysemu.h +1 -0
index 06dc4c6..df19f02 100644
@@ -124,6 +124,7 @@ extern int graphic_rotate;
 extern int no_quit;
 extern int semihosting_enabled;
 extern int old_param;
+extern int boot_menu;
 
 #ifdef CONFIG_KQEMU
 extern int kqemu_allowed;
vl.c +15 -1
index cd71710..0cae0bc 100644
@@ -268,6 +268,7 @@ const char *prom_envs[MAX_PROM_ENVS];
 #endif
 int nb_drives_opt;
 struct drive_opt drives_opt[MAX_DRIVES];
+int boot_menu;
 
 int nb_numa_nodes;
 uint64_t node_mem[MAX_NODES];
@@ -5334,7 +5335,7 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_boot:
                 {
                     static const char * const params[] = {
-                        "order", "once", NULL
+                        "order", "once", "menu", NULL
                     };
                     char buf[sizeof(boot_devices)];
                     char *standard_boot_devices;
@@ -5364,6 +5365,19 @@ int main(int argc, char **argv, char **envp)
                             qemu_register_reset(restore_boot_devices,
                                                 standard_boot_devices);
                         }
+                        if (get_param_value(buf, sizeof(buf),
+                                            "menu", optarg)) {
+                            if (!strcmp(buf, "on")) {
+                                boot_menu = 1;
+                            } else if (!strcmp(buf, "off")) {
+                                boot_menu = 0;
+                            } else {
+                                fprintf(stderr,
+                                        "qemu: invalid option value '%s'\n",
+                                        buf);
+                                exit(1);
+                            }
+                        }
                     }
                 }
                 break;
#7 lore raw

PATCH 7/7 Update boot option documentation

In reply to Jan Kiszka

---

 qemu-options.hx |   27 +++++++++++++++++++++++----
 1 files changed, 23 insertions(+), 4 deletions(-)
qemu-options.hx +23 -4
index a94f9d3..d3a20f5 100644
@@ -221,11 +221,30 @@ Use 'file' as a parallel flash image.
 ETEXI
 
 DEF("boot", HAS_ARG, QEMU_OPTION_boot,
-    "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n")
+    "-boot [order=drives][,once=drives][,menu=on|off]\n"
+    "                'drives': floppy (a), hard disk (c), CD-ROM (d), network (n)\n")
 STEXI
-@item -boot [a|c|d|n]
-Boot on floppy (a), hard disk (c), CD-ROM (d), or Etherboot (n). Hard disk boot
-is the default.
+@item -boot [order=@var{drives}][,once=@var{drives}][,menu=on|off]
+
+Specify boot order @var{drives} as a string of drive letters. Valid
+drive letters depend on the target achitecture. The x86 PC uses: a, b
+(floppy 1 and 2), c (first hard disk), d (first CD-ROM), n-p (Etherboot
+from network adapter 1-4), hard disk boot is the default. To apply a
+particular boot order only on the first startup, specify it via
+@option{once}.
+
+Interactive boot menus/prompts can be enabled via @option{menu=on} as far
+as firmware/BIOS supports them. The default is non-interactive boot.
+
+@example
+# try to boot from network first, then from hard disk
+qemu -boot order=nc
+# boot from CD-ROM first, switch back to default order after reboot
+qemu -boot once=d
+@end example
+
+Note: The legacy format '-boot @var{drives}' is still supported but its
+use is discouraged as it may be removed from future versions.
 ETEXI
 
 DEF("snapshot", 0, QEMU_OPTION_snapshot,