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>
44 static char *progname = NULL;
48 fprintf(out, "%s v"LUSTRE_VERSION_STRING"\n", progname);
49 fprintf(out, "\nThis mount helper should only be invoked via the "
50 "mount (8) command,\ne.g. mount -t lustre dev dir\n\n");
51 fprintf(out, "usage: %s [-fhnv] [-o <mntopt>] <device> <mountpt>\n",
54 "\t<device>: the disk device, or for a client:\n"
55 "\t\t<mgmtnid>[:<altmgtnid>...]:/<filesystem>-client\n"
56 "\t<filesystem>: name of the Lustre filesystem (e.g. lustre1)\n"
57 "\t<mountpt>: filesystem mountpoint (e.g. /mnt/lustre)\n"
58 "\t-f|--fake: fake mount (updates /etc/mtab)\n"
59 "\t--force: force mount even if already in /etc/mtab\n"
60 "\t-h|--help: print this usage message\n"
61 "\t-n|--nomtab: do not update /etc/mtab after mount\n"
62 "\t-v|--verbose: print verbose config settings\n"
63 "\t<mntopt>: one or more comma separated of:\n"
64 "\t\t(no)flock,(no)user_xattr,(no)acl\n"
65 "\t\tnosvc: only start MGC/MGS obds\n"
66 "\t\texclude=<ostname>[:<ostname>] : colon-separated list of "
67 "inactive OSTs (e.g. lustre-OST0001)\n"
69 exit((out != stdout) ? EINVAL : 0);
72 static int check_mtab_entry(char *spec, char *mtpt, char *type)
77 fp = setmntent(MOUNTED, "r");
81 while ((mnt = getmntent(fp)) != NULL) {
82 if (strcmp(mnt->mnt_fsname, spec) == 0 &&
83 strcmp(mnt->mnt_dir, mtpt) == 0 &&
84 strcmp(mnt->mnt_type, type) == 0) {
95 update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
96 int flags, int freq, int pass)
102 mnt.mnt_fsname = spec;
105 mnt.mnt_opts = opts ? opts : "";
107 mnt.mnt_passno = pass;
109 fp = setmntent(MOUNTED, "a+");
111 fprintf(stderr, "%s: setmntent(%s): %s:",
112 progname, MOUNTED, strerror (errno));
115 if ((addmntent(fp, &mnt)) == 1) {
116 fprintf(stderr, "%s: addmntent: %s:",
117 progname, strerror (errno));
126 /* Get rid of symbolic hostnames for tcp, since kernel can't do lookups */
127 #define MAXNIDSTR 1024
128 static char *convert_hostnames(char *s1)
130 char *converted, *s2 = 0, *c;
132 int left = MAXNIDSTR;
135 converted = malloc(left);
137 while ((left > 0) && (*s1 != '/')) {
138 s2 = strpbrk(s1, ",:");
143 nid = libcfs_str2nid(s1);
144 if (nid == LNET_NID_ANY)
146 c += snprintf(c, left, "%s%c", libcfs_nid2str(nid), sep);
147 left = converted + MAXNIDSTR - c;
150 snprintf(c, left, "%s", s1);
153 fprintf(stderr, "%s: Can't parse NID '%s'\n", progname, s1);
158 /*****************************************************************************
160 * This part was cribbed from util-linux/mount/mount.c. There was no clear
161 * license information, but many other files in the package are identified as
162 * GNU GPL, so it's a pretty safe bet that was their intent.
164 ****************************************************************************/
166 const char *opt; /* option name */
167 int inv; /* true if flag value should be inverted */
168 int mask; /* flag mask value */
171 static const struct opt_map opt_map[] = {
172 /*"optname", inv,ms_mask */
173 /* These flags are parsed by mount, not lustre */
174 { "defaults", 0, 0 }, /* default options */
175 { "remount", 0, MS_REMOUNT}, /* remount with different options */
176 { "rw", 1, MS_RDONLY }, /* read-write */
177 { "ro", 0, MS_RDONLY }, /* read-only */
178 { "exec", 1, MS_NOEXEC }, /* permit execution of binaries */
179 { "noexec", 0, MS_NOEXEC }, /* don't execute binaries */
180 { "suid", 1, MS_NOSUID }, /* honor suid executables */
181 { "nosuid", 0, MS_NOSUID }, /* don't honor suid executables */
182 { "dev", 1, MS_NODEV }, /* interpret device files */
183 { "nodev", 0, MS_NODEV }, /* don't interpret devices */
184 { "async", 1, MS_SYNCHRONOUS}, /* asynchronous I/O */
185 { "auto", 0, 0 }, /* Can be mounted using -a */
186 { "noauto", 0, 0 }, /* Can only be mounted explicitly */
187 { "nousers", 1, 0 }, /* Forbid ordinary user to mount */
188 { "nouser", 1, 0 }, /* Forbid ordinary user to mount */
189 { "noowner", 1, 0 }, /* Device owner has no special privs */
190 { "_netdev", 0, 0 }, /* Device accessible only via network */
193 /****************************************************************************/
195 /* 1 = don't pass on to lustre
196 0 = pass on to lustre */
197 static int parse_one_option(const char *check, int *flagp)
199 const struct opt_map *opt;
201 for (opt = &opt_map[0]; opt->opt != NULL; opt++) {
202 if (strncmp(check, opt->opt, strlen(opt->opt)) == 0) {
205 *flagp &= ~(opt->mask);
212 /* Assume any unknown options are valid and pass them on. The mount
213 will fail if lmd_parse, ll_options or ldiskfs doesn't recognize it.*/
217 /* Replace options with subset of Lustre-specific options, and
218 fill in mount flags */
219 int parse_options(char *orig_options, int *flagp)
221 char *options, *opt, *nextopt;
223 options = calloc(strlen(orig_options) + 1, 1);
225 nextopt = orig_options;
226 while ((opt = strsep(&nextopt, ","))) {
230 if (parse_one_option(opt, flagp) == 0) {
231 /* pass this on as an option */
233 strcat(options, ",");
234 strcat(options, opt);
237 strcpy(orig_options, options);
243 int main(int argc, char *const argv[])
245 char default_options[] = "";
246 char *source, *target, *ptr;
247 char *options, *optcopy, *orig_options = default_options;
248 int i, nargs = 3, opt, rc, flags, optlen;
249 static struct option long_opt[] = {
253 {"nomtab", 0, 0, 'n'},
254 {"options", 1, 0, 'o'},
255 {"verbose", 0, 0, 'v'},
259 progname = strrchr(argv[0], '/');
260 progname = progname ? progname + 1 : argv[0];
262 while ((opt = getopt_long(argc, argv, "fhno:v",
263 long_opt, NULL)) != EOF){
267 printf("force: %d\n", force);
272 printf("fake: %d\n", fake);
280 printf("nomtab: %d\n", nomtab);
284 orig_options = optarg;
292 fprintf(stderr, "%s: unknown option '%c'\n",
299 if (optind + 2 > argc) {
300 fprintf(stderr, "%s: too few arguments\n", progname);
304 source = convert_hostnames(argv[optind]);
305 target = argv[optind + 1];
306 ptr = target + strlen(target) - 1;
307 while ((ptr > target) && (*ptr == '/')) {
317 for (i = 0; i < argc; i++)
318 printf("arg[%d] = %s\n", i, argv[i]);
319 printf("source = %s, target = %s\n", source, target);
320 printf("options = %s\n", orig_options);
323 options = malloc(strlen(orig_options) + 1);
324 strcpy(options, orig_options);
325 rc = parse_options(options, &flags);
327 fprintf(stderr, "%s: can't parse options: %s\n",
333 rc = check_mtab_entry(source, target, "lustre");
334 if (rc && !(flags & MS_REMOUNT)) {
335 fprintf(stderr, "%s: according to %s %s is "
336 "already mounted on %s\n",
337 progname, MOUNTED, source, target);
340 if (!rc && (flags & MS_REMOUNT)) {
341 fprintf(stderr, "%s: according to %s %s is "
342 "not already mounted on %s\n",
343 progname, MOUNTED, source, target);
347 if (flags & MS_REMOUNT)
350 rc = access(target, F_OK);
353 fprintf(stderr, "%s: %s inaccessible: %s\n", progname, target,
358 /* In Linux 2.4, the target device doesn't get passed to any of our
359 functions. So we'll stick it on the end of the options. */
360 optlen = strlen(options) + strlen(",device=") + strlen(source) + 1;
361 optcopy = malloc(optlen);
362 strcpy(optcopy, options);
364 strcat(optcopy, ",");
365 strcat(optcopy, "device=");
366 strcat(optcopy, source);
369 printf("mounting device %s at %s, flags=%#x options=%s\n",
370 source, target, flags, optcopy);
373 /* flags and target get to lustre_get_sb, but not
374 lustre_fill_super. Lustre ignores the flags, but mount
376 rc = mount(source, target, "lustre", flags, (void *)optcopy);
383 cli = strrchr(source, ':');
384 if (cli && (strlen(cli) > 2))
389 fprintf(stderr, "%s: mount %s at %s failed: %s\n", progname,
390 source, target, strerror(errno));
392 fprintf(stderr, "Are the lustre modules loaded?\n"
393 "Check /etc/modprobe.conf and /proc/filesystems"
394 "\nNote 'alias lustre llite' should be removed"
395 " from modprobe.conf\n");
396 if (errno == ENOTBLK)
397 fprintf(stderr, "Do you need -o loop?\n");
398 if (errno == ENOMEDIUM)
400 "This filesystem needs at least 1 OST\n");
401 if (errno == ENOENT) {
402 fprintf(stderr, "Is the MGS specification correct?\n");
403 fprintf(stderr, "Is the filesystem name correct?\n");
404 fprintf(stderr, "If upgrading, is the copied client log"
405 " valid? (see upgrade docs)\n");
407 if (errno == EALREADY)
408 fprintf(stderr, "The target service is already running."
411 fprintf(stderr, "The target service failed to start "
412 "(bad config log?) (%s). "
413 "See /var/log/messages.\n", source);
415 fprintf(stderr, "Is the MGS running?\n");
416 if (errno == EADDRINUSE)
417 fprintf(stderr, "The target service's index is already "
418 "in use. (%s)\n", source);
419 if (errno == EINVAL) {
420 fprintf(stderr, "This may have multiple causes.\n");
422 fprintf(stderr, "Is '%s' the correct filesystem"
424 fprintf(stderr, "Are the mount options correct?\n");
425 fprintf(stderr, "Check the syslog for more info.\n");
428 /* May as well try to clean up loop devs */
429 if (strncmp(source, "/dev/loop", 9) == 0) {
431 sprintf(cmd, "/sbin/losetup -d %s", source);
435 } else if (!nomtab) {
436 rc = update_mtab_entry(source, target, "lustre", orig_options,