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 [sun.com URL with a
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"
68 char *progname = NULL;
72 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
73 fprintf(out, "\nThis mount helper should only be invoked via the "
74 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
75 fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n",
78 "\t<device>: the disk device, or for a client:\n"
79 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
80 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
81 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
82 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
83 "\t--force: force mount even if already in /etc/mtab\n"
84 "\t-h|--help: print this usage message\n"
85 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
86 "\t-v|--verbose: print verbose config settings\n"
87 "\t<mntopt>: one or more comma separated of:\n"
88 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
89 "\t\tnosvc: only start MGC/MGS obds\n"
90 "\t\tnomgs: only start target obds, using existing MGS\n"
91 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
92 "inactive OSTs (e.g. lustre-OST0001)\n"
94 exit((out != stdout) ? EINVAL : 0);
97 static int check_mtab_entry(char *spec, char *mtpt, char *type)
102 fp = setmntent(MOUNTED, "r");
106 while ((mnt = getmntent(fp)) != NULL) {
107 if (strcmp(mnt->mnt_fsname, spec) == 0 &&
108 strcmp(mnt->mnt_dir, mtpt) == 0 &&
109 strcmp(mnt->mnt_type, type) == 0) {
120 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
121 int flags, int freq, int pass)
127 mnt.mnt_fsname = spec;
130 mnt.mnt_opts = opts ? opts : "";
132 mnt.mnt_passno = pass;
134 fp = setmntent(MOUNTED, "a+");
136 fprintf(stderr, "%s: setmntent(%s): %s:",
137 progname, MOUNTED, strerror (errno));
140 if ((addmntent(fp, &mnt)) == 1) {
141 fprintf(stderr, "%s: addmntent: %s:",
142 progname, strerror (errno));
151 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
152 #define MAXNIDSTR 1024
153 static char *convert_hostnames(char *s1)
155 char *converted, *s2 = 0, *c;
157 int left = MAXNIDSTR;
160 converted = malloc(left);
162 while ((left > 0) && (*s1 != '/')) {
163 s2 = strpbrk(s1, ",:");
168 nid = libcfs_str2nid(s1);
169 *s2 = sep; /* back to original string */
170 if (nid == LNET_NID_ANY)
172 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
173 left = converted + MAXNIDSTR - c;
176 snprintf(c, left, "%s", s1);
179 fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
184 /*****************************************************************************
186 * This part was cribbed from util-linux/mount/mount.c. There was no clear
187 * license information, but many other files in the package are identified as
188 * GNU GPL, so it's a pretty safe bet that was their intent.
190 ****************************************************************************/
192 const char *opt; /* option name */
193 int inv; /* true if flag value should be inverted */
194 int mask; /* flag mask value */
197 static const struct opt_map opt_map[] = {
198 /*"optname", inv,ms_mask */
199 /* These flags are parsed by mount, not lustre */
200 { "defaults", 0, 0 }, /* default options */
201 { "remount", 0, MS_REMOUNT}, /* remount with different options */
202 { "rw", 1, MS_RDONLY }, /* read-write */
203 { "ro", 0, MS_RDONLY }, /* read-only */
204 { "exec", 1, MS_NOEXEC }, /* permit execution of binaries */
205 { "noexec", 0, MS_NOEXEC }, /* don't execute binaries */
206 { "suid", 1, MS_NOSUID }, /* honor suid executables */
207 { "nosuid", 0, MS_NOSUID }, /* don't honor suid executables */
208 { "dev", 1, MS_NODEV }, /* interpret device files */
209 { "nodev", 0, MS_NODEV }, /* don't interpret devices */
210 { "sync", 0, MS_SYNCHRONOUS}, /* synchronous I/O */
211 { "async", 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
212 { "atime", 1, MS_NOATIME }, /* set file access time on read */
213 { "noatime", 0, MS_NOATIME }, /* do not set file access time on read */
215 { "diratime", 1, MS_NODIRATIME }, /* set file access time on read */
216 { "nodiratime",0,MS_NODIRATIME }, /* do not set file access time on read */
219 { "relatime", 0, MS_RELATIME }, /* set file access time on read */
220 { "norelatime",1,MS_RELATIME }, /* do not set file access time on read */
222 { "auto", 0, 0 }, /* Can be mounted using -a */
223 { "noauto", 0, 0 }, /* Can only be mounted explicitly */
224 { "nousers", 1, 0 }, /* Forbid ordinary user to mount */
225 { "nouser", 1, 0 }, /* Forbid ordinary user to mount */
226 { "noowner", 1, 0 }, /* Device owner has no special privs */
227 { "_netdev", 0, 0 }, /* Device accessible only via network */
231 /****************************************************************************/
233 /* 1 = don't pass on to lustre
234 0 = pass on to lustre */
235 static int parse_one_option(const char *check, int *flagp)
237 const struct opt_map *opt;
239 for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
240 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
243 *flagp &= ~(opt->mask);
250 /* Assume any unknown options are valid and pass them on. The mount
251 will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
255 /* Replace options with subset of Lustre-specific options, and
256 fill in mount flags */
257 int parse_options(char *orig_options, int *flagp)
259 char *options, *opt, *nextopt;
261 options = calloc(strlen(orig_options) + 1, 1);
263 nextopt = orig_options;
264 while ((opt = strsep(&nextopt, ","))) {
268 if (parse_one_option(opt, flagp) == 0) {
269 /* pass this on as an option */
271 strcat(options, ",");
272 strcat(options, opt);
275 strcpy(orig_options, options);
281 int read_file(char *path, char *buf, int size)
285 fd = fopen(path, "r");
289 fgets(buf, size, fd);
294 int write_file(char *path, char *buf)
298 fd = fopen(path, "w");
307 /* This is to tune the kernel for good SCSI performance.
308 * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
309 * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
310 int set_tunables(char *source, int src_len)
313 struct stat stat_buf;
314 char *chk_major, *chk_minor;
315 char *savept, *dev, *s2 = 0;
317 char buf[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
318 char real_path[PATH_MAX] = {'\0'};
325 ret_path = realpath(source, real_path);
326 if (ret_path == NULL) {
328 fprintf(stderr, "warning: %s: cannot resolve: %s",
329 source, strerror(errno));
333 src_len = sizeof(real_path);
335 if (strncmp(real_path, "/dev/loop", 9) == 0)
338 if ((real_path[0] != '/') && ((s2 = strpbrk(real_path, ",:")) != NULL))
341 dev = real_path + src_len - 1;
342 while (dev > real_path && (*dev != '/')) {
347 snprintf(path, sizeof(path), "/sys/block%s/%s", dev,
348 MAX_HW_SECTORS_KB_PATH);
349 rc = read_file(path, buf, sizeof(buf));
350 if (rc == 0 && (strlen(buf) - 1) > 0) {
351 snprintf(path, sizeof(path), "/sys/block%s/%s", dev,
352 MAX_SECTORS_KB_PATH);
353 rc = write_file(path, buf);
355 fprintf(stderr, "warning: opening %s: %s\n",
356 path, strerror(errno));
363 /* The name of the device say 'X' specified in /dev/X may not
364 * match any entry under /sys/block/. In that case we need to
365 * match the major/minor number to find the entry under
366 * sys/block corresponding to /dev/X */
367 dev = real_path + src_len - 1;
368 while (dev > real_path) {
374 rc = stat(dev, &stat_buf);
377 fprintf(stderr, "warning: %s, device %s stat failed\n",
378 strerror(errno), dev);
382 major = major(stat_buf.st_rdev);
383 minor = minor(stat_buf.st_rdev);
384 rc = glob("/sys/block/*", GLOB_NOSORT, NULL, &glob_info);
387 fprintf(stderr, "warning: failed to read entries under "
392 for (i = 0; i < glob_info.gl_pathc; i++){
393 snprintf(path, sizeof(path), "%s/dev", glob_info.gl_pathv[i]);
395 rc = read_file(path, buf, sizeof(buf));
399 if (buf[strlen(buf) - 1] == '\n')
400 buf[strlen(buf) - 1] = '\0';
402 chk_major = strtok_r(buf, ":", &savept);
404 if (major == atoi(chk_major) &&minor == atoi(chk_minor))
408 if (i == glob_info.gl_pathc) {
410 fprintf(stderr,"warning: device %s does not match any "
411 "entry under /sys/block\n", real_path);
416 snprintf(path, sizeof(path), "%s/%s", glob_info.gl_pathv[i],
417 MAX_HW_SECTORS_KB_PATH);
418 rc = read_file(path, buf, sizeof(buf));
421 fprintf(stderr, "warning: opening %s: %s\n",
422 path, strerror(errno));
426 if (strlen(buf) - 1 > 0) {
427 snprintf(path, sizeof(path), "%s/%s",
428 glob_info.gl_pathv[i], MAX_SECTORS_KB_PATH);
429 rc = write_file(path, buf);
431 fprintf(stderr, "warning: writing to %s: %s\n",
432 path, strerror(errno));
436 globfree(&glob_info);
440 int main(int argc, char *const argv[])
442 char default_options[] = "";
443 char *usource, *source, *target, *ptr;
444 char *options, *optcopy, *orig_options = default_options;
445 int i, nargs = 3, opt, rc, flags, optlen;
446 static struct option long_opt[] = {
450 {"nomtab", 0, 0, 'n'},
451 {"options", 1, 0, 'o'},
452 {"verbose", 0, 0, 'v'},
456 progname = strrchr(argv[0], '/');
457 progname = progname ? progname + 1 : argv[0];
459 while ((opt = getopt_long(argc, argv, "fhno:v",
460 long_opt, NULL)) != EOF){
464 printf("force: %d\n", force);
469 printf("fake: %d\n", fake);
477 printf("nomtab: %d\n", nomtab);
481 orig_options = optarg;
489 fprintf(stderr, "%s: unknown option '%c'\n",
496 if (optind + 2 > argc) {
497 fprintf(stderr, "%s: too few arguments\n", progname);
501 usource = argv[optind];
502 source = convert_hostnames(usource);
503 target = argv[optind + 1];
504 ptr = target + strlen(target) - 1;
505 while ((ptr > target) && (*ptr == '/')) {
510 if (!usource || !source) {
515 for (i = 0; i < argc; i++)
516 printf("arg[%d] = %s\n", i, argv[i]);
517 printf("source = %s (%s), target = %s\n", usource, source,
519 printf("options = %s\n", orig_options);
522 options = malloc(strlen(orig_options) + 1);
523 strcpy(options, orig_options);
524 rc = parse_options(options, &flags);
526 fprintf(stderr, "%s: can't parse options: %s\n",
532 rc = check_mtab_entry(usource, target, "lustre");
533 if (rc && !(flags & MS_REMOUNT)) {
534 fprintf(stderr, "%s: according to %s %s is "
535 "already mounted on %s\n",
536 progname, MOUNTED, usource, target);
539 if (!rc && (flags & MS_REMOUNT)) {
540 fprintf(stderr, "%s: according to %s %s is "
541 "not already mounted on %s\n",
542 progname, MOUNTED, usource, target);
546 if (flags & MS_REMOUNT)
549 rc = access(target, F_OK);
552 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
557 /* In Linux 2.4, the target device doesn't get passed to any of our
558 functions. So we'll stick it on the end of the options. */
559 optlen = strlen(options) + strlen(",device=") + strlen(source) + 1;
560 optcopy = malloc(optlen);
561 strcpy(optcopy, options);
563 strcat(optcopy, ",");
564 strcat(optcopy, "device=");
565 strcat(optcopy, source);
568 printf("mounting device %s at %s, flags=%#x options=%s\n",
569 source, target, flags, optcopy);
571 if (set_tunables(source, strlen(source)) && verbose)
572 fprintf(stderr, "%s: unable to set tunables for %s"
573 " (may cause reduced IO performance)\n",
576 register_service_tags(usource, source, target);
579 /* flags and target get to lustre_get_sb, but not
580 lustre_fill_super. Lustre ignores the flags, but mount
582 rc = mount(source, target, "lustre", flags, (void *)optcopy);
589 cli = strrchr(usource, ':');
590 if (cli && (strlen(cli) > 2))
595 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
596 usource, target, strerror(errno));
598 fprintf(stderr, "Are the lustre modules loaded?\n"
599 "Check /etc/modprobe.conf and /proc/filesystems"
600 "\nNote 'alias lustre llite' should be removed"
601 " from modprobe.conf\n");
602 if (errno == ENOTBLK)
603 fprintf(stderr, "Do you need -o loop?\n");
604 if (errno == ENOMEDIUM)
606 "This filesystem needs at least 1 OST\n");
607 if (errno == ENOENT) {
608 fprintf(stderr, "Is the MGS specification correct?\n");
609 fprintf(stderr, "Is the filesystem name correct?\n");
610 fprintf(stderr, "If upgrading, is the copied client log"
611 " valid? (see upgrade docs)\n");
613 if (errno == EALREADY)
614 fprintf(stderr, "The target service is already running."
617 fprintf(stderr, "The target service failed to start "
618 "(bad config log?) (%s). "
619 "See /var/log/messages.\n", usource);
621 fprintf(stderr, "Is the MGS running?\n");
622 if (errno == EADDRINUSE)
623 fprintf(stderr, "The target service's index is already "
624 "in use. (%s)\n", usource);
625 if (errno == EINVAL) {
626 fprintf(stderr, "This may have multiple causes.\n");
628 fprintf(stderr, "Is '%s' the correct filesystem"
630 fprintf(stderr, "Are the mount options correct?\n");
631 fprintf(stderr, "Check the syslog for more info.\n");
634 /* May as well try to clean up loop devs */
635 if (strncmp(usource, "/dev/loop", 9) == 0) {
637 sprintf(cmd, "/sbin/losetup -d %s", usource);
641 } else if (!nomtab) {
642 rc = update_mtab_entry(usource, target, "lustre", orig_options,