4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/mount_lustre.c
38 * Author: Robert Read <rread@clusterfs.com>
39 * Author: Nathan Rutman <nathan@clusterfs.com>
52 #include <lustre_ver.h>
55 #include "mount_utils.h"
58 #define MAX_RETRIES 99
61 char *progname = NULL;
65 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
66 fprintf(out, "\nThis mount helper should only be invoked via the "
67 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
68 fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n",
71 "\t<device>: the disk device, or for a client:\n"
72 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
73 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
74 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
75 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
76 "\t-o force|--force: force mount even if already in /etc/mtab\n"
77 "\t-h|--help: print this usage message\n"
78 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
79 "\t-v|--verbose: print verbose config settings\n"
80 "\t<mntopt>: one or more comma separated of:\n"
81 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
82 "\t\tabort_recov: abort server recovery handling\n"
83 "\t\tnosvc: only start MGC/MGS obds\n"
84 "\t\tnomgs: only start target obds, using existing MGS\n"
85 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
86 "inactive OSTs (e.g. lustre-OST0001)\n"
87 "\t\tretry=<num>: number of times mount is retried by client\n"
88 "\t\tmd_stripe_cache_size=<num>: set the raid stripe cache "
89 "size for the underlying raid if present\n"
91 exit((out != stdout) ? EINVAL : 0);
94 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
95 #define MAXNIDSTR 1024
96 static char *convert_hostnames(char *s1)
98 char *converted, *s2 = 0, *c;
100 int left = MAXNIDSTR;
103 converted = malloc(left);
104 if (converted == NULL) {
105 fprintf(stderr, "out of memory: needed %d bytes\n",
110 while ((left > 0) && (*s1 != '/')) {
111 s2 = strpbrk(s1, ",:");
116 nid = libcfs_str2nid(s1);
117 *s2 = sep; /* back to original string */
118 if (nid == LNET_NID_ANY)
120 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
121 left = converted + MAXNIDSTR - c;
124 snprintf(c, left, "%s", s1);
127 fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
132 /*****************************************************************************
134 * This part was cribbed from util-linux/mount/mount.c. There was no clear
135 * license information, but many other files in the package are identified as
136 * GNU GPL, so it's a pretty safe bet that was their intent.
138 ****************************************************************************/
140 const char *opt; /* option name */
141 int inv; /* true if flag value should be inverted */
142 int mask; /* flag mask value */
145 static const struct opt_map opt_map[] = {
146 /*"optname", inv,ms_mask */
147 /* These flags are parsed by mount, not lustre */
148 { "defaults", 0, 0 }, /* default options */
149 { "remount", 0, MS_REMOUNT}, /* remount with different options */
150 { "rw", 1, MS_RDONLY }, /* read-write */
151 { "ro", 0, MS_RDONLY }, /* read-only */
152 { "exec", 1, MS_NOEXEC }, /* permit execution of binaries */
153 { "noexec", 0, MS_NOEXEC }, /* don't execute binaries */
154 { "suid", 1, MS_NOSUID }, /* honor suid executables */
155 { "nosuid", 0, MS_NOSUID }, /* don't honor suid executables */
156 { "dev", 1, MS_NODEV }, /* interpret device files */
157 { "nodev", 0, MS_NODEV }, /* don't interpret devices */
158 { "sync", 0, MS_SYNCHRONOUS}, /* synchronous I/O */
159 { "async", 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
160 { "atime", 1, MS_NOATIME }, /* set file access time on read */
161 { "noatime", 0, MS_NOATIME }, /* do not set file access time on read */
163 { "diratime", 1, MS_NODIRATIME }, /* set file access time on read */
164 { "nodiratime",0,MS_NODIRATIME }, /* do not set file access time on read */
167 { "relatime", 0, MS_RELATIME }, /* set file access time on read */
168 { "norelatime",1,MS_RELATIME }, /* do not set file access time on read */
170 #ifdef MS_STRICTATIME
171 { "strictatime",0,MS_STRICTATIME }, /* update access time strictly */
173 { "auto", 0, 0 }, /* Can be mounted using -a */
174 { "noauto", 0, 0 }, /* Can only be mounted explicitly */
175 { "nousers", 1, 0 }, /* Forbid ordinary user to mount */
176 { "nouser", 1, 0 }, /* Forbid ordinary user to mount */
177 { "noowner", 1, 0 }, /* Device owner has no special privs */
178 { "_netdev", 0, 0 }, /* Device accessible only via network */
182 /****************************************************************************/
184 /* 1 = don't pass on to lustre
185 0 = pass on to lustre */
186 static int parse_one_option(const char *check, int *flagp)
188 const struct opt_map *opt;
190 for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
191 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
194 *flagp &= ~(opt->mask);
201 /* Assume any unknown options are valid and pass them on. The mount
202 will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
206 static void append_option(char *options, const char *one)
209 strcat(options, ",");
210 strcat(options, one);
213 /* Replace options with subset of Lustre-specific options, and
214 fill in mount flags */
215 int parse_options(struct mount_opts *mop, char *orig_options, int *flagp)
217 char *options, *opt, *nextopt, *arg, *val;
219 options = calloc(strlen(orig_options) + 1, 1);
221 nextopt = orig_options;
222 while ((opt = strsep(&nextopt, ","))) {
227 /* Handle retries in a slightly different
230 val = strchr(opt, '=');
231 /* please note that some ldiskfs mount options are also in the form
232 * of param=value. We should pay attention not to remove those
233 * mount options, see bug 22097. */
234 if (val && strncmp(arg, "md_stripe_cache_size", 20) == 0) {
235 mop->mo_md_stripe_cache_size = atoi(val + 1);
236 } else if (val && strncmp(arg, "retry", 5) == 0) {
237 mop->mo_retry = atoi(val + 1);
238 if (mop->mo_retry > MAX_RETRIES)
239 mop->mo_retry = MAX_RETRIES;
240 else if (mop->mo_retry < 0)
242 } else if (val && strncmp(arg, "mgssec", 6) == 0) {
243 append_option(options, opt);
244 } else if (strcmp(opt, "force") == 0) {
245 //XXX special check for 'force' option
247 printf("force: %d\n", mop->mo_force);
248 } else if (parse_one_option(opt, flagp) == 0) {
249 /* pass this on as an option */
250 append_option(options, opt);
253 #ifdef MS_STRICTATIME
254 /* set strictatime to default if NOATIME or RELATIME
255 not given explicit */
256 if (!(*flagp & (MS_NOATIME | MS_RELATIME)))
257 *flagp |= MS_STRICTATIME;
259 strcpy(orig_options, options);
264 /* Add mgsnids from ldd params */
265 static int add_mgsnids(struct mount_opts *mop, char *options,
268 char *ptr = (char *)params;
271 while ((ptr = strstr(ptr, PARAM_MGSNODE)) != NULL) {
272 sep = strchr(ptr, ' ');
277 append_option(options, ptr);
278 mop->mo_have_mgsnid++;
290 static int parse_ldd(char *source, struct mount_opts *mop, char *options)
292 struct lustre_disk_data *ldd = &mop->mo_ldd;
295 rc = osd_is_lustre(source, &ldd->ldd_mount_type);
297 fprintf(stderr, "%s: %s has not been formatted with mkfs.lustre"
298 " or the backend filesystem type is not supported by "
299 "this tool\n", progname, source);
303 /* for new backends (i.e. ZFS) we will be parsing mount data
304 * in the userspace and pass it in the form of mount options.
305 * to adopt this schema smoothly we're still doing old way
306 * (parsing mount data within the kernel) for ldiskfs */
307 if (ldd->ldd_mount_type == LDD_MT_EXT3 ||
308 ldd->ldd_mount_type == LDD_MT_LDISKFS ||
309 ldd->ldd_mount_type == LDD_MT_LDISKFS2)
312 rc = osd_read_ldd(source, ldd);
314 fprintf(stderr, "%s: %s failed to read permanent mount"
315 " data: %s\n", progname, source, strerror(rc));
319 if (ldd->ldd_flags & LDD_F_NEED_INDEX) {
320 fprintf(stderr, "%s: %s has no index assigned "
321 "(probably formatted with old mkfs)\n",
326 if (ldd->ldd_flags & LDD_F_UPGRADE14) {
327 fprintf(stderr, "%s: we cannot upgrade %s from this (very old) "
328 "Lustre version\n", progname, source);
332 /* Since we never rewrite ldd, ignore temp flags */
333 ldd->ldd_flags &= ~(LDD_F_VIRGIN | LDD_F_UPDATE);
335 /* svname of the form lustre:OST1234 means never registered */
336 rc = strlen(ldd->ldd_svname);
337 if (ldd->ldd_svname[rc - 8] == ':') {
338 ldd->ldd_svname[rc - 8] = '-';
339 ldd->ldd_flags |= LDD_F_VIRGIN;
342 /* backend osd type */
343 append_option(options, "osd=");
344 strcat(options, mt_type(ldd->ldd_mount_type));
346 append_option(options, ldd->ldd_mount_opts);
348 if (!mop->mo_have_mgsnid) {
349 /* Only use disk data if mount -o mgsnode=nid wasn't
351 if (ldd->ldd_flags & LDD_F_SV_TYPE_MGS) {
352 append_option(options, "mgs");
353 mop->mo_have_mgsnid++;
355 add_mgsnids(mop, options, ldd->ldd_params);
358 /* Better have an mgsnid by now */
359 if (!mop->mo_have_mgsnid) {
360 fprintf(stderr, "%s: missing option mgsnode=<nid>\n",
365 if (ldd->ldd_flags & (LDD_F_VIRGIN | LDD_F_WRITECONF))
366 append_option(options, "writeconf");
367 if (ldd->ldd_flags & LDD_F_IAM_DIR)
368 append_option(options, "iam");
369 if (ldd->ldd_flags & LDD_F_NO_PRIMNODE)
370 append_option(options, "noprimnode");
372 /* svname must be last option */
373 append_option(options, "svname=");
374 strcat(options, ldd->ldd_svname);
379 static void set_defaults(struct mount_opts *mop)
381 memset(mop, 0, sizeof(*mop));
382 mop->mo_usource = NULL;
383 mop->mo_source = NULL;
388 mop->mo_have_mgsnid = 0;
389 mop->mo_md_stripe_cache_size = 16384;
390 mop->mo_orig_options = "";
393 static int parse_opts(int argc, char *const argv[], struct mount_opts *mop)
395 static struct option long_opt[] = {
399 {"nomtab", 0, 0, 'n'},
400 {"options", 1, 0, 'o'},
401 {"verbose", 0, 0, 'v'},
404 char real_path[PATH_MAX] = {'\0'};
406 char path[256], name[256];
411 while ((opt = getopt_long(argc, argv, "fhno:v",
412 long_opt, NULL)) != EOF){
416 printf("force: %d\n", mop->mo_force);
420 printf("fake: %d\n", mop->mo_fake);
427 printf("nomtab: %d\n", mop->mo_nomtab);
430 mop->mo_orig_options = optarg;
436 fprintf(stderr, "%s: unknown option '%c'\n",
443 if (optind + 2 > argc) {
444 fprintf(stderr, "%s: too few arguments\n", progname);
448 mop->mo_usource = argv[optind];
449 if (!mop->mo_usource) {
454 * Try to get the real path to the device, in case it is a
455 * symbolic link for instance
457 if (realpath(mop->mo_usource, real_path) != NULL) {
458 mop->mo_usource = strdup(real_path);
460 ptr = strrchr(real_path, '/');
461 if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
462 snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
463 if ((f = fopen(path, "r"))) {
464 /* read "<name>\n" from sysfs */
465 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
467 snprintf(real_path, sizeof(real_path), "/dev/mapper/%s", name);
474 ptr = strstr(mop->mo_usource, ":/");
476 mop->mo_source = convert_hostnames(mop->mo_usource);
480 mop->mo_source = strdup(mop->mo_usource);
483 if (realpath(argv[optind + 1], mop->mo_target) == NULL) {
485 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
486 argv[optind + 1], strerror(errno));
493 int main(int argc, char *const argv[])
495 struct mount_opts mop;
499 progname = strrchr(argv[0], '/');
500 progname = progname ? progname + 1 : argv[0];
508 rc = parse_opts(argc, argv, &mop);
513 for (i = 0; i < argc; i++)
514 printf("arg[%d] = %s\n", i, argv[i]);
515 printf("source = %s (%s), target = %s\n", mop.mo_usource,
516 mop.mo_source, mop.mo_target);
517 printf("options = %s\n", mop.mo_orig_options);
520 options = malloc(MAXOPT);
521 if (options == NULL) {
522 fprintf(stderr, "can't allocate memory for options\n");
525 strcpy(options, mop.mo_orig_options);
526 rc = parse_options(&mop, options, &flags);
528 fprintf(stderr, "%s: can't parse options: %s\n",
534 rc = check_mtab_entry(mop.mo_usource, mop.mo_source,
535 mop.mo_target, "lustre");
536 if (rc && !(flags & MS_REMOUNT)) {
537 fprintf(stderr, "%s: according to %s %s is "
538 "already mounted on %s\n", progname, MOUNTED,
539 mop.mo_usource, mop.mo_target);
542 if (!rc && (flags & MS_REMOUNT)) {
543 fprintf(stderr, "%s: according to %s %s is "
544 "not already mounted on %s\n", progname, MOUNTED,
545 mop.mo_usource, mop.mo_target);
549 if (flags & MS_REMOUNT)
552 rc = access(mop.mo_target, F_OK);
555 fprintf(stderr, "%s: %s inaccessible: %s\n", progname,
556 mop.mo_target, strerror(errno));
560 if (!strstr(mop.mo_usource, ":/")) {
561 rc = parse_ldd(mop.mo_source, &mop, options);
566 /* In Linux 2.4, the target device doesn't get passed to any of our
567 functions. So we'll stick it on the end of the options. */
568 append_option(options, "device=");
569 strcat(options, mop.mo_source);
572 printf("mounting device %s at %s, flags=%#x options=%s\n",
573 mop.mo_source, mop.mo_target, flags, options);
575 if (!strstr(mop.mo_usource, ":/") &&
576 osd_tune_lustre(mop.mo_source, &mop)) {
578 fprintf(stderr, "%s: unable to set tunables for %s"
579 " (may cause reduced IO performance)\n",
580 argv[0], mop.mo_source);
584 /* flags and target get to lustre_get_sb, but not
585 lustre_fill_super. Lustre ignores the flags, but mount
587 for (i = 0, rc = -EAGAIN; i <= mop.mo_retry && rc != 0; i++) {
588 rc = mount(mop.mo_source, mop.mo_target, "lustre",
589 flags, (void *)options);
592 fprintf(stderr, "%s: mount %s at %s "
593 "failed: %s retries left: "
594 "%d\n", basename(progname),
595 mop.mo_usource, mop.mo_target,
601 sleep(1 << max((i/2), 5));
615 cli = strrchr(mop.mo_usource, ':');
616 if (cli && (strlen(cli) > 2))
621 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
622 mop.mo_usource, mop.mo_target, strerror(errno));
624 fprintf(stderr, "Are the lustre modules loaded?\n"
625 "Check /etc/modprobe.conf and "
626 "/proc/filesystems\n");
627 if (errno == ENOTBLK)
628 fprintf(stderr, "Do you need -o loop?\n");
629 if (errno == ENOMEDIUM)
631 "This filesystem needs at least 1 OST\n");
632 if (errno == ENOENT) {
633 fprintf(stderr, "Is the MGS specification correct?\n");
634 fprintf(stderr, "Is the filesystem name correct?\n");
635 fprintf(stderr, "If upgrading, is the copied client log"
636 " valid? (see upgrade docs)\n");
638 if (errno == EALREADY)
639 fprintf(stderr, "The target service is already running."
640 " (%s)\n", mop.mo_usource);
642 fprintf(stderr, "The target service failed to start "
643 "(bad config log?) (%s). "
644 "See /var/log/messages.\n", mop.mo_usource);
646 fprintf(stderr, "Is the MGS running?\n");
647 if (errno == EADDRINUSE)
648 fprintf(stderr, "The target service's index is already "
649 "in use. (%s)\n", mop.mo_usource);
650 if (errno == EINVAL) {
651 fprintf(stderr, "This may have multiple causes.\n");
653 fprintf(stderr, "Is '%s' the correct filesystem"
655 fprintf(stderr, "Are the mount options correct?\n");
656 fprintf(stderr, "Check the syslog for more info.\n");
659 /* May as well try to clean up loop devs */
660 if (strncmp(mop.mo_usource, "/dev/loop", 9) == 0) {
663 sprintf(cmd, "/sbin/losetup -d %s", mop.mo_usource);
664 if ((ret = system(cmd)) < 0)
667 rc = WEXITSTATUS(ret);
670 } else if (!mop.mo_nomtab) {
671 rc = update_mtab_entry(mop.mo_usource, mop.mo_target, "lustre",
672 mop.mo_orig_options, 0,0,0);
676 /* mo_usource should be freed, but we can rely on the kernel */