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 2008 Sun Microsystems, Inc. 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>
51 #include <sys/mount.h>
55 #include <lustre_ver.h>
59 #include "mount_utils.h"
61 #define MAX_HW_SECTORS_KB_PATH "queue/max_hw_sectors_kb"
62 #define MAX_SECTORS_KB_PATH "queue/max_sectors_kb"
63 #define MAX_RETRIES 99
70 char *progname = NULL;
74 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
75 fprintf(out, "\nThis mount helper should only be invoked via the "
76 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
77 fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n",
80 "\t<device>: the disk device, or for a client:\n"
81 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
82 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
83 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
84 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
85 "\t--force: force mount even if already in /etc/mtab\n"
86 "\t-h|--help: print this usage message\n"
87 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
88 "\t-v|--verbose: print verbose config settings\n"
89 "\t<mntopt>: one or more comma separated of:\n"
90 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
91 "\t\tabort_recov: abort server recovery handling\n"
92 "\t\tnosvc: only start MGC/MGS obds\n"
93 "\t\tnomgs: only start target obds, using existing MGS\n"
94 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
95 "inactive OSTs (e.g. lustre-OST0001)\n"
96 "\t\tretry=<num>: number of times mount is retried by client\n"
98 exit((out != stdout) ? EINVAL : 0);
101 static int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
106 fp = setmntent(MOUNTED, "r");
110 while ((mnt = getmntent(fp)) != NULL) {
111 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
112 strcmp(mnt->mnt_fsname, spec2) == 0) &&
113 strcmp(mnt->mnt_dir, mtpt) == 0 &&
114 strcmp(mnt->mnt_type, type) == 0) {
125 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
126 int flags, int freq, int pass)
132 mnt.mnt_fsname = spec;
135 mnt.mnt_opts = opts ? opts : "";
137 mnt.mnt_passno = pass;
139 fp = setmntent(MOUNTED, "a+");
141 fprintf(stderr, "%s: setmntent(%s): %s:",
142 progname, MOUNTED, strerror (errno));
145 if ((addmntent(fp, &mnt)) == 1) {
146 fprintf(stderr, "%s: addmntent: %s:",
147 progname, strerror (errno));
156 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
157 #define MAXNIDSTR 1024
158 static char *convert_hostnames(char *s1)
160 char *converted, *s2 = 0, *c;
162 int left = MAXNIDSTR;
165 converted = malloc(left);
166 if (converted == NULL) {
167 fprintf(stderr, "out of memory: needed %d bytes\n",
172 while ((left > 0) && (*s1 != '/')) {
173 s2 = strpbrk(s1, ",:");
178 nid = libcfs_str2nid(s1);
179 *s2 = sep; /* back to original string */
180 if (nid == LNET_NID_ANY)
182 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
183 left = converted + MAXNIDSTR - c;
186 snprintf(c, left, "%s", s1);
189 fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
194 /*****************************************************************************
196 * This part was cribbed from util-linux/mount/mount.c. There was no clear
197 * license information, but many other files in the package are identified as
198 * GNU GPL, so it's a pretty safe bet that was their intent.
200 ****************************************************************************/
202 const char *opt; /* option name */
203 int inv; /* true if flag value should be inverted */
204 int mask; /* flag mask value */
207 static const struct opt_map opt_map[] = {
208 /*"optname", inv,ms_mask */
209 /* These flags are parsed by mount, not lustre */
210 { "defaults", 0, 0 }, /* default options */
211 { "remount", 0, MS_REMOUNT}, /* remount with different options */
212 { "rw", 1, MS_RDONLY }, /* read-write */
213 { "ro", 0, MS_RDONLY }, /* read-only */
214 { "exec", 1, MS_NOEXEC }, /* permit execution of binaries */
215 { "noexec", 0, MS_NOEXEC }, /* don't execute binaries */
216 { "suid", 1, MS_NOSUID }, /* honor suid executables */
217 { "nosuid", 0, MS_NOSUID }, /* don't honor suid executables */
218 { "dev", 1, MS_NODEV }, /* interpret device files */
219 { "nodev", 0, MS_NODEV }, /* don't interpret devices */
220 { "sync", 0, MS_SYNCHRONOUS}, /* synchronous I/O */
221 { "async", 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
222 { "atime", 1, MS_NOATIME }, /* set file access time on read */
223 { "noatime", 0, MS_NOATIME }, /* do not set file access time on read */
225 { "diratime", 1, MS_NODIRATIME }, /* set file access time on read */
226 { "nodiratime",0,MS_NODIRATIME }, /* do not set file access time on read */
229 { "relatime", 0, MS_RELATIME }, /* set file access time on read */
230 { "norelatime",1,MS_RELATIME }, /* do not set file access time on read */
232 { "auto", 0, 0 }, /* Can be mounted using -a */
233 { "noauto", 0, 0 }, /* Can only be mounted explicitly */
234 { "nousers", 1, 0 }, /* Forbid ordinary user to mount */
235 { "nouser", 1, 0 }, /* Forbid ordinary user to mount */
236 { "noowner", 1, 0 }, /* Device owner has no special privs */
237 { "_netdev", 0, 0 }, /* Device accessible only via network */
241 /****************************************************************************/
243 /* 1 = don't pass on to lustre
244 0 = pass on to lustre */
245 static int parse_one_option(const char *check, int *flagp)
247 const struct opt_map *opt;
249 for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
250 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
253 *flagp &= ~(opt->mask);
260 /* Assume any unknown options are valid and pass them on. The mount
261 will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
265 /* Replace options with subset of Lustre-specific options, and
266 fill in mount flags */
267 int parse_options(char *orig_options, int *flagp)
269 char *options, *opt, *nextopt, *arg, *val;
271 options = calloc(strlen(orig_options) + 1, 1);
273 nextopt = orig_options;
274 while ((opt = strsep(&nextopt, ","))) {
279 /* Handle retries in a slightly different
282 val = strchr(opt, '=');
283 if (val != NULL && strncmp(arg, "retry", 5) == 0) {
284 retry = atoi(val + 1);
285 if (retry > MAX_RETRIES)
290 else if (parse_one_option(opt, flagp) == 0) {
291 /* pass this on as an option */
293 strcat(options, ",");
294 strcat(options, opt);
297 strcpy(orig_options, options);
303 int read_file(char *path, char *buf, int size)
307 fd = fopen(path, "r");
311 fgets(buf, size, fd);
316 int write_file(char *path, char *buf)
320 fd = fopen(path, "w");
329 /* This is to tune the kernel for good SCSI performance.
330 * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
331 * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
332 int set_tunables(char *source, int src_len)
335 struct stat stat_buf;
336 char *chk_major, *chk_minor;
339 char buf[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
340 char real_path[PATH_MAX] = {'\0'};
347 ret_path = realpath(source, real_path);
348 if (ret_path == NULL) {
350 fprintf(stderr, "warning: %s: cannot resolve: %s\n",
351 source, strerror(errno));
355 src_len = sizeof(real_path);
357 if (strncmp(real_path, "/dev/loop", 9) == 0)
360 if ((real_path[0] != '/') && (strpbrk(real_path, ",:") != NULL))
363 dev = real_path + src_len - 1;
364 while (dev > real_path && (*dev != '/')) {
369 snprintf(path, sizeof(path), "/sys/block%s/%s", dev,
370 MAX_HW_SECTORS_KB_PATH);
371 rc = read_file(path, buf, sizeof(buf));
372 if (rc == 0 && (strlen(buf) - 1) > 0) {
373 snprintf(path, sizeof(path), "/sys/block%s/%s", dev,
374 MAX_SECTORS_KB_PATH);
375 rc = write_file(path, buf);
377 fprintf(stderr, "warning: opening %s: %s\n",
378 path, strerror(errno));
385 /* The name of the device say 'X' specified in /dev/X may not
386 * match any entry under /sys/block/. In that case we need to
387 * match the major/minor number to find the entry under
388 * sys/block corresponding to /dev/X */
389 dev = real_path + src_len - 1;
390 while (dev > real_path) {
396 rc = stat(dev, &stat_buf);
399 fprintf(stderr, "warning: %s, device %s stat failed\n",
400 strerror(errno), dev);
404 major = major(stat_buf.st_rdev);
405 minor = minor(stat_buf.st_rdev);
406 rc = glob("/sys/block/*", GLOB_NOSORT, NULL, &glob_info);
409 fprintf(stderr, "warning: failed to read entries under "
414 for (i = 0; i < glob_info.gl_pathc; i++){
415 snprintf(path, sizeof(path), "%s/dev", glob_info.gl_pathv[i]);
417 rc = read_file(path, buf, sizeof(buf));
421 if (buf[strlen(buf) - 1] == '\n')
422 buf[strlen(buf) - 1] = '\0';
424 chk_major = strtok_r(buf, ":", &savept);
426 if (major == atoi(chk_major) &&minor == atoi(chk_minor))
430 if (i == glob_info.gl_pathc) {
432 fprintf(stderr,"warning: device %s does not match any "
433 "entry under /sys/block\n", real_path);
438 snprintf(path, sizeof(path), "%s/%s", glob_info.gl_pathv[i],
439 MAX_HW_SECTORS_KB_PATH);
440 rc = read_file(path, buf, sizeof(buf));
443 fprintf(stderr, "warning: opening %s: %s\n",
444 path, strerror(errno));
448 if (strlen(buf) - 1 > 0) {
449 snprintf(path, sizeof(path), "%s/%s",
450 glob_info.gl_pathv[i], MAX_SECTORS_KB_PATH);
451 rc = write_file(path, buf);
453 fprintf(stderr, "warning: writing to %s: %s\n",
454 path, strerror(errno));
458 globfree(&glob_info);
462 int main(int argc, char *const argv[])
464 char default_options[] = "";
465 char *usource, *source, *target, *ptr;
466 char *options, *optcopy, *orig_options = default_options;
467 int i, nargs = 3, opt, rc, flags, optlen;
468 static struct option long_opt[] = {
472 {"nomtab", 0, 0, 'n'},
473 {"options", 1, 0, 'o'},
474 {"verbose", 0, 0, 'v'},
478 progname = strrchr(argv[0], '/');
479 progname = progname ? progname + 1 : argv[0];
481 while ((opt = getopt_long(argc, argv, "fhno:v",
482 long_opt, NULL)) != EOF){
486 printf("force: %d\n", force);
491 printf("fake: %d\n", fake);
499 printf("nomtab: %d\n", nomtab);
503 orig_options = optarg;
511 fprintf(stderr, "%s: unknown option '%c'\n",
518 if (optind + 2 > argc) {
519 fprintf(stderr, "%s: too few arguments\n", progname);
523 usource = argv[optind];
528 source = convert_hostnames(usource);
533 target = argv[optind + 1];
534 ptr = target + strlen(target) - 1;
535 while ((ptr > target) && (*ptr == '/')) {
541 for (i = 0; i < argc; i++)
542 printf("arg[%d] = %s\n", i, argv[i]);
543 printf("source = %s (%s), target = %s\n", usource, source,
545 printf("options = %s\n", orig_options);
548 options = malloc(strlen(orig_options) + 1);
549 if (options == NULL) {
550 fprintf(stderr, "can't allocate memory for options\n");
553 strcpy(options, orig_options);
554 rc = parse_options(options, &flags);
556 fprintf(stderr, "%s: can't parse options: %s\n",
562 rc = check_mtab_entry(usource, source, target, "lustre");
563 if (rc && !(flags & MS_REMOUNT)) {
564 fprintf(stderr, "%s: according to %s %s is "
565 "already mounted on %s\n",
566 progname, MOUNTED, usource, target);
569 if (!rc && (flags & MS_REMOUNT)) {
570 fprintf(stderr, "%s: according to %s %s is "
571 "not already mounted on %s\n",
572 progname, MOUNTED, usource, target);
576 if (flags & MS_REMOUNT)
579 rc = access(target, F_OK);
582 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
587 /* In Linux 2.4, the target device doesn't get passed to any of our
588 functions. So we'll stick it on the end of the options. */
589 optlen = strlen(options) + strlen(",device=") + strlen(source) + 1;
590 optcopy = malloc(optlen);
591 if (optcopy == NULL) {
592 fprintf(stderr, "can't allocate memory to optcopy\n");
595 strcpy(optcopy, options);
597 strcat(optcopy, ",");
598 strcat(optcopy, "device=");
599 strcat(optcopy, source);
602 printf("mounting device %s at %s, flags=%#x options=%s\n",
603 source, target, flags, optcopy);
605 if (!strstr(usource, ":/") && set_tunables(source, strlen(source)) &&
607 fprintf(stderr, "%s: unable to set tunables for %s"
608 " (may cause reduced IO performance)\n",
611 register_service_tags(usource, source, target);
614 /* flags and target get to lustre_get_sb, but not
615 lustre_fill_super. Lustre ignores the flags, but mount
617 for (i = 0, rc = -EAGAIN; i <= retry && rc != 0; i++) {
618 rc = mount(source, target, "lustre", flags,
622 fprintf(stderr, "%s: mount %s at %s "
623 "failed: %s retries left: "
624 "%d\n", basename(progname),
626 strerror(errno), retry-i);
630 sleep(1 << max((i/2), 5));
644 cli = strrchr(usource, ':');
645 if (cli && (strlen(cli) > 2))
650 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
651 usource, target, strerror(errno));
653 fprintf(stderr, "Are the lustre modules loaded?\n"
654 "Check /etc/modprobe.conf and /proc/filesystems"
655 "\nNote 'alias lustre llite' should be removed"
656 " from modprobe.conf\n");
657 if (errno == ENOTBLK)
658 fprintf(stderr, "Do you need -o loop?\n");
659 if (errno == ENOMEDIUM)
661 "This filesystem needs at least 1 OST\n");
662 if (errno == ENOENT) {
663 fprintf(stderr, "Is the MGS specification correct?\n");
664 fprintf(stderr, "Is the filesystem name correct?\n");
665 fprintf(stderr, "If upgrading, is the copied client log"
666 " valid? (see upgrade docs)\n");
668 if (errno == EALREADY)
669 fprintf(stderr, "The target service is already running."
672 fprintf(stderr, "The target service failed to start "
673 "(bad config log?) (%s). "
674 "See /var/log/messages.\n", usource);
676 fprintf(stderr, "Is the MGS running?\n");
677 if (errno == EADDRINUSE)
678 fprintf(stderr, "The target service's index is already "
679 "in use. (%s)\n", usource);
680 if (errno == EINVAL) {
681 fprintf(stderr, "This may have multiple causes.\n");
683 fprintf(stderr, "Is '%s' the correct filesystem"
685 fprintf(stderr, "Are the mount options correct?\n");
686 fprintf(stderr, "Check the syslog for more info.\n");
689 /* May as well try to clean up loop devs */
690 if (strncmp(usource, "/dev/loop", 9) == 0) {
692 sprintf(cmd, "/sbin/losetup -d %s", usource);
696 } else if (!nomtab) {
697 rc = update_mtab_entry(usource, target, "lustre", orig_options,