Commit 1dae12e6d02f8714c04bc0681ef4527980b13a9a

Authored by Gerd Hoffmann
Committed by Anthony Liguori
1 parent 751c6a17

add support for drive ids.

-drive accepts the new id= now, allowing to explicitely name your
drives.  They will show up with that name in "info block" if specified,
otherwise the existing namimg scheme is used to autogenerate one.

There is also a new function to lookup drives by name.  Not used yet.
The plan is to link disk drivers and drives using the drive id instead
of passing around pointers to BlockDriveState.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing 2 changed files with 34 additions and 18 deletions
sysemu.h
@@ -161,6 +161,7 @@ typedef enum { @@ -161,6 +161,7 @@ typedef enum {
161 161
162 typedef struct DriveInfo { 162 typedef struct DriveInfo {
163 BlockDriverState *bdrv; 163 BlockDriverState *bdrv;
  164 + char *id;
164 const char *devaddr; 165 const char *devaddr;
165 BlockInterfaceType type; 166 BlockInterfaceType type;
166 int bus; 167 int bus;
@@ -178,6 +179,7 @@ typedef struct DriveInfo { @@ -178,6 +179,7 @@ typedef struct DriveInfo {
178 extern TAILQ_HEAD(drivelist, DriveInfo) drives; 179 extern TAILQ_HEAD(drivelist, DriveInfo) drives;
179 180
180 extern DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit); 181 extern DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit);
  182 +extern DriveInfo *drive_get_by_id(char *id);
181 extern int drive_get_max_bus(BlockInterfaceType type); 183 extern int drive_get_max_bus(BlockInterfaceType type);
182 extern void drive_uninit(BlockDriverState *bdrv); 184 extern void drive_uninit(BlockDriverState *bdrv);
183 extern void drive_remove(int index); 185 extern void drive_remove(int index);
@@ -1920,6 +1920,18 @@ DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) @@ -1920,6 +1920,18 @@ DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
1920 return NULL; 1920 return NULL;
1921 } 1921 }
1922 1922
  1923 +DriveInfo *drive_get_by_id(char *id)
  1924 +{
  1925 + DriveInfo *dinfo;
  1926 +
  1927 + TAILQ_FOREACH(dinfo, &drives, next) {
  1928 + if (strcmp(id, dinfo->id))
  1929 + continue;
  1930 + return dinfo;
  1931 + }
  1932 + return NULL;
  1933 +}
  1934 +
1923 int drive_get_max_bus(BlockInterfaceType type) 1935 int drive_get_max_bus(BlockInterfaceType type)
1924 { 1936 {
1925 int max_bus; 1937 int max_bus;
@@ -1989,7 +2001,6 @@ DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *opaque, @@ -1989,7 +2001,6 @@ DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *opaque,
1989 enum { MEDIA_DISK, MEDIA_CDROM } media; 2001 enum { MEDIA_DISK, MEDIA_CDROM } media;
1990 int bus_id, unit_id; 2002 int bus_id, unit_id;
1991 int cyls, heads, secs, translation; 2003 int cyls, heads, secs, translation;
1992 - BlockDriverState *bdrv;  
1993 BlockDriver *drv = NULL; 2004 BlockDriver *drv = NULL;
1994 QEMUMachine *machine = opaque; 2005 QEMUMachine *machine = opaque;
1995 int max_devs; 2006 int max_devs;
@@ -2003,7 +2014,7 @@ DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *opaque, @@ -2003,7 +2014,7 @@ DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *opaque,
2003 "cyls", "heads", "secs", "trans", 2014 "cyls", "heads", "secs", "trans",
2004 "media", "snapshot", "file", 2015 "media", "snapshot", "file",
2005 "cache", "format", "serial", 2016 "cache", "format", "serial",
2006 - "werror", "addr", 2017 + "werror", "addr", "id",
2007 NULL }; 2018 NULL };
2008 *fatal_error = 1; 2019 *fatal_error = 1;
2009 2020
@@ -2279,17 +2290,20 @@ DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *opaque, @@ -2279,17 +2290,20 @@ DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *opaque,
2279 2290
2280 /* init */ 2291 /* init */
2281 2292
2282 - if (type == IF_IDE || type == IF_SCSI)  
2283 - mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";  
2284 - if (max_devs)  
2285 - snprintf(buf, sizeof(buf), "%s%i%s%i",  
2286 - devname, bus_id, mediastr, unit_id);  
2287 - else  
2288 - snprintf(buf, sizeof(buf), "%s%s%i",  
2289 - devname, mediastr, unit_id);  
2290 - bdrv = bdrv_new(buf);  
2291 dinfo = qemu_mallocz(sizeof(*dinfo)); 2293 dinfo = qemu_mallocz(sizeof(*dinfo));
2292 - dinfo->bdrv = bdrv; 2294 + if (!get_param_value(buf, sizeof(buf), "id", str)) {
  2295 + /* no id supplied -> create one */
  2296 + if (type == IF_IDE || type == IF_SCSI)
  2297 + mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
  2298 + if (max_devs)
  2299 + snprintf(buf, sizeof(buf), "%s%i%s%i",
  2300 + devname, bus_id, mediastr, unit_id);
  2301 + else
  2302 + snprintf(buf, sizeof(buf), "%s%s%i",
  2303 + devname, mediastr, unit_id);
  2304 + }
  2305 + dinfo->id = qemu_strdup(buf);
  2306 + dinfo->bdrv = bdrv_new(dinfo->id);
2293 dinfo->devaddr = devaddr; 2307 dinfo->devaddr = devaddr;
2294 dinfo->type = type; 2308 dinfo->type = type;
2295 dinfo->bus = bus_id; 2309 dinfo->bus = bus_id;
@@ -2306,12 +2320,12 @@ DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *opaque, @@ -2306,12 +2320,12 @@ DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *opaque,
2306 switch(media) { 2320 switch(media) {
2307 case MEDIA_DISK: 2321 case MEDIA_DISK:
2308 if (cyls != 0) { 2322 if (cyls != 0) {
2309 - bdrv_set_geometry_hint(bdrv, cyls, heads, secs);  
2310 - bdrv_set_translation_hint(bdrv, translation); 2323 + bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
  2324 + bdrv_set_translation_hint(dinfo->bdrv, translation);
2311 } 2325 }
2312 break; 2326 break;
2313 case MEDIA_CDROM: 2327 case MEDIA_CDROM:
2314 - bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM); 2328 + bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
2315 break; 2329 break;
2316 } 2330 }
2317 break; 2331 break;
@@ -2319,7 +2333,7 @@ DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *opaque, @@ -2319,7 +2333,7 @@ DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *opaque,
2319 /* FIXME: This isn't really a floppy, but it's a reasonable 2333 /* FIXME: This isn't really a floppy, but it's a reasonable
2320 approximation. */ 2334 approximation. */
2321 case IF_FLOPPY: 2335 case IF_FLOPPY:
2322 - bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY); 2336 + bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
2323 break; 2337 break;
2324 case IF_PFLASH: 2338 case IF_PFLASH:
2325 case IF_MTD: 2339 case IF_MTD:
@@ -2341,12 +2355,12 @@ DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *opaque, @@ -2341,12 +2355,12 @@ DriveInfo *drive_init(struct drive_opt *arg, int snapshot, void *opaque,
2341 bdrv_flags |= BDRV_O_NOCACHE; 2355 bdrv_flags |= BDRV_O_NOCACHE;
2342 else if (cache == 2) /* write-back */ 2356 else if (cache == 2) /* write-back */
2343 bdrv_flags |= BDRV_O_CACHE_WB; 2357 bdrv_flags |= BDRV_O_CACHE_WB;
2344 - if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) { 2358 + if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
2345 fprintf(stderr, "qemu: could not open disk image %s\n", 2359 fprintf(stderr, "qemu: could not open disk image %s\n",
2346 file); 2360 file);
2347 return NULL; 2361 return NULL;
2348 } 2362 }
2349 - if (bdrv_key_required(bdrv)) 2363 + if (bdrv_key_required(dinfo->bdrv))
2350 autostart = 0; 2364 autostart = 0;
2351 *fatal_error = 0; 2365 *fatal_error = 0;
2352 return dinfo; 2366 return dinfo;