From 6d2d0a74e8effe65175473352bf58fc4beda1718 Mon Sep 17 00:00:00 2001 From: Vivek Goyal Date: Wed, 7 Oct 2015 16:28:49 -0400 Subject: [PATCH] devmapper: Make sure device is a thin pool device Right now we check for the existence of device but don't make sure it is a thin pool device. We assume it is a thin pool device and call poolStatus() on the device which returns an error EOF. And that error does not tell anything. So before we reach the stage of calling poolStatus() make sure we are working with a thin pool device otherwise error out. Signed-off-by: Vivek Goyal --- daemon/graphdriver/devmapper/deviceset.go | 37 +++++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/daemon/graphdriver/devmapper/deviceset.go b/daemon/graphdriver/devmapper/deviceset.go index 499221ed80..f2d2b9dc60 100644 --- a/daemon/graphdriver/devmapper/deviceset.go +++ b/daemon/graphdriver/devmapper/deviceset.go @@ -897,6 +897,33 @@ func (devices *DeviceSet) createBaseImage() error { return nil } +// Returns if thin pool device exists or not. If device exists, also makes +// sure it is a thin pool device and not some other type of device. +func (devices *DeviceSet) thinPoolExists(thinPoolDevice string) (bool, error) { + logrus.Debugf("devmapper: Checking for existence of the pool %s", thinPoolDevice) + + info, err := devicemapper.GetInfo(thinPoolDevice) + if err != nil { + return false, fmt.Errorf("devmapper: GetInfo() on device %s failed: %v", thinPoolDevice, err) + } + + // Device does not exist. + if info.Exists == 0 { + return false, nil + } + + _, _, deviceType, _, err := devicemapper.GetStatus(thinPoolDevice) + if err != nil { + return false, fmt.Errorf("devmapper: GetStatus() on device %s failed: %v", thinPoolDevice, err) + } + + if deviceType != "thin-pool" { + return false, fmt.Errorf("devmapper: Device %s is not a thin pool", thinPoolDevice) + } + + return true, nil +} + func (devices *DeviceSet) checkThinPool() error { _, transactionID, dataUsed, _, _, _, err := devices.poolStatus() if err != nil { @@ -1441,10 +1468,8 @@ func (devices *DeviceSet) initDevmapper(doInit bool) error { logrus.Debugf("Generated prefix: %s", devices.devicePrefix) // Check for the existence of the thin-pool device - logrus.Debugf("Checking for existence of the pool '%s'", devices.getPoolName()) - info, err := devicemapper.GetInfo(devices.getPoolName()) - if info == nil { - logrus.Debugf("Error device devicemapper.GetInfo: %s", err) + poolExists, err := devices.thinPoolExists(devices.getPoolName()) + if err != nil { return err } @@ -1459,7 +1484,7 @@ func (devices *DeviceSet) initDevmapper(doInit bool) error { createdLoopback := false // If the pool doesn't exist, create it - if info.Exists == 0 && devices.thinPoolDevice == "" { + if !poolExists && devices.thinPoolDevice == "" { logrus.Debugf("Pool doesn't exist. Creating it.") var ( @@ -1542,7 +1567,7 @@ func (devices *DeviceSet) initDevmapper(doInit bool) error { // we probably created pool earlier and could not remove it as some // containers were still using it. Detect some of the properties of // pool, like is it using loop devices. - if info.Exists != 0 && devices.thinPoolDevice == "" { + if poolExists && devices.thinPoolDevice == "" { if err := devices.loadThinPoolLoopBackInfo(); err != nil { logrus.Debugf("Failed to load thin pool loopback device information:%v", err) return err