govmm: Refactor qmp functions for adding block device

Instead of passing a bunch of arguments to qmp functions for
adding block devices, use govmm BlockDevice structure to reduce these.

Signed-off-by: Archana Shinde <archana.m.shinde@intel.com>
This commit is contained in:
Archana Shinde 2022-08-02 23:51:34 -07:00
parent 598884f374
commit c1e3b8f40f
3 changed files with 35 additions and 19 deletions

View File

@ -771,18 +771,18 @@ func (q *QMP) ExecuteQuit(ctx context.Context) error {
return q.executeCommand(ctx, "quit", nil, nil) return q.executeCommand(ctx, "quit", nil, nil)
} }
func (q *QMP) blockdevAddBaseArgs(driver, device, blockdevID, aio string, ro bool) map[string]interface{} { func (q *QMP) blockdevAddBaseArgs(driver string, blockDevice *BlockDevice) map[string]interface{} {
blockdevArgs := map[string]interface{}{ blockdevArgs := map[string]interface{}{
"driver": "raw", "driver": "raw",
"read-only": ro, "read-only": blockDevice.ReadOnly,
"file": map[string]interface{}{ "file": map[string]interface{}{
"driver": driver, "driver": driver,
"filename": device, "filename": blockDevice.File,
"aio": aio, "aio": string(blockDevice.AIO),
}, },
} }
blockdevArgs["node-name"] = blockdevID blockdevArgs["node-name"] = blockDevice.ID
return blockdevArgs return blockdevArgs
} }
@ -791,8 +791,8 @@ func (q *QMP) blockdevAddBaseArgs(driver, device, blockdevID, aio string, ro boo
// path of the device to add, e.g., /dev/rdb0, and blockdevID is an identifier // path of the device to add, e.g., /dev/rdb0, and blockdevID is an identifier
// used to name the device. As this identifier will be passed directly to QMP, // used to name the device. As this identifier will be passed directly to QMP,
// it must obey QMP's naming rules, e,g., it must start with a letter. // it must obey QMP's naming rules, e,g., it must start with a letter.
func (q *QMP) ExecuteBlockdevAdd(ctx context.Context, device, blockdevID, aio string, ro bool) error { func (q *QMP) ExecuteBlockdevAdd(ctx context.Context, blockDevice *BlockDevice) error {
args := q.blockdevAddBaseArgs("host_device", device, blockdevID, aio, ro) args := q.blockdevAddBaseArgs("host_device", blockDevice)
return q.executeCommand(ctx, "blockdev-add", args, nil) return q.executeCommand(ctx, "blockdev-add", args, nil)
} }
@ -804,8 +804,8 @@ func (q *QMP) ExecuteBlockdevAdd(ctx context.Context, device, blockdevID, aio st
// direct denotes whether use of O_DIRECT (bypass the host page cache) // direct denotes whether use of O_DIRECT (bypass the host page cache)
// is enabled. noFlush denotes whether flush requests for the device are // is enabled. noFlush denotes whether flush requests for the device are
// ignored. // ignored.
func (q *QMP) ExecuteBlockdevAddWithCache(ctx context.Context, device, blockdevID, aio string, direct, noFlush, ro bool) error { func (q *QMP) ExecuteBlockdevAddWithCache(ctx context.Context, blockDevice *BlockDevice, direct, noFlush bool) error {
blockdevArgs := q.blockdevAddBaseArgs("host_device", device, blockdevID, aio, ro) blockdevArgs := q.blockdevAddBaseArgs("host_device", blockDevice)
blockdevArgs["cache"] = map[string]interface{}{ blockdevArgs["cache"] = map[string]interface{}{
"direct": direct, "direct": direct,
@ -818,8 +818,8 @@ func (q *QMP) ExecuteBlockdevAddWithCache(ctx context.Context, device, blockdevI
// ExecuteBlockdevAddWithDriverCache has three one parameter driver // ExecuteBlockdevAddWithDriverCache has three one parameter driver
// than ExecuteBlockdevAddWithCache. // than ExecuteBlockdevAddWithCache.
// Parameter driver can set the driver of block device. // Parameter driver can set the driver of block device.
func (q *QMP) ExecuteBlockdevAddWithDriverCache(ctx context.Context, driver, device, blockdevID, aio string, direct, noFlush, ro bool) error { func (q *QMP) ExecuteBlockdevAddWithDriverCache(ctx context.Context, driver string, blockDevice *BlockDevice, direct, noFlush bool) error {
blockdevArgs := q.blockdevAddBaseArgs(driver, device, blockdevID, aio, ro) blockdevArgs := q.blockdevAddBaseArgs(driver, blockDevice)
blockdevArgs["cache"] = map[string]interface{}{ blockdevArgs["cache"] = map[string]interface{}{
"direct": direct, "direct": direct,

View File

@ -400,8 +400,13 @@ func TestQMPBlockdevAdd(t *testing.T) {
cfg := QMPConfig{Logger: qmpTestLogger{}} cfg := QMPConfig{Logger: qmpTestLogger{}}
q := startQMPLoop(buf, cfg, connectedCh, disconnectedCh) q := startQMPLoop(buf, cfg, connectedCh, disconnectedCh)
q.version = checkVersion(t, connectedCh) q.version = checkVersion(t, connectedCh)
err := q.ExecuteBlockdevAdd(context.Background(), "/dev/rbd0", dev := BlockDevice{
fmt.Sprintf("drive_%s", volumeUUID), false) ID: fmt.Sprintf("drive_%s", volumeUUID),
File: "/dev/rbd0",
ReadOnly: false,
AIO: Native,
}
err := q.ExecuteBlockdevAdd(context.Background(), &dev)
if err != nil { if err != nil {
t.Fatalf("Unexpected error %v", err) t.Fatalf("Unexpected error %v", err)
} }
@ -424,8 +429,13 @@ func TestQMPBlockdevAddWithCache(t *testing.T) {
cfg := QMPConfig{Logger: qmpTestLogger{}} cfg := QMPConfig{Logger: qmpTestLogger{}}
q := startQMPLoop(buf, cfg, connectedCh, disconnectedCh) q := startQMPLoop(buf, cfg, connectedCh, disconnectedCh)
q.version = checkVersion(t, connectedCh) q.version = checkVersion(t, connectedCh)
err := q.ExecuteBlockdevAddWithCache(context.Background(), "/dev/rbd0", dev := BlockDevice{
fmt.Sprintf("drive_%s", volumeUUID), true, true, false) ID: fmt.Sprintf("drive_%s", volumeUUID),
File: "/dev/rbd0",
ReadOnly: false,
AIO: Native,
}
err := q.ExecuteBlockdevAddWithCache(context.Background(), &dev, true, true)
if err != nil { if err != nil {
t.Fatalf("Unexpected error %v", err) t.Fatalf("Unexpected error %v", err)
} }

View File

@ -1292,13 +1292,19 @@ func (q *qemu) hotplugAddBlockDevice(ctx context.Context, drive *config.BlockDri
return nil return nil
} }
aio := q.config.BlockDeviceAIO qblkDevice := govmmQemu.BlockDevice{
ID: drive.ID,
File: drive.File,
ReadOnly: drive.ReadOnly,
AIO: govmmQemu.BlockDeviceAIO(q.config.BlockDeviceAIO),
}
if drive.Swap { if drive.Swap {
err = q.qmpMonitorCh.qmp.ExecuteBlockdevAddWithDriverCache(q.qmpMonitorCh.ctx, "file", drive.File, drive.ID, aio, false, false, false) err = q.qmpMonitorCh.qmp.ExecuteBlockdevAddWithDriverCache(q.qmpMonitorCh.ctx, "file", &qblkDevice, false, false)
} else if q.config.BlockDeviceCacheSet { } else if q.config.BlockDeviceCacheSet {
err = q.qmpMonitorCh.qmp.ExecuteBlockdevAddWithCache(q.qmpMonitorCh.ctx, drive.File, drive.ID, aio, q.config.BlockDeviceCacheDirect, q.config.BlockDeviceCacheNoflush, drive.ReadOnly) err = q.qmpMonitorCh.qmp.ExecuteBlockdevAddWithCache(q.qmpMonitorCh.ctx, &qblkDevice, q.config.BlockDeviceCacheDirect, q.config.BlockDeviceCacheNoflush)
} else { } else {
err = q.qmpMonitorCh.qmp.ExecuteBlockdevAdd(q.qmpMonitorCh.ctx, drive.File, drive.ID, aio, drive.ReadOnly) err = q.qmpMonitorCh.qmp.ExecuteBlockdevAdd(q.qmpMonitorCh.ctx, &qblkDevice)
} }
if err != nil { if err != nil {
return err return err