blockdev: Make drive_add() take explicit type, index parameters

Before, type & index were hidden in printf-like fmt, ... parameters,
which get expanded into an option string.  Rather inconvenient for
uses later in this series.

New IF_DEFAULT to ask for the machine's default interface.  Before,
that was done by having no option "if" in the option string.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Markus Armbruster 2011-01-28 11:21:41 +01:00 committed by Kevin Wolf
parent 27d6bf40ed
commit 2292ddaeab
4 changed files with 48 additions and 25 deletions

View file

@ -75,20 +75,34 @@ void blockdev_auto_del(BlockDriverState *bs)
} }
} }
QemuOpts *drive_add(const char *file, const char *fmt, ...) QemuOpts *drive_def(const char *optstr)
{
return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0);
}
QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
const char *fmt, ...)
{ {
va_list ap; va_list ap;
char optstr[1024]; char optstr[1024];
QemuOpts *opts; QemuOpts *opts;
char buf[32];
va_start(ap, fmt); va_start(ap, fmt);
vsnprintf(optstr, sizeof(optstr), fmt, ap); vsnprintf(optstr, sizeof(optstr), fmt, ap);
va_end(ap); va_end(ap);
opts = qemu_opts_parse(qemu_find_opts("drive"), optstr, 0); opts = drive_def(optstr);
if (!opts) { if (!opts) {
return NULL; return NULL;
} }
if (type != IF_DEFAULT) {
qemu_opt_set(opts, "if", if_name[type]);
}
if (index >= 0) {
snprintf(buf, sizeof(buf), "%d", index);
qemu_opt_set(opts, "index", buf);
}
if (file) if (file)
qemu_opt_set(opts, "file", file); qemu_opt_set(opts, "file", file);
return opts; return opts;
@ -473,7 +487,7 @@ DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi, int *fatal_error)
if (devaddr) if (devaddr)
qemu_opt_set(opts, "addr", devaddr); qemu_opt_set(opts, "addr", devaddr);
break; break;
case IF_COUNT: default:
abort(); abort();
} }
if (!file || !*file) { if (!file || !*file) {

View file

@ -19,6 +19,7 @@ void blockdev_auto_del(BlockDriverState *bs);
#define BLOCK_SERIAL_STRLEN 20 #define BLOCK_SERIAL_STRLEN 20
typedef enum { typedef enum {
IF_DEFAULT = -1, /* for use with drive_add() only */
IF_NONE, IF_NONE,
IF_IDE, IF_SCSI, IF_FLOPPY, IF_PFLASH, IF_MTD, IF_SD, IF_VIRTIO, IF_XEN, IF_IDE, IF_SCSI, IF_FLOPPY, IF_PFLASH, IF_MTD, IF_SD, IF_VIRTIO, IF_XEN,
IF_COUNT IF_COUNT
@ -43,7 +44,12 @@ DriveInfo *drive_get_next(BlockInterfaceType type);
void drive_uninit(DriveInfo *dinfo); void drive_uninit(DriveInfo *dinfo);
DriveInfo *drive_get_by_blockdev(BlockDriverState *bs); DriveInfo *drive_get_by_blockdev(BlockDriverState *bs);
QemuOpts *drive_add(const char *file, const char *fmt, ...) GCC_FMT_ATTR(2, 3); QemuOpts *drive_def(const char *optstr);
QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file,
const char *fmt, ...) /*GCC_FMT_ATTR(4, 5)*/;
/* GCC_FMT_ATTR() commented out to avoid the (pretty useless)
* "zero-length gnu_printf format string" warning we insist to
* enable */
DriveInfo *drive_init(QemuOpts *arg, int default_to_scsi, int *fatal_error); DriveInfo *drive_init(QemuOpts *arg, int default_to_scsi, int *fatal_error);
/* device-hotplug */ /* device-hotplug */

View file

@ -33,7 +33,7 @@ DriveInfo *add_init_drive(const char *optstr)
DriveInfo *dinfo; DriveInfo *dinfo;
QemuOpts *opts; QemuOpts *opts;
opts = drive_add(NULL, "%s", optstr); opts = drive_def(optstr);
if (!opts) if (!opts)
return NULL; return NULL;

43
vl.c
View file

@ -621,12 +621,13 @@ static int bt_parse(const char *opt)
/***********************************************************/ /***********************************************************/
/* QEMU Block devices */ /* QEMU Block devices */
#define HD_ALIAS "index=%d,media=disk" /* Any % in the following strings must be escaped as %% */
#define CDROM_ALIAS "index=2,media=cdrom" #define HD_OPTS "media=disk"
#define FD_ALIAS "index=%d,if=floppy" #define CDROM_OPTS "media=cdrom"
#define PFLASH_ALIAS "if=pflash" #define FD_OPTS ""
#define MTD_ALIAS "if=mtd" #define PFLASH_OPTS ""
#define SD_ALIAS "index=0,if=sd" #define MTD_OPTS ""
#define SD_OPTS ""
static int drive_init_func(QemuOpts *opts, void *opaque) static int drive_init_func(QemuOpts *opts, void *opaque)
{ {
@ -1987,7 +1988,7 @@ int main(int argc, char **argv, char **envp)
if (optind >= argc) if (optind >= argc)
break; break;
if (argv[optind][0] != '-') { if (argv[optind][0] != '-') {
hda_opts = drive_add(argv[optind++], HD_ALIAS, 0); hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
} else { } else {
const QEMUOption *popt; const QEMUOption *popt;
@ -2027,11 +2028,11 @@ int main(int argc, char **argv, char **envp)
break; break;
case QEMU_OPTION_hda: case QEMU_OPTION_hda:
if (cyls == 0) if (cyls == 0)
hda_opts = drive_add(optarg, HD_ALIAS, 0); hda_opts = drive_add(IF_DEFAULT, 0, optarg, HD_OPTS);
else else
hda_opts = drive_add(optarg, HD_ALIAS hda_opts = drive_add(IF_DEFAULT, 0, optarg, HD_OPTS
",cyls=%d,heads=%d,secs=%d%s", ",cyls=%d,heads=%d,secs=%d%s",
0, cyls, heads, secs, cyls, heads, secs,
translation == BIOS_ATA_TRANSLATION_LBA ? translation == BIOS_ATA_TRANSLATION_LBA ?
",trans=lba" : ",trans=lba" :
translation == BIOS_ATA_TRANSLATION_NONE ? translation == BIOS_ATA_TRANSLATION_NONE ?
@ -2040,10 +2041,11 @@ int main(int argc, char **argv, char **envp)
case QEMU_OPTION_hdb: case QEMU_OPTION_hdb:
case QEMU_OPTION_hdc: case QEMU_OPTION_hdc:
case QEMU_OPTION_hdd: case QEMU_OPTION_hdd:
drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
HD_OPTS);
break; break;
case QEMU_OPTION_drive: case QEMU_OPTION_drive:
drive_add(NULL, "%s", optarg); drive_def(optarg);
break; break;
case QEMU_OPTION_set: case QEMU_OPTION_set:
if (qemu_set_option(optarg) != 0) if (qemu_set_option(optarg) != 0)
@ -2054,13 +2056,13 @@ int main(int argc, char **argv, char **envp)
exit(1); exit(1);
break; break;
case QEMU_OPTION_mtdblock: case QEMU_OPTION_mtdblock:
drive_add(optarg, MTD_ALIAS); drive_add(IF_MTD, -1, optarg, MTD_OPTS);
break; break;
case QEMU_OPTION_sd: case QEMU_OPTION_sd:
drive_add(optarg, SD_ALIAS); drive_add(IF_SD, 0, optarg, SD_OPTS);
break; break;
case QEMU_OPTION_pflash: case QEMU_OPTION_pflash:
drive_add(optarg, PFLASH_ALIAS); drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
break; break;
case QEMU_OPTION_snapshot: case QEMU_OPTION_snapshot:
snapshot = 1; snapshot = 1;
@ -2139,7 +2141,7 @@ int main(int argc, char **argv, char **envp)
kernel_cmdline = optarg; kernel_cmdline = optarg;
break; break;
case QEMU_OPTION_cdrom: case QEMU_OPTION_cdrom:
drive_add(optarg, CDROM_ALIAS); drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
break; break;
case QEMU_OPTION_boot: case QEMU_OPTION_boot:
{ {
@ -2192,7 +2194,8 @@ int main(int argc, char **argv, char **envp)
break; break;
case QEMU_OPTION_fda: case QEMU_OPTION_fda:
case QEMU_OPTION_fdb: case QEMU_OPTION_fdb:
drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
optarg, FD_OPTS);
break; break;
case QEMU_OPTION_no_fd_bootchk: case QEMU_OPTION_no_fd_bootchk:
fd_bootchk = 0; fd_bootchk = 0;
@ -2892,17 +2895,17 @@ int main(int argc, char **argv, char **envp)
if (default_cdrom) { if (default_cdrom) {
/* we always create the cdrom drive, even if no disk is there */ /* we always create the cdrom drive, even if no disk is there */
drive_add(NULL, CDROM_ALIAS); drive_add(IF_DEFAULT, 2, NULL, CDROM_OPTS);
} }
if (default_floppy) { if (default_floppy) {
/* we always create at least one floppy */ /* we always create at least one floppy */
drive_add(NULL, FD_ALIAS, 0); drive_add(IF_FLOPPY, 0, NULL, FD_OPTS);
} }
if (default_sdcard) { if (default_sdcard) {
/* we always create one sd slot, even if no card is in it */ /* we always create one sd slot, even if no card is in it */
drive_add(NULL, SD_ALIAS); drive_add(IF_SD, 0, NULL, SD_OPTS);
} }
/* open the virtual block devices */ /* open the virtual block devices */