Bladeren bron

Add error checking and error messages

Guillaume J. Charmes 11 jaren geleden
bovenliggende
commit
31b883b076
3 gewijzigde bestanden met toevoegingen van 51 en 41 verwijderingen
  1. 5 5
      devmapper/deviceset_devmapper.go
  2. 35 25
      devmapper/devmapper.go
  3. 11 11
      runtime_test.go

+ 5 - 5
devmapper/deviceset_devmapper.go

@@ -1,7 +1,6 @@
 package devmapper
 
 import (
-	"time"
 	"encoding/json"
 	"fmt"
 	"github.com/dotcloud/docker/utils"
@@ -14,6 +13,7 @@ import (
 	"strconv"
 	"sync"
 	"syscall"
+	"time"
 )
 
 var (
@@ -371,7 +371,7 @@ func (devices *DeviceSetDM) initDevmapper() error {
 	// "reg-" stands for "regular file".
 	// In the future we might use "dev-" for "device file", etc.
 	devices.devicePrefix = fmt.Sprintf("docker-reg-%d-%d", sysSt.Dev, sysSt.Ino)
-
+	utils.Debugf("Generated prefix: %s", devices.devicePrefix)
 
 	// Check for the existence of the device <prefix>-pool
 	utils.Debugf("Checking for existence of the pool '%s'", devices.getPoolName())
@@ -389,6 +389,7 @@ func (devices *DeviceSetDM) initDevmapper() error {
 	// If the pool doesn't exist, create it
 	if info.Exists == 0 {
 		utils.Debugf("Pool doesn't exist. Creating it.")
+
 		dataFile, err := AttachLoopDevice(data)
 		if err != nil {
 			utils.Debugf("\n--->Err: %s\n", err)
@@ -550,7 +551,7 @@ func (devices *DeviceSetDM) waitRemove(hash string) error {
 		return err
 	}
 	i := 0
-	for ; i<1000; i+=1 {
+	for ; i < 1000; i += 1 {
 		devinfo, err := getInfo(devname)
 		if err != nil {
 			// If there is an error we assume the device doesn't exist.
@@ -578,7 +579,7 @@ func (devices *DeviceSetDM) waitClose(hash string) error {
 		return err
 	}
 	i := 0
-	for ; i<1000; i+=1 {
+	for ; i < 1000; i += 1 {
 		devinfo, err := getInfo(devname)
 		if err != nil {
 			return err
@@ -610,7 +611,6 @@ func (devices *DeviceSetDM) byHash(hash string) (devname string, err error) {
 	return info.Name(), nil
 }
 
-
 func (devices *DeviceSetDM) Shutdown() error {
 	utils.Debugf("[deviceset %s] shutdown()", devices.devicePrefix)
 	defer utils.Debugf("[deviceset %s] shutdown END", devices.devicePrefix)

+ 35 - 25
devmapper/devmapper.go

@@ -27,6 +27,7 @@ char*			attach_loop_device(const char *filename, int *loop_fd_out)
   int			i, loop_fd, fd, start_index;
   char*			loopname;
 
+
   *loop_fd_out = -1;
 
   start_index = 0;
@@ -48,71 +49,80 @@ char*			attach_loop_device(const char *filename, int *loop_fd_out)
   loop_fd = -1;
   for (i = start_index ; loop_fd < 0 ; i++ ) {
     if (sprintf(buf, "/dev/loop%d", i) < 0) {
-      close(fd);
-	perror("sprintf");
-      return NULL;
+	close(fd);
+	return NULL;
     }
 
-    if (stat(buf, &st) || !S_ISBLK(st.st_mode)) {
+    if (stat(buf, &st)) {
+      if (!S_ISBLK(st.st_mode)) {
+	 fprintf(stderr, "[error] Loopback device %s is not a block device.\n", buf);
+      } else if (errno == ENOENT) {
+	fprintf(stderr, "[error] There are no more loopback device available.\n");
+      } else {
+	fprintf(stderr, "[error] Unkown error trying to stat the loopback device %s (errno: %d).\n", buf, errno);
+      }
       close(fd);
       return NULL;
     }
 
     loop_fd = open(buf, O_RDWR);
     if (loop_fd < 0 && errno == ENOENT) {
+      fprintf(stderr, "[error] The loopback device %s does not exists.\n", buf);
       close(fd);
-      fprintf (stderr, "no available loopback device!");
       return NULL;
-    } else if (loop_fd < 0)
-      continue;
+    } else if (loop_fd < 0) {
+	fprintf(stderr, "[error] Unkown error openning the loopback device %s. (errno: %d)\n", buf, errno);
+	continue;
+    }
 
-    if (ioctl (loop_fd, LOOP_SET_FD, (void *)(size_t)fd) < 0) {
+    if (ioctl(loop_fd, LOOP_SET_FD, (void *)(size_t)fd) < 0) {
       int errsv = errno;
       close(loop_fd);
       loop_fd = -1;
       if (errsv != EBUSY) {
-        close (fd);
-        fprintf (stderr, "cannot set up loopback device %s: %s", buf, strerror(errsv));
+        close(fd);
+        fprintf(stderr, "cannot set up loopback device %s: %s", buf, strerror(errsv));
         return NULL;
       }
       continue;
     }
 
-    close (fd);
+    close(fd);
 
     strncpy((char*)loopinfo.lo_file_name, buf, LO_NAME_SIZE);
     loopinfo.lo_offset = 0;
     loopinfo.lo_flags = LO_FLAGS_AUTOCLEAR;
 
     if (ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo) < 0) {
-      perror("ioctl1");
+      perror("ioctl LOOP_SET_STATUS64");
       if (ioctl(loop_fd, LOOP_CLR_FD, 0) < 0) {
-        perror("ioctl2");
+        perror("ioctl LOOP_CLR_FD");
       }
       close(loop_fd);
       fprintf (stderr, "cannot set up loopback device info");
-      return NULL;
+      return (NULL);
     }
 
     loopname = strdup(buf);
     if (loopname == NULL) {
       close(loop_fd);
-      return NULL;
+      return (NULL);
     }
 
     *loop_fd_out = loop_fd;
-    return loopname;
+    return (loopname);
   }
-  return NULL;
+
+  return (NULL);
 }
 
-static int64_t
-get_block_size(int fd)
+static int64_t	get_block_size(int fd)
 {
-  uint64_t size;
+  uint64_t	size;
+
   if (ioctl(fd, BLKGETSIZE64, &size) == -1)
     return -1;
-  return (int64_t)size;
+  return ((int64_t)size);
 }
 
 extern void DevmapperLogCallback(int level, char *file, int line, int dm_errno_or_class, char *str);
@@ -150,7 +160,7 @@ import (
 	"unsafe"
 )
 
-type DevmapperLogger interface  {
+type DevmapperLogger interface {
 	log(level int, file string, line int, dmError int, message string)
 }
 
@@ -214,7 +224,7 @@ type (
 		ReadOnly      int
 		TargetCount   int32
 	}
-	TaskType int
+	TaskType    int
 	AddNodeType int
 )
 
@@ -279,7 +289,7 @@ func (t *Task) SetCookie(cookie *uint32, flags uint16) error {
 }
 
 func (t *Task) SetAddNode(add_node AddNodeType) error {
-	if res := C.dm_task_set_add_node(t.unmanaged, C.dm_add_node_t (add_node)); res != 1 {
+	if res := C.dm_task_set_add_node(t.unmanaged, C.dm_add_node_t(add_node)); res != 1 {
 		return ErrTaskSetAddNode
 	}
 	return nil
@@ -352,7 +362,7 @@ func AttachLoopDevice(filename string) (*os.File, error) {
 	res := C.attach_loop_device(c_filename, &fd)
 	if res == nil {
 		if os.Getenv("DEBUG") != "" {
-			C.perror(C.CString(fmt.Sprintf("[debug] Error attach_loop_device(%s, $#v)", c_filename, &fd)))
+			C.perror(C.CString(fmt.Sprintf("[debug] Error attach_loop_device(%s, %d)", filename, int(fd))))
 		}
 		return nil, ErrAttachLoopbackDevice
 	}

+ 11 - 11
runtime_test.go

@@ -1,7 +1,6 @@
 package docker
 
 import (
-	"path"
 	"bytes"
 	"fmt"
 	"github.com/dotcloud/docker/devmapper"
@@ -11,6 +10,7 @@ import (
 	"log"
 	"net"
 	"os"
+	"path"
 	"path/filepath"
 	"runtime"
 	"strconv"
@@ -22,12 +22,12 @@ import (
 )
 
 const (
-	unitTestImageName        = "docker-test-image"
-	unitTestImageID          = "83599e29c455eb719f77d799bc7c51521b9551972f5a850d7ad265bc1b5292f6" // 1.0
-	unitTestNetworkBridge    = "testdockbr0"
-	unitTestStoreBase        = "/var/lib/docker/unit-tests"
-	testDaemonAddr           = "127.0.0.1:4270"
-	testDaemonProto          = "tcp"
+	unitTestImageName     = "docker-test-image"
+	unitTestImageID       = "83599e29c455eb719f77d799bc7c51521b9551972f5a850d7ad265bc1b5292f6" // 1.0
+	unitTestNetworkBridge = "testdockbr0"
+	unitTestStoreBase     = "/var/lib/docker/unit-tests"
+	testDaemonAddr        = "127.0.0.1:4270"
+	testDaemonProto       = "tcp"
 
 	unitTestDMDataLoopbackSize     = 209715200 // 200MB
 	unitTestDMMetaDataLoopbackSize = 104857600 // 100MB
@@ -136,7 +136,7 @@ func cleanupDevMapper() error {
 	}
 	pools := []string{}
 	for _, info := range infos {
-		if name := info.Name(); strings.HasPrefix(name, filter + "-") {
+		if name := info.Name(); strings.HasPrefix(name, filter+"-") {
 			if strings.HasSuffix(name, "-pool") {
 				pools = append(pools, name)
 			} else {
@@ -187,7 +187,6 @@ func init() {
 	startFds, startGoroutines = utils.GetTotalUsedFds(), runtime.NumGoroutine()
 }
 
-
 func setupBaseImage() {
 	runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false)
 	if err != nil {
@@ -197,7 +196,9 @@ func setupBaseImage() {
 	// Create a device, which triggers the initiation of the base FS
 	// This avoids other tests doing this and timing out
 	deviceset := devmapper.NewDeviceSetDM(unitTestStoreBase)
-	deviceset.AddDevice("init", "")
+	if err := deviceset.AddDevice("init", ""); err != nil {
+		log.Fatalf("Unable to setup the base image: %s", err)
+	}
 
 	// Create the "Server"
 	srv := &Server{
@@ -216,7 +217,6 @@ func setupBaseImage() {
 	}
 }
 
-
 func spawnGlobalDaemon() {
 	if globalRuntime != nil {
 		utils.Debugf("Global runtime already exists. Skipping.")