diff options
| author | Rob Landley <rob@landley.net> | 2010-09-23 02:03:47 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-09-23 02:03:47 +0200 |
| commit | 1fc20c446fde454bbff690a17b0b89de5e127d96 (patch) | |
| tree | 8691bea0205a54cb678cd3600e5cd901f91f0ffc | |
| parent | 361b1a6884bc9d4f5cece22d0eb9fe5eae17bda7 (diff) | |
| download | busybox-w32-1fc20c446fde454bbff690a17b0b89de5e127d96.tar.gz busybox-w32-1fc20c446fde454bbff690a17b0b89de5e127d96.tar.bz2 busybox-w32-1fc20c446fde454bbff690a17b0b89de5e127d96.zip | |
nbd-client: new applet
function old new delta
nbdclient_main - 537 +537
daemon - 162 +162
packed_usage 27682 27724 +42
applet_names 2355 2366 +11
bbconfig_config_bz2 4918 4922 +4
applet_main 1376 1380 +4
applet_nameofs 688 690 +2
applet_install_loc 172 173 +1
------------------------------------------------------------------------------
(add/remove: 4/0 grow/shrink: 6/0 up/down: 763/0) Total: 763 bytes
Signed-off-by: Rob Landley <rob@landley.net>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | networking/nbd-client.c | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/networking/nbd-client.c b/networking/nbd-client.c new file mode 100644 index 000000000..5ac190c32 --- /dev/null +++ b/networking/nbd-client.c | |||
| @@ -0,0 +1,153 @@ | |||
| 1 | /* | ||
| 2 | * Copyright 2010 Rob Landley <rob@landley.net> | ||
| 3 | * | ||
| 4 | * Licensed under GPLv2, see file LICENSE in this source tree. | ||
| 5 | */ | ||
| 6 | #include "libbb.h" | ||
| 7 | #include <netinet/tcp.h> | ||
| 8 | #include <linux/fs.h> | ||
| 9 | |||
| 10 | //applet:IF_NBDCLIENT(APPLET_ODDNAME(nbd-client, nbdclient, _BB_DIR_USR_SBIN, _BB_SUID_DROP, nbdclient)) | ||
| 11 | |||
| 12 | //kbuild:lib-$(CONFIG_NBDCLIENT) += nbd-client.o | ||
| 13 | |||
| 14 | //config:config NBDCLIENT | ||
| 15 | //config: bool "nbd-client" | ||
| 16 | //config: default y | ||
| 17 | //config: help | ||
| 18 | //config: Network block device client | ||
| 19 | |||
| 20 | #define NBD_SET_SOCK _IO(0xab, 0) | ||
| 21 | #define NBD_SET_BLKSIZE _IO(0xab, 1) | ||
| 22 | #define NBD_SET_SIZE _IO(0xab, 2) | ||
| 23 | #define NBD_DO_IT _IO(0xab, 3) | ||
| 24 | #define NBD_CLEAR_SOCK _IO(0xab, 4) | ||
| 25 | #define NBD_CLEAR_QUEUE _IO(0xab, 5) | ||
| 26 | #define NBD_PRINT_DEBUG _IO(0xab, 6) | ||
| 27 | #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7) | ||
| 28 | #define NBD_DISCONNECT _IO(0xab, 8) | ||
| 29 | #define NBD_SET_TIMEOUT _IO(0xab, 9) | ||
| 30 | |||
| 31 | //usage:#define nbdclient_trivial_usage | ||
| 32 | //usage: "HOST PORT BLOCKDEV" | ||
| 33 | //usage:#define nbdclient_full_usage "\n\n" | ||
| 34 | //usage: "Connect to HOST and provide a network block device on BLOCKDEV" | ||
| 35 | |||
| 36 | //TODO: more compat with nbd-client version 2.9.13 - | ||
| 37 | //Usage: nbd-client [bs=blocksize] [timeout=sec] host port nbd_device [-swap] [-persist] [-nofork] | ||
| 38 | //Or : nbd-client -d nbd_device | ||
| 39 | //Or : nbd-client -c nbd_device | ||
| 40 | //Default value for blocksize is 1024 (recommended for ethernet) | ||
| 41 | //Allowed values for blocksize are 512,1024,2048,4096 | ||
| 42 | //Note, that kernel 2.4.2 and older ones do not work correctly with | ||
| 43 | //blocksizes other than 1024 without patches | ||
| 44 | |||
| 45 | int nbdclient_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
| 46 | int nbdclient_main(int argc, char **argv) | ||
| 47 | { | ||
| 48 | unsigned long timeout = 0; | ||
| 49 | int nofork = 0; | ||
| 50 | char *host, *port, *device; | ||
| 51 | struct nbd_header_t { | ||
| 52 | uint64_t magic1; // "NBDMAGIC" | ||
| 53 | uint64_t magic2; // 0x420281861253 big endian | ||
| 54 | uint64_t devsize; | ||
| 55 | uint32_t flags; | ||
| 56 | char data[124]; | ||
| 57 | } nbd_header; | ||
| 58 | struct bug_check { | ||
| 59 | char c[offsetof(struct nbd_header_t, data) == 8+8+8+4 ? 1 : -1]; | ||
| 60 | }; | ||
| 61 | |||
| 62 | // Parse command line stuff (just a stub now) | ||
| 63 | if (argc != 4) | ||
| 64 | bb_show_usage(); | ||
| 65 | |||
| 66 | #if !BB_MMU | ||
| 67 | bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv); | ||
| 68 | #endif | ||
| 69 | |||
| 70 | host = argv[1]; | ||
| 71 | port = argv[2]; | ||
| 72 | device = argv[3]; | ||
| 73 | |||
| 74 | // Repeat until spanked (-persist behavior) | ||
| 75 | for (;;) { | ||
| 76 | int sock, nbd; | ||
| 77 | int ro; | ||
| 78 | |||
| 79 | // Make sure the /dev/nbd exists | ||
| 80 | nbd = xopen(device, O_RDWR); | ||
| 81 | |||
| 82 | // Find and connect to server | ||
| 83 | sock = create_and_connect_stream_or_die(host, xatou16(port)); | ||
| 84 | setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &const_int_1, sizeof(const_int_1)); | ||
| 85 | |||
| 86 | // Log on to the server | ||
| 87 | xread(sock, &nbd_header, 8+8+8+4 + 124); | ||
| 88 | if (memcmp(&nbd_header.magic1, "NBDMAGIC""\x00\x00\x42\x02\x81\x86\x12\x53", 16) != 0) | ||
| 89 | bb_error_msg_and_die("login failed"); | ||
| 90 | |||
| 91 | // Set 4k block size. Everything uses that these days | ||
| 92 | ioctl(nbd, NBD_SET_BLKSIZE, 4096); | ||
| 93 | ioctl(nbd, NBD_SET_SIZE_BLOCKS, SWAP_BE64(nbd_header.devsize) / 4096); | ||
| 94 | ioctl(nbd, NBD_CLEAR_SOCK); | ||
| 95 | |||
| 96 | // If the sucker was exported read only, respect that locally | ||
| 97 | ro = (nbd_header.flags & SWAP_BE32(2)) / SWAP_BE32(2); | ||
| 98 | if (ioctl(nbd, BLKROSET, &ro) < 0) | ||
| 99 | bb_perror_msg_and_die("BLKROSET"); | ||
| 100 | |||
| 101 | if (timeout) | ||
| 102 | if (ioctl(nbd, NBD_SET_TIMEOUT, timeout)) | ||
| 103 | bb_perror_msg_and_die("NBD_SET_TIMEOUT"); | ||
| 104 | if (ioctl(nbd, NBD_SET_SOCK, sock)) | ||
| 105 | bb_perror_msg_and_die("NBD_SET_SOCK"); | ||
| 106 | |||
| 107 | // if (swap) mlockall(MCL_CURRENT|MCL_FUTURE); | ||
| 108 | |||
| 109 | #if BB_MMU | ||
| 110 | // Open the device to force reread of the partition table. | ||
| 111 | // Need to do it in a separate process, since open(device) | ||
| 112 | // needs some other process to sit in ioctl(nbd, NBD_DO_IT). | ||
| 113 | if (fork() == 0) { | ||
| 114 | char *s = strrchr(device, '/'); | ||
| 115 | sprintf(nbd_header.data, "/sys/block/%.32s/pid", s ? s + 1 : device); | ||
| 116 | // Is it up yet? | ||
| 117 | for (;;) { | ||
| 118 | int fd = open(nbd_header.data, O_RDONLY); | ||
| 119 | if (fd >= 0) { | ||
| 120 | //close(fd); | ||
| 121 | break; | ||
| 122 | } | ||
| 123 | sleep(1); | ||
| 124 | } | ||
| 125 | open(device, O_RDONLY); | ||
| 126 | return 0; | ||
| 127 | } | ||
| 128 | |||
| 129 | // Daemonize here | ||
| 130 | if (!nofork) { | ||
| 131 | daemon(0, 0); | ||
| 132 | nofork = 1; | ||
| 133 | } | ||
| 134 | #endif | ||
| 135 | |||
| 136 | // This turns us (the process that calls this ioctl) | ||
| 137 | // into a dedicated NBD request handler. | ||
| 138 | // We block here for a long time. | ||
| 139 | // When exactly ioctl returns? On a signal, | ||
| 140 | // or if someone does ioctl(NBD_DISCONNECT) [nbd-client -d]. | ||
| 141 | if (ioctl(nbd, NBD_DO_IT) >= 0 || errno == EBADR) { | ||
| 142 | // Flush queue and exit | ||
| 143 | ioctl(nbd, NBD_CLEAR_QUEUE); | ||
| 144 | ioctl(nbd, NBD_CLEAR_SOCK); | ||
| 145 | break; | ||
| 146 | } | ||
| 147 | |||
| 148 | close(sock); | ||
| 149 | close(nbd); | ||
| 150 | } | ||
| 151 | |||
| 152 | return 0; | ||
| 153 | } | ||
