This patch introduces a new parameter allowing to define disks. The new parameter is "-disk":
-disk file[,if=type][,index=i][,bus=n][,unit=m][,media=d][,cyls=c,heads=h,secs=s[,trans=t]][snapshot=on|off] where: file is the disk image type is the interface type (ide) i is the index of the disk on the interface (0, 1, 2, ...) n is the bus number of the given type m is the unit number on the the given bus d is the type of the media (disk, cdrom) c,h,s,t are the parameters usually given by -hdachs snapshot allows to enable or not the snapshot for this disk "-cdrom file" is an alias for "-disk file,index=2,media=cdrom" "-hda file" is an alias for "-disk file,index=0,media=disk" "-hdb file" is an alias for "-disk file,index=1,media=disk" "-hdc file" is an alias for "-disk file,index=2,media=disk" "-hdd file" is an alias for "-disk file,index=3,media=disk" "-hda file -hdachs a,b,c" is an alias for "-disk file,index=0,cyls=a,heads=b,secs=c" You can also define a cdrom on the slave of ide0 with: "-disk file,if=ide,bus=0,unit=1,media=cdrom" -- hw/mips_pica61.c | 5 hw/mips_r4k.c | 5 hw/pc.c | 5 hw/ppc_prep.c | 5 monitor.c | 2 vl.c | 346 ++++++++++++++++++++++++++++++++++++++++++++++--------- vl.h | 8 - 7 files changed, 311 insertions(+), 65 deletions(-) Index: qemu/vl.c =================================================================== --- qemu.orig/vl.c 2007-10-31 14:12:40.000000000 +0100 +++ qemu/vl.c 2007-10-31 14:18:25.000000000 +0100 @@ -149,9 +149,9 @@ void *ioport_opaque[MAX_IOPORTS]; IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; -/* Note: bs_table[MAX_DISKS] is a dummy block driver if none available +/* Note: bs_table[MAX_IDE_DISKS] is a dummy block driver if none available to store the VM snapshots */ -BlockDriverState *bs_table[MAX_DISKS + 1], *fd_table[MAX_FD]; +BlockDriverState *bs_table[MAX_IDE_DISKS + 1], *fd_table[MAX_FD]; BlockDriverState *pflash_table[MAX_PFLASH]; BlockDriverState *sd_bdrv; BlockDriverState *mtd_bdrv; @@ -1748,7 +1748,7 @@ case 's': { int i; - for (i = 0; i < MAX_DISKS; i++) { + for (i = 0; i < MAX_IDE_DISKS; i++) { if (bs_table[i]) bdrv_commit(bs_table[i]); } @@ -4706,6 +4706,205 @@ } } +#define HD_ALIAS ",index=%d,media=disk" + +#ifdef TARGET_PPC +#define CDROM_ALIAS ",index=1,media=cdrom" +#else +#define CDROM_ALIAS ",index=2,media=cdrom" +#endif + +static int disk_init(const char *str, int snapshot) +{ + char buf[16]; + enum { IF_IDE, IF_SCSI } interface; + enum { MEDIA_DISK, MEDIA_CDROM } media; + int bus_id, unit_id; + int cyls, heads, secs, translation; + char *p; + char *file; + int disk_index; + + file = str; + p = str; + while (*p != '\0' && *p != ',') + p++; + if (*p == ',') { + *p = '\0'; + p++; + } + + cyls = heads = secs = 0; + bus_id = -1; + unit_id = -1; + disk_index = -1; + translation = BIOS_ATA_TRANSLATION_AUTO; + interface = IF_IDE; + media = MEDIA_DISK; + + /* extract parameters */ + + if (get_param_value(buf, sizeof(buf), "bus", p)) { + bus_id = strtol(buf, NULL, 0); + if (bus_id < 0) { + fprintf(stderr, "qemu: invalid bus id\n"); + return -1; + } + } + + if (get_param_value(buf, sizeof(buf), "unit", p)) { + unit_id = strtol(buf, NULL, 0); + if (unit_id < 0) { + fprintf(stderr, "qemu: invalid unit id\n"); + return -1; + } + } + + if (get_param_value(buf, sizeof(buf), "index", p)) { + disk_index = strtol(buf, NULL, 0); + if (disk_index < 0) { + fprintf(stderr, "qemu: invalid disk index\n"); + return -1; + } + } + + if (get_param_value(buf, sizeof(buf), "cyls", p)) { + cyls = strtol(buf, NULL, 0); + if (cyls < 1 || cyls > 16383) { + fprintf(stderr, "qemu: invalid physical cyls number\n"); + return -1; + } + } + + if (get_param_value(buf, sizeof(buf), "heads", p)) { + heads = strtol(buf, NULL, 0); + if (heads < 1 || heads > 16) { + fprintf(stderr, "qemu: invalid physical heads number\n"); + return -1; + } + } + + if (get_param_value(buf, sizeof(buf), "secs", p)) { + secs = strtol(buf, NULL, 0); + if (secs < 1 || secs > 63) { + fprintf(stderr, "qemu: invalid physical secs number\n"); + return -1; + } + } + + if (get_param_value(buf, sizeof(buf), "if", p)) { + if (!strcmp(buf, "ide")) + interface = IF_IDE; + else if (!strcmp(buf, "scsi")) + interface = IF_SCSI; + else { + fprintf(stderr, "unsupported bus type '%s'\n", buf); + return -1; + } + } + + if (get_param_value(buf, sizeof(buf), "trans", p)) { + if (!strcmp(buf, "none")) + translation = BIOS_ATA_TRANSLATION_NONE; + else if (!strcmp(buf, "lba")) + translation = BIOS_ATA_TRANSLATION_LBA; + else if (!strcmp(buf, "auto")) + translation = BIOS_ATA_TRANSLATION_AUTO; + else { + fprintf(stderr, "qemu: invalid translation type\n"); + return -1; + } + } + + if (get_param_value(buf, sizeof(buf), "media", p)) { + if (!(strcmp(buf, "disk"))) { + media = MEDIA_DISK; + } else if (!strcmp(buf, "cdrom")) { + if (cyls || secs || heads) { + fprintf(stderr, "qemu: invalid physical CHS format\n"); + return -1; + } + media = MEDIA_CDROM; + } else { + fprintf(stderr, "qemu: invalid media\n"); + return -1; + } + } + + if (get_param_value(buf, sizeof(buf), "snapshot", p)) { + if (!(strcmp(buf, "on"))) + snapshot = 1; + else if (!(strcmp(buf, "off"))) + snapshot = 0; + else { + fprintf(stderr, "qemu: invalid snapshot option\n"); + return -1; + } + } + + switch(interface) { + case IF_IDE: + if (bus_id >= MAX_IDE_BUS) { + fprintf(stderr, "qemu: bus number too big\n"); + return -1; + } + if (unit_id >= MAX_IDE_DEVS) { + fprintf(stderr, "qemu: unit number too big\n"); + return -1; + } + + if (disk_index == -1) { + if (bus_id != -1 && unit_id != -1) { + disk_index = bus_id * MAX_IDE_DEVS + unit_id; + } else { + if (bus_id != -1) + disk_index = bus_id * MAX_IDE_DEVS; + else + disk_index = 0; + while (bs_table[disk_index] && disk_index < MAX_IDE_DISKS) + disk_index++; + } + } + + if (disk_index >= MAX_IDE_DISKS) { + fprintf(stderr, "qemu: too many disks\n"); + return -1; + } + + if (!bs_table[disk_index]) { + switch(media) { + case MEDIA_DISK: + snprintf(buf, sizeof(buf), "hd%c", disk_index + 'a'); + bs_table[disk_index] = bdrv_new(buf); + break; + case MEDIA_CDROM: + snprintf(buf, sizeof(buf), "cdrom%c", disk_index + '0'); + bs_table[disk_index] = bdrv_new(buf); + bdrv_set_type_hint(bs_table[disk_index], BDRV_TYPE_CDROM); + break; + } + + if (file[0] && bdrv_open(bs_table[disk_index], file, + snapshot ? BDRV_O_SNAPSHOT : 0) < 0) { + fprintf(stderr, "qemu: could not open hard disk image '%s'\n", + file); + return -1; + } + + if (cyls != 0) { + bdrv_set_geometry_hint(bs_table[disk_index], + cyls, heads, secs); + bdrv_set_translation_hint(bs_table[disk_index], translation); + } + } + break; + case IF_SCSI: + /* TODO */ + break; + } + return 0; +} + /***********************************************************/ /* USB devices */ @@ -5489,7 +5688,7 @@ if (bs_snapshots) return bs_snapshots; - for(i = 0; i <= MAX_DISKS; i++) { + for(i = 0; i <= MAX_IDE_DISKS; i++) { bs = bs_table[i]; if (bdrv_can_snapshot(bs)) goto ok; @@ -5598,7 +5797,7 @@ /* create the snapshots */ - for(i = 0; i < MAX_DISKS; i++) { + for(i = 0; i < MAX_IDE_DISKS; i++) { bs1 = bs_table[i]; if (bdrv_has_snapshot(bs1)) { if (must_delete) { @@ -5641,7 +5840,7 @@ saved_vm_running = vm_running; vm_stop(0); - for(i = 0; i <= MAX_DISKS; i++) { + for(i = 0; i <= MAX_IDE_DISKS; i++) { bs1 = bs_table[i]; if (bdrv_has_snapshot(bs1)) { ret = bdrv_snapshot_goto(bs1, name); @@ -5702,7 +5901,7 @@ return; } - for(i = 0; i <= MAX_DISKS; i++) { + for(i = 0; i <= MAX_IDE_DISKS; i++) { bs1 = bs_table[i]; if (bdrv_has_snapshot(bs1)) { ret = bdrv_snapshot_delete(bs1, name); @@ -5731,7 +5930,7 @@ return; } term_printf("Snapshot devices:"); - for(i = 0; i <= MAX_DISKS; i++) { + for(i = 0; i <= MAX_IDE_DISKS; i++) { bs1 = bs_table[i]; if (bdrv_has_snapshot(bs1)) { if (bs == bs1) @@ -6436,7 +6635,7 @@ /* find if the memory block is available on a virtual block device */ sector_num = -1; - for(j = 0; j < MAX_DISKS; j++) { + for(j = 0; j < MAX_IDE_DISKS; j++) { if (bs_table[j]) { sector_num = bdrv_hash_find(bs_table[j], phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); @@ -6444,7 +6643,7 @@ break; } } - if (j == MAX_DISKS) + if (j == MAX_IDE_DISKS) goto normal_compress; buf[0] = 1; buf[1] = j; @@ -6495,7 +6694,7 @@ ram_decompress_buf(s, buf + 1, 9); bs_index = buf[1]; sector_num = be64_to_cpupu((const uint64_t *)(buf + 2)); - if (bs_index >= MAX_DISKS || bs_table[bs_index] == NULL) { + if (bs_index >= MAX_IDE_DISKS || bs_table[bs_index] == NULL) { fprintf(stderr, "Invalid block device index %d\n", bs_index); goto error; } @@ -6996,6 +7195,8 @@ "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n" "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n" "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n" + "-disk file[,if=type][,index=i][,bus=n][,unit=m][,media=d][,cyls=c,heads=h,secs=s[,trans=t]][snapshot=on|off]\n" + " use 'file' as a disk image\n" "-mtdblock file use 'file' as on-board Flash memory image\n" "-sd file use 'file' as SecureDigital card image\n" "-pflash file use 'file' as a parallel flash image\n" @@ -7144,6 +7345,7 @@ QEMU_OPTION_hdb, QEMU_OPTION_hdc, QEMU_OPTION_hdd, + QEMU_OPTION_disk, QEMU_OPTION_cdrom, QEMU_OPTION_mtdblock, QEMU_OPTION_sd, @@ -7232,6 +7434,7 @@ { "hdb", HAS_ARG, QEMU_OPTION_hdb }, { "hdc", HAS_ARG, QEMU_OPTION_hdc }, { "hdd", HAS_ARG, QEMU_OPTION_hdd }, + { "disk", HAS_ARG, QEMU_OPTION_disk }, { "cdrom", HAS_ARG, QEMU_OPTION_cdrom }, { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock }, { "sd", HAS_ARG, QEMU_OPTION_sd }, @@ -7354,10 +7557,10 @@ { BlockDriverState *bs; - if (index < 4) { + if (index < MAX_IDE_DISKS) { bs = bs_table[index]; - } else if (index < 6) { - bs = fd_table[index - 4]; + } else if (index < MAX_IDE_DISKS + MAX_FD) { + bs = fd_table[index - MAX_IDE_DISKS]; } else { bs = NULL; } @@ -7556,10 +7759,10 @@ int use_gdbstub; const char *gdbstub_port; #endif - int i, cdrom_index, pflash_index; + int i, pflash_index; int snapshot, linux_boot; const char *initrd_filename; - const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD]; + const char *fd_filename[MAX_FD]; const char *pflash_filename[MAX_PFLASH]; const char *sd_filename; const char *mtd_filename; @@ -7567,7 +7770,10 @@ DisplayState *ds = &display_state; int cyls, heads, secs, translation; char net_clients[MAX_NET_CLIENTS][256]; + char disks[MAX_DISKS][256]; int nb_net_clients; + int nb_disks; + int hda_index; int optind; const char *r, *optarg; CharDriverState *monitor_hd; @@ -7622,8 +7828,6 @@ initrd_filename = NULL; for(i = 0; i < MAX_FD; i++) fd_filename[i] = NULL; - for(i = 0; i < MAX_DISKS; i++) - hd_filename[i] = NULL; for(i = 0; i < MAX_PFLASH; i++) pflash_filename[i] = NULL; pflash_index = 0; @@ -7639,11 +7843,6 @@ nographic = 0; kernel_filename = NULL; kernel_cmdline = ""; -#ifdef TARGET_PPC - cdrom_index = 1; -#else - cdrom_index = 2; -#endif cyls = heads = secs = 0; translation = BIOS_ATA_TRANSLATION_AUTO; pstrcpy(monitor_device, sizeof(monitor_device), "vc"); @@ -7661,6 +7860,8 @@ usb_devices_index = 0; nb_net_clients = 0; + nb_disks = 0; + hda_index = -1; nb_nics = 0; /* default mac address of the first network interface */ @@ -7671,7 +7872,10 @@ break; r = argv[optind]; if (r[0] != '-') { - hd_filename[0] = argv[optind++]; + hda_index = nb_disks; + snprintf(disks[nb_disks], sizeof(disks[0]), + "%s" HD_ALIAS, argv[optind++], 0); + nb_disks++; } else { const QEMUOption *popt; @@ -7731,17 +7935,51 @@ initrd_filename = optarg; break; case QEMU_OPTION_hda: + if (nb_disks >= MAX_DISKS) { + fprintf(stderr, "qemu: too many disks\n"); + exit(1); + } + hda_index = nb_disks; + if (cyls == 0) + snprintf(disks[nb_disks], sizeof(disks[0]), + "%s" HD_ALIAS, optarg, 0); + else { + snprintf(disks[nb_disks], sizeof(disks[0]), + "%s" HD_ALIAS + ",cyls=%d,heads=%d,secs=%d%s", + optarg, 0, cyls, heads, secs, + translation == BIOS_ATA_TRANSLATION_LBA ? + ",trans=lba" : + translation == BIOS_ATA_TRANSLATION_NONE ? + ",trans=none" : ""); + } + nb_disks++; + break; case QEMU_OPTION_hdb: case QEMU_OPTION_hdc: case QEMU_OPTION_hdd: { int hd_index; + if (nb_disks >= MAX_DISKS) { + fprintf(stderr, "qemu: too many disks\n"); + exit(1); + } hd_index = popt->index - QEMU_OPTION_hda; - hd_filename[hd_index] = optarg; - if (hd_index == cdrom_index) - cdrom_index = -1; + snprintf(disks[nb_disks], sizeof(disks[0]), + "%s" HD_ALIAS, optarg, hd_index); + nb_disks++; } break; + case QEMU_OPTION_disk: + if (nb_disks >= MAX_DISKS) { + fprintf(stderr, "qemu: too many disks\n"); + exit(1); + } + pstrcpy(disks[nb_disks], + sizeof(disks[0]), + optarg); + nb_disks++; + break; case QEMU_OPTION_mtdblock: mtd_filename = optarg; break; @@ -7792,6 +8030,15 @@ fprintf(stderr, "qemu: invalid physical CHS format\n"); exit(1); } + if (hda_index != -1) + snprintf(disks[hda_index] + strlen(disks[hda_index]), + sizeof(disks[0]) - strlen(disks[hda_index]), + ",cyls=%d,heads=%d,secs=%d%s", + cyls, heads, secs, + translation == BIOS_ATA_TRANSLATION_LBA ? + ",trans=lba" : + translation == BIOS_ATA_TRANSLATION_NONE ? + ",trans=none" : ""); } break; case QEMU_OPTION_nographic: @@ -7810,9 +8057,13 @@ kernel_cmdline = optarg; break; case QEMU_OPTION_cdrom: - if (cdrom_index >= 0) { - hd_filename[cdrom_index] = optarg; + if (nb_disks >= MAX_DISKS) { + fprintf(stderr, "qemu: too many disks\n"); + exit(1); } + snprintf(disks[nb_disks], sizeof(disks[0]), + "%s" CDROM_ALIAS, optarg); + nb_disks++; break; case QEMU_OPTION_boot: if (strlen(optarg) > MAX_BOOT_DEVICES) { @@ -8179,14 +8430,13 @@ if (!linux_boot && (!strchr(boot_device, 'n')) && - hd_filename[0] == '\0' && - (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') && + nb_disks == 0 && fd_filename[0] == '\0') help(1); /* boot to floppy or the default cd if no hard disk defined yet */ if (!boot_device[0]) { - if (hd_filename[0] != '\0') + if (nb_disks) boot_device[0] = 'c'; else if (fd_filename[0] != '\0') boot_device[0] = 'a'; @@ -8261,32 +8511,20 @@ exit(1); } - /* we always create the cdrom drive, even if no disk is there */ bdrv_init(); - if (cdrom_index >= 0) { - bs_table[cdrom_index] = bdrv_new("cdrom"); - bdrv_set_type_hint(bs_table[cdrom_index], BDRV_TYPE_CDROM); + + /* we always create the cdrom drive, even if no disk is there */ + + if (nb_disks < MAX_DISKS) { + pstrcpy(disks[nb_disks], sizeof(disks[0]), CDROM_ALIAS); + nb_disks++; } /* open the virtual block devices */ - for(i = 0; i < MAX_DISKS; i++) { - if (hd_filename[i]) { - if (!bs_table[i]) { - char buf[64]; - snprintf(buf, sizeof(buf), "hd%c", i + 'a'); - bs_table[i] = bdrv_new(buf); - } - if (bdrv_open(bs_table[i], hd_filename[i], snapshot ? BDRV_O_SNAPSHOT : 0) < 0) { - fprintf(stderr, "qemu: could not open hard disk image '%s'\n", - hd_filename[i]); - exit(1); - } - if (i == 0 && cyls != 0) { - bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs); - bdrv_set_translation_hint(bs_table[i], translation); - } - } - } + + for(i = 0; i < nb_disks; i++) + if (disk_init(disks[i], snapshot) == -1) + exit(1); /* we always create at least one floppy disk */ fd_table[0] = bdrv_new("fda"); Index: qemu/vl.h =================================================================== --- qemu.orig/vl.h 2007-10-31 14:12:40.000000000 +0100 +++ qemu/vl.h 2007-10-31 14:13:20.000000000 +0100 @@ -984,9 +984,13 @@ extern uint8_t _translate_keycode(const int key); /* ide.c */ -#define MAX_DISKS 4 +#define MAX_IDE_BUS 2 +#define MAX_IDE_DEVS 2 +#define MAX_IDE_DISKS (MAX_IDE_BUS * MAX_IDE_DEVS) -extern BlockDriverState *bs_table[MAX_DISKS + 1]; +#define MAX_DISKS (MAX_IDE_DISKS) + +extern BlockDriverState *bs_table[MAX_IDE_DISKS + 1]; extern BlockDriverState *sd_bdrv; extern BlockDriverState *mtd_bdrv; Index: qemu/monitor.c =================================================================== --- qemu.orig/monitor.c 2007-10-31 14:12:18.000000000 +0100 +++ qemu/monitor.c 2007-10-31 14:13:20.000000000 +0100 @@ -204,7 +204,7 @@ int i, all_devices; all_devices = !strcmp(device, "all"); - for (i = 0; i < MAX_DISKS; i++) { + for (i = 0; i < MAX_IDE_DISKS; i++) { if (bs_table[i]) { if (all_devices || !strcmp(bdrv_get_device_name(bs_table[i]), device)) Index: qemu/hw/mips_pica61.c =================================================================== --- qemu.orig/hw/mips_pica61.c 2007-10-31 14:12:40.000000000 +0100 +++ qemu/hw/mips_pica61.c 2007-10-31 14:13:20.000000000 +0100 @@ -136,9 +136,10 @@ i8042_mm_init(i8259[6], i8259[7], 0x80005060, 0); /* IDE controller */ - for(i = 0; i < 2; i++) + for(i = 0; i < MAX_IDE_BUS; i++) isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]], - bs_table[2 * i], bs_table[2 * i + 1]); + bs_table[MAX_IDE_DEVS * i], + bs_table[MAX_IDE_DEVS * i + 1]); /* Network controller */ /* FIXME: missing NS SONIC DP83932 */ Index: qemu/hw/mips_r4k.c =================================================================== --- qemu.orig/hw/mips_r4k.c 2007-10-31 14:12:40.000000000 +0100 +++ qemu/hw/mips_r4k.c 2007-10-31 14:13:20.000000000 +0100 @@ -240,9 +240,10 @@ } } - for(i = 0; i < 2; i++) + for(i = 0; i < MAX_IDE_BUS; i++) isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]], - bs_table[2 * i], bs_table[2 * i + 1]); + bs_table[MAX_IDE_DEVS * i], + bs_table[MAX_IDE_DEVS * i + 1]); i8042_init(i8259[1], i8259[12], 0x60); ds1225y_init(0x9000, "nvram"); Index: qemu/hw/pc.c =================================================================== --- qemu.orig/hw/pc.c 2007-10-31 14:12:40.000000000 +0100 +++ qemu/hw/pc.c 2007-10-31 14:13:20.000000000 +0100 @@ -892,9 +892,10 @@ if (pci_enabled) { pci_piix3_ide_init(pci_bus, bs_table, piix3_devfn + 1, i8259); } else { - for(i = 0; i < 2; i++) { + for(i = 0; i < MAX_IDE_BUS; i++) { isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]], - bs_table[2 * i], bs_table[2 * i + 1]); + bs_table[MAX_IDE_DEVS * i], + bs_table[MAX_IDE_DEVS * i + 1]); } } Index: qemu/hw/ppc_prep.c =================================================================== --- qemu.orig/hw/ppc_prep.c 2007-10-31 14:12:40.000000000 +0100 +++ qemu/hw/ppc_prep.c 2007-10-31 14:13:20.000000000 +0100 @@ -649,9 +649,10 @@ } } - for(i = 0; i < 2; i++) { + for(i = 0; i < MAX_IDE_BUS; i++) { isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]], - bs_table[2 * i], bs_table[2 * i + 1]); + bs_table[MAX_IDE_DEVS * i], + bs_table[MAX_IDE_DEVS * i + 1]); } i8042_init(i8259[1], i8259[12], 0x60); DMA_init(1);