virtio-blk: add default serial id
PATCH 1/1 virtio-blk: add default serial id
For virtio block device, if user does not specify the serial attribute, There will be not serial availabe, this is not convinient for identify the disk.
Doing something similar to ide disks, add a "VD0000?" default serial number if user does not specify it.
--- hw/virtio-blk.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
hw/virtio-blk.c +8 -1
@@ -22,6 +22,7 @@# include <scsi/sg.h> #endif +#define DEFAULT_VIRTIO_BLK_SERIAL_LEN 8 typedef struct VirtIOBlock { VirtIODevice vdev;
@@ -33,6 +34,7 @@ typedef struct VirtIOBlockVirtIOBlkConf *blk;unsigned short sector_mask;DeviceState *qdev; +int drive_serial; } VirtIOBlock;static VirtIOBlock *to_virtio_blk (VirtIODevice *vdev)
@@ -364,6 +366,7 @@ static void virtio_blk_handle_request(Vi MultiReqBuffer *mrb) {uint32_t type; +char serial[DEFAULT_VIRTIO_BLK_SERIAL_LEN ];if (req->elem.out_num <1 || req->elem.in_num <1 ) {error_report ("virtio-blk missing headers" );
@@ -388,12 +391,14 @@ static void virtio_blk_handle_request(Vi }else if (type &VIRTIO_BLK_T_GET_ID ) {VirtIOBlock *s = req->dev; +snprintf (serial,DEFAULT_VIRTIO_BLK_SERIAL_LEN , +"VD%05d" , s->drive_serial);/* * NB: per existing s/n string convention the string is * terminated by '\0' only when shorter than buffer. */ strncpy (req->elem.in_sg[0 ].iov_base, - s->blk->serial ? s->blk->serial :"" , + s->blk->serial ? s->blk->serial : serial,MIN (req->elem.in_sg[0 ].iov_len,VIRTIO_BLK_ID_BYTES ));virtio_blk_req_complete (req,VIRTIO_BLK_S_OK );g_free (req);
@@ -611,6 +616,7 @@ static const BlockDevOps virtio_block_opVirtIODevice *virtio_blk_init (DeviceState *dev,VirtIOBlkConf *blk) { +static int drive_serial =1 ;VirtIOBlock *s;static int virtio_blk_id;
@@ -632,6 +638,7 @@ VirtIODevice *virtio_blk_init(DeviceStatsizeof (struct virtio_blk_config ), sizeof(VirtIOBlock)); + s->drive_serial = drive_serial++; s->vdev.get_config = virtio_blk_update_config; s->vdev.set_config = virtio_blk_set_config; s->vdev.get_features = virtio_blk_get_features;
Quoted 2 lines · Dave Young
For virtio block device, if user does not specify the serial attribute, There will be not serial availabe, this is not convinient for identify
s/not serial availabe/no serial available/
s/convinient/convenient/
s/identify/identifying/
Quoted 16 lines
the disk.
Doing something similar to ide disks, add a "VD0000?" default serial number if user does not specify it.
Signed-off-by Dave Young <[email protected]>---
hw/virtio-blk.c +2
-0+#define DEFAULT_VIRTIO_BLK_SERIAL_LEN 8 +char serial[DEFAULT_VIRTIO_BLK_SERIAL_LEN ];if (req->elem.out_num <1 || req->elem.in_num <1 ) {error_report ("virtio-blk missing headers" );
hw/virtio-blk.c static void virtio_blk_handle_request(Vi · line 395
Quoted 5 lines
hw/virtio-blk.c +2
-0@@ -388,12 +391,14 @@ static void virtio_blk_handle_request(Vi }else if (type &VIRTIO_BLK_T_GET_ID ) {VirtIOBlock *s = req->dev; +snprintf (serial,DEFAULT_VIRTIO_BLK_SERIAL_LEN , +"VD%05d" , s->drive_serial);
So if I cycle through hotplug and unplug for 100000 times, my string will now be silently truncated, and I can end up with disks with shared serial numbers?
Signature · 2 lines
Eric Blake [email protected] +1-919-301-3266 Libvirt virtualization library http://libvirt.org