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, 2013, Intel Corporation.
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>
45 #include "mount_utils.h"
53 #include <lustre_ver.h>
56 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 10, 51, 0)
59 * We only #include a kernel level include file here because
60 * important MS_ flag #defines are missing from the SLES version
62 * In the future if SLES updates sys/mount.h to have a more complete
63 * set of flag #defines we should stop including linux/fs.h
65 #warn remove kernel include
71 #define MAX_RETRIES 99
74 char *progname = NULL;
78 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
79 fprintf(out, "\nThis mount helper should only be invoked via the "
80 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
81 fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n",
84 "\t<device>: the disk device, or for a client:\n"
85 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
86 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
87 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
88 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
89 "\t-o force|--force: force mount even if already in /etc/mtab\n"
90 "\t-h|--help: print this usage message\n"
91 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
92 "\t-v|--verbose: print verbose config settings\n"
93 "\t<mntopt>: one or more comma separated of:\n"
94 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
95 "\t\tabort_recov: abort server recovery handling\n"
96 "\t\tnosvc: only start MGC/MGS obds\n"
97 "\t\tnomgs: only start target obds, using existing MGS\n"
98 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
99 "inactive OSTs (e.g. lustre-OST0001)\n"
100 "\t\tretry=<num>: number of times mount is retried by client\n"
101 "\t\tmd_stripe_cache_size=<num>: set the raid stripe cache "
102 "size for the underlying raid if present\n"
104 exit((out != stdout) ? EINVAL : 0);
107 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
108 #define MAXNIDSTR 1024
109 static char *convert_hostnames(char *s1)
111 char *converted, *s2 = 0, *c;
113 int left = MAXNIDSTR;
116 converted = malloc(left);
117 if (converted == NULL) {
118 fprintf(stderr, "out of memory: needed %d bytes\n",
123 while ((left > 0) && (*s1 != '/')) {
124 s2 = strpbrk(s1, ",:");
129 nid = libcfs_str2nid(s1);
130 *s2 = sep; /* back to original string */
131 if (nid == LNET_NID_ANY)
133 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
134 left = converted + MAXNIDSTR - c;
137 snprintf(c, left, "%s", s1);
140 fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
145 /*****************************************************************************
147 * This part was cribbed from util-linux/mount/mount.c. There was no clear
148 * license information, but many other files in the package are identified as
149 * GNU GPL, so it's a pretty safe bet that was their intent.
151 ****************************************************************************/
153 const char *opt; /* option name */
154 int inv; /* true if flag value should be inverted */
155 int mask; /* flag mask value */
158 static const struct opt_map opt_map[] = {
159 /*"optname", inv,ms_mask */
160 /* These flags are parsed by mount, not lustre */
161 { "defaults", 0, 0 }, /* default options */
162 { "remount", 0, MS_REMOUNT}, /* remount with different options */
163 { "rw", 1, MS_RDONLY }, /* read-write */
164 { "ro", 0, MS_RDONLY }, /* read-only */
165 { "exec", 1, MS_NOEXEC }, /* permit execution of binaries */
166 { "noexec", 0, MS_NOEXEC }, /* don't execute binaries */
167 { "suid", 1, MS_NOSUID }, /* honor suid executables */
168 { "nosuid", 0, MS_NOSUID }, /* don't honor suid executables */
169 { "dev", 1, MS_NODEV }, /* interpret device files */
170 { "nodev", 0, MS_NODEV }, /* don't interpret devices */
171 { "sync", 0, MS_SYNCHRONOUS}, /* synchronous I/O */
172 { "async", 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
173 { "atime", 1, MS_NOATIME }, /* set file access time on read */
174 { "noatime", 0, MS_NOATIME }, /* do not set file access time on read */
176 { "diratime", 1, MS_NODIRATIME }, /* set file access time on read */
177 { "nodiratime",0,MS_NODIRATIME }, /* do not set file access time on read */
180 { "relatime", 0, MS_RELATIME }, /* set file access time on read */
181 { "norelatime",1,MS_RELATIME }, /* do not set file access time on read */
183 #ifdef MS_STRICTATIME
184 { "strictatime",0,MS_STRICTATIME }, /* update access time strictly */
186 { "auto", 0, 0 }, /* Can be mounted using -a */
187 { "noauto", 0, 0 }, /* Can only be mounted explicitly */
188 { "nousers", 1, 0 }, /* Forbid ordinary user to mount */
189 { "nouser", 1, 0 }, /* Forbid ordinary user to mount */
190 { "noowner", 1, 0 }, /* Device owner has no special privs */
191 { "_netdev", 0, 0 }, /* Device accessible only via network */
195 /****************************************************************************/
197 /* 1 = don't pass on to lustre
198 0 = pass on to lustre */
199 static int parse_one_option(const char *check, int *flagp)
201 const struct opt_map *opt;
203 for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
204 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
207 *flagp &= ~(opt->mask);
214 /* Assume any unknown options are valid and pass them on. The mount
215 will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
219 static void append_option(char *options, const char *one)
222 strcat(options, ",");
223 strcat(options, one);
226 /* Replace options with subset of Lustre-specific options, and
227 fill in mount flags */
228 int parse_options(struct mount_opts *mop, char *orig_options, int *flagp)
230 char *options, *opt, *nextopt, *arg, *val;
232 options = calloc(strlen(orig_options) + 1, 1);
234 nextopt = orig_options;
235 while ((opt = strsep(&nextopt, ","))) {
240 /* Handle retries in a slightly different
243 val = strchr(opt, '=');
244 /* please note that some ldiskfs mount options are also in the form
245 * of param=value. We should pay attention not to remove those
246 * mount options, see bug 22097. */
247 if (val && strncmp(arg, "md_stripe_cache_size", 20) == 0) {
248 mop->mo_md_stripe_cache_size = atoi(val + 1);
249 } else if (val && strncmp(arg, "retry", 5) == 0) {
250 mop->mo_retry = atoi(val + 1);
251 if (mop->mo_retry > MAX_RETRIES)
252 mop->mo_retry = MAX_RETRIES;
253 else if (mop->mo_retry < 0)
255 } else if (val && strncmp(arg, "mgssec", 6) == 0) {
256 append_option(options, opt);
257 } else if (strncmp(arg, "nosvc", 5) == 0) {
259 append_option(options, opt);
260 } else if (strcmp(opt, "force") == 0) {
261 //XXX special check for 'force' option
263 printf("force: %d\n", mop->mo_force);
264 } else if (parse_one_option(opt, flagp) == 0) {
265 /* pass this on as an option */
266 append_option(options, opt);
269 #ifdef MS_STRICTATIME
270 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(2, 10, 51, 0)
273 * In the future when upstream fixes land in all supported kernels
274 * we should stop forcing MS_STRICTATIME in lustre mounts.
275 * We override the kernel level default of MS_RELATIME for now
276 * due to a kernel vfs level bug in atime updates that fails
277 * to reset timestamps from the future.
279 #warn remove MS_STRICTATIME override
281 /* set strictatime to default if NOATIME or RELATIME
282 not given explicit */
283 if (!(*flagp & (MS_NOATIME | MS_RELATIME)))
284 *flagp |= MS_STRICTATIME;
286 strcpy(orig_options, options);
291 /* Add mgsnids from ldd params */
292 static int add_mgsnids(struct mount_opts *mop, char *options,
295 char *ptr = (char *)params;
298 while ((ptr = strstr(ptr, PARAM_MGSNODE)) != NULL) {
299 sep = strchr(ptr, ' ');
304 append_option(options, ptr);
305 mop->mo_have_mgsnid++;
317 static int clear_update_ondisk(char *source, struct lustre_disk_data *ldd)
319 char always_mountopts[512] = "";
320 char default_mountopts[512] = "";
321 struct mkfs_opts mkop;
324 memset(&mkop, 0, sizeof(mkop));
326 mkop.mo_ldd.ldd_flags &= ~LDD_F_UPDATE;
327 strcpy(mkop.mo_device, source);
329 ret = osd_prepare_lustre(&mkop,
330 default_mountopts, sizeof(default_mountopts),
331 always_mountopts, sizeof(always_mountopts));
334 fprintf(stderr, "Can't prepare device %s: %s\n",
335 source, strerror(ret));
339 /* Create the loopback file */
340 if (mkop.mo_flags & MO_IS_LOOP) {
341 ret = access(mkop.mo_device, F_OK);
345 fprintf(stderr, "Can't access device %s: %s\n",
346 source, strerror(ret));
350 ret = loop_setup(&mkop);
353 fprintf(stderr, "Loop device setup for %s failed: %s\n",
354 mkop.mo_device, strerror(ret));
358 ret = osd_write_ldd(&mkop);
361 fprintf(stderr, "failed to write local files: %s\n",
369 static int parse_ldd(char *source, struct mount_opts *mop, char *options)
371 struct lustre_disk_data *ldd = &mop->mo_ldd;
375 rc = osd_is_lustre(source, &ldd->ldd_mount_type);
377 fprintf(stderr, "%s: %s has not been formatted with mkfs.lustre"
378 " or the backend filesystem type is not supported by "
379 "this tool\n", progname, source);
383 rc = osd_read_ldd(source, ldd);
385 fprintf(stderr, "%s: %s failed to read permanent mount"
386 " data: %s\n", progname, source,
387 rc >= 0 ? strerror(rc) : "");
391 if (ldd->ldd_flags & LDD_F_NEED_INDEX) {
392 fprintf(stderr, "%s: %s has no index assigned "
393 "(probably formatted with old mkfs)\n",
398 if (ldd->ldd_flags & LDD_F_UPGRADE14) {
399 fprintf(stderr, "%s: we cannot upgrade %s from this (very old) "
400 "Lustre version\n", progname, source);
404 if (ldd->ldd_flags & LDD_F_UPDATE)
405 clear_update_ondisk(source, ldd);
407 /* Since we never rewrite ldd, ignore temp flags */
408 ldd->ldd_flags &= ~(LDD_F_VIRGIN | LDD_F_WRITECONF);
410 /* svname of the form lustre:OST1234 means never registered */
411 rc = strlen(ldd->ldd_svname);
412 if (ldd->ldd_svname[rc - 8] == ':') {
413 ldd->ldd_svname[rc - 8] = '-';
414 ldd->ldd_flags |= LDD_F_VIRGIN;
415 } else if (ldd->ldd_svname[rc - 8] == '=') {
416 ldd->ldd_svname[rc - 8] = '-';
417 ldd->ldd_flags |= LDD_F_WRITECONF;
420 /* backend osd type */
421 append_option(options, "osd=");
422 strcat(options, mt_type(ldd->ldd_mount_type));
424 append_option(options, ldd->ldd_mount_opts);
426 if (!mop->mo_have_mgsnid) {
427 /* Only use disk data if mount -o mgsnode=nid wasn't
429 if (ldd->ldd_flags & LDD_F_SV_TYPE_MGS) {
430 append_option(options, "mgs");
431 mop->mo_have_mgsnid++;
433 add_mgsnids(mop, options, ldd->ldd_params);
436 /* Better have an mgsnid by now */
437 if (!mop->mo_have_mgsnid) {
438 fprintf(stderr, "%s: missing option mgsnode=<nid>\n",
443 if (ldd->ldd_flags & LDD_F_VIRGIN)
444 append_option(options, "virgin");
445 if (ldd->ldd_flags & LDD_F_UPDATE)
446 append_option(options, "update");
447 if (ldd->ldd_flags & LDD_F_WRITECONF)
448 append_option(options, "writeconf");
449 if (ldd->ldd_flags & LDD_F_NO_PRIMNODE)
450 append_option(options, "noprimnode");
452 /* prefix every lustre parameter with param= so that in-kernel
453 * mount can recognize them properly and send to MGS at registration */
454 start = ldd->ldd_params;
455 while (start && *start != '\0') {
456 while (*start == ' ') start++;
460 start = strchr(cur, ' ');
465 append_option(options, "param=");
466 strcat(options, cur);
469 /* svname must be last option */
470 append_option(options, "svname=");
471 strcat(options, ldd->ldd_svname);
476 static void set_defaults(struct mount_opts *mop)
478 memset(mop, 0, sizeof(*mop));
479 mop->mo_usource = NULL;
480 mop->mo_source = NULL;
485 mop->mo_have_mgsnid = 0;
486 mop->mo_md_stripe_cache_size = 16384;
487 mop->mo_orig_options = "";
491 static int parse_opts(int argc, char *const argv[], struct mount_opts *mop)
493 static struct option long_opt[] = {
497 {"nomtab", 0, 0, 'n'},
498 {"options", 1, 0, 'o'},
499 {"verbose", 0, 0, 'v'},
502 char real_path[PATH_MAX] = {'\0'};
504 char path[256], name[256];
509 while ((opt = getopt_long(argc, argv, "fhno:v",
510 long_opt, NULL)) != EOF){
514 printf("force: %d\n", mop->mo_force);
518 printf("fake: %d\n", mop->mo_fake);
525 printf("nomtab: %d\n", mop->mo_nomtab);
528 mop->mo_orig_options = optarg;
534 fprintf(stderr, "%s: unknown option '%c'\n",
541 if (optind + 2 > argc) {
542 fprintf(stderr, "%s: too few arguments\n", progname);
546 mop->mo_usource = argv[optind];
547 if (!mop->mo_usource) {
552 * Try to get the real path to the device, in case it is a
553 * symbolic link for instance
555 if (realpath(mop->mo_usource, real_path) != NULL) {
556 ptr = strrchr(real_path, '/');
557 if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
558 snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
559 if ((f = fopen(path, "r"))) {
560 /* read "<name>\n" from sysfs */
561 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
563 snprintf(real_path, sizeof(real_path), "/dev/mapper/%s", name);
568 mop->mo_usource = strdup(real_path);
571 ptr = strstr(mop->mo_usource, ":/");
573 mop->mo_source = convert_hostnames(mop->mo_usource);
577 mop->mo_source = strdup(mop->mo_usource);
580 if (realpath(argv[optind + 1], mop->mo_target) == NULL) {
582 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
583 argv[optind + 1], strerror(errno));
590 int main(int argc, char *const argv[])
592 struct mount_opts mop;
596 progname = strrchr(argv[0], '/');
597 progname = progname ? progname + 1 : argv[0];
605 rc = parse_opts(argc, argv, &mop);
610 for (i = 0; i < argc; i++)
611 printf("arg[%d] = %s\n", i, argv[i]);
612 printf("source = %s (%s), target = %s\n", mop.mo_usource,
613 mop.mo_source, mop.mo_target);
614 printf("options = %s\n", mop.mo_orig_options);
617 options = malloc(MAXOPT);
618 if (options == NULL) {
619 fprintf(stderr, "can't allocate memory for options\n");
622 strcpy(options, mop.mo_orig_options);
623 rc = parse_options(&mop, options, &flags);
625 fprintf(stderr, "%s: can't parse options: %s\n",
631 rc = check_mtab_entry(mop.mo_usource, mop.mo_source,
632 mop.mo_target, "lustre");
633 if (rc && !(flags & MS_REMOUNT)) {
634 fprintf(stderr, "%s: according to %s %s is "
635 "already mounted on %s\n", progname, MOUNTED,
636 mop.mo_usource, mop.mo_target);
639 if (!rc && (flags & MS_REMOUNT)) {
640 fprintf(stderr, "%s: according to %s %s is "
641 "not already mounted on %s\n", progname, MOUNTED,
642 mop.mo_usource, mop.mo_target);
646 if (flags & MS_REMOUNT)
649 rc = access(mop.mo_target, F_OK);
652 fprintf(stderr, "%s: %s inaccessible: %s\n", progname,
653 mop.mo_target, strerror(errno));
657 if (!strstr(mop.mo_usource, ":/")) {
658 rc = parse_ldd(mop.mo_source, &mop, options);
663 /* In Linux 2.4, the target device doesn't get passed to any of our
664 functions. So we'll stick it on the end of the options. */
665 append_option(options, "device=");
666 strcat(options, mop.mo_source);
669 printf("mounting device %s at %s, flags=%#x options=%s\n",
670 mop.mo_source, mop.mo_target, flags, options);
672 if (!strstr(mop.mo_usource, ":/") &&
673 osd_tune_lustre(mop.mo_source, &mop)) {
675 fprintf(stderr, "%s: unable to set tunables for %s"
676 " (may cause reduced IO performance)\n",
677 argv[0], mop.mo_source);
681 /* flags and target get to lustre_get_sb, but not
682 lustre_fill_super. Lustre ignores the flags, but mount
684 for (i = 0, rc = -EAGAIN; i <= mop.mo_retry && rc != 0; i++) {
685 rc = mount(mop.mo_source, mop.mo_target, "lustre",
686 flags, (void *)options);
689 fprintf(stderr, "%s: mount %s at %s "
690 "failed: %s retries left: "
691 "%d\n", basename(progname),
692 mop.mo_usource, mop.mo_target,
698 sleep(1 << max((i/2), 5));
712 cli = strrchr(mop.mo_usource, ':');
713 if (cli && (strlen(cli) > 2))
718 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
719 mop.mo_usource, mop.mo_target, strerror(errno));
721 fprintf(stderr, "Is the backend filesystem mounted?\n"
722 "Check /etc/mtab and /proc/mounts\n");
724 fprintf(stderr, "Are the lustre modules loaded?\n"
725 "Check /etc/modprobe.conf and "
726 "/proc/filesystems\n");
727 if (errno == ENOTBLK)
728 fprintf(stderr, "Do you need -o loop?\n");
729 if (errno == ENOMEDIUM)
731 "This filesystem needs at least 1 OST\n");
732 if (errno == ENOENT) {
733 fprintf(stderr, "Is the MGS specification correct?\n");
734 fprintf(stderr, "Is the filesystem name correct?\n");
735 fprintf(stderr, "If upgrading, is the copied client log"
736 " valid? (see upgrade docs)\n");
738 if (errno == EALREADY)
739 fprintf(stderr, "The target service is already running."
740 " (%s)\n", mop.mo_usource);
742 fprintf(stderr, "The target service failed to start "
743 "(bad config log?) (%s). "
744 "See /var/log/messages.\n", mop.mo_usource);
746 fprintf(stderr, "Is the MGS running?\n");
747 if (errno == EADDRINUSE)
748 fprintf(stderr, "The target service's index is already "
749 "in use. (%s)\n", mop.mo_usource);
750 if (errno == EINVAL) {
751 fprintf(stderr, "This may have multiple causes.\n");
753 fprintf(stderr, "Is '%s' the correct filesystem"
755 fprintf(stderr, "Are the mount options correct?\n");
756 fprintf(stderr, "Check the syslog for more info.\n");
759 /* May as well try to clean up loop devs */
760 if (strncmp(mop.mo_usource, "/dev/loop", 9) == 0) {
763 sprintf(cmd, "/sbin/losetup -d %s", mop.mo_usource);
764 if ((ret = system(cmd)) < 0)
767 rc = WEXITSTATUS(ret);
770 } else if (!mop.mo_nomtab) {
771 rc = update_mtab_entry(mop.mo_usource, mop.mo_target, "lustre",
772 mop.mo_orig_options, 0,0,0);
774 /* change label from <fsname>:<index> to <fsname>-<index>
775 * to indicate the device has been registered.
776 * only if the label is supposed to be changed and
777 * target service is supposed to start */
778 if (mop.mo_ldd.ldd_flags & (LDD_F_VIRGIN | LDD_F_WRITECONF)) {
779 if (mop.mo_nosvc == 0 )
780 (void) osd_label_lustre(&mop);
785 /* mo_usource should be freed, but we can rely on the kernel */