Debian Codegraph @

utils/diodmount.c

raw
1 /*****************************************************************************
2 * Copyright (C) 2010-14 Lawrence Livermore National Security, LLC.
3 * Written by Jim Garlick <[email protected]> LLNL-CODE-423279
4 * All Rights Reserved.
5 *
6 * This file is part of the Distributed I/O Daemon (diod).
7 * For details, see http://code.google.com/p/diod.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the license, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * See also: http://www.gnu.org/licenses
23 *****************************************************************************/
24
25 /* diodmount.c - diod file system mount helper
26 *
27 * Usage: /sbin/mount.diod spec dir [-sfnv] [-o options]
28 */
29
30 #if HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 #include <sched.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <sys/socket.h>
40 #include <sys/wait.h>
41 #include <stdint.h>
42 #include <netdb.h>
43 #include <stdio.h>
44 #if HAVE_GETOPT_H
45 #include <getopt.h>
46 #endif
47 #include <mntent.h>
48 #include <sys/mount.h>
49 #include <string.h>
50 #include <errno.h>
51 #include <ctype.h>
52 #include <pwd.h>
53 #include <libgen.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 #include <paths.h>
58
59
60 #ifndef PATH_MAX
61 #define PATH_MAX 1024
62 #endif
63
64 #include "9p.h"
65 #include "npfs.h"
66 #include "npclient.h"
67 #include "list.h"
68 #include "hostlist.h"
69 #include "diod_log.h"
70 #include "diod_sock.h"
71 #include "diod_auth.h"
72 #include "opt.h"
73
74 #define OPTIONS "fnvo:ad"
75 #if HAVE_GETOPT_LONG
76 #define GETOPT(ac,av,opt,lopt) getopt_long (ac,av,opt,lopt,NULL)
77 static const struct option longopts[] = {
78 {"fake-mount", no_argument, 0, 'f'},
79 {"no-mtab", no_argument, 0, 'n'},
80 {"verbose", no_argument, 0, 'v'},
81 {"options", required_argument, 0, 'o'},
82 {"9nbd-attach", no_argument, 0, 'a'},
83 {"9nbd-detach", no_argument, 0, 'd'},
84 {0, 0, 0, 0},
85 };
86 #else
87 #define GETOPT(ac,av,opt,lopt) getopt (ac,av,opt)
88 #endif
89
90 #define NBD_SET_BLKSIZE _IO( 0xab, 1 )
91 #define NBD_SET_TIMEOUT _IO( 0xab, 2 )
92 #define NBD_START _IO( 0xab, 3 )
93 #define NBD_STOP _IO( 0xab, 4 )
94
95 #define NBD_SET_OPTS _IOW( 0xab, 10, char* )
96 #define NBD_SET_ADDR _IOW( 0xab, 11, char* )
97 #define NBD_SET_PATH _IOW( 0xab, 12, char* )
98
99 #define DIOD_DEFAULT_MSIZE 65536
100 static uid_t _uname2uid (char *uname);
101 static void _diod_mount (Opt o, int rfd, int wfd, char *spec, char *dir,
102 int vopt, int fopt, int nopt);
103 static void _diod_remount (Opt o, char *spec, char *dir, int vopt, int fopt);
104 static void _verify_mountpoint (char *path);
105 static void _parse_uname_access (Opt o);
106 static char *_parse_spec (char *spec, Opt o);
107 static void _mount (const char *source, const char *target,
108 unsigned long mountflags, const void *data);
109
110 static void _nbd_attach (Opt o, int argc, char **argv, int nopt, int vopt);
111 static void _nbd_detach (Opt o, int argc, char **argv, int nopt, int vopt);
112
113 static void
114 usage (void)
115 {
116 fprintf (stderr,
117 "Usage: mount.diod [OPTIONS] host[:aname] directory\n"
118 " -f,--fake-mount do everything but the actual mount\n"
119 " -n,--no-mtab do not update /etc/mtab\n"
120 " -v,--verbose verbose mode\n"
121 " -o,--options opt[,opt,...] specify mount options\n"
122 //"Usage: mount.diod --9nbd-attach host[:aname] 9nbd-device\n"
123 //" --9nbd-detach 9nbd-device\n"
124 );
125 exit (1);
126 }
127
128 int
129 main (int argc, char *argv[])
130 {
131 char *dir = NULL;
132 char *spec, *host;
133 char *nspec = NULL;
134 int c, i;
135 int nopt = 0;
136 int vopt = 0;
137 int fopt = 0;
138 int aopt = 0;
139 int dopt = 0;
140 int rfd = -1, wfd = -1;
141 Opt o;
142
143 diod_log_init (argv[0]);
144
145 o = opt_create ();
146
147 opterr = 0;
148 while ((c = GETOPT (argc, argv, OPTIONS, longopts)) != -1) {
149 switch (c) {
150 case 'f': /* --fake-mount */
151 fopt = 1;
152 break;
153 case 'n': /* --no-mtab */
154 nopt = 1;
155 break;
156 case 'v': /* --verbose */
157 vopt++;
158 break;
159 case 'o': /* --options OPT[,OPT]... */
160 opt_addf (o, "%s", optarg);
161 break;
162 case 'a': /* --9nbd-attach */
163 aopt++;
164 break;
165 case 'd': /* --9nbd-detach */
166 dopt++;
167 break;
168 default:
169 usage ();
170 }
171 }
172
173 /* Take care of 9nbd operations and exit.
174 */
175 if (aopt) {
176 _nbd_attach (o, argc - optind, argv + optind, nopt, vopt);
177 exit (0);
178 }
179 if (dopt) {
180 _nbd_detach (o, argc - optind, argv + optind, nopt, vopt);
181 exit (0);
182 }
183
184 if (optind != argc - 2)
185 usage ();
186
187 if (geteuid () != 0)
188 msg_exit ("you must be root");
189
190 spec = argv[optind++];
191 dir = argv[optind++];
192 host = _parse_spec (spec, o);
193
194 _verify_mountpoint (dir);
195
196 /* Remount - only pass mount flags into the VFS for an existing mount.
197 * Take care of it here and exit.
198 */
199 if (opt_find (o, "remount")) {
200 if (opt_check_allowed_csv (o, "ro,rw,aname,remount"))
201 msg_exit ("-oremount can only be used with ro,rw");
202 _diod_remount (o, spec, dir, vopt, fopt);
203 goto done;
204 }
205
206 /* Ensure uname and access are set, and to diod-compatible values.
207 * The uname user becomes the euid which will be used by munge auth.
208 */
209 _parse_uname_access (o);
210 if (seteuid (_uname2uid (opt_find (o, "uname"))) < 0)
211 err_exit ("seteuid");
212
213 /* We require -otrans=fd because auth occurs in user space, then live fd
214 * is passed to the kernel via -orfdno,wfdno.
215 */
216 if (!opt_find (o, "trans"))
217 opt_addf (o, "trans=%s", "fd");
218 else if (!opt_find (o, "trans=fd"))
219 msg_exit ("only -otrans=fd transport is supported");
220
221 /* Set msize if not already set. Validate it later.
222 */
223 if (!opt_find (o, "msize"))
224 opt_addf (o, "msize=%d", DIOD_DEFAULT_MSIZE);
225
226 /* Only .L version is supported.
227 */
228 if (!opt_find (o, "version"))
229 opt_addf (o, "version=%s", "9p2000.L");
230 else if (!opt_find (o, "version=9p2000.L"))
231 msg_exit ("only -oversion=9p2000.L is supported (little p, big L)");
232
233 /* Set debug level.
234 */
235 if (!opt_find (o, "debug"))
236 opt_addf (o, "debug=%d", 0x1); /* send errors to dmesg */
237
238 /* Set rwdepth (number of concurrent reads with buffer > msize).
239 * N.B. this option is not upstream yet but unknown options are ignored.
240 */
241 if (!opt_find (o, "rwdepth"))
242 opt_addf (o, "rwdepth=%d", 1);
243
244 /* Server is on an inherited file descriptor.
245 * For testing, we start server on a socketpair duped to fd 0.
246 */
247 if (opt_find (o, "rfdno") || opt_find (o, "wfdno")) {
248 if (!opt_scanf (o, "rfdno=%d", &rfd) || !opt_scanf (o, "wfdno=%d",&wfd))
249 msg_exit ("-orfdno,wfdno must be used together");
250 nopt = 1; /* force no mtab */
251
252 /* Connect to server on UNIX domain socket
253 */
254 } else if (host[0] == '/') {
255 if (opt_find (o, "port"))
256 msg_exit ("-oport won't work with UNIX domain socket");
257 if ((rfd = diod_sock_connect_unix (host, 0)) < 0)
258 exit (1);
259 wfd = rfd;
260
261 opt_addf (o, "rfdno=%d", rfd);
262 opt_addf (o, "wfdno=%d", wfd);
263
264 /* Connect to server on IANA port (or user-specified) and host.
265 */
266 } else {
267 char *port = opt_find (o, "port");
268 hostlist_iterator_t hi;
269 hostlist_t hl;
270 char *h;
271
272 if (!port)
273 port = "564";
274 if (!(hl = hostlist_create (host)))
275 msg_exit ("error parsing host string: %s", host);
276 if (!(hi = hostlist_iterator_create (hl)))
277 msg_exit ("out of memory");
278 while ((h = hostlist_next (hi))) {
279 if (vopt)
280 msg ("trying to connect to %s:%s", h, port);
281 if ((rfd = diod_sock_connect_inet (h, port, DIOD_SOCK_QUIET)) >= 0)
282 break;
283 }
284 if (h) { /* create new 'spec' string identifying successful host */
285 char *p = strchr (spec , ':');
286 int len = strlen (h) + (p ? strlen (p) : 0) + 1;
287
288 if (!(nspec = malloc (len)))
289 msg_exit ("out of memory");
290 snprintf (nspec, len, "%s%s", h, p ? p : "");
291 }
292 hostlist_destroy (hl);
293 if (rfd < 0)
294 msg_exit ("could not connect to server(s), giving up");
295 wfd = rfd;
296
297 opt_delete (o, "port");
298 opt_addf (o, "rfdno=%d", rfd);
299 opt_addf (o, "wfdno=%d", wfd);
300 }
301
302 NP_ASSERT (opt_find (o, "trans=fd"));
303 NP_ASSERT (opt_scanf (o, "msize=%d", &i));
304 NP_ASSERT (opt_find (o, "version=9p2000.L"));
305 NP_ASSERT (opt_scanf (o, "debug=%d", &i) || opt_scanf (o, "debug=%x", &i));
306 NP_ASSERT (opt_scanf (o, "wfdno=%d", &i) && opt_scanf (o, "rfdno=%d", &i));
307 NP_ASSERT ((opt_find (o, "access=user") && opt_find(o, "uname=root"))
308 || (opt_scanf (o, "access=%d", &i) && opt_find(o, "uname")));
309
310 NP_ASSERT (!opt_find (o, "port"));
311
312 _diod_mount (o, rfd, wfd, nspec ? nspec : spec, dir, vopt, fopt, nopt);
313
314 done:
315 opt_destroy (o);
316 exit (0);
317 }
318
319 static uid_t
320 _uname2uid (char *uname)
321 {
322 struct passwd *pw;
323
324 if (!(pw = getpwnam (uname)))
325 msg_exit ("could not look up uname='%s'", uname);
326 return pw->pw_uid;
327 }
328
329 static char *
330 _parse_spec (char *spec, Opt o)
331 {
332 char *host, *aname;
333
334 if (!(host = strdup (spec)))
335 msg_exit ("out of memory");
336 if ((aname = strchr (host, ':')))
337 *aname++ = '\0';
338 if (strlen (host) == 0)
339 msg_exit ("no host specified");
340 if (!aname || strlen (aname) == 0)
341 ; /* aname = opt_find (o, "aname"); */
342 else if (!opt_addf (o, "aname=%s", aname))
343 msg_exit ("you cannot have both -oaname and spec=host:aname");
344
345 return host;
346 }
347
348 /* Verify that directory exists, exiting on failure.
349 */
350 static void
351 _verify_mountpoint (char *path)
352 {
353 struct stat sb;
354
355 if (stat (path, &sb) < 0)
356 err_exit ("stat %s", path);
357 if (!S_ISDIR (sb.st_mode))
358 msg_exit ("%s: not a directory", path);
359 }
360
361 /* Private or public mount? Only allow two modes:
362 * private: -ouname=USER,access=UID (uname could be root)
363 * public (dflt): -ouname=root,access=user
364 */
365 static void
366 _parse_uname_access (Opt o)
367 {
368 char *uname = opt_find (o, "uname");
369 int uname_uid = -1;
370 char *access = opt_find (o, "access");
371 int access_uid = -1;
372 char *access_name = NULL;
373 struct passwd *pw;
374
375 if (uname) {
376 if (!(pw = getpwnam (uname)))
377 msg_exit ("could not look up uname='%s'", uname);
378 uname_uid = pw->pw_uid;
379 }
380 if (access && opt_scanf (o, "access=%d", &access_uid)) {
381 if (!(pw = getpwuid (access_uid)))
382 msg_exit ("could not look up access='%d'", access_uid);
383 if (!(access_name = strdup (pw->pw_name)))
384 msg_exit ("out of memory");
385 }
386
387 if (!uname && !access) {
388 opt_addf (o, "uname=%s", "root");
389 opt_addf (o, "access=%s", "user");
390
391 } else if (uname && !access) {
392 if (uname_uid == 0)
393 opt_addf (o, "access=%s", "user");
394 else
395 opt_addf (o, "access=%d", uname_uid);
396
397 } else if (!uname && access) {
398 if (strcmp (access, "user") == 0)
399 opt_addf (o, "uname=%s", "root");
400 else if (access_name) /* access=<uid> */
401 opt_addf (o, "uname=%s", access_name);
402 else
403 msg_exit ("unsupported -oaccess=%s", access);
404 } else { /* if (uname && access) */
405 if (strcmp (access, "user") == 0) {
406 if (uname_uid != 0)
407 msg_exit ("-oaccess=user can only be used with -ouname=root");
408 } else if (access_name) { /* access=<uid> */
409 if (uname_uid != access_uid)
410 msg_exit ("-oaccess=<uid> requires matching -ouname=<name>");
411 } else
412 msg_exit ("unsupported -oaccess=%s", access);
413 }
414 if (access_name)
415 free (access_name);
416 }
417
418 typedef struct {
419 char *opt;
420 unsigned long flag;
421 } map_t;
422
423 static map_t setopt[] = {
424 { "noatime", MS_NOATIME},
425 { "nodev", MS_NODEV},
426 { "nodiratime", MS_NODIRATIME},
427 { "noexec", MS_NOEXEC},
428 { "nosuid", MS_NOSUID},
429 { "ro", MS_RDONLY },
430 { "remount", MS_REMOUNT},
431 { "sync", MS_SYNCHRONOUS},
432 };
433
434 static map_t clropt[] = {
435 { "atime", MS_NOATIME},
436 { "dev", MS_NODEV},
437 { "diratime", MS_NODIRATIME},
438 { "exec", MS_NOEXEC},
439 { "suid", MS_NOSUID},
440 { "rw", MS_RDONLY },
441 { "nosync", MS_SYNCHRONOUS},
442 };
443
444 static void
445 _getflags (Opt o, unsigned long *flags)
446 {
447 int i;
448
449 for (i = 0; i < sizeof (setopt) / sizeof (map_t); i++) {
450 if (opt_find (o, setopt[i].opt)) {
451 *flags |= setopt[i].flag;
452 opt_delete (o, setopt[i].opt);
453 }
454 }
455 for (i = 0; i < sizeof (clropt) / sizeof (map_t); i++) {
456 if (opt_find (o, clropt[i].opt)) {
457 *flags &= ~clropt[i].flag;
458 opt_delete (o, clropt[i].opt);
459 }
460 }
461 }
462
463 static void
464 _diod_remount (Opt o, char *spec, char *dir, int vopt, int fopt)
465 {
466 char *options = opt_csv (o);
467 unsigned long mountflags = 0;
468
469 _getflags (o, &mountflags);
470 if (vopt)
471 msg ("mount %s %s -o%s", spec, dir, options);
472 if (!fopt)
473 _mount (spec, dir, mountflags, NULL);
474
475 free (options);
476 }
477
478 static int
479 _update_mtab (char *options, char *spec, char *dir)
480 {
481 uid_t saved_euid = geteuid ();
482 FILE *f;
483 int ret = 0;
484 struct mntent mnt;
485
486 mnt.mnt_fsname = spec;
487 mnt.mnt_dir = dir;
488 mnt.mnt_type = "diod";
489 mnt.mnt_opts = options;
490 mnt.mnt_freq = 0;
491 mnt.mnt_passno = 0;
492
493 if (seteuid (0) < 0) {
494 err ("failed to set effective uid to root");
495 goto done;
496 }
497 if (!(f = setmntent (_PATH_MOUNTED, "a"))) {
498 err (_PATH_MOUNTED);
499 goto done;
500 }
501 if (addmntent (f, &mnt) != 0) {
502 msg ("failed to add entry to %s", _PATH_MOUNTED);
503 endmntent (f);
504 goto done;
505 }
506 endmntent (f);
507 if (seteuid (saved_euid) < 0) {
508 err ("failed to restore effective uid to %d", saved_euid);
509 goto done;
510 }
511 ret = 1;
512 done:
513 return ret;
514 }
515
516 static void
517 _diod_mount (Opt o, int rfd, int wfd, char *spec, char *dir, int vopt,
518 int fopt, int nopt)
519 {
520 char *options, *options9p, *aname, *uname;
521 uid_t uid;
522 int msize;
523 Npcfsys *fs;
524 Npcfid *afid, *root;
525 unsigned long mountflags = 0;
526
527 options = opt_csv (o);
528 _getflags (o, &mountflags);
529 options9p = opt_csv (o); /* after mountflags removed from opt list */
530
531 if (!(uname = opt_find (o, "uname")))
532 msg_exit ("uname is not set"); /* can't happen */
533 uid = _uname2uid (uname);
534 aname = opt_find (o, "aname"); /* can be null */
535 if (!opt_scanf (o, "msize=%d", &msize) || msize < P9_IOHDRSZ)
536 msg_exit ("msize must be set to integer >= %d", P9_IOHDRSZ);
537
538 if (vopt)
539 msg ("pre-authenticating connection to server");
540 if (!(fs = npc_start (rfd, wfd, msize, 0)))
541 errn_exit (np_rerror (), "version");
542 if (!(afid = npc_auth (fs, aname, uid, diod_auth)) && np_rerror () != 0)
543 errn_exit (np_rerror (), "auth");
544 if (!(root = npc_attach (fs, afid, aname, uid))) {
545 errn (np_rerror (), "attach");
546 if (afid)
547 (void)npc_clunk (afid);
548 exit (1);
549 }
550 if (afid && npc_clunk (afid) < 0)
551 errn_exit (np_rerror (), "clunk afid");
552 if (npc_clunk (root) < 0)
553 errn_exit (np_rerror (), "clunk root");
554 if (vopt)
555 msg ("mount -t 9p %s %s -o%s", spec, dir, options);
556 if (!fopt) {
557 /* kernel wants non-blocking */
558 if (fcntl (rfd, F_SETFL, O_NONBLOCK) < 0)
559 err_exit ("setting O_NONBLOCK flag on rfd=%d", rfd);
560 if (fcntl (wfd, F_SETFL, O_NONBLOCK) < 0)
561 err_exit ("setting O_NONBLOCK flag on wfd=%d", wfd);
562 _mount (spec, dir, mountflags, options9p);
563 }
564 npc_finish (fs); /* closes fd */
565
566 if (!nopt) {
567 if (!_update_mtab (options, spec, dir))
568 msg_exit ("failed to update /etc/mtab");
569 }
570 free (options);
571 free (options9p);
572 }
573
574 /* Mount 9p file system [source] on [target] with options [data].
575 * Swap effective (user) and real (root) uid's for the duration of mount call.
576 * Exit on error.
577 */
578 static void
579 _mount (const char *source, const char *target, unsigned long mountflags,
580 const void *data)
581 {
582 uid_t saved_euid = geteuid ();
583
584 if (seteuid (0) < 0)
585 err_exit ("failed to set effective uid to root");
586 if (mount (source, target, "9p", mountflags, data) < 0)
587 err_exit ("mount");
588 if (seteuid (saved_euid) < 0)
589 err_exit ("failed to restore effective uid to %d", saved_euid);
590 }
591
592 /* Convert hostname to ascii address that trans=tcp can use.
593 * Caller must free.
594 */
595 static char *
596 _name2addr (char *host)
597 {
598 int err;
599 struct addrinfo hints, *res = NULL;
600 char s[64], *addr;
601
602 memset (&hints, 0, sizeof (hints));
603 hints.ai_family = PF_UNSPEC;
604 hints.ai_socktype = SOCK_STREAM;
605
606 if ((err = getaddrinfo (host, NULL, &hints, &res)))
607 msg_exit ("%s: %s", host, gai_strerror (err));
608 if ((err = getnameinfo(res->ai_addr, res->ai_addrlen, s, sizeof(s),
609 NULL, 0, NI_NUMERICHOST)))
610 msg_exit ("%s: %s", host, gai_strerror (err));
611 if (!(addr = strdup (s)))
612 msg_exit ("out of memory");
613
614 return addr;
615 }
616
617 static void
618 _parse_nbdspec (char *spec, char **hp, char **fp)
619 {
620 char *host, *file;
621
622 if (!(host = strdup (spec)))
623 msg_exit ("out of memory");
624 if ((file = strchr (host, ':')))
625 *file++ = '\0';
626 if (strlen (host) == 0)
627 msg_exit ("no host specified");
628 if ((!file || strlen (file) == 0))
629 msg_exit ("no file specified");
630
631 *hp = host;
632 *fp = file;
633 }
634
635
636 /* Attach 9nbd device to remote file.
637 */
638 static void
639 _nbd_attach (Opt o, int argc, char **argv, int nopt, int vopt)
640 {
641 char *spec;
642 char *host;
643 char *addr;
644 char *dev;
645 char *path;
646 int fd;
647 char *options;
648 int blksize = 4096;
649 int uid;
650
651 if (argc != 2)
652 usage();
653 spec = argv[0];
654 dev = argv[1];
655
656 _parse_nbdspec (spec, &host, &path);
657 addr = _name2addr (host);
658
659 if (!opt_find (o, "aname")) {
660 opt_addf (o, "aname=%s", path);
661 path = NULL;
662 }
663 if (!opt_find (o, "msize"))
664 opt_addf (o, "msize=%d", DIOD_DEFAULT_MSIZE);
665
666 if (opt_find (o, "trans=fd"))
667 msg_exit ("9nbd doesn't work with trans=fd");
668 if (!opt_find (o, "trans"))
669 opt_addf (o, "trans=%s", "tcp");
670
671 if (opt_find (o, "version") && !opt_find (o, "version=9p2000.L"))
672 msg_exit ("9nbd only works with version=9p2000.L");
673 if (!opt_find (o, "version"))
674 opt_addf (o, "version=%s", "9p2000.L");
675
676 if (!opt_find (o, "port"))
677 opt_addf (o, "port=564");
678
679 /* for 9nbd we require uid=<int> instead of uname=<str> */
680 if (!opt_scanf (o, "uid=%d", &uid)) {
681 char uname[256];
682 if (opt_scanf (o, "uname=%255s", uname)) {
683 uid = _uname2uid (uname);
684 opt_delete (o, "uname");
685 } else
686 uid = 0;
687 opt_addf (o, "uid=%d", uid);
688 }
689
690 if (!opt_find (o, "auth"))
691 opt_addf (o, "auth=%s", "munge");
692
693 options = opt_csv (o);
694
695 if (!nopt) {
696 fd = open (dev, O_RDWR);
697 if (fd < 0 && (errno == ENOENT || errno == ENXIO)) {
698 system ("/sbin/modprobe 9nbd");
699 fd = open (dev, O_RDWR);
700 }
701 if (fd < 0)
702 err_exit ("open %s", dev);
703 }
704
705 if (vopt)
706 msg ("set blocksize=%d", blksize);
707 if (!nopt && ioctl (fd, NBD_SET_BLKSIZE, blksize) < 0)
708 err_exit ("ioctl set_blksize");
709
710 if (vopt)
711 msg ("set opts=%s", options);
712 if (!nopt && ioctl (fd, NBD_SET_OPTS, options) < 0)
713 err_exit ("ioctl set_opts");
714
715 if (vopt)
716 msg ("set addr=%s", addr);
717 if (!nopt && ioctl (fd, NBD_SET_ADDR, addr) < 0)
718 err_exit ("ioctl set_addr");
719
720 if (vopt)
721 msg ("set path=%s", path ? path : "null");
722 if (!nopt && ioctl (fd, NBD_SET_PATH, path) < 0)
723 err_exit ("ioctl set_path");
724
725 if (vopt)
726 msg ("start");
727 if (!nopt && ioctl (fd, NBD_START) < 0)
728 err_exit ("ioctl start");
729 if (!nopt)
730 close (fd);
731
732 free (options);
733 free (host);
734 free (addr);
735 }
736
737 /* Unwire 9nbd device and disconnect transport.
738 */
739 static void
740 _nbd_detach (Opt o, int argc, char **argv, int nopt, int vopt)
741 {
742 char *dev;
743 int fd = -1;
744
745 if (argc != 1)
746 usage();
747 dev = argv[0];
748 if (!nopt) {
749 fd = open (dev, O_RDWR);
750 if (fd < 0)
751 err_exit ("open %s", dev);
752 }
753 if (vopt)
754 msg ("stop");
755 if (!nopt && ioctl (fd, NBD_STOP) < 0)
756 err_exit ("ioctl stop");
757 if (fd != -1)
758 close (fd);
759 }
760
761 /*
762 * vi:tabstop=4 shiftwidth=4 expandtab
763 */