|
@@ -2,124 +2,14 @@ package devmapper
|
|
|
|
|
|
/*
|
|
|
#cgo LDFLAGS: -L. -ldevmapper
|
|
|
-#include <stdio.h>
|
|
|
-#include <stdlib.h>
|
|
|
-#include <unistd.h>
|
|
|
#include <libdevmapper.h>
|
|
|
-#include <linux/loop.h>
|
|
|
-#include <sys/types.h>
|
|
|
-#include <sys/stat.h>
|
|
|
-#include <fcntl.h>
|
|
|
-#include <sys/ioctl.h>
|
|
|
-#include <linux/fs.h>
|
|
|
-#include <errno.h>
|
|
|
-
|
|
|
-#ifndef LOOP_CTL_GET_FREE
|
|
|
-#define LOOP_CTL_GET_FREE 0x4C82
|
|
|
-#endif
|
|
|
-
|
|
|
-// FIXME: this could easily be rewritten in go
|
|
|
-char* attach_loop_device(const char *filename, int *loop_fd_out)
|
|
|
-{
|
|
|
- struct loop_info64 loopinfo = {0};
|
|
|
- struct stat st;
|
|
|
- char buf[64];
|
|
|
- int i, loop_fd, fd, start_index;
|
|
|
- char* loopname;
|
|
|
-
|
|
|
-
|
|
|
- *loop_fd_out = -1;
|
|
|
-
|
|
|
- start_index = 0;
|
|
|
- fd = open("/dev/loop-control", O_RDONLY);
|
|
|
- if (fd >= 0) {
|
|
|
- start_index = ioctl(fd, LOOP_CTL_GET_FREE);
|
|
|
- close(fd);
|
|
|
-
|
|
|
- if (start_index < 0)
|
|
|
- start_index = 0;
|
|
|
- }
|
|
|
-
|
|
|
- fd = open(filename, O_RDWR);
|
|
|
- if (fd < 0) {
|
|
|
- perror("open");
|
|
|
- return NULL;
|
|
|
- }
|
|
|
-
|
|
|
- loop_fd = -1;
|
|
|
- for (i = start_index ; loop_fd < 0 ; i++ ) {
|
|
|
- if (sprintf(buf, "/dev/loop%d", i) < 0) {
|
|
|
- close(fd);
|
|
|
- return NULL;
|
|
|
- }
|
|
|
-
|
|
|
- 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);
|
|
|
- return NULL;
|
|
|
- } 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) {
|
|
|
- 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));
|
|
|
- return NULL;
|
|
|
- }
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- 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("ioctl LOOP_SET_STATUS64");
|
|
|
- if (ioctl(loop_fd, LOOP_CLR_FD, 0) < 0) {
|
|
|
- perror("ioctl LOOP_CLR_FD");
|
|
|
- }
|
|
|
- close(loop_fd);
|
|
|
- fprintf (stderr, "cannot set up loopback device info");
|
|
|
- return (NULL);
|
|
|
- }
|
|
|
-
|
|
|
- loopname = strdup(buf);
|
|
|
- if (loopname == NULL) {
|
|
|
- close(loop_fd);
|
|
|
- return (NULL);
|
|
|
- }
|
|
|
-
|
|
|
- *loop_fd_out = loop_fd;
|
|
|
- return (loopname);
|
|
|
- }
|
|
|
-
|
|
|
- return (NULL);
|
|
|
-}
|
|
|
+#include <linux/loop.h> // FIXME: present only for defines, maybe we can remove it?
|
|
|
+#include <linux/fs.h> // FIXME: present only for BLKGETSIZE64, maybe we can remove it?
|
|
|
|
|
|
+// FIXME: Can't we find a way to do the logging in pure Go?
|
|
|
extern void DevmapperLogCallback(int level, char *file, int line, int dm_errno_or_class, char *str);
|
|
|
|
|
|
-static void log_cb(int level, const char *file, int line,
|
|
|
- int dm_errno_or_class, const char *f, ...)
|
|
|
+static void log_cb(int level, const char *file, int line, int dm_errno_or_class, const char *f, ...)
|
|
|
{
|
|
|
char buffer[256];
|
|
|
va_list ap;
|
|
@@ -135,7 +25,6 @@ static void log_with_errno_init()
|
|
|
{
|
|
|
dm_log_with_errno_init(log_cb);
|
|
|
}
|
|
|
-
|
|
|
*/
|
|
|
import "C"
|
|
|
|
|
@@ -147,6 +36,19 @@ type (
|
|
|
CDmTask C.struct_dm_task
|
|
|
)
|
|
|
|
|
|
+// FIXME: Make sure the values are defined in C
|
|
|
+const (
|
|
|
+ LoopSetFd = C.LOOP_SET_FD
|
|
|
+ LoopCtlGetFree = C.LOOP_CTL_GET_FREE
|
|
|
+ LoopSetStatus64 = C.LOOP_SET_STATUS64
|
|
|
+ LoopClrFd = C.LOOP_CLR_FD
|
|
|
+ LoFlagsAutoClear = C.LO_FLAGS_AUTOCLEAR
|
|
|
+ LoFlagsReadOnly = C.LO_FLAGS_READ_ONLY
|
|
|
+ LoFlagsPartScan = C.LO_FLAGS_PARTSCAN
|
|
|
+ LoKeySize = C.LO_KEY_SIZE
|
|
|
+ LoNameSize = C.LO_NAME_SIZE
|
|
|
+)
|
|
|
+
|
|
|
var (
|
|
|
DmAttachLoopDevice = dmAttachLoopDeviceFct
|
|
|
DmGetBlockSize = dmGetBlockSizeFct
|
|
@@ -185,28 +87,26 @@ func dmTaskCreateFct(taskType int) *CDmTask {
|
|
|
}
|
|
|
|
|
|
func dmTaskRunFct(task *CDmTask) int {
|
|
|
- return int(C.dm_task_run((*C.struct_dm_task)(task)))
|
|
|
+ ret, _ := C.dm_task_run((*C.struct_dm_task)(task))
|
|
|
+ return int(ret)
|
|
|
}
|
|
|
|
|
|
func dmTaskSetNameFct(task *CDmTask, name string) int {
|
|
|
Cname := C.CString(name)
|
|
|
defer free(Cname)
|
|
|
|
|
|
- return int(C.dm_task_set_name((*C.struct_dm_task)(task),
|
|
|
- Cname))
|
|
|
+ return int(C.dm_task_set_name((*C.struct_dm_task)(task), Cname))
|
|
|
}
|
|
|
|
|
|
func dmTaskSetMessageFct(task *CDmTask, message string) int {
|
|
|
Cmessage := C.CString(message)
|
|
|
defer free(Cmessage)
|
|
|
|
|
|
- return int(C.dm_task_set_message((*C.struct_dm_task)(task),
|
|
|
- Cmessage))
|
|
|
+ return int(C.dm_task_set_message((*C.struct_dm_task)(task), Cmessage))
|
|
|
}
|
|
|
|
|
|
func dmTaskSetSectorFct(task *CDmTask, sector uint64) int {
|
|
|
- return int(C.dm_task_set_sector((*C.struct_dm_task)(task),
|
|
|
- C.uint64_t(sector)))
|
|
|
+ return int(C.dm_task_set_sector((*C.struct_dm_task)(task), C.uint64_t(sector)))
|
|
|
}
|
|
|
|
|
|
func dmTaskSetCookieFct(task *CDmTask, cookie *uint, flags uint16) int {
|
|
@@ -214,13 +114,11 @@ func dmTaskSetCookieFct(task *CDmTask, cookie *uint, flags uint16) int {
|
|
|
defer func() {
|
|
|
*cookie = uint(cCookie)
|
|
|
}()
|
|
|
- return int(C.dm_task_set_cookie((*C.struct_dm_task)(task), &cCookie,
|
|
|
- C.uint16_t(flags)))
|
|
|
+ return int(C.dm_task_set_cookie((*C.struct_dm_task)(task), &cCookie, C.uint16_t(flags)))
|
|
|
}
|
|
|
|
|
|
func dmTaskSetAddNodeFct(task *CDmTask, addNode AddNodeType) int {
|
|
|
- return int(C.dm_task_set_add_node((*C.struct_dm_task)(task),
|
|
|
- C.dm_add_node_t(addNode)))
|
|
|
+ return int(C.dm_task_set_add_node((*C.struct_dm_task)(task), C.dm_add_node_t(addNode)))
|
|
|
}
|
|
|
|
|
|
func dmTaskSetRoFct(task *CDmTask) int {
|
|
@@ -236,14 +134,12 @@ func dmTaskAddTargetFct(task *CDmTask,
|
|
|
Cparams := C.CString(params)
|
|
|
defer free(Cparams)
|
|
|
|
|
|
- return int(C.dm_task_add_target((*C.struct_dm_task)(task),
|
|
|
- C.uint64_t(start), C.uint64_t(size), Cttype, Cparams))
|
|
|
+ return int(C.dm_task_add_target((*C.struct_dm_task)(task), C.uint64_t(start), C.uint64_t(size), Cttype, Cparams))
|
|
|
}
|
|
|
|
|
|
func dmGetLoopbackBackingFileFct(fd uintptr) (uint64, uint64, sysErrno) {
|
|
|
var lo64 C.struct_loop_info64
|
|
|
- _, _, err := sysSyscall(sysSysIoctl, fd, C.LOOP_GET_STATUS64,
|
|
|
- uintptr(unsafe.Pointer(&lo64)))
|
|
|
+ _, _, err := sysSyscall(sysSysIoctl, fd, C.LOOP_GET_STATUS64, uintptr(unsafe.Pointer(&lo64)))
|
|
|
return uint64(lo64.lo_device), uint64(lo64.lo_inode), sysErrno(err)
|
|
|
}
|
|
|
|
|
@@ -287,23 +183,23 @@ func dmGetNextTargetFct(task *CDmTask, next uintptr, start, length *uint64, targ
|
|
|
*params = C.GoString(Cparams)
|
|
|
}()
|
|
|
|
|
|
- nextp := C.dm_get_next_target((*C.struct_dm_task)(task),
|
|
|
- unsafe.Pointer(next), &Cstart, &Clength, &CtargetType, &Cparams)
|
|
|
+ nextp := C.dm_get_next_target((*C.struct_dm_task)(task), unsafe.Pointer(next), &Cstart, &Clength, &CtargetType, &Cparams)
|
|
|
return uintptr(nextp)
|
|
|
}
|
|
|
|
|
|
func dmAttachLoopDeviceFct(filename string, fd *int) string {
|
|
|
- cFilename := C.CString(filename)
|
|
|
- defer free(cFilename)
|
|
|
-
|
|
|
- var cFd C.int
|
|
|
- defer func() {
|
|
|
- *fd = int(cFd)
|
|
|
- }()
|
|
|
-
|
|
|
- ret := C.attach_loop_device(cFilename, &cFd)
|
|
|
- defer free(ret)
|
|
|
- return C.GoString(ret)
|
|
|
+ return ""
|
|
|
+ // cFilename := C.CString(filename)
|
|
|
+ // defer free(cFilename)
|
|
|
+
|
|
|
+ // var cFd C.int
|
|
|
+ // defer func() {
|
|
|
+ // *fd = int(cFd)
|
|
|
+ // }()
|
|
|
+
|
|
|
+ // ret := C.attach_loop_device(cFilename, &cFd)
|
|
|
+ // defer free(ret)
|
|
|
+ // return C.GoString(ret)
|
|
|
}
|
|
|
|
|
|
func getBlockSizeFct(fd uintptr, size *uint64) sysErrno {
|