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)
@ -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;
@ -49,22 +50,30 @@ char* attach_loop_device(const char *filename, int *loop_fd_out)
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) {
fprintf(stderr, "[error] Unkown error openning the loopback device %s. (errno: %d)\n", buf, errno);
continue; 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;
@ -85,34 +94,35 @@ char* attach_loop_device(const char *filename, int *loop_fd_out)
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;
} }
static int64_t return (NULL);
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) 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);
@ -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"
@ -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.")