Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 Rob Landley <rob@landley.net> |
| 3 | * |
| 4 | * Licensed under GPLv2, see file LICENSE in this source tree. |
| 5 | */ |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 6 | //config:config NBDCLIENT |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 7 | //config: bool "nbd-client (4.6 kb)" |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 8 | //config: default y |
| 9 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 10 | //config: Network block device client |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 11 | |
Denys Vlasenko | 0c4dbd4 | 2017-09-18 16:28:43 +0200 | [diff] [blame] | 12 | //applet:IF_NBDCLIENT(APPLET_NOEXEC(nbd-client, nbdclient, BB_DIR_USR_SBIN, BB_SUID_DROP, nbdclient)) |
| 13 | |
| 14 | //kbuild:lib-$(CONFIG_NBDCLIENT) += nbd-client.o |
| 15 | |
| 16 | #include "libbb.h" |
| 17 | #include <netinet/tcp.h> |
| 18 | #include <linux/fs.h> |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 19 | #include <getopt.h> |
Denys Vlasenko | 0c4dbd4 | 2017-09-18 16:28:43 +0200 | [diff] [blame] | 20 | |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 21 | #define NBD_SET_SOCK _IO(0xab, 0) |
| 22 | #define NBD_SET_BLKSIZE _IO(0xab, 1) |
| 23 | #define NBD_SET_SIZE _IO(0xab, 2) |
| 24 | #define NBD_DO_IT _IO(0xab, 3) |
| 25 | #define NBD_CLEAR_SOCK _IO(0xab, 4) |
| 26 | #define NBD_CLEAR_QUEUE _IO(0xab, 5) |
| 27 | #define NBD_PRINT_DEBUG _IO(0xab, 6) |
| 28 | #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7) |
| 29 | #define NBD_DISCONNECT _IO(0xab, 8) |
| 30 | #define NBD_SET_TIMEOUT _IO(0xab, 9) |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 31 | #define NBD_SET_FLAGS _IO(0xab, 10) |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 32 | |
| 33 | //usage:#define nbdclient_trivial_usage |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 34 | //usage: "{ [-b BLKSIZE] [-N NAME] [-t SEC] [-p] HOST [PORT] | -d } BLOCKDEV" |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 35 | //usage:#define nbdclient_full_usage "\n\n" |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 36 | //usage: "Connect to HOST and provide network block device on BLOCKDEV" |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 37 | |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 38 | //TODO: more compat with nbd-client version 3.17 - |
| 39 | //nbd-client host [ port ] nbd-device [ -connections num ] [ -sdp ] [ -swap ] |
| 40 | // [ -persist ] [ -nofork ] [ -nonetlink ] [ -systemd-mark ] |
| 41 | // [ -block-size block size ] [ -timeout seconds ] [ -name name ] |
| 42 | // [ -certfile certfile ] [ -keyfile keyfile ] [ -cacertfile cacertfile ] |
| 43 | // [ -tlshostname hostname ] |
| 44 | //nbd-client -unix path nbd-device [ -connections num ] [ -sdp ] [ -swap ] |
| 45 | // [ -persist ] [ -nofork ] [ -nonetlink ] [ -systemd-mark ] |
| 46 | // [ -block-size block size ] [ -timeout seconds ] [ -name name ] |
| 47 | //nbd-client nbd-device |
| 48 | //nbd-client -d nbd-device |
| 49 | //nbd-client -c nbd-device |
| 50 | //nbd-client -l host [ port ] |
| 51 | //nbd-client [ -netlink ] -l host |
| 52 | // |
| 53 | //Default value for blocksize is 4096 |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 54 | //Allowed values for blocksize are 512,1024,2048,4096 |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 55 | |
| 56 | int nbdclient_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 57 | int nbdclient_main(int argc, char **argv) |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 58 | { |
Alexander Shishkin | 46b6cd7 | 2010-10-21 13:32:27 +0300 | [diff] [blame] | 59 | #if BB_MMU |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 60 | bool nofork; |
Alexander Shishkin | 46b6cd7 | 2010-10-21 13:32:27 +0300 | [diff] [blame] | 61 | #endif |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 62 | bool opt_d; |
| 63 | bool opt_p; |
| 64 | const char *host, *port, *device; |
| 65 | const char *name; |
| 66 | unsigned blksize, size_blocks; |
| 67 | unsigned timeout; |
| 68 | int ch; |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 69 | struct nbd_header_t { |
| 70 | uint64_t magic1; // "NBDMAGIC" |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 71 | uint64_t magic2; // old style: 0x420281861253 big endian |
| 72 | // // new style: 0x49484156454F5054 (IHAVEOPT) |
| 73 | } nbd_header; |
| 74 | struct old_nbd_header_t { |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 75 | uint64_t devsize; |
| 76 | uint32_t flags; |
| 77 | char data[124]; |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 78 | } old_nbd_header; |
| 79 | struct new_nbd_header_t { |
| 80 | uint64_t devsize; |
| 81 | uint16_t transmission_flags; |
| 82 | char data[124]; |
| 83 | } new_nbd_header; |
| 84 | struct nbd_opt_t { |
| 85 | uint64_t magic; |
| 86 | uint32_t opt; |
| 87 | uint32_t len; |
| 88 | } nbd_opts; |
Denys Vlasenko | 7b85ec3 | 2015-10-13 17:17:34 +0200 | [diff] [blame] | 89 | |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 90 | static const struct option long_options[] = { |
| 91 | { "block-size", required_argument, NULL, 'b' }, |
| 92 | { "timeout" , required_argument, NULL, 't' }, |
| 93 | { "name" , required_argument, NULL, 'n' }, |
| 94 | { "persist" , no_argument , NULL, 'p' }, |
| 95 | { NULL } |
| 96 | }; |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 97 | |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 98 | BUILD_BUG_ON(offsetof(struct old_nbd_header_t, data) != 8 + 4); |
| 99 | BUILD_BUG_ON(offsetof(struct new_nbd_header_t, data) != 8 + 2); |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 100 | |
| 101 | #if !BB_MMU |
| 102 | bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv); |
| 103 | #endif |
| 104 | |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 105 | // Parse args. nbd-client uses stupid "one-dash long options" style :( |
| 106 | // Even though short forms (-b,-t,-N,-p) exist for all long opts, |
| 107 | // older manpages only contained long forms, which probably resulted |
| 108 | // in many scripts using them. |
| 109 | blksize = 4096; |
| 110 | timeout = 0; |
| 111 | name = ""; // use of "" instead of NULL simplifies strlen() later |
| 112 | opt_d = opt_p = 0; |
| 113 | while ((ch = getopt_long_only(argc, argv, "dN:", long_options, NULL)) != -1) { |
| 114 | switch (ch) { |
| 115 | case 'p': // -persist |
| 116 | opt_p = 1; |
| 117 | break; |
| 118 | case 'd': // -d |
| 119 | opt_d = 1; |
| 120 | break; |
| 121 | case 'b': // -block-size |
| 122 | blksize = xatou(optarg); |
| 123 | break; |
| 124 | case 't': // -timeout |
| 125 | timeout = xatou(optarg); |
| 126 | break; |
| 127 | case 'N': // -N |
| 128 | case 'n': // -name |
| 129 | name = optarg; |
| 130 | break; |
| 131 | default: |
| 132 | bb_show_usage(); |
| 133 | } |
| 134 | } |
| 135 | argv += optind; |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 136 | |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 137 | if (opt_d) { // -d |
| 138 | if (argv[0] && !argv[1]) { |
| 139 | int nbd = xopen(argv[0], O_RDWR); |
| 140 | ioctl(nbd, NBD_DISCONNECT); |
| 141 | ioctl(nbd, NBD_CLEAR_SOCK); |
| 142 | if (ENABLE_FEATURE_CLEAN_UP) |
| 143 | close(nbd); |
| 144 | return 0; |
| 145 | } |
| 146 | bb_show_usage(); |
| 147 | } |
| 148 | |
| 149 | // Allow only argv[] of: HOST [PORT] BLOCKDEV |
| 150 | if (!argv[0] || !argv[1] || (argv[2] && argv[3])) { |
| 151 | bb_show_usage(); |
| 152 | } |
| 153 | |
| 154 | host = argv[0]; |
| 155 | port = argv[2] ? argv[1] : "10809"; |
| 156 | device = argv[2] ? argv[2] : argv[1]; |
| 157 | |
| 158 | // Repeat until spanked if -persist |
| 159 | #if BB_MMU |
| 160 | nofork = 0; |
| 161 | #endif |
| 162 | do { |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 163 | int sock, nbd; |
| 164 | int ro; |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 165 | int proto_new; // 0 for old, 1 for new |
| 166 | char *data; |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 167 | |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 168 | // Make sure BLOCKDEV exists |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 169 | nbd = xopen(device, O_RDWR); |
| 170 | |
| 171 | // Find and connect to server |
| 172 | sock = create_and_connect_stream_or_die(host, xatou16(port)); |
Denys Vlasenko | c52cbea | 2015-08-24 19:48:03 +0200 | [diff] [blame] | 173 | setsockopt_1(sock, IPPROTO_TCP, TCP_NODELAY); |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 174 | |
| 175 | // Log on to the server |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 176 | xread(sock, &nbd_header, 8 + 8); |
| 177 | if (memcmp(&nbd_header.magic1, "NBDMAGIC", |
| 178 | sizeof(nbd_header.magic1)) != 0 |
| 179 | ) { |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 180 | bb_error_msg_and_die("login failed"); |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 181 | } |
| 182 | if (memcmp(&nbd_header.magic2, |
| 183 | "\x00\x00\x42\x02\x81\x86\x12\x53", |
| 184 | sizeof(nbd_header.magic2)) == 0 |
| 185 | ) { |
| 186 | proto_new = 0; |
| 187 | } else if (memcmp(&nbd_header.magic2, "IHAVEOPT", 8) == 0) { |
| 188 | proto_new = 1; |
| 189 | } else { |
| 190 | bb_error_msg_and_die("login failed"); |
| 191 | } |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 192 | |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 193 | if (!proto_new) { |
| 194 | xread(sock, &old_nbd_header, |
| 195 | sizeof(old_nbd_header.devsize) + |
| 196 | sizeof(old_nbd_header.flags) + |
| 197 | sizeof(old_nbd_header.data)); |
| 198 | size_blocks = SWAP_BE64(old_nbd_header.devsize) / blksize; |
| 199 | ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long) blksize); |
| 200 | ioctl(nbd, NBD_SET_SIZE_BLOCKS, size_blocks); |
| 201 | ioctl(nbd, NBD_CLEAR_SOCK); |
| 202 | ro = !!(old_nbd_header.flags & htons(2)); |
| 203 | data = old_nbd_header.data; |
| 204 | } else { |
| 205 | unsigned namelen; |
| 206 | uint16_t handshake_flags; |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 207 | |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 208 | xread(sock, &handshake_flags, sizeof(handshake_flags)); |
| 209 | xwrite(sock, &const_int_0, sizeof(const_int_0)); // client_flags |
| 210 | |
| 211 | memcpy(&nbd_opts.magic, "IHAVEOPT", |
| 212 | sizeof(nbd_opts.magic)); |
| 213 | nbd_opts.opt = htonl(1); // NBD_OPT_EXPORT_NAME |
| 214 | namelen = strlen(name); |
| 215 | nbd_opts.len = htonl(namelen); |
| 216 | xwrite(sock, &nbd_opts, |
| 217 | sizeof(nbd_opts.magic) + |
| 218 | sizeof(nbd_opts.opt) + |
| 219 | sizeof(nbd_opts.len)); |
| 220 | xwrite(sock, name, namelen); |
| 221 | |
| 222 | xread(sock, &new_nbd_header, |
| 223 | sizeof(new_nbd_header.devsize) + |
| 224 | sizeof(new_nbd_header.transmission_flags) + |
| 225 | sizeof(new_nbd_header.data)); |
| 226 | size_blocks = SWAP_BE64(new_nbd_header.devsize) / blksize; |
| 227 | ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long) blksize); |
| 228 | ioctl(nbd, NBD_SET_SIZE_BLOCKS, size_blocks); |
| 229 | ioctl(nbd, NBD_CLEAR_SOCK); |
| 230 | ioctl(nbd, NBD_SET_FLAGS, |
| 231 | ntohs(new_nbd_header.transmission_flags)); |
| 232 | ro = !!(new_nbd_header.transmission_flags & htons(2)); |
| 233 | data = new_nbd_header.data; |
| 234 | } |
| 235 | |
| 236 | if (ioctl(nbd, BLKROSET, &ro) < 0) { |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 237 | bb_perror_msg_and_die("BLKROSET"); |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 238 | } |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 239 | |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 240 | if (timeout) { |
| 241 | if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long) timeout)) { |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 242 | bb_perror_msg_and_die("NBD_SET_TIMEOUT"); |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | if (ioctl(nbd, NBD_SET_SOCK, sock)) { |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 247 | bb_perror_msg_and_die("NBD_SET_SOCK"); |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 248 | } |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 249 | |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 250 | //if (swap) mlockall(MCL_CURRENT|MCL_FUTURE); |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 251 | #if BB_MMU |
| 252 | // Open the device to force reread of the partition table. |
| 253 | // Need to do it in a separate process, since open(device) |
| 254 | // needs some other process to sit in ioctl(nbd, NBD_DO_IT). |
| 255 | if (fork() == 0) { |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 256 | /* child */ |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 257 | char *s = strrchr(device, '/'); |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 258 | sprintf(data, "/sys/block/%.32s/pid", s ? s + 1 : device); |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 259 | // Is it up yet? |
| 260 | for (;;) { |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 261 | int fd = open(data, O_RDONLY); |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 262 | if (fd >= 0) { |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 263 | if (ENABLE_FEATURE_CLEAN_UP) |
| 264 | close(fd); |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 265 | break; |
| 266 | } |
| 267 | sleep(1); |
| 268 | } |
| 269 | open(device, O_RDONLY); |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | // Daemonize here |
| 274 | if (!nofork) { |
| 275 | daemon(0, 0); |
| 276 | nofork = 1; |
| 277 | } |
| 278 | #endif |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 279 | // This turns us (the process that calls this ioctl) |
| 280 | // into a dedicated NBD request handler. |
| 281 | // We block here for a long time. |
| 282 | // When exactly ioctl returns? On a signal, |
| 283 | // or if someone does ioctl(NBD_DISCONNECT) [nbd-client -d]. |
| 284 | if (ioctl(nbd, NBD_DO_IT) >= 0 || errno == EBADR) { |
| 285 | // Flush queue and exit |
| 286 | ioctl(nbd, NBD_CLEAR_QUEUE); |
| 287 | ioctl(nbd, NBD_CLEAR_SOCK); |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | close(sock); |
| 292 | close(nbd); |
Denys Vlasenko | 4329116 | 2018-10-24 15:51:38 +0200 | [diff] [blame] | 293 | } while (opt_p); |
Rob Landley | 1fc20c4 | 2010-09-23 02:03:47 +0200 | [diff] [blame] | 294 | |
| 295 | return 0; |
| 296 | } |