Add error checking and error messages

This commit is contained in:
Guillaume J. Charmes 2013-10-17 15:04:14 -07:00
parent bdb3b2a88c
commit 31b883b076
No known key found for this signature in database
GPG Key ID: B33E4642CB6E3FF3
3 changed files with 51 additions and 41 deletions

View File

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

View File

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

View File

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