Browse code

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 <vgoyal@redhat.com>

Vivek Goyal authored on 2015/10/08 05:28:49
Showing 1 changed files
... ...
@@ -897,6 +897,33 @@ func (devices *DeviceSet) createBaseImage() error {
897 897
 	return nil
898 898
 }
899 899
 
900
+// Returns if thin pool device exists or not. If device exists, also makes
901
+// sure it is a thin pool device and not some other type of device.
902
+func (devices *DeviceSet) thinPoolExists(thinPoolDevice string) (bool, error) {
903
+	logrus.Debugf("devmapper: Checking for existence of the pool %s", thinPoolDevice)
904
+
905
+	info, err := devicemapper.GetInfo(thinPoolDevice)
906
+	if err != nil {
907
+		return false, fmt.Errorf("devmapper: GetInfo() on device %s failed: %v", thinPoolDevice, err)
908
+	}
909
+
910
+	// Device does not exist.
911
+	if info.Exists == 0 {
912
+		return false, nil
913
+	}
914
+
915
+	_, _, deviceType, _, err := devicemapper.GetStatus(thinPoolDevice)
916
+	if err != nil {
917
+		return false, fmt.Errorf("devmapper: GetStatus() on device %s failed: %v", thinPoolDevice, err)
918
+	}
919
+
920
+	if deviceType != "thin-pool" {
921
+		return false, fmt.Errorf("devmapper: Device %s is not a thin pool", thinPoolDevice)
922
+	}
923
+
924
+	return true, nil
925
+}
926
+
900 927
 func (devices *DeviceSet) checkThinPool() error {
901 928
 	_, transactionID, dataUsed, _, _, _, err := devices.poolStatus()
902 929
 	if err != nil {
... ...
@@ -1441,10 +1468,8 @@ func (devices *DeviceSet) initDevmapper(doInit bool) error {
1441 1441
 	logrus.Debugf("Generated prefix: %s", devices.devicePrefix)
1442 1442
 
1443 1443
 	// Check for the existence of the thin-pool device
1444
-	logrus.Debugf("Checking for existence of the pool '%s'", devices.getPoolName())
1445
-	info, err := devicemapper.GetInfo(devices.getPoolName())
1446
-	if info == nil {
1447
-		logrus.Debugf("Error device devicemapper.GetInfo: %s", err)
1444
+	poolExists, err := devices.thinPoolExists(devices.getPoolName())
1445
+	if err != nil {
1448 1446
 		return err
1449 1447
 	}
1450 1448
 
... ...
@@ -1459,7 +1484,7 @@ func (devices *DeviceSet) initDevmapper(doInit bool) error {
1459 1459
 	createdLoopback := false
1460 1460
 
1461 1461
 	// If the pool doesn't exist, create it
1462
-	if info.Exists == 0 && devices.thinPoolDevice == "" {
1462
+	if !poolExists && devices.thinPoolDevice == "" {
1463 1463
 		logrus.Debugf("Pool doesn't exist. Creating it.")
1464 1464
 
1465 1465
 		var (
... ...
@@ -1542,7 +1567,7 @@ func (devices *DeviceSet) initDevmapper(doInit bool) error {
1542 1542
 	// we probably created pool earlier and could not remove it as some
1543 1543
 	// containers were still using it. Detect some of the properties of
1544 1544
 	// pool, like is it using loop devices.
1545
-	if info.Exists != 0 && devices.thinPoolDevice == "" {
1545
+	if poolExists && devices.thinPoolDevice == "" {
1546 1546
 		if err := devices.loadThinPoolLoopBackInfo(); err != nil {
1547 1547
 			logrus.Debugf("Failed to load thin pool loopback device information:%v", err)
1548 1548
 			return err