1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
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 <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\tnosvc: only start MGC/MGS obds\n"
95 "\t\tnomgs: only start target obds, using existing MGS\n"
96 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
97 "inactive OSTs (e.g. lustre-OST0001)\n"
98 "\t\tretry=<num>: number of times mount is retried by client\n"
99 "\t\tmd_stripe_cache_size=<num>: set the raid stripe cache "
100 "size for the underlying raid if present\n"
102 exit((out != stdout) ? EINVAL : 0);
105 static int check_mtab_entry(char *spec, char *mtpt, char *type)
110 fp = setmntent(MOUNTED, "r");
114 while ((mnt = getmntent(fp)) != NULL) {
115 if (strcmp(mnt->mnt_fsname, spec) == 0 &&
116 strcmp(mnt->mnt_dir, mtpt) == 0 &&
117 strcmp(mnt->mnt_type, type) == 0) {
128 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
129 int flags, int freq, int pass)
135 mnt.mnt_fsname = spec;
138 mnt.mnt_opts = opts ? opts : "";
140 mnt.mnt_passno = pass;
142 fp = setmntent(MOUNTED, "a+");
144 fprintf(stderr, "%s: setmntent(%s): %s:",
145 progname, MOUNTED, strerror (errno));
148 if ((addmntent(fp, &mnt)) == 1) {
149 fprintf(stderr, "%s: addmntent: %s:",
150 progname, strerror (errno));
159 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
160 #define MAXNIDSTR 1024
161 static char *convert_hostnames(char *s1)
163 char *converted, *s2 = 0, *c;
165 int left = MAXNIDSTR;
168 converted = malloc(left);
169 if (converted == NULL) {
170 fprintf(stderr, "out of memory: needed %d bytes\n",
175 while ((left > 0) && (*s1 != '/')) {
176 s2 = strpbrk(s1, ",:");
181 nid = libcfs_str2nid(s1);
182 *s2 = sep; /* back to original string */
183 if (nid == LNET_NID_ANY)
185 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
186 left = converted + MAXNIDSTR - c;
189 snprintf(c, left, "%s", s1);
192 fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
197 /*****************************************************************************
199 * This part was cribbed from util-linux/mount/mount.c. There was no clear
200 * license information, but many other files in the package are identified as
201 * GNU GPL, so it's a pretty safe bet that was their intent.
203 ****************************************************************************/
205 const char *opt; /* option name */
206 int inv; /* true if flag value should be inverted */
207 int mask; /* flag mask value */
210 static const struct opt_map opt_map[] = {
211 /*"optname", inv,ms_mask */
212 /* These flags are parsed by mount, not lustre */
213 { "defaults", 0, 0 }, /* default options */
214 { "remount", 0, MS_REMOUNT}, /* remount with different options */
215 { "rw", 1, MS_RDONLY }, /* read-write */
216 { "ro", 0, MS_RDONLY }, /* read-only */
217 { "exec", 1, MS_NOEXEC }, /* permit execution of binaries */
218 { "noexec", 0, MS_NOEXEC }, /* don't execute binaries */
219 { "suid", 1, MS_NOSUID }, /* honor suid executables */
220 { "nosuid", 0, MS_NOSUID }, /* don't honor suid executables */
221 { "dev", 1, MS_NODEV }, /* interpret device files */
222 { "nodev", 0, MS_NODEV }, /* don't interpret devices */
223 { "sync", 0, MS_SYNCHRONOUS}, /* synchronous I/O */
224 { "async", 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
225 { "atime", 1, MS_NOATIME }, /* set file access time on read */
226 { "noatime", 0, MS_NOATIME }, /* do not set file access time on read */
228 { "diratime", 1, MS_NODIRATIME }, /* set file access time on read */
229 { "nodiratime",0,MS_NODIRATIME }, /* do not set file access time on read */
232 { "relatime", 0, MS_RELATIME }, /* set file access time on read */
233 { "norelatime",1,MS_RELATIME }, /* do not set file access time on read */
235 { "auto", 0, 0 }, /* Can be mounted using -a */
236 { "noauto", 0, 0 }, /* Can only be mounted explicitly */
237 { "nousers", 1, 0 }, /* Forbid ordinary user to mount */
238 { "nouser", 1, 0 }, /* Forbid ordinary user to mount */
239 { "noowner", 1, 0 }, /* Device owner has no special privs */
240 { "_netdev", 0, 0 }, /* Device accessible only via network */
243 /****************************************************************************/
245 /* 1 = don't pass on to lustre
246 0 = pass on to lustre */
247 static int parse_one_option(const char *check, int *flagp)
249 const struct opt_map *opt;
251 for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
252 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
255 *flagp &= ~(opt->mask);
262 /* Assume any unknown options are valid and pass them on. The mount
263 will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
267 /* Replace options with subset of Lustre-specific options, and
268 fill in mount flags */
269 int parse_options(char *orig_options, int *flagp)
271 char *options, *opt, *nextopt, *arg, *val;
273 options = calloc(strlen(orig_options) + 1, 1);
275 nextopt = orig_options;
276 while ((opt = strsep(&nextopt, ","))) {
281 /* Handle retries in a slightly different
284 val = strchr(opt, '=');
285 /* please note that some ldiskfs mount options are also in the form
286 * of param=value. We should pay attention not to remove those
287 * mount options, see bug 22097. */
288 if (val && strncmp(arg, "md_stripe_cache_size", 20) == 0) {
289 md_stripe_cache_size = atoi(val + 1);
290 } else if (val && strncmp(arg, "retry", 5) == 0) {
291 retry = atoi(val + 1);
292 if (retry > MAX_RETRIES)
296 } else if (strncmp(opt, "force", 5) == 0) {
297 //XXX special check for 'force' option
299 printf("force: %d\n", force);
300 } else if (parse_one_option(opt, flagp) == 0) {
301 /* pass this on as an option */
303 strcat(options, ",");
304 strcat(options, opt);
307 strcpy(orig_options, options);
313 int read_file(char *path, char *buf, int size)
317 fd = fopen(path, "r");
321 /* should not ignore fgets(3)'s return value */
322 if (!fgets(buf, size, fd)) {
323 fprintf(stderr, "reading from %s: %s", path, strerror(errno));
331 int write_file(char *path, char *buf)
335 fd = fopen(path, "w");
344 /* This is to tune the kernel for good SCSI performance.
345 * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
346 * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
347 int set_blockdev_tunables(char *source)
350 struct stat stat_buf;
351 char *chk_major, *chk_minor;
352 char *savept = NULL, *dev;
354 char buf[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
355 char real_path[PATH_MAX] = {'\0'};
362 ret_path = realpath(source, real_path);
363 if (ret_path == NULL) {
365 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
366 source, strerror(errno));
370 if (strncmp(real_path, "/dev/loop", 9) == 0)
373 if ((real_path[0] != '/') && (strpbrk(real_path, ",:") != NULL))
376 snprintf(path, sizeof(path), "/sys/block%s", real_path + 4);
377 if (access(path, X_OK) == 0)
380 /* The name of the device say 'X' specified in /dev/X may not
381 * match any entry under /sys/block/. In that case we need to
382 * match the major/minor number to find the entry under
383 * sys/block corresponding to /dev/X */
384 dev = real_path + strlen(real_path);
385 while (--dev > real_path && isdigit(*dev))
388 if (strncmp(real_path, "/dev/md_", 8) == 0)
391 rc = stat(real_path, &stat_buf);
394 fprintf(stderr, "warning: %s, device %s stat failed\n",
395 strerror(errno), real_path);
399 major = major(stat_buf.st_rdev);
400 minor = minor(stat_buf.st_rdev);
401 rc = glob("/sys/block/*", GLOB_NOSORT, NULL, &glob_info);
404 fprintf(stderr, "warning: failed to read entries under "
409 for (i = 0; i < glob_info.gl_pathc; i++){
410 snprintf(path, sizeof(path), "%s/dev", glob_info.gl_pathv[i]);
412 rc = read_file(path, buf, sizeof(buf));
416 if (buf[strlen(buf) - 1] == '\n')
417 buf[strlen(buf) - 1] = '\0';
419 chk_major = strtok_r(buf, ":", &savept);
421 if (major == atoi(chk_major) &&minor == atoi(chk_minor))
425 if (i == glob_info.gl_pathc) {
427 fprintf(stderr,"warning: device %s does not match any "
428 "entry under /sys/block\n", real_path);
429 globfree(&glob_info);
433 /* Chop off "/dev" from path we found */
434 path[strlen(glob_info.gl_pathv[i])] = '\0';
435 globfree(&glob_info);
438 if (strncmp(real_path, "/dev/md", 7) == 0) {
439 snprintf(real_path, sizeof(real_path), "%s/%s", path,
442 rc = read_file(real_path, buf, sizeof(buf));
445 fprintf(stderr, "warning: opening %s: %s\n",
446 real_path, strerror(errno));
450 if (atoi(buf) >= md_stripe_cache_size)
453 if (strlen(buf) - 1 > 0) {
454 snprintf(buf, sizeof(buf), "%d", md_stripe_cache_size);
455 rc = write_file(real_path, buf);
457 fprintf(stderr, "warning: opening %s: %s\n",
458 real_path, strerror(errno));
460 /* Return since raid and disk tunables are different */
464 snprintf(real_path, sizeof(real_path), "%s/%s", path,
465 MAX_HW_SECTORS_KB_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 (strlen(buf) - 1 > 0) {
475 snprintf(real_path, sizeof(real_path), "%s/%s", path,
476 MAX_SECTORS_KB_PATH);
477 rc = write_file(real_path, buf);
479 fprintf(stderr, "warning: writing to %s: %s\n",
480 real_path, strerror(errno));
485 int main(int argc, char *const argv[])
487 char default_options[] = "";
488 char *usource, *source;
489 char target[PATH_MAX] = {'\0'};
490 char *options, *optcopy, *orig_options = default_options;
491 int i, nargs = 3, opt, rc, flags, optlen;
492 static struct option long_opt[] = {
496 {"nomtab", 0, 0, 'n'},
497 {"options", 1, 0, 'o'},
498 {"verbose", 0, 0, 'v'},
502 progname = strrchr(argv[0], '/');
503 progname = progname ? progname + 1 : argv[0];
505 while ((opt = getopt_long(argc, argv, "fhno:v",
506 long_opt, NULL)) != EOF){
510 printf("force: %d\n", force);
515 printf("fake: %d\n", fake);
523 printf("nomtab: %d\n", nomtab);
527 orig_options = optarg;
535 fprintf(stderr, "%s: unknown option '%c'\n",
542 if (optind + 2 > argc) {
543 fprintf(stderr, "%s: too few arguments\n", progname);
547 usource = argv[optind];
552 source = convert_hostnames(usource);
557 if (realpath(argv[optind + 1], target) == NULL) {
559 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
560 argv[optind + 1], strerror(errno));
565 for (i = 0; i < argc; i++)
566 printf("arg[%d] = %s\n", i, argv[i]);
567 printf("source = %s (%s), target = %s\n", usource, source, target);
568 printf("options = %s\n", orig_options);
571 options = malloc(strlen(orig_options) + 1);
572 if (options == NULL) {
573 fprintf(stderr, "can't allocate memory for options\n");
576 strcpy(options, orig_options);
577 rc = parse_options(options, &flags);
579 fprintf(stderr, "%s: can't parse options: %s\n",
585 rc = check_mtab_entry(usource, target, "lustre");
586 if (rc && !(flags & MS_REMOUNT)) {
587 fprintf(stderr, "%s: according to %s %s is "
588 "already mounted on %s\n",
589 progname, MOUNTED, usource, target);
592 if (!rc && (flags & MS_REMOUNT)) {
593 fprintf(stderr, "%s: according to %s %s is "
594 "not already mounted on %s\n",
595 progname, MOUNTED, usource, target);
599 if (flags & MS_REMOUNT)
602 rc = access(target, F_OK);
605 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
610 /* In Linux 2.4, the target device doesn't get passed to any of our
611 functions. So we'll stick it on the end of the options. */
612 optlen = strlen(options) + strlen(",device=") + strlen(source) + 1;
613 optcopy = malloc(optlen);
614 if (optcopy == NULL) {
615 fprintf(stderr, "can't allocate memory to optcopy\n");
618 strcpy(optcopy, options);
620 strcat(optcopy, ",");
621 strcat(optcopy, "device=");
622 strcat(optcopy, source);
625 printf("mounting device %s at %s, flags=%#x options=%s\n",
626 source, target, flags, optcopy);
628 if (!strstr(usource, ":/") && set_blockdev_tunables(source)) {
630 fprintf(stderr, "%s: unable to set tunables for %s"
631 " (may cause reduced IO performance)\n",
635 register_service_tags(usource, source, target);
638 /* flags and target get to lustre_get_sb, but not
639 lustre_fill_super. Lustre ignores the flags, but mount
641 for (i = 0, rc = -EAGAIN; i <= retry && rc != 0; i++) {
642 rc = mount(source, target, "lustre", flags,
646 fprintf(stderr, "%s: mount %s at %s "
647 "failed: %s retries left: "
648 "%d\n", basename(progname),
650 strerror(errno), retry-i);
654 sleep(1 << max((i/2), 5));
668 cli = strrchr(usource, ':');
669 if (cli && (strlen(cli) > 2))
674 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
675 usource, target, strerror(errno));
677 fprintf(stderr, "Are the lustre modules loaded?\n"
678 "Check /etc/modprobe.conf and /proc/filesystems"
679 "\nNote 'alias lustre llite' should be removed"
680 " from modprobe.conf\n");
681 if (errno == ENOTBLK)
682 fprintf(stderr, "Do you need -o loop?\n");
683 if (errno == ENOMEDIUM)
685 "This filesystem needs at least 1 OST\n");
686 if (errno == ENOENT) {
687 fprintf(stderr, "Is the MGS specification correct?\n");
688 fprintf(stderr, "Is the filesystem name correct?\n");
689 fprintf(stderr, "If upgrading, is the copied client log"
690 " valid? (see upgrade docs)\n");
692 if (errno == EALREADY)
693 fprintf(stderr, "The target service is already running."
696 fprintf(stderr, "The target service failed to start "
697 "(bad config log?) (%s). "
698 "See /var/log/messages.\n", usource);
700 fprintf(stderr, "Is the MGS running?\n");
701 if (errno == EADDRINUSE)
702 fprintf(stderr, "The target service's index is already "
703 "in use. (%s)\n", usource);
704 if (errno == EINVAL) {
705 fprintf(stderr, "This may have multiple causes.\n");
707 fprintf(stderr, "Is '%s' the correct filesystem"
709 fprintf(stderr, "Are the mount options correct?\n");
710 fprintf(stderr, "Check the syslog for more info.\n");
713 /* May as well try to clean up loop devs */
714 if (strncmp(usource, "/dev/loop", 9) == 0) {
717 sprintf(cmd, "/sbin/losetup -d %s", usource);
718 if ((ret = system(cmd)) < 0)
721 rc = WEXITSTATUS(ret);
724 } else if (!nomtab) {
725 rc = update_mtab_entry(usource, target, "lustre", orig_options,