[0/4] Add virtio disk identification support

Submitted by john cooper on July 2, 2010, 5:50 a.m.

Details

Message ID 4C2D7E17.10105@redhat.com
State New
Headers show

Commit Message

john cooper July 2, 2010, 5:50 a.m.
This patch adds the final missing bits for support of
passing a serial/id string to a virtio-blk guest driver.

The guest-side component already exists in the virtio
driver, and has recently been reworked by Ryan to export
a /sys interface for retrival of the id from guest userland.

Signed-off-by: john cooper<john.cooper@redhat.com>
---

Comments

john cooper July 2, 2010, 6:27 a.m.
Markus Armbruster wrote:
> Subject is confusing: suggests a series of four parts.

Sorry.  My bad for recycling old mail.

> Looks like this conflicts with my "PATCH v3 00/13] More block-related
> fixes and cleanups".
> 
> Perhaps the easiest way to get this in would be to rebase to Kevin's
> block branch, which already has my series, and cc the respin to him.
> See ide_dev_initfn() and scsi_initfn() there.
> 
> Kevin's tree: git://repo.or.cz/qemu/kevin.git

Will do.

-john
Markus Armbruster July 2, 2010, 6:39 a.m.
Subject is confusing: suggests a series of four parts.

john cooper <john.cooper@redhat.com> writes:

> This patch adds the final missing bits for support of
> passing a serial/id string to a virtio-blk guest driver.
>
> The guest-side component already exists in the virtio
> driver, and has recently been reworked by Ryan to export
> a /sys interface for retrival of the id from guest userland.
>
> Signed-off-by: john cooper<john.cooper@redhat.com>

Looks like this conflicts with my "PATCH v3 00/13] More block-related
fixes and cleanups".

Perhaps the easiest way to get this in would be to rebase to Kevin's
block branch, which already has my series, and cc the respin to him.
See ide_dev_initfn() and scsi_initfn() there.

Kevin's tree: git://repo.or.cz/qemu/kevin.git

Patch hide | download patch | download mbox

diff --git a/hw/virtio-blk.c b/hw/virtio-blk.c
index 0bf929a..bd6b896 100644
--- a/hw/virtio-blk.c
+++ b/hw/virtio-blk.c
@@ -26,6 +26,7 @@  typedef struct VirtIOBlock
     QEMUBH *bh;
     BlockConf *conf;
     unsigned short sector_mask;
+    char sn[BLOCK_SERIAL_STRLEN];
 } VirtIOBlock;
 
 static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
@@ -324,6 +325,12 @@  static void virtio_blk_handle_request(VirtIOBlockReq *req,
         virtio_blk_handle_flush(req, mrb);
     } else if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) {
         virtio_blk_handle_scsi(req);
+    } else if (req->out->type & VIRTIO_BLK_T_GET_ID) {
+        VirtIOBlock *s = req->dev;
+
+        memcpy(req->elem.in_sg[0].iov_base, s->sn,
+               MIN(req->elem.in_sg[0].iov_len, sizeof(s->sn)));
+        virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
     } else if (req->out->type & VIRTIO_BLK_T_OUT) {
         qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
                                  req->elem.out_num - 1);
@@ -495,6 +502,12 @@  VirtIODevice *virtio_blk_init(DeviceState *dev, BlockConf *conf)
     s->sector_mask = (s->conf->logical_block_size / BDRV_SECTOR_SIZE) - 1;
     bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
 
+    /* NB: per existing s/n string convention we really intend to hard-limit
+     * the copy length to sizeof (s->sn) even in the case we're left without
+     * a trailing '\0'
+     */
+    strncpy(s->sn, drive_get_serial(s->bs), sizeof (s->sn));
+
     s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
 
     qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
diff --git a/hw/virtio-blk.h b/hw/virtio-blk.h
index 7a7ece3..fff46da 100644
--- a/hw/virtio-blk.h
+++ b/hw/virtio-blk.h
@@ -59,6 +59,9 @@  struct virtio_blk_config
 /* Flush the volatile write cache */
 #define VIRTIO_BLK_T_FLUSH      4
 
+/* return the device ID string */
+#define VIRTIO_BLK_T_GET_ID     8
+
 /* Barrier before this op. */
 #define VIRTIO_BLK_T_BARRIER    0x80000000