1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Robert Read <rread@clusterfs.com>
6 * Author: Nathan Rutman <nathan@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <sys/mount.h>
36 #include <sys/utsname.h>
38 #include <lustre_ver.h>
43 #define MAX_HW_SECTORS_KB_PATH "queue/max_hw_sectors_kb"
44 #define MAX_SECTORS_KB_PATH "queue/max_sectors_kb"
50 static char *progname = NULL;
54 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
55 fprintf(out, "\nThis mount helper should only be invoked via the "
56 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
57 fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n",
60 "\t<device>: the disk device, or for a client:\n"
61 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
62 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
63 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
64 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
65 "\t--force: force mount even if already in /etc/mtab\n"
66 "\t-h|--help: print this usage message\n"
67 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
68 "\t-v|--verbose: print verbose config settings\n"
69 "\t<mntopt>: one or more comma separated of:\n"
70 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
71 "\t\tnosvc: only start MGC/MGS obds\n"
72 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
73 "inactive OSTs (e.g. lustre-OST0001)\n"
75 exit((out != stdout) ? EINVAL : 0);
78 static int check_mtab_entry(char *spec, char *mtpt, char *type)
83 fp = setmntent(MOUNTED, "r");
87 while ((mnt = getmntent(fp)) != NULL) {
88 if (strcmp(mnt->mnt_fsname, spec) == 0 &&
89 strcmp(mnt->mnt_dir, mtpt) == 0 &&
90 strcmp(mnt->mnt_type, type) == 0) {
101 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
102 int flags, int freq, int pass)
108 mnt.mnt_fsname = spec;
111 mnt.mnt_opts = opts ? opts : "";
113 mnt.mnt_passno = pass;
115 fp = setmntent(MOUNTED, "a+");
117 fprintf(stderr, "%s: setmntent(%s): %s:",
118 progname, MOUNTED, strerror (errno));
121 if ((addmntent(fp, &mnt)) == 1) {
122 fprintf(stderr, "%s: addmntent: %s:",
123 progname, strerror (errno));
132 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
133 #define MAXNIDSTR 1024
134 static char *convert_hostnames(char *s1)
136 char *converted, *s2 = 0, *c;
138 int left = MAXNIDSTR;
141 converted = malloc(left);
143 while ((left > 0) && (*s1 != '/')) {
144 s2 = strpbrk(s1, ",:");
149 nid = libcfs_str2nid(s1);
150 *s2 = sep; /* back to original string */
151 if (nid == LNET_NID_ANY)
153 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
154 left = converted + MAXNIDSTR - c;
157 snprintf(c, left, "%s", s1);
160 fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
165 /*****************************************************************************
167 * This part was cribbed from util-linux/mount/mount.c. There was no clear
168 * license information, but many other files in the package are identified as
169 * GNU GPL, so it's a pretty safe bet that was their intent.
171 ****************************************************************************/
173 const char *opt; /* option name */
174 int inv; /* true if flag value should be inverted */
175 int mask; /* flag mask value */
178 static const struct opt_map opt_map[] = {
179 /*"optname", inv,ms_mask */
180 /* These flags are parsed by mount, not lustre */
181 { "defaults", 0, 0 }, /* default options */
182 { "remount", 0, MS_REMOUNT}, /* remount with different options */
183 { "rw", 1, MS_RDONLY }, /* read-write */
184 { "ro", 0, MS_RDONLY }, /* read-only */
185 { "exec", 1, MS_NOEXEC }, /* permit execution of binaries */
186 { "noexec", 0, MS_NOEXEC }, /* don't execute binaries */
187 { "suid", 1, MS_NOSUID }, /* honor suid executables */
188 { "nosuid", 0, MS_NOSUID }, /* don't honor suid executables */
189 { "dev", 1, MS_NODEV }, /* interpret device files */
190 { "nodev", 0, MS_NODEV }, /* don't interpret devices */
191 { "sync", 0, MS_SYNCHRONOUS}, /* synchronous I/O */
192 { "async", 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
193 { "atime", 1, MS_NOATIME }, /* set file access time on read */
194 { "noatime", 0, MS_NOATIME }, /* do not set file access time on read */
196 { "diratime", 1, MS_NODIRATIME }, /* set file access time on read */
197 { "nodiratime",0,MS_NODIRATIME }, /* do not set file access time on read */
200 { "relatime", 0, MS_RELATIME }, /* set file access time on read */
201 { "norelatime",1,MS_RELATIME }, /* do not set file access time on read */
203 { "auto", 0, 0 }, /* Can be mounted using -a */
204 { "noauto", 0, 0 }, /* Can only be mounted explicitly */
205 { "nousers", 1, 0 }, /* Forbid ordinary user to mount */
206 { "nouser", 1, 0 }, /* Forbid ordinary user to mount */
207 { "noowner", 1, 0 }, /* Device owner has no special privs */
208 { "_netdev", 0, 0 }, /* Device accessible only via network */
211 /****************************************************************************/
213 /* 1 = don't pass on to lustre
214 0 = pass on to lustre */
215 static int parse_one_option(const char *check, int *flagp)
217 const struct opt_map *opt;
219 for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
220 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
223 *flagp &= ~(opt->mask);
230 /* Assume any unknown options are valid and pass them on. The mount
231 will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
235 /* Replace options with subset of Lustre-specific options, and
236 fill in mount flags */
237 int parse_options(char *orig_options, int *flagp)
239 char *options, *opt, *nextopt;
241 options = calloc(strlen(orig_options) + 1, 1);
243 nextopt = orig_options;
244 while ((opt = strsep(&nextopt, ","))) {
248 if (parse_one_option(opt, flagp) == 0) {
249 /* pass this on as an option */
251 strcat(options, ",");
252 strcat(options, opt);
255 strcpy(orig_options, options);
261 int read_file(char *path, char *buf, int size)
265 fd = fopen(path, "r");
269 fgets(buf, size, fd);
274 int write_file(char *path, char *buf)
278 fd = fopen(path, "w");
287 /* This is to tune the kernel for good SCSI performance.
288 * For that we set the value of /sys/block/{dev}/queue/max_sectors_kb
289 * to the value of /sys/block/{dev}/queue/max_hw_sectors_kb */
290 int set_tunables(char *source, int src_len)
293 struct stat stat_buf;
294 char *chk_major, *chk_minor;
295 char *savept, *dev, *s2 = 0;
297 char buf[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
298 char real_path[PATH_MAX] = {'\0'};
305 ret_path = realpath(source, real_path);
306 if (ret_path == NULL) {
308 fprintf(stderr, "warning: %s: cannot resolve: %s",
309 source, strerror(errno));
313 src_len = sizeof(real_path);
315 if (strncmp(real_path, "/dev/loop", 9) == 0)
318 if ((real_path[0] != '/') && ((s2 = strpbrk(real_path, ",:")) != NULL))
321 dev = real_path + src_len - 1;
322 while (dev > real_path && (*dev != '/')) {
327 snprintf(path, sizeof(path), "/sys/block%s/%s", dev,
328 MAX_HW_SECTORS_KB_PATH);
329 rc = read_file(path, buf, sizeof(buf));
330 if (rc == 0 && (strlen(buf) - 1) > 0) {
331 snprintf(path, sizeof(path), "/sys/block%s/%s", dev,
332 MAX_SECTORS_KB_PATH);
333 rc = write_file(path, buf);
335 fprintf(stderr, "warning: opening %s: %s\n",
336 path, strerror(errno));
343 /* The name of the device say 'X' specified in /dev/X may not
344 * match any entry under /sys/block/. In that case we need to
345 * match the major/minor number to find the entry under
346 * sys/block corresponding to /dev/X */
347 dev = real_path + src_len - 1;
348 while (dev > real_path) {
354 rc = stat(dev, &stat_buf);
357 fprintf(stderr, "warning: %s, device %s stat failed\n",
358 strerror(errno), dev);
362 major = major(stat_buf.st_rdev);
363 minor = minor(stat_buf.st_rdev);
364 rc = glob("/sys/block/*", GLOB_NOSORT, NULL, &glob_info);
367 fprintf(stderr, "warning: failed to read entries under "
372 for (i = 0; i < glob_info.gl_pathc; i++){
373 snprintf(path, sizeof(path), "%s/dev", glob_info.gl_pathv[i]);
375 rc = read_file(path, buf, sizeof(buf));
379 if (buf[strlen(buf) - 1] == '\n')
380 buf[strlen(buf) - 1] = '\0';
382 chk_major = strtok_r(buf, ":", &savept);
384 if (major == atoi(chk_major) &&minor == atoi(chk_minor))
388 if (i == glob_info.gl_pathc) {
390 fprintf(stderr,"warning: device %s does not match any "
391 "entry under /sys/block\n", real_path);
395 snprintf(path, sizeof(path), "%s/%s", glob_info.gl_pathv[i],
396 MAX_HW_SECTORS_KB_PATH);
397 rc = read_file(path, buf, sizeof(buf));
400 fprintf(stderr, "warning: opening %s: %s\n",
401 path, strerror(errno));
405 if (strlen(buf) - 1 > 0) {
406 snprintf(path, sizeof(path), "%s/%s",
407 glob_info.gl_pathv[i], MAX_SECTORS_KB_PATH);
408 rc = write_file(path, buf);
410 fprintf(stderr, "warning: writing to %s: %s\n",
411 path, strerror(errno));
416 int main(int argc, char *const argv[])
418 char default_options[] = "";
419 char *usource, *source, *target, *ptr;
420 char *options, *optcopy, *orig_options = default_options;
421 int i, nargs = 3, opt, rc, flags, optlen;
422 static struct option long_opt[] = {
426 {"nomtab", 0, 0, 'n'},
427 {"options", 1, 0, 'o'},
428 {"verbose", 0, 0, 'v'},
432 progname = strrchr(argv[0], '/');
433 progname = progname ? progname + 1 : argv[0];
435 while ((opt = getopt_long(argc, argv, "fhno:v",
436 long_opt, NULL)) != EOF){
440 printf("force: %d\n", force);
445 printf("fake: %d\n", fake);
453 printf("nomtab: %d\n", nomtab);
457 orig_options = optarg;
465 fprintf(stderr, "%s: unknown option '%c'\n",
472 if (optind + 2 > argc) {
473 fprintf(stderr, "%s: too few arguments\n", progname);
477 usource = argv[optind];
478 source = convert_hostnames(usource);
479 target = argv[optind + 1];
480 ptr = target + strlen(target) - 1;
481 while ((ptr > target) && (*ptr == '/')) {
486 if (!usource || !source) {
491 for (i = 0; i < argc; i++)
492 printf("arg[%d] = %s\n", i, argv[i]);
493 printf("source = %s (%s), target = %s\n", usource, source, target);
494 printf("options = %s\n", orig_options);
497 options = malloc(strlen(orig_options) + 1);
498 strcpy(options, orig_options);
499 rc = parse_options(options, &flags);
501 fprintf(stderr, "%s: can't parse options: %s\n",
507 rc = check_mtab_entry(usource, target, "lustre");
508 if (rc && !(flags & MS_REMOUNT)) {
509 fprintf(stderr, "%s: according to %s %s is "
510 "already mounted on %s\n",
511 progname, MOUNTED, usource, target);
514 if (!rc && (flags & MS_REMOUNT)) {
515 fprintf(stderr, "%s: according to %s %s is "
516 "not already mounted on %s\n",
517 progname, MOUNTED, usource, target);
521 if (flags & MS_REMOUNT)
524 rc = access(target, F_OK);
527 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
532 /* In Linux 2.4, the target device doesn't get passed to any of our
533 functions. So we'll stick it on the end of the options. */
534 optlen = strlen(options) + strlen(",device=") + strlen(source) + 1;
535 optcopy = malloc(optlen);
536 strcpy(optcopy, options);
538 strcat(optcopy, ",");
539 strcat(optcopy, "device=");
540 strcat(optcopy, source);
543 printf("mounting device %s at %s, flags=%#x options=%s\n",
544 source, target, flags, optcopy);
546 if (set_tunables(source, strlen(source)) && verbose)
547 fprintf(stderr, "%s: unable to set tunables for %s"
548 " (may cause reduced IO performance)",
552 /* flags and target get to lustre_get_sb, but not
553 lustre_fill_super. Lustre ignores the flags, but mount
555 rc = mount(source, target, "lustre", flags, (void *)optcopy);
562 cli = strrchr(usource, ':');
563 if (cli && (strlen(cli) > 2))
568 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
569 usource, target, strerror(errno));
571 fprintf(stderr, "Are the lustre modules loaded?\n"
572 "Check /etc/modprobe.conf and /proc/filesystems"
573 "\nNote 'alias lustre llite' should be removed"
574 " from modprobe.conf\n");
575 if (errno == ENOTBLK)
576 fprintf(stderr, "Do you need -o loop?\n");
577 if (errno == ENOMEDIUM)
579 "This filesystem needs at least 1 OST\n");
580 if (errno == ENOENT) {
581 fprintf(stderr, "Is the MGS specification correct?\n");
582 fprintf(stderr, "Is the filesystem name correct?\n");
583 fprintf(stderr, "If upgrading, is the copied client log"
584 " valid? (see upgrade docs)\n");
586 if (errno == EALREADY)
587 fprintf(stderr, "The target service is already running."
590 fprintf(stderr, "The target service failed to start "
591 "(bad config log?) (%s). "
592 "See /var/log/messages.\n", usource);
594 fprintf(stderr, "Is the MGS running?\n");
595 if (errno == EADDRINUSE)
596 fprintf(stderr, "The target service's index is already "
597 "in use. (%s)\n", usource);
598 if (errno == EINVAL) {
599 fprintf(stderr, "This may have multiple causes.\n");
601 fprintf(stderr, "Is '%s' the correct filesystem"
603 fprintf(stderr, "Are the mount options correct?\n");
604 fprintf(stderr, "Check the syslog for more info.\n");
607 /* May as well try to clean up loop devs */
608 if (strncmp(usource, "/dev/loop", 9) == 0) {
610 sprintf(cmd, "/sbin/losetup -d %s", usource);
614 } else if (!nomtab) {
615 rc = update_mtab_entry(usource, target, "lustre", orig_options,