blob: bedb01a1c33f744137c88d2bfc9c7bf28497b718 [file] [log] [blame]
Rob Landley1fc20c42010-09-23 02:03:47 +02001/*
2 * Copyright 2010 Rob Landley <rob@landley.net>
3 *
4 * Licensed under GPLv2, see file LICENSE in this source tree.
5 */
Rob Landley1fc20c42010-09-23 02:03:47 +02006//config:config NBDCLIENT
Denys Vlasenko4eed2c62017-07-18 22:01:24 +02007//config: bool "nbd-client (4.6 kb)"
Rob Landley1fc20c42010-09-23 02:03:47 +02008//config: default y
9//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020010//config: Network block device client
Rob Landley1fc20c42010-09-23 02:03:47 +020011
Denys Vlasenko0c4dbd42017-09-18 16:28:43 +020012//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>
19
Rob Landley1fc20c42010-09-23 02:03:47 +020020#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
45int nbdclient_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkobfc66d42017-08-06 21:53:39 +020046int nbdclient_main(int argc UNUSED_PARAM, char **argv)
Rob Landley1fc20c42010-09-23 02:03:47 +020047{
48 unsigned long timeout = 0;
Alexander Shishkin46b6cd72010-10-21 13:32:27 +030049#if BB_MMU
Rob Landley1fc20c42010-09-23 02:03:47 +020050 int nofork = 0;
Alexander Shishkin46b6cd72010-10-21 13:32:27 +030051#endif
Rob Landley1fc20c42010-09-23 02:03:47 +020052 char *host, *port, *device;
53 struct nbd_header_t {
54 uint64_t magic1; // "NBDMAGIC"
55 uint64_t magic2; // 0x420281861253 big endian
56 uint64_t devsize;
57 uint32_t flags;
58 char data[124];
59 } nbd_header;
Denys Vlasenko7b85ec32015-10-13 17:17:34 +020060
61 BUILD_BUG_ON(offsetof(struct nbd_header_t, data) != 8+8+8+4);
Rob Landley1fc20c42010-09-23 02:03:47 +020062
63 // Parse command line stuff (just a stub now)
Denys Vlasenkobfc66d42017-08-06 21:53:39 +020064 if (!argv[1] || !argv[2] || !argv[3] || argv[4])
Rob Landley1fc20c42010-09-23 02:03:47 +020065 bb_show_usage();
66
67#if !BB_MMU
68 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
69#endif
70
71 host = argv[1];
72 port = argv[2];
73 device = argv[3];
74
75 // Repeat until spanked (-persist behavior)
76 for (;;) {
77 int sock, nbd;
78 int ro;
79
80 // Make sure the /dev/nbd exists
81 nbd = xopen(device, O_RDWR);
82
83 // Find and connect to server
84 sock = create_and_connect_stream_or_die(host, xatou16(port));
Denys Vlasenkoc52cbea2015-08-24 19:48:03 +020085 setsockopt_1(sock, IPPROTO_TCP, TCP_NODELAY);
Rob Landley1fc20c42010-09-23 02:03:47 +020086
87 // Log on to the server
88 xread(sock, &nbd_header, 8+8+8+4 + 124);
89 if (memcmp(&nbd_header.magic1, "NBDMAGIC""\x00\x00\x42\x02\x81\x86\x12\x53", 16) != 0)
90 bb_error_msg_and_die("login failed");
91
92 // Set 4k block size. Everything uses that these days
93 ioctl(nbd, NBD_SET_BLKSIZE, 4096);
94 ioctl(nbd, NBD_SET_SIZE_BLOCKS, SWAP_BE64(nbd_header.devsize) / 4096);
95 ioctl(nbd, NBD_CLEAR_SOCK);
96
97 // If the sucker was exported read only, respect that locally
98 ro = (nbd_header.flags & SWAP_BE32(2)) / SWAP_BE32(2);
99 if (ioctl(nbd, BLKROSET, &ro) < 0)
100 bb_perror_msg_and_die("BLKROSET");
101
102 if (timeout)
103 if (ioctl(nbd, NBD_SET_TIMEOUT, timeout))
104 bb_perror_msg_and_die("NBD_SET_TIMEOUT");
105 if (ioctl(nbd, NBD_SET_SOCK, sock))
106 bb_perror_msg_and_die("NBD_SET_SOCK");
107
108 // if (swap) mlockall(MCL_CURRENT|MCL_FUTURE);
109
110#if BB_MMU
111 // Open the device to force reread of the partition table.
112 // Need to do it in a separate process, since open(device)
113 // needs some other process to sit in ioctl(nbd, NBD_DO_IT).
114 if (fork() == 0) {
115 char *s = strrchr(device, '/');
116 sprintf(nbd_header.data, "/sys/block/%.32s/pid", s ? s + 1 : device);
117 // Is it up yet?
118 for (;;) {
119 int fd = open(nbd_header.data, O_RDONLY);
120 if (fd >= 0) {
121 //close(fd);
122 break;
123 }
124 sleep(1);
125 }
126 open(device, O_RDONLY);
127 return 0;
128 }
129
130 // Daemonize here
131 if (!nofork) {
132 daemon(0, 0);
133 nofork = 1;
134 }
135#endif
136
137 // This turns us (the process that calls this ioctl)
138 // into a dedicated NBD request handler.
139 // We block here for a long time.
140 // When exactly ioctl returns? On a signal,
141 // or if someone does ioctl(NBD_DISCONNECT) [nbd-client -d].
142 if (ioctl(nbd, NBD_DO_IT) >= 0 || errno == EBADR) {
143 // Flush queue and exit
144 ioctl(nbd, NBD_CLEAR_QUEUE);
145 ioctl(nbd, NBD_CLEAR_SOCK);
146 break;
147 }
148
149 close(sock);
150 close(nbd);
151 }
152
153 return 0;
154}