diff options
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | dnbd3-client.conf | 29 | ||||
-rw-r--r-- | dnbd3-server.conf | 2 | ||||
-rw-r--r-- | src/client/client.c | 80 | ||||
-rw-r--r-- | src/config.h | 3 | ||||
-rw-r--r-- | src/server/server.c | 16 |
6 files changed, 107 insertions, 26 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bfaca60..d6e74f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${GLIB_INCLUDE_DIRS} FILE(GLOB_RECURSE CLIENT_SRCS src/client/*.c) ADD_EXECUTABLE(dnbd3-client ${CLIENT_SRCS}) +TARGET_LINK_LIBRARIES(dnbd3-client ${GLIB_LDFLAGS}) @@ -82,6 +83,8 @@ ADD_CUSTOM_TARGET(${MODULE_NAME} ALL DEPENDS ${CMAKE_BINARY_DIR}/${MODULE_FILE}) INSTALL(TARGETS dnbd3-server dnbd3-client RUNTIME DESTINATION sbin) +INSTALL(FILES dnbd3-server.conf dnbd3-client.conf DESTINATION /etc/) + INSTALL(FILES ${CMAKE_BINARY_DIR}/${MODULE_NAME}.ko DESTINATION /lib/modules/${CMAKE_SYSTEM_VERSION}/kernel/drivers/block PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ diff --git a/dnbd3-client.conf b/dnbd3-client.conf new file mode 100644 index 0000000..1b0ccbd --- /dev/null +++ b/dnbd3-client.conf @@ -0,0 +1,29 @@ +# This is a sample configuration file for dnbd3-client + +[Ubuntu 10.04] +server=132.230.4.29 +port=5003 +vid=1 +rid=3 +device=/dev/dnbd0 + +[Ubuntu 10.10] +server=132.230.4.29 +port=5003 +vid=2 +rid=1 +device=/dev/dnbd1 + +[Ubuntu 11.04] +server=132.230.4.29 +port=5003 +vid=3 +rid=1 +device=/dev/dnbd2 + +[Ubuntu 11.10] +server=132.230.4.29 +port=5003 +vid=4 +rid=1 +device=/dev/dnbd3 diff --git a/dnbd3-server.conf b/dnbd3-server.conf index 4a8ec03..3235106 100644 --- a/dnbd3-server.conf +++ b/dnbd3-server.conf @@ -1,4 +1,4 @@ -# This is a sample configuration file for dnbd3-server.conf +# This is a sample configuration file for dnbd3-server [Ubuntu 10.04] file=/home/jjl/Data/ISOs/Ubuntu/ubuntu-10.04.3-desktop-i386.iso diff --git a/src/client/client.c b/src/client/client.c index ff95ba1..564d6b0 100644 --- a/src/client/client.c +++ b/src/client/client.c @@ -25,14 +25,18 @@ #include <getopt.h> #include <sys/ioctl.h> #include <netinet/in.h> +#include <glib.h> #include "../types.h" #include "../version.h" +char *_config_file_name = DEFAULT_CLIENT_CONFIG_FILE; + void dnbd3_print_help(char* argv_0) { - printf("Usage: %s -h <host> -p <port> -v <vid> -r <rid> -d <device>\n", argv_0); + printf("Usage: %s -h <host> -p <port> -v <vid> -r <rid> -d <device> || -f file\n", argv_0); printf("Start the DNBD3 client.\n"); + printf("-f or --file \t\t Configuration file (default /etc/dnbd3-client.conf)\n"); printf("-h or --host \t\t Host running dnbd3-server.\n"); printf("-p or --port \t\t Port used by server.\n"); printf("-v or --vid \t\t Volume-ID of exported image.\n"); @@ -50,6 +54,30 @@ void dnbd3_print_version() exit(EXIT_SUCCESS); } +void dnbd3_connect(char *host, char *port, int vid, int rid, char *dev) +{ + int fd = open(dev, O_WRONLY); + + printf("Connecting %s to %s:%s vid:%i rid:%i\n", dev, host, port, vid, rid); + + if (ioctl(fd, IOCTL_SET_HOST, host) < 0) + printf("ERROR: ioctl not successful\n"); + + if (ioctl(fd, IOCTL_SET_PORT, port) < 0) + printf("ERROR: ioctl not successful\n"); + + if (ioctl(fd, IOCTL_SET_VID, vid) < 0) + printf("ERROR: ioctl not successful\n"); + + if (ioctl(fd, IOCTL_SET_RID, rid) < 0) + printf("ERROR: ioctl not successful\n"); + + if (ioctl(fd, IOCTL_CONNECT) < 0) + printf("ERROR: ioctl not successful\n"); + + close(fd); +} + int main(int argc, char *argv[]) { int fd; @@ -62,9 +90,10 @@ int main(int argc, char *argv[]) int opt = 0; int longIndex = 0; - static const char *optString = "h:p:v:r:d:c:HV?"; + static const char *optString = "f:h:p:v:r:d:c:HV?"; static const struct option longOpts[] = { + { "file", required_argument, NULL, 'f' }, { "host", required_argument, NULL, 'h' }, { "port", required_argument, NULL, 'p' }, { "vid", required_argument, NULL, 'v' }, @@ -80,6 +109,9 @@ int main(int argc, char *argv[]) { switch (opt) { + case 'f': + _config_file_name = optarg; + break; case 'h': host = optarg; break; @@ -114,7 +146,7 @@ int main(int argc, char *argv[]) // change host if (change_host && host && dev && !port && (vid == 0) && (rid == 0)) { - fd = open(dev, O_RDONLY); + fd = open(dev, O_WRONLY); if (ioctl(fd, IOCTL_DISCONNECT) < 0) printf("ERROR: ioctl not successful\n"); @@ -132,26 +164,42 @@ int main(int argc, char *argv[]) // connect if (host && port && dev && (vid != 0) && (rid != 0)) { - fd = open(dev, O_RDONLY); - - if (ioctl(fd, IOCTL_SET_HOST, host) < 0) - printf("ERROR: ioctl not successful\n"); + dnbd3_connect(host, port, vid, rid, dev); + exit(EXIT_SUCCESS); + } - if (ioctl(fd, IOCTL_SET_PORT, port) < 0) - printf("ERROR: ioctl not successful\n"); + // use configuration file if exist + GKeyFile* gkf; + int i = 0; + size_t j = 0; - if (ioctl(fd, IOCTL_SET_VID, vid) < 0) - printf("ERROR: ioctl not successful\n"); + gkf = g_key_file_new(); - if (ioctl(fd, IOCTL_SET_RID, rid) < 0) - printf("ERROR: ioctl not successful\n"); + if (g_key_file_load_from_file(gkf, _config_file_name, G_KEY_FILE_NONE, NULL)) + { + gchar **groups = NULL; + groups = g_key_file_get_groups(gkf, &j); - if (ioctl(fd, IOCTL_CONNECT) < 0) - printf("ERROR: ioctl not successful\n"); + for (i = 0; i < j; i++) + { + host = g_key_file_get_string(gkf, groups[i], "server", NULL); + port = g_key_file_get_string(gkf, groups[i], "port", NULL); + vid = g_key_file_get_integer(gkf, groups[i], "vid", NULL); + rid = g_key_file_get_integer(gkf, groups[i], "rid", NULL); + dev = g_key_file_get_string(gkf, groups[i], "device", NULL); + dnbd3_connect(host, port, vid, rid, dev); + } - close(fd); + g_strfreev(groups); + g_key_file_free(gkf); exit(EXIT_SUCCESS); } + else + { + printf("WARN: Config file not found: %s\n", _config_file_name); + } + + g_key_file_free(gkf); dnbd3_print_help(argv[0]); exit(EXIT_FAILURE); diff --git a/src/config.h b/src/config.h index e9d564a..d5e356e 100644 --- a/src/config.h +++ b/src/config.h @@ -32,7 +32,8 @@ #define MAX_NUMBER_DEVICES 8 // misc -#define DEFAULT_CONFIG_FILE "/etc/dnbd3-server.conf" +#define DEFAULT_SERVER_CONFIG_FILE "/etc/dnbd3-server.conf" +#define DEFAULT_CLIENT_CONFIG_FILE "/etc/dnbd3-client.conf" #define UNIX_SOCKET "/tmp/dnbd3-server.sock" #endif /* CONFIG_H_ */ diff --git a/src/server/server.c b/src/server/server.c index 4309989..bd09115 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -38,7 +38,7 @@ int _sock; GSList *_dnbd3_clients = NULL; pthread_spinlock_t _spinlock; -char *_config_file_name = DEFAULT_CONFIG_FILE; +char *_config_file_name = DEFAULT_SERVER_CONFIG_FILE; dnbd3_image_t *_images; size_t _num_images = 0; @@ -51,8 +51,8 @@ void dnbd3_print_help(char* argv_0) printf("-r or --reload \t\t Reload configuration file\n"); printf("-s or --stop \t\t Stop running dnbd3-server\n"); printf("-i or --info \t\t Print connected clients and used images\n"); - printf("-h or --help \t\t Show this help text and quit\n"); - printf("-v or --version \t Show version and quit\n"); + printf("-H or --help \t\t Show this help text and quit\n"); + printf("-V or --version \t Show version and quit\n"); exit(0); } @@ -86,7 +86,7 @@ int main(int argc, char* argv[]) int demonize = 1; int opt = 0; int longIndex = 0; - static const char *optString = "f:nrsihv?"; + static const char *optString = "f:nrsiHV?"; static const struct option longOpts[] = { { "file", required_argument, NULL, 'f' }, @@ -94,8 +94,8 @@ int main(int argc, char* argv[]) { "reload", no_argument, NULL, 'r' }, { "stop", no_argument, NULL, 's' }, { "info", no_argument, NULL, 'i' }, - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'v' } }; + { "help", no_argument, NULL, 'H' }, + { "version", no_argument, NULL, 'V' } }; opt = getopt_long(argc, argv, optString, longOpts, &longIndex); @@ -121,10 +121,10 @@ int main(int argc, char* argv[]) printf("INFO: Requesting information...\n"); dnbd3_ipc_send(IPC_INFO); return EXIT_SUCCESS; - case 'h': + case 'H': dnbd3_print_help(argv[0]); break; - case 'v': + case 'V': dnbd3_print_version(); break; case '?': |