Recognize the "newstyle" protocol and switch to it automatically. Add options for setting blocksize (-b) and for disconnecting a nbd device (-d).
Signed-off-by: Elvira Khabirova <lineprin...@altlinux.org> --- networking/nbd-client.c | 168 +++++++++++++++++++++++++++++++++------- 1 file changed, 140 insertions(+), 28 deletions(-) diff --git a/networking/nbd-client.c b/networking/nbd-client.c index bedb01a1c..9e718ff93 100644 --- a/networking/nbd-client.c +++ b/networking/nbd-client.c @@ -27,17 +27,17 @@ #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7) #define NBD_DISCONNECT _IO(0xab, 8) #define NBD_SET_TIMEOUT _IO(0xab, 9) +#define NBD_SET_FLAGS _IO(0xab, 10) //usage:#define nbdclient_trivial_usage -//usage: "HOST PORT BLOCKDEV" +//usage: "{ [-b BLKSIZE] [-N name] HOST PORT | -d } BLOCKDEV" //usage:#define nbdclient_full_usage "\n\n" //usage: "Connect to HOST and provide a network block device on BLOCKDEV" //TODO: more compat with nbd-client version 2.9.13 - //Usage: nbd-client [bs=blocksize] [timeout=sec] host port nbd_device [-swap] [-persist] [-nofork] -//Or : nbd-client -d nbd_device //Or : nbd-client -c nbd_device -//Default value for blocksize is 1024 (recommended for ethernet) +//Default value for blocksize is 4096 //Allowed values for blocksize are 512,1024,2048,4096 //Note, that kernel 2.4.2 and older ones do not work correctly with //blocksizes other than 1024 without patches @@ -45,37 +45,90 @@ int nbdclient_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int nbdclient_main(int argc UNUSED_PARAM, char **argv) { - unsigned long timeout = 0; #if BB_MMU int nofork = 0; #endif - char *host, *port, *device; + char *host, *port, *device, *name; + unsigned blksize, size_blocks; + unsigned timeout; + unsigned opt; + + uint16_t handshake_flags; + uint32_t client_flags = 0; + struct nbd_header_t { uint64_t magic1; // "NBDMAGIC" - uint64_t magic2; // 0x420281861253 big endian + uint64_t magic2; // old style: 0x420281861253 big endian + // new style: 0x49484156454F5054 (IHAVEOPT) + } nbd_header; + + struct old_nbd_header_t { uint64_t devsize; uint32_t flags; char data[124]; - } nbd_header; + } old_nbd_header; - BUILD_BUG_ON(offsetof(struct nbd_header_t, data) != 8+8+8+4); + struct new_nbd_header_t { + uint64_t devsize; + uint16_t transmission_flags; + char data[124]; + } new_nbd_header; - // Parse command line stuff (just a stub now) - if (!argv[1] || !argv[2] || !argv[3] || argv[4]) - bb_show_usage(); + struct nbd_opt_t { + uint64_t magic; + uint32_t opt; + uint32_t len; + } nbd_opts; + + BUILD_BUG_ON(offsetof(struct old_nbd_header_t, data) != 8+4); + BUILD_BUG_ON(offsetof(struct new_nbd_header_t, data) != 8+2); #if !BB_MMU bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv); #endif - host = argv[1]; - port = argv[2]; - device = argv[3]; + // Parse args + opt = getopt32(argv, "b:+t:+N:d", &blksize, &timeout, &name); + argv += optind; + + if (opt & 0x8) { + if (argv[0]) { + int nbd = xopen(argv[0], O_RDWR); + ioctl(nbd, NBD_DISCONNECT); + ioctl(nbd, NBD_CLEAR_SOCK); + close(nbd); + return 0; + } else { + bb_show_usage(); + } + } + + if (!(opt & 0x1)) { + blksize = 4096; + } + + if (!(opt & 0x2)) { + timeout = 0; + } + + if (!(opt & 0x4)) { + name = NULL; + } + + if (!argv[0] || !argv[1] || !argv[2] || argv[3]) { + bb_show_usage(); + } + + host = argv[0]; + port = argv[1]; + device = argv[2]; // Repeat until spanked (-persist behavior) for (;;) { int sock, nbd; int ro; + int protover; // 0 for old, 1 for new + char *data; // Make sure the /dev/nbd exists nbd = xopen(device, O_RDWR); @@ -85,25 +138,84 @@ int nbdclient_main(int argc UNUSED_PARAM, char **argv) setsockopt_1(sock, IPPROTO_TCP, TCP_NODELAY); // Log on to the server - xread(sock, &nbd_header, 8+8+8+4 + 124); - if (memcmp(&nbd_header.magic1, "NBDMAGIC""\x00\x00\x42\x02\x81\x86\x12\x53", 16) != 0) + xread(sock, &nbd_header, 8+8); + if (memcmp(&nbd_header.magic1, "NBDMAGIC", + sizeof(nbd_header.magic1)) != 0 + ) { + bb_error_msg_and_die("login failed"); + } + + if (memcmp(&nbd_header.magic2, + "\x00\x00\x42\x02\x81\x86\x12\x53", + sizeof(nbd_header.magic2)) == 0 + ) { + protover = 0; + } else if (memcmp(&nbd_header.magic2, "IHAVEOPT", 8) == 0) { + protover = 1; + } else { bb_error_msg_and_die("login failed"); + } - // Set 4k block size. Everything uses that these days - ioctl(nbd, NBD_SET_BLKSIZE, 4096); - ioctl(nbd, NBD_SET_SIZE_BLOCKS, SWAP_BE64(nbd_header.devsize) / 4096); - ioctl(nbd, NBD_CLEAR_SOCK); + if (protover == 0) { + xread(sock, &old_nbd_header, + sizeof(old_nbd_header.devsize) + + sizeof(old_nbd_header.flags) + + sizeof(old_nbd_header.data)); + size_blocks = SWAP_BE64(old_nbd_header.devsize) / blksize; + ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long) blksize); + ioctl(nbd, NBD_SET_SIZE_BLOCKS, size_blocks); + ioctl(nbd, NBD_CLEAR_SOCK); + ro = !!(ntohl(old_nbd_header.flags) & 0x2); + data = old_nbd_header.data; + } else { + xread(sock, &handshake_flags, sizeof(handshake_flags)); + xwrite(sock, &client_flags, sizeof(client_flags)); - // If the sucker was exported read only, respect that locally - ro = (nbd_header.flags & SWAP_BE32(2)) / SWAP_BE32(2); - if (ioctl(nbd, BLKROSET, &ro) < 0) + memcpy(&nbd_opts.magic, "IHAVEOPT", + sizeof(nbd_opts.magic)); + nbd_opts.opt = htonl(0x1); // NBD_OPT_EXPORT_NAME + if (!name) { + nbd_opts.len = 0; + xwrite(sock, &nbd_opts, + sizeof(nbd_opts.magic) + + sizeof(nbd_opts.opt) + + sizeof(nbd_opts.len)); + } else { + nbd_opts.len = htonl(strlen(name)); + xwrite(sock, &nbd_opts, + sizeof(nbd_opts.magic) + + sizeof(nbd_opts.opt) + + sizeof(nbd_opts.len)); + xwrite(sock, name, strlen(name)); + } + + xread(sock, &new_nbd_header, + sizeof(new_nbd_header.devsize) + + sizeof(new_nbd_header.transmission_flags) + + sizeof(new_nbd_header.data)); + size_blocks = SWAP_BE64(new_nbd_header.devsize) / blksize; + ioctl(nbd, NBD_SET_BLKSIZE, (unsigned long) blksize); + ioctl(nbd, NBD_SET_SIZE_BLOCKS, size_blocks); + ioctl(nbd, NBD_CLEAR_SOCK); + ioctl(nbd, NBD_SET_FLAGS, + ntohs(new_nbd_header.transmission_flags)); + ro = !!(ntohs(new_nbd_header.transmission_flags) & 0x2); + data = new_nbd_header.data; + } + + if (ioctl(nbd, BLKROSET, &ro) < 0) { bb_perror_msg_and_die("BLKROSET"); + } - if (timeout) - if (ioctl(nbd, NBD_SET_TIMEOUT, timeout)) + if (timeout) { + if (ioctl(nbd, NBD_SET_TIMEOUT, (unsigned long) timeout)) { bb_perror_msg_and_die("NBD_SET_TIMEOUT"); - if (ioctl(nbd, NBD_SET_SOCK, sock)) + } + } + + if (ioctl(nbd, NBD_SET_SOCK, sock)) { bb_perror_msg_and_die("NBD_SET_SOCK"); + } // if (swap) mlockall(MCL_CURRENT|MCL_FUTURE); @@ -113,10 +225,10 @@ int nbdclient_main(int argc UNUSED_PARAM, char **argv) // needs some other process to sit in ioctl(nbd, NBD_DO_IT). if (fork() == 0) { char *s = strrchr(device, '/'); - sprintf(nbd_header.data, "/sys/block/%.32s/pid", s ? s + 1 : device); + sprintf(data, "/sys/block/%.32s/pid", s ? s + 1 : device); // Is it up yet? for (;;) { - int fd = open(nbd_header.data, O_RDONLY); + int fd = open(data, O_RDONLY); if (fd >= 0) { //close(fd); break; -- 2.19.0 _______________________________________________ busybox mailing list busybox@busybox.net http://lists.busybox.net/mailman/listinfo/busybox