Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
clh: Enable disk block device hotplug support
Browse files Browse the repository at this point in the history
With this patch, the container image can be shared from host with guest
as a block device when the 'devicemapper' is used as the storage driver
for docker. Note: The 'block_device_driver="virtio-blk"' entry is
required in the hypervisor config file to work properly.

Fixes: #2570

Signed-off-by: Bo Chen <[email protected]>
  • Loading branch information
likebreath committed May 13, 2020
1 parent 80bd453 commit c5f97b2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
33 changes: 33 additions & 0 deletions virtcontainers/clh.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ type clhClient interface {
VmResizePut(ctx context.Context, vmResize chclient.VmResize) (*http.Response, error)
// Add VFIO PCI device to the VM
VmAddDevicePut(ctx context.Context, vmAddDevice chclient.VmAddDevice) (*http.Response, error)
// Add a new disk device to the VM
VmAddDiskPut(ctx context.Context, diskConfig chclient.DiskConfig) (*http.Response, error)
}

type CloudHypervisorVersion struct {
Expand Down Expand Up @@ -410,6 +412,33 @@ func (clh *cloudHypervisor) getThreadIDs() (vcpuThreadIDs, error) {
return vcpuInfo, nil
}

func (clh *cloudHypervisor) hotplugBlockDevice(drive *config.BlockDrive) error {
cl := clh.client()
ctx, cancel := context.WithTimeout(context.Background(), clhHotPlugAPITimeout*time.Second)
defer cancel()

_, _, err := cl.VmmPingGet(ctx)
if err != nil {
return openAPIClientError(err)
}

if drive.Pmem {
err = fmt.Errorf("pmem device hotplug not supported")
} else {
blkDevice := chclient.DiskConfig{
Path: drive.File,
Readonly: drive.ReadOnly,
VhostUser: false,
}
_, err = cl.VmAddDiskPut(ctx, blkDevice)
}

if err != nil {
err = fmt.Errorf("failed to hotplug block device %+v %s", drive, openAPIClientError(err))
}
return err
}

func (clh *cloudHypervisor) hotPlugVFIODevice(device config.VFIODev) error {
cl := clh.client()
ctx, cancel := context.WithTimeout(context.Background(), clhHotPlugAPITimeout*time.Second)
Expand All @@ -432,6 +461,9 @@ func (clh *cloudHypervisor) hotplugAddDevice(devInfo interface{}, devType device
defer span.Finish()

switch devType {
case blockDev:
drive := devInfo.(*config.BlockDrive)
return nil, clh.hotplugBlockDevice(drive)
case vfioDev:
device := devInfo.(*config.VFIODev)
return nil, clh.hotPlugVFIODevice(*device)
Expand Down Expand Up @@ -663,6 +695,7 @@ func (clh *cloudHypervisor) capabilities() types.Capabilities {
clh.Logger().WithField("function", "capabilities").Info("get Capabilities")
var caps types.Capabilities
caps.SetFsSharingSupport()
caps.SetBlockDeviceHotplugSupport()
return caps
}

Expand Down
27 changes: 27 additions & 0 deletions virtcontainers/clh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func (c *clhClientMock) VmAddDevicePut(ctx context.Context, vmAddDevice chclient
return nil, nil
}

//nolint:golint
func (c *clhClientMock) VmAddDiskPut(ctx context.Context, diskConfig chclient.DiskConfig) (*http.Response, error) {
return nil, nil
}

func TestCloudHypervisorAddVSock(t *testing.T) {
assert := assert.New(t)
clh := cloudHypervisor{}
Expand Down Expand Up @@ -357,3 +362,25 @@ func TestCheckVersion(t *testing.T) {
}
}
}

func TestCloudHypervisorHotplugBlockDevice(t *testing.T) {
assert := assert.New(t)

clhConfig, err := newClhConfig()
assert.NoError(err)

clh := &cloudHypervisor{}
clh.config = clhConfig
clh.APIClient = &clhClientMock{}

clh.config.BlockDeviceDriver = config.VirtioBlock
err = clh.hotplugBlockDevice(&config.BlockDrive{Pmem: false})
assert.NoError(err, "Hotplug disk block device expected no error")

err = clh.hotplugBlockDevice(&config.BlockDrive{Pmem: true})
assert.Error(err, "Hotplug pmem block device expected error")

clh.config.BlockDeviceDriver = config.VirtioSCSI
err = clh.hotplugBlockDevice(&config.BlockDrive{Pmem: false})
assert.Error(err, "Hotplug block device not using 'virtio-blk' expected error")
}

0 comments on commit c5f97b2

Please sign in to comment.