4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 * Copyright (c) 2012, 2013, Intel Corporation.
32 * This file is part of Lustre, http://www.lustre.org/
33 * Lustre is a trademark of Sun Microsystems, Inc.
38 #endif /* HAVE_CONFIG_H */
40 #include "mount_utils.h"
45 #include <lustre_disk.h>
46 #include <lustre_ver.h>
48 #include <sys/utsname.h>
50 extern char *progname;
53 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
54 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
59 fprintf(stderr, "\n%s FATAL: ", progname);
62 int run_command(char *cmd, int cmdsz)
64 char log[] = "/tmp/run_command_logXXXXXX";
67 if ((cmdsz - strlen(cmd)) < 6) {
69 fprintf(stderr, "Command buffer overflow: %.*s...\n",
75 printf("cmd: %s\n", cmd);
77 if ((fd = mkstemp(log)) >= 0) {
85 /* Can't use popen because we need the rv of the command */
87 if (rc && (fd >= 0)) {
92 while (fgets(buf, sizeof(buf), fp) != NULL) {
103 int add_param(char *buf, char *key, char *val)
105 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
106 int start = strlen(buf);
110 keylen = strlen(key);
111 if (start + 1 + keylen + strlen(val) >= end) {
112 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
113 progname, buf, key ? key : "", val);
117 sprintf(buf + start, " %s%s", key ? key : "", val);
121 int get_param(char *buf, char *key, char **val)
123 int i, key_len = strlen(key);
126 ptr = strstr(buf, key);
128 *val = strdup(ptr + key_len);
132 for (i = 0; i < strlen(*val); i++)
133 if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
143 char *strscat(char *dst, char *src, int buflen)
146 if (strlen(dst) + strlen(src) >= buflen) {
147 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
148 "\n", buflen, dst, src);
151 return strcat(dst, src);
154 char *strscpy(char *dst, char *src, int buflen)
157 return strscat(dst, src, buflen);
160 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
165 fp = setmntent(MOUNTED, "r");
169 while ((mnt = getmntent(fp)) != NULL) {
170 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
171 strcmp(mnt->mnt_fsname, spec2) == 0) &&
172 (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
173 (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
183 #define PROC_DIR "/proc/"
184 static int mtab_is_proc(const char *mtab)
188 if (readlink(mtab, path, sizeof(path)) < 0)
191 if (strncmp(path, PROC_DIR, strlen(PROC_DIR)))
197 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
198 int flags, int freq, int pass)
204 /* Don't update mtab if it is linked to any file in /proc direcotry.*/
205 if (mtab_is_proc(MOUNTED))
208 mnt.mnt_fsname = spec;
211 mnt.mnt_opts = opts ? opts : "";
213 mnt.mnt_passno = pass;
215 fp = setmntent(MOUNTED, "a+");
217 fprintf(stderr, "%s: setmntent(%s): %s:",
218 progname, MOUNTED, strerror (errno));
221 if ((addmntent(fp, &mnt)) == 1) {
222 fprintf(stderr, "%s: addmntent: %s:",
223 progname, strerror (errno));
232 /* Search for opt in mntlist, returning true if found.
234 static int in_mntlist(char *opt, char *mntlist)
236 char *ml, *mlp, *item, *ctx = NULL;
238 if (!(ml = strdup(mntlist))) {
239 fprintf(stderr, "%s: out of memory\n", progname);
243 while ((item = strtok_r(mlp, ",", &ctx))) {
244 if (!strcmp(opt, item))
249 return (item != NULL);
252 /* Issue a message on stderr for every item in wanted_mountopts that is not
253 * present in mountopts. The justwarn boolean toggles between error and
254 * warning message. Return an error count.
256 int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
259 char *ml, *mlp, *item, *ctx = NULL;
262 if (!(ml = strdup(wanted_mountopts))) {
263 fprintf(stderr, "%s: out of memory\n", progname);
267 while ((item = strtok_r(mlp, ",", &ctx))) {
268 if (!in_mntlist(item, mountopts)) {
269 fprintf(stderr, "%s: %s mount option `%s' is missing\n",
270 progname, justwarn ? "Warning: default"
271 : "Error: mandatory", item);
280 /* Trim embedded white space, leading and trailing commas from string s.
282 void trim_mountfsoptions(char *s)
288 memmove(p, p + 1, strlen(p + 1) + 1);
295 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
297 p = s + strlen(s) - 1;
298 while (p >= s && *p == ',')
302 /* Setup a file in the first unused loop_device */
303 int loop_setup(struct mkfs_opts *mop)
309 /* Figure out the loop device names */
310 if (!access("/dev/loop0", F_OK | R_OK)) {
311 strcpy(loop_base, "/dev/loop\0");
312 } else if (!access("/dev/loop/0", F_OK | R_OK)) {
313 strcpy(loop_base, "/dev/loop/\0");
315 fprintf(stderr, "%s: can't access loop devices\n", progname);
319 /* Find unused loop device */
320 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
322 int cmdsz = sizeof(cmd);
324 sprintf(l_device, "%s%d", loop_base, i);
325 if (access(l_device, F_OK | R_OK))
327 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
330 /* losetup gets 1 (ret=256) for non-set-up device */
332 /* Set up a loopback device to our file */
333 snprintf(cmd, cmdsz, "losetup %s %s", l_device,
335 ret = run_command(cmd, cmdsz);
337 /* someone else picked up this loop device
341 fprintf(stderr, "%s: error %d on losetup: %s\n",
343 ret >= 0 ? strerror(ret) : "");
346 strscpy(mop->mo_loopdev, l_device,
347 sizeof(mop->mo_loopdev));
352 fprintf(stderr, "%s: out of loop devices!\n", progname);
356 int loop_cleanup(struct mkfs_opts *mop)
360 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
361 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
362 ret = run_command(cmd, sizeof(cmd));
367 int loop_format(struct mkfs_opts *mop)
371 if (mop->mo_device_sz == 0) {
373 fprintf(stderr, "loop device requires a --device-size= "
378 fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
381 fprintf(stderr, "%s: Unable to create backing store: %s\n",
382 progname, strerror(errno));
386 if (ftruncate(fd, mop->mo_device_sz * 1024) != 0) {
389 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
390 progname, strerror(errno));
398 /* Write the server config files */
399 int osd_write_ldd(struct mkfs_opts *mop)
401 struct lustre_disk_data *ldd = &mop->mo_ldd;
404 switch (ldd->ldd_mount_type) {
405 #ifdef HAVE_LDISKFS_OSD
407 case LDD_MT_LDISKFS2:
408 ret = ldiskfs_write_ldd(mop);
410 #endif /* HAVE_LDISKFS_OSD */
413 ret = zfs_write_ldd(mop);
415 #endif /* HAVE_ZFS_OSD */
418 fprintf(stderr, "unknown fs type %d '%s'\n",
419 ldd->ldd_mount_type, MT_STR(ldd));
427 /* Read the server config files */
428 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
432 switch (ldd->ldd_mount_type) {
433 #ifdef HAVE_LDISKFS_OSD
435 case LDD_MT_LDISKFS2:
436 ret = ldiskfs_read_ldd(dev, ldd);
438 #endif /* HAVE_LDISKFS_OSD */
441 ret = zfs_read_ldd(dev, ldd);
443 #endif /* HAVE_ZFS_OSD */
446 fprintf(stderr, "unknown fs type %d '%s'\n",
447 ldd->ldd_mount_type, MT_STR(ldd));
455 /* Was this device formatted for Lustre */
456 int osd_is_lustre(char *dev, unsigned *mount_type)
458 vprint("checking for existing Lustre data: ");
460 #ifdef HAVE_LDISKFS_OSD
461 if (ldiskfs_is_lustre(dev, mount_type)) {
465 #endif /* HAVE_LDISKFS_OSD */
467 if (zfs_is_lustre(dev, mount_type)) {
471 #endif /* HAVE_ZFS_OSD */
473 vprint("not found\n");
477 /* Build fs according to type */
478 int osd_make_lustre(struct mkfs_opts *mop)
480 struct lustre_disk_data *ldd = &mop->mo_ldd;
483 switch (ldd->ldd_mount_type) {
484 #ifdef HAVE_LDISKFS_OSD
486 case LDD_MT_LDISKFS2:
487 ret = ldiskfs_make_lustre(mop);
489 #endif /* HAVE_LDISKFS_OSD */
492 ret = zfs_make_lustre(mop);
494 #endif /* HAVE_ZFS_OSD */
497 fprintf(stderr, "unknown fs type %d '%s'\n",
498 ldd->ldd_mount_type, MT_STR(ldd));
506 int osd_prepare_lustre(struct mkfs_opts *mop,
507 char *default_mountopts, int default_len,
508 char *always_mountopts, int always_len)
510 struct lustre_disk_data *ldd = &mop->mo_ldd;
513 switch (ldd->ldd_mount_type) {
514 #ifdef HAVE_LDISKFS_OSD
516 case LDD_MT_LDISKFS2:
517 ret = ldiskfs_prepare_lustre(mop,
518 default_mountopts, default_len,
519 always_mountopts, always_len);
521 #endif /* HAVE_LDISKFS_OSD */
524 ret = zfs_prepare_lustre(mop,
525 default_mountopts, default_len,
526 always_mountopts, always_len);
528 #endif /* HAVE_ZFS_OSD */
531 fprintf(stderr, "unknown fs type %d '%s'\n",
532 ldd->ldd_mount_type, MT_STR(ldd));
540 int osd_tune_lustre(char *dev, struct mount_opts *mop)
542 struct lustre_disk_data *ldd = &mop->mo_ldd;
545 switch (ldd->ldd_mount_type) {
546 #ifdef HAVE_LDISKFS_OSD
548 case LDD_MT_LDISKFS2:
549 ret = ldiskfs_tune_lustre(dev, mop);
551 #endif /* HAVE_LDISKFS_OSD */
554 ret = zfs_tune_lustre(dev, mop);
556 #endif /* HAVE_ZFS_OSD */
559 fprintf(stderr, "unknown fs type %d '%s'\n",
560 ldd->ldd_mount_type, MT_STR(ldd));
568 int osd_label_lustre(struct mount_opts *mop)
570 struct lustre_disk_data *ldd = &mop->mo_ldd;
573 switch (ldd->ldd_mount_type) {
574 #ifdef HAVE_LDISKFS_OSD
576 case LDD_MT_LDISKFS2:
577 ret = ldiskfs_label_lustre(mop);
579 #endif /* HAVE_LDISKFS_OSD */
582 ret = zfs_label_lustre(mop);
584 #endif /* HAVE_ZFS_OSD */
587 fprintf(stderr, "unknown fs type %d '%s'\n",
588 ldd->ldd_mount_type, MT_STR(ldd));
596 /* Enable quota accounting */
597 int osd_enable_quota(struct mkfs_opts *mop)
599 struct lustre_disk_data *ldd = &mop->mo_ldd;
602 switch (ldd->ldd_mount_type) {
603 #ifdef HAVE_LDISKFS_OSD
606 case LDD_MT_LDISKFS2:
607 ret = ldiskfs_enable_quota(mop);
609 #endif /* HAVE_LDISKFS_OSD */
612 fprintf(stderr, "this option is only valid for ldiskfs\n");
615 #endif /* HAVE_ZFS_OSD */
618 fprintf(stderr, "unknown fs type %d '%s'\n",
619 ldd->ldd_mount_type, MT_STR(ldd));
631 #ifdef HAVE_LDISKFS_OSD
632 ret = ldiskfs_init();
635 #endif /* HAVE_LDISKFS_OSD */
638 /* we want to be able to set up a ldiskfs-based filesystem w/o
639 * the ZFS modules installed, see ORI-425 */
642 #endif /* HAVE_ZFS_OSD */
649 #ifdef HAVE_LDISKFS_OSD
651 #endif /* HAVE_LDISKFS_OSD */
654 #endif /* HAVE_ZFS_OSD */
657 __u64 get_device_size(char* device)
662 fd = open(device, O_RDONLY);
664 fprintf(stderr, "%s: cannot open %s: %s\n",
665 progname, device, strerror(errno));
670 /* size in bytes. bz5831 */
671 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
676 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
677 size = (__u64)lsize * 512;
682 fprintf(stderr, "%s: size ioctl failed: %s\n",
683 progname, strerror(errno));
687 vprint("device size = "LPU64"MB\n", size >> 20);
688 /* return value in KB */
692 int file_create(char *path, __u64 size)
699 * Since "size" is in KB, the file offset it represents could overflow
702 size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
703 if (size >= size_max) {
704 fprintf(stderr, "%s: "LPU64" KB: Backing store size must be "
705 "smaller than "LPU64" KB\n", progname, size, size_max);
709 ret = access(path, F_OK);
716 fd = creat(path, S_IRUSR|S_IWUSR);
719 fprintf(stderr, "%s: Unable to create backing store: %s\n",
720 progname, strerror(errno));
724 ret = ftruncate(fd, size * 1024);
728 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
729 progname, strerror(errno));