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>
51 #include <sys/mount.h>
56 #include <lustre_ver.h>
60 #include "mount_utils.h"
62 #define MAX_HW_SECTORS_KB_PATH "queue/max_hw_sectors_kb"
63 #define MAX_SECTORS_KB_PATH "queue/max_sectors_kb"
64 #define STRIPE_CACHE_SIZE "md/stripe_cache_size"
65 #define MAX_RETRIES 99
72 int md_stripe_cache_size = 16384;
73 char *progname = NULL;
77 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
78 fprintf(out, "\nThis mount helper should only be invoked via the "
79 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
80 fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n",
83 "\t<device>: the disk device, or for a client:\n"
84 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
85 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
86 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
87 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
88 "\t-o force|--force: force mount even if already in /etc/mtab\n"
89 "\t-h|--help: print this usage message\n"
90 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
91 "\t-v|--verbose: print verbose config settings\n"
92 "\t<mntopt>: one or more comma separated of:\n"
93 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
94 "\t\tabort_recov: abort server recovery handling\n"
95 "\t\tnosvc: only start MGC/MGS obds\n"
96 "\t\tnomgs: only start target obds, using existing MGS\n"
97 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
98 "inactive OSTs (e.g. lustre-OST0001)\n"
99 "\t\tretry=<num>: number of times mount is retried by client\n"
100 "\t\tmd_stripe_cache_size=<num>: set the raid stripe cache "
101 "size for the underlying raid if present\n"
103 exit((out != stdout) ? EINVAL : 0);
106 static int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
111 fp = setmntent(MOUNTED, "r");
115 while ((mnt = getmntent(fp)) != NULL) {
116 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
117 strcmp(mnt->mnt_fsname, spec2) == 0) &&
118 strcmp(mnt->mnt_dir, mtpt) == 0 &&
119 strcmp(mnt->mnt_type, type) == 0) {
130 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
131 int flags, int freq, int pass)
137 mnt.mnt_fsname = spec;
140 mnt.mnt_opts = opts ? opts : "";
142 mnt.mnt_passno = pass;
144 fp = setmntent(MOUNTED, "a+");
146 fprintf(stderr, "%s: setmntent(%s): %s:",
147 progname, MOUNTED, strerror (errno));
150 if ((addmntent(fp, &mnt)) == 1) {
151 fprintf(stderr, "%s: addmntent: %s:",
152 progname, strerror (errno));
161 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
162 #define MAXNIDSTR 1024
163 static char *convert_hostnames(char *s1)
165 char *converted, *s2 = 0, *c;
167 int left = MAXNIDSTR;
170 converted = malloc(left);
171 if (converted == NULL) {
172 fprintf(stderr, "out of memory: needed %d bytes\n",
177 while ((left > 0) && (*s1 != '/')) {
178 s2 = strpbrk(s1, ",:");
183 nid = libcfs_str2nid(s1);
184 *s2 = sep; /* back to original string */
185 if (nid == LNET_NID_ANY)
187 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
188 left = converted + MAXNIDSTR - c;
191 snprintf(c, left, "%s", s1);
194 fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
199 /*****************************************************************************
201 * This part was cribbed from util-linux/mount/mount.c. There was no clear
202 * license information, but many other files in the package are identified as
203 * GNU GPL, so it's a pretty safe bet that was their intent.
205 ****************************************************************************/
207 const char *opt; /* option name */
208 int inv; /* true if flag value should be inverted */
209 int mask; /* flag mask value */
212 static const struct opt_map opt_map[] = {
213 /*"optname", inv,ms_mask */
214 /* These flags are parsed by mount, not lustre */
215 { "defaults", 0, 0 }, /* default options */
216 { "remount", 0, MS_REMOUNT}, /* remount with different options */
217 { "rw", 1, MS_RDONLY }, /* read-write */
218 { "ro", 0, MS_RDONLY }, /* read-only */
219 { "exec", 1, MS_NOEXEC }, /* permit execution of binaries */
220 { "noexec", 0, MS_NOEXEC }, /* don't execute binaries */
221 { "suid", 1, MS_NOSUID }, /* honor suid executables */
222 { "nosuid", 0, MS_NOSUID }, /* don't honor suid executables */
223 { "dev", 1, MS_NODEV }, /* interpret device files */
224 { "nodev", 0, MS_NODEV }, /* don't interpret devices */
225 { "sync", 0, MS_SYNCHRONOUS}, /* synchronous I/O */
226 { "async", 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
227 { "atime", 1, MS_NOATIME }, /* set file access time on read */
228 { "noatime", 0, MS_NOATIME }, /* do not set file access time on read */
230 { "diratime", 1, MS_NODIRATIME }, /* set file access time on read */
231 { "nodiratime",0,MS_NODIRATIME }, /* do not set file access time on read */
234 { "relatime", 0, MS_RELATIME }, /* set file access time on read */
235 { "norelatime",1,MS_RELATIME }, /* do not set file access time on read */
237 #ifdef MS_STRICTATIME
238 { "strictatime",0,MS_STRICTATIME }, /* update access time strictly */
240 { "auto", 0, 0 }, /* Can be mounted using -a */
241 { "noauto", 0, 0 }, /* Can only be mounted explicitly */
242 { "nousers", 1, 0 }, /* Forbid ordinary user to mount */
243 { "nouser", 1, 0 }, /* Forbid ordinary user to mount */
244 { "noowner", 1, 0 }, /* Device owner has no special privs */
245 { "_netdev", 0, 0 }, /* Device accessible only via network */
249 /****************************************************************************/
251 /* 1 = don't pass on to lustre
252 0 = pass on to lustre */
253 static int parse_one_option(const char *check, int *flagp)
255 const struct opt_map *opt;
257 for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
258 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
261 *flagp &= ~(opt->mask);
268 /* Assume any unknown options are valid and pass them on. The mount
269 will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
273 static void append_option(char *options, const char *one)
276 strcat(options, ",");
277 strcat(options, one);
280 /* Replace options with subset of Lustre-specific options, and
281 fill in mount flags */
282 int parse_options(char *orig_options, int *flagp)
284 char *options, *opt, *nextopt, *arg, *val;
286 options = calloc(strlen(orig_options) + 1, 1);
288 nextopt = orig_options;
289 while ((opt = strsep(&nextopt, ","))) {
294 /* Handle retries in a slightly different
297 val = strchr(opt, '=');
298 /* please note that some ldiskfs mount options are also in the form
299 * of param=value. We should pay attention not to remove those
300 * mount options, see bug 22097. */
301 if (val && strncmp(arg, "md_stripe_cache_size", 20) == 0) {
302 md_stripe_cache_size = atoi(val + 1);
303 } else if (val && strncmp(arg, "retry", 5) == 0) {
304 retry = atoi(val + 1);
305 if (retry > MAX_RETRIES)
309 } else if (val && strncmp(arg, "mgssec", 6) == 0) {
310 append_option(options, opt);
311 } else if (strcmp(opt, "force") == 0) {
312 //XXX special check for 'force' option
314 printf("force: %d\n", force);
315 } else if (parse_one_option(opt, flagp) == 0) {
316 /* pass this on as an option */
317 append_option(options, opt);
320 #ifdef MS_STRICTATIME
321 /* set strictatime to default if NOATIME or RELATIME
322 not given explicit */
323 if (!(*flagp & (MS_NOATIME | MS_RELATIME)))
324 *flagp |= MS_STRICTATIME;
326 strcpy(orig_options, options);
332 int read_file(char *path, char *buf, int size)
336 fd = fopen(path, "r");
340 /* should not ignore fgets(3)'s return value */
341 if (!fgets(buf, size, fd)) {
342 fprintf(stderr, "reading from %s: %s", path, strerror(errno));
350 int write_file(char *path, char *buf)
354 fd = fopen(path, "w");
363 /* This is to tune the kernel for good SCSI performance.
364 * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
365 * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
366 int set_blockdev_tunables(char *source, int fan_out)
368 glob_t glob_info = { 0 };
369 struct stat stat_buf;
370 char *chk_major, *chk_minor;
371 char *savept = NULL, *dev;
373 char buf[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
374 char real_path[PATH_MAX] = {'\0'};
381 ret_path = realpath(source, real_path);
382 if (ret_path == NULL) {
384 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
385 source, strerror(errno));
389 if (strncmp(real_path, "/dev/loop", 9) == 0)
392 if ((real_path[0] != '/') && (strpbrk(real_path, ",:") != NULL))
395 snprintf(path, sizeof(path), "/sys/block%s", real_path + 4);
396 if (access(path, X_OK) == 0)
399 /* The name of the device say 'X' specified in /dev/X may not
400 * match any entry under /sys/block/. In that case we need to
401 * match the major/minor number to find the entry under
402 * sys/block corresponding to /dev/X */
404 /* Don't chop tail digit on /dev/mapper/xxx, LU-478 */
405 if (strncmp(real_path, "/dev/mapper", 11) != 0) {
406 dev = real_path + strlen(real_path);
407 while (--dev > real_path && isdigit(*dev))
410 if (strncmp(real_path, "/dev/md_", 8) == 0)
414 rc = stat(real_path, &stat_buf);
417 fprintf(stderr, "warning: %s, device %s stat failed\n",
418 strerror(errno), real_path);
422 major = major(stat_buf.st_rdev);
423 minor = minor(stat_buf.st_rdev);
424 rc = glob("/sys/block/*", GLOB_NOSORT, NULL, &glob_info);
427 fprintf(stderr, "warning: failed to read entries under "
429 globfree(&glob_info);
433 for (i = 0; i < glob_info.gl_pathc; i++){
434 snprintf(path, sizeof(path), "%s/dev", glob_info.gl_pathv[i]);
436 rc = read_file(path, buf, sizeof(buf));
440 if (buf[strlen(buf) - 1] == '\n')
441 buf[strlen(buf) - 1] = '\0';
443 chk_major = strtok_r(buf, ":", &savept);
445 if (major == atoi(chk_major) &&minor == atoi(chk_minor))
449 if (i == glob_info.gl_pathc) {
451 fprintf(stderr,"warning: device %s does not match any "
452 "entry under /sys/block\n", real_path);
453 globfree(&glob_info);
457 /* Chop off "/dev" from path we found */
458 path[strlen(glob_info.gl_pathv[i])] = '\0';
459 globfree(&glob_info);
462 if (strncmp(real_path, "/dev/md", 7) == 0) {
463 snprintf(real_path, sizeof(real_path), "%s/%s", path,
466 rc = read_file(real_path, buf, sizeof(buf));
469 fprintf(stderr, "warning: opening %s: %s\n",
470 real_path, strerror(errno));
474 if (atoi(buf) >= md_stripe_cache_size)
477 if (strlen(buf) - 1 > 0) {
478 snprintf(buf, sizeof(buf), "%d", md_stripe_cache_size);
479 rc = write_file(real_path, buf);
481 fprintf(stderr, "warning: opening %s: %s\n",
482 real_path, strerror(errno));
484 /* Return since raid and disk tunables are different */
488 snprintf(real_path, sizeof(real_path), "%s/%s", path,
489 MAX_HW_SECTORS_KB_PATH);
490 rc = read_file(real_path, buf, sizeof(buf));
493 fprintf(stderr, "warning: opening %s: %s\n",
494 real_path, strerror(errno));
495 /* No MAX_HW_SECTORS_KB_PATH isn't necessary an
496 * error for some device. */
500 if (strlen(buf) - 1 > 0) {
501 snprintf(real_path, sizeof(real_path), "%s/%s", path,
502 MAX_SECTORS_KB_PATH);
503 rc = write_file(real_path, buf);
506 fprintf(stderr, "warning: writing to %s: %s\n",
507 real_path, strerror(errno));
508 /* No MAX_SECTORS_KB_PATH isn't necessary an
509 * error for some device. */
516 glob_info.gl_pathc = 0;
517 glob_info.gl_offs = 0;
518 /* if device is multipath device, tune its slave devices */
519 snprintf(real_path, sizeof(real_path), "%s/slaves/*", path);
520 rc = glob(real_path, GLOB_NOSORT, NULL, &glob_info);
522 for (i = 0; rc == 0 && i < glob_info.gl_pathc; i++){
523 slave = basename(glob_info.gl_pathv[i]);
524 snprintf(real_path, sizeof(real_path), "/dev/%s", slave);
525 rc = set_blockdev_tunables(real_path, 0);
528 if (rc == GLOB_NOMATCH) {
529 /* no slave device is not an error */
531 } else if (rc && verbose) {
533 fprintf(stderr, "warning: %s, failed to read"
534 " entries under %s/slaves\n",
535 strerror(errno), path);
537 fprintf(stderr, "unable to set tunables for"
538 " slave device %s (slave would be"
539 " unable to handle IO request from"
544 globfree(&glob_info);
550 int main(int argc, char *const argv[])
552 char default_options[] = "";
553 char *usource, *source, *ptr;
554 char target[PATH_MAX] = {'\0'};
555 char real_path[PATH_MAX] = {'\0'};
556 char path[256], name[256];
559 char *options, *optcopy, *orig_options = default_options;
560 int i, nargs = 3, opt, rc, flags, optlen;
561 static struct option long_opt[] = {
565 {"nomtab", 0, 0, 'n'},
566 {"options", 1, 0, 'o'},
567 {"verbose", 0, 0, 'v'},
571 progname = strrchr(argv[0], '/');
572 progname = progname ? progname + 1 : argv[0];
574 while ((opt = getopt_long(argc, argv, "fhno:v",
575 long_opt, NULL)) != EOF){
579 printf("force: %d\n", force);
584 printf("fake: %d\n", fake);
592 printf("nomtab: %d\n", nomtab);
596 orig_options = optarg;
604 fprintf(stderr, "%s: unknown option '%c'\n",
611 if (optind + 2 > argc) {
612 fprintf(stderr, "%s: too few arguments\n", progname);
616 usource = argv[optind];
622 * Try to get the real path to the device, in case it is a
623 * symbolic link for instance
625 if (realpath(usource, real_path) != NULL) {
628 ptr = strrchr(real_path, '/');
629 if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
630 snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
631 if ((f = fopen(path, "r"))) {
632 /* read "<name>\n" from sysfs */
633 if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) {
635 snprintf(real_path, sizeof(real_path), "/dev/mapper/%s", name);
642 source = convert_hostnames(usource);
647 if (realpath(argv[optind + 1], target) == NULL) {
649 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
650 argv[optind + 1], strerror(errno));
655 for (i = 0; i < argc; i++)
656 printf("arg[%d] = %s\n", i, argv[i]);
657 printf("source = %s (%s), target = %s\n", usource, source,
659 printf("options = %s\n", orig_options);
662 options = malloc(strlen(orig_options) + 1);
663 if (options == NULL) {
664 fprintf(stderr, "can't allocate memory for options\n");
667 strcpy(options, orig_options);
668 rc = parse_options(options, &flags);
670 fprintf(stderr, "%s: can't parse options: %s\n",
676 rc = check_mtab_entry(usource, source, target, "lustre");
677 if (rc && !(flags & MS_REMOUNT)) {
678 fprintf(stderr, "%s: according to %s %s is "
679 "already mounted on %s\n",
680 progname, MOUNTED, usource, target);
683 if (!rc && (flags & MS_REMOUNT)) {
684 fprintf(stderr, "%s: according to %s %s is "
685 "not already mounted on %s\n",
686 progname, MOUNTED, usource, target);
690 if (flags & MS_REMOUNT)
693 rc = access(target, F_OK);
696 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
701 /* In Linux 2.4, the target device doesn't get passed to any of our
702 functions. So we'll stick it on the end of the options. */
703 optlen = strlen(options) + strlen(",device=") + strlen(source) + 1;
704 optcopy = malloc(optlen);
705 if (optcopy == NULL) {
706 fprintf(stderr, "can't allocate memory to optcopy\n");
709 strcpy(optcopy, options);
711 strcat(optcopy, ",");
712 strcat(optcopy, "device=");
713 strcat(optcopy, source);
716 printf("mounting device %s at %s, flags=%#x options=%s\n",
717 source, target, flags, optcopy);
719 if (!strstr(usource, ":/") && set_blockdev_tunables(source, 1)) {
721 fprintf(stderr, "%s: unable to set tunables for %s"
722 " (may cause reduced IO performance)\n",
727 /* flags and target get to lustre_get_sb, but not
728 lustre_fill_super. Lustre ignores the flags, but mount
730 for (i = 0, rc = -EAGAIN; i <= retry && rc != 0; i++) {
731 rc = mount(source, target, "lustre", flags,
735 fprintf(stderr, "%s: mount %s at %s "
736 "failed: %s retries left: "
737 "%d\n", basename(progname),
739 strerror(errno), retry-i);
743 sleep(1 << max((i/2), 5));
757 cli = strrchr(usource, ':');
758 if (cli && (strlen(cli) > 2))
763 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
764 usource, target, strerror(errno));
766 fprintf(stderr, "Are the lustre modules loaded?\n"
767 "Check /etc/modprobe.conf and "
768 "/proc/filesystems\n");
769 if (errno == ENOTBLK)
770 fprintf(stderr, "Do you need -o loop?\n");
771 if (errno == ENOMEDIUM)
773 "This filesystem needs at least 1 OST\n");
774 if (errno == ENOENT) {
775 fprintf(stderr, "Is the MGS specification correct?\n");
776 fprintf(stderr, "Is the filesystem name correct?\n");
777 fprintf(stderr, "If upgrading, is the copied client log"
778 " valid? (see upgrade docs)\n");
780 if (errno == EALREADY)
781 fprintf(stderr, "The target service is already running."
784 fprintf(stderr, "The target service failed to start "
785 "(bad config log?) (%s). "
786 "See /var/log/messages.\n", usource);
788 fprintf(stderr, "Is the MGS running?\n");
789 if (errno == EADDRINUSE)
790 fprintf(stderr, "The target service's index is already "
791 "in use. (%s)\n", usource);
792 if (errno == EINVAL) {
793 fprintf(stderr, "This may have multiple causes.\n");
795 fprintf(stderr, "Is '%s' the correct filesystem"
797 fprintf(stderr, "Are the mount options correct?\n");
798 fprintf(stderr, "Check the syslog for more info.\n");
801 /* May as well try to clean up loop devs */
802 if (strncmp(usource, "/dev/loop", 9) == 0) {
805 sprintf(cmd, "/sbin/losetup -d %s", usource);
806 if ((ret = system(cmd)) < 0)
809 rc = WEXITSTATUS(ret);
812 } else if (!nomtab) {
813 rc = update_mtab_entry(usource, target, "lustre", orig_options,