virtcontainers: reimplement createBlockDevices

Reimplement `createBlockDevices` to identify possible volumes that can be
used as pmem devices

Signed-off-by: Julio Montes <julio.montes@intel.com>
This commit is contained in:
Julio Montes 2020-03-20 14:12:38 +00:00
parent abbdf078cd
commit 84e0ee13c8
2 changed files with 37 additions and 10 deletions

View File

@ -643,6 +643,11 @@ func filterDevices(c *Container, devices []ContainerDevice) (ret []ContainerDevi
} }
func (c *Container) createBlockDevices() error { func (c *Container) createBlockDevices() error {
if !c.checkBlockDeviceSupport() {
c.Logger().Warn("Block device not supported")
return nil
}
// iterate all mounts and create block device if it's block based. // iterate all mounts and create block device if it's block based.
for i, m := range c.mounts { for i, m := range c.mounts {
if len(m.BlockDeviceID) > 0 || m.Type != "bind" { if len(m.BlockDeviceID) > 0 || m.Type != "bind" {
@ -657,18 +662,36 @@ func (c *Container) createBlockDevices() error {
return fmt.Errorf("stat %q failed: %v", m.Source, err) return fmt.Errorf("stat %q failed: %v", m.Source, err)
} }
var di *config.DeviceInfo
var err error
// Check if mount is a block device file. If it is, the block device will be attached to the host // Check if mount is a block device file. If it is, the block device will be attached to the host
// instead of passing this as a shared mount. // instead of passing this as a shared mount.
if c.checkBlockDeviceSupport() && stat.Mode&unix.S_IFBLK == unix.S_IFBLK { if stat.Mode&unix.S_IFBLK == unix.S_IFBLK {
b, err := c.sandbox.devManager.NewDevice(config.DeviceInfo{ di = &config.DeviceInfo{
HostPath: m.Source, HostPath: m.Source,
ContainerPath: m.Destination, ContainerPath: m.Destination,
DevType: "b", DevType: "b",
Major: int64(unix.Major(stat.Rdev)), Major: int64(unix.Major(stat.Rdev)),
Minor: int64(unix.Minor(stat.Rdev)), Minor: int64(unix.Minor(stat.Rdev)),
}) }
// check whether source can be used as a pmem device
} else if di, err = config.PmemDeviceInfo(m.Source, m.Destination); err != nil {
c.Logger().WithError(err).
WithField("mount-source", m.Source).
Debug("no loop device")
}
if err == nil && di != nil {
b, err := c.sandbox.devManager.NewDevice(*di)
if err != nil { if err != nil {
return fmt.Errorf("device manager failed to create new device for %q: %v", m.Source, err) // Do not return an error, try to create
// devices for other mounts
c.Logger().WithError(err).WithField("mount-source", m.Source).
Error("device manager failed to create new device")
continue
} }
c.mounts[i].BlockDeviceID = b.DeviceID() c.mounts[i].BlockDeviceID = b.DeviceID()

View File

@ -98,11 +98,15 @@ func (dm *deviceManager) findDeviceByMajorMinor(major, minor int64) api.Device {
// createDevice creates one device based on DeviceInfo // createDevice creates one device based on DeviceInfo
func (dm *deviceManager) createDevice(devInfo config.DeviceInfo) (dev api.Device, err error) { func (dm *deviceManager) createDevice(devInfo config.DeviceInfo) (dev api.Device, err error) {
// pmem device may points to block devices or raw files,
// do not change its HostPath.
if !devInfo.Pmem {
path, err := config.GetHostPathFunc(devInfo, dm.vhostUserStoreEnabled, dm.vhostUserStorePath) path, err := config.GetHostPathFunc(devInfo, dm.vhostUserStoreEnabled, dm.vhostUserStorePath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
devInfo.HostPath = path devInfo.HostPath = path
}
defer func() { defer func() {
if err == nil { if err == nil {
@ -119,7 +123,7 @@ func (dm *deviceManager) createDevice(devInfo config.DeviceInfo) (dev api.Device
if devInfo.ID, err = dm.newDeviceID(); err != nil { if devInfo.ID, err = dm.newDeviceID(); err != nil {
return nil, err return nil, err
} }
if isVFIO(path) { if isVFIO(devInfo.HostPath) {
return drivers.NewVFIODevice(&devInfo), nil return drivers.NewVFIODevice(&devInfo), nil
} else if isVhostUserBlk(devInfo) { } else if isVhostUserBlk(devInfo) {
if devInfo.DriverOptions == nil { if devInfo.DriverOptions == nil {
@ -134,7 +138,7 @@ func (dm *deviceManager) createDevice(devInfo config.DeviceInfo) (dev api.Device
devInfo.DriverOptions["block-driver"] = dm.blockDriver devInfo.DriverOptions["block-driver"] = dm.blockDriver
return drivers.NewBlockDevice(&devInfo), nil return drivers.NewBlockDevice(&devInfo), nil
} else { } else {
deviceLogger().WithField("device", path).Info("Device has not been passed to the container") deviceLogger().WithField("device", devInfo.HostPath).Info("Device has not been passed to the container")
return drivers.NewGenericDevice(&devInfo), nil return drivers.NewGenericDevice(&devInfo), nil
} }
} }