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 char *sub_val = NULL;
106 int buflen = strlen(buf);
107 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
112 keylen = strlen(key);
115 while ((sub_val = strsep(&val, ",")) != NULL) {
119 if (start + 1 + keylen + strlen(sub_val) >= end) {
120 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
121 progname, buf, key != NULL ? key : "", sub_val);
126 sprintf(buf + start, " %s%s", key != NULL ? key : "", sub_val);
133 int get_param(char *buf, char *key, char **val)
135 int i, key_len = strlen(key);
138 ptr = strstr(buf, key);
140 *val = strdup(ptr + key_len);
144 for (i = 0; i < strlen(*val); i++)
145 if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
155 char *strscat(char *dst, char *src, int buflen)
158 if (strlen(dst) + strlen(src) >= buflen) {
159 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
160 "\n", buflen, dst, src);
163 return strcat(dst, src);
166 char *strscpy(char *dst, char *src, int buflen)
169 return strscat(dst, src, buflen);
172 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
177 fp = setmntent(MOUNTED, "r");
181 while ((mnt = getmntent(fp)) != NULL) {
182 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
183 strcmp(mnt->mnt_fsname, spec2) == 0) &&
184 (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
185 (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
195 #define PROC_DIR "/proc/"
196 static int mtab_is_proc(const char *mtab)
200 if (readlink(mtab, path, sizeof(path)) < 0)
203 if (strncmp(path, PROC_DIR, strlen(PROC_DIR)))
209 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
210 int flags, int freq, int pass)
216 /* Don't update mtab if it is linked to any file in /proc direcotry.*/
217 if (mtab_is_proc(MOUNTED))
220 mnt.mnt_fsname = spec;
223 mnt.mnt_opts = opts ? opts : "";
225 mnt.mnt_passno = pass;
227 fp = setmntent(MOUNTED, "a+");
229 fprintf(stderr, "%s: setmntent(%s): %s:",
230 progname, MOUNTED, strerror (errno));
233 if ((addmntent(fp, &mnt)) == 1) {
234 fprintf(stderr, "%s: addmntent: %s:",
235 progname, strerror (errno));
244 /* Search for opt in mntlist, returning true if found.
246 static int in_mntlist(char *opt, char *mntlist)
248 char *ml, *mlp, *item, *ctx = NULL;
250 if (!(ml = strdup(mntlist))) {
251 fprintf(stderr, "%s: out of memory\n", progname);
255 while ((item = strtok_r(mlp, ",", &ctx))) {
256 if (!strcmp(opt, item))
261 return (item != NULL);
264 /* Issue a message on stderr for every item in wanted_mountopts that is not
265 * present in mountopts. The justwarn boolean toggles between error and
266 * warning message. Return an error count.
268 int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
271 char *ml, *mlp, *item, *ctx = NULL;
274 if (!(ml = strdup(wanted_mountopts))) {
275 fprintf(stderr, "%s: out of memory\n", progname);
279 while ((item = strtok_r(mlp, ",", &ctx))) {
280 if (!in_mntlist(item, mountopts)) {
281 fprintf(stderr, "%s: %s mount option `%s' is missing\n",
282 progname, justwarn ? "Warning: default"
283 : "Error: mandatory", item);
292 /* Trim embedded white space, leading and trailing commas from string s.
294 void trim_mountfsoptions(char *s)
300 memmove(p, p + 1, strlen(p + 1) + 1);
307 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
309 p = s + strlen(s) - 1;
310 while (p >= s && *p == ',')
314 /* Setup a file in the first unused loop_device */
315 int loop_setup(struct mkfs_opts *mop)
321 /* Figure out the loop device names */
322 if (!access("/dev/loop0", F_OK | R_OK)) {
323 strcpy(loop_base, "/dev/loop\0");
324 } else if (!access("/dev/loop/0", F_OK | R_OK)) {
325 strcpy(loop_base, "/dev/loop/\0");
327 fprintf(stderr, "%s: can't access loop devices\n", progname);
331 /* Find unused loop device */
332 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
334 int cmdsz = sizeof(cmd);
336 sprintf(l_device, "%s%d", loop_base, i);
337 if (access(l_device, F_OK | R_OK))
339 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
342 /* losetup gets 1 (ret=256) for non-set-up device */
344 /* Set up a loopback device to our file */
345 snprintf(cmd, cmdsz, "losetup %s %s", l_device,
347 ret = run_command(cmd, cmdsz);
349 /* someone else picked up this loop device
353 fprintf(stderr, "%s: error %d on losetup: %s\n",
355 ret >= 0 ? strerror(ret) : "");
358 strscpy(mop->mo_loopdev, l_device,
359 sizeof(mop->mo_loopdev));
364 fprintf(stderr, "%s: out of loop devices!\n", progname);
368 int loop_cleanup(struct mkfs_opts *mop)
372 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
373 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
374 ret = run_command(cmd, sizeof(cmd));
379 int loop_format(struct mkfs_opts *mop)
383 if (mop->mo_device_sz == 0) {
385 fprintf(stderr, "loop device requires a --device-size= "
390 fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
393 fprintf(stderr, "%s: Unable to create backing store: %s\n",
394 progname, strerror(errno));
398 if (ftruncate(fd, mop->mo_device_sz * 1024) != 0) {
401 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
402 progname, strerror(errno));
410 /* Write the server config files */
411 int osd_write_ldd(struct mkfs_opts *mop)
413 struct lustre_disk_data *ldd = &mop->mo_ldd;
416 switch (ldd->ldd_mount_type) {
417 #ifdef HAVE_LDISKFS_OSD
419 case LDD_MT_LDISKFS2:
420 ret = ldiskfs_write_ldd(mop);
422 #endif /* HAVE_LDISKFS_OSD */
425 ret = zfs_write_ldd(mop);
427 #endif /* HAVE_ZFS_OSD */
430 fprintf(stderr, "unknown fs type %d '%s'\n",
431 ldd->ldd_mount_type, MT_STR(ldd));
439 /* Read the server config files */
440 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
444 switch (ldd->ldd_mount_type) {
445 #ifdef HAVE_LDISKFS_OSD
447 case LDD_MT_LDISKFS2:
448 ret = ldiskfs_read_ldd(dev, ldd);
450 #endif /* HAVE_LDISKFS_OSD */
453 ret = zfs_read_ldd(dev, ldd);
455 #endif /* HAVE_ZFS_OSD */
458 fprintf(stderr, "unknown fs type %d '%s'\n",
459 ldd->ldd_mount_type, MT_STR(ldd));
467 /* Was this device formatted for Lustre */
468 int osd_is_lustre(char *dev, unsigned *mount_type)
470 vprint("checking for existing Lustre data: ");
472 #ifdef HAVE_LDISKFS_OSD
473 if (ldiskfs_is_lustre(dev, mount_type)) {
477 #endif /* HAVE_LDISKFS_OSD */
479 if (zfs_is_lustre(dev, mount_type)) {
483 #endif /* HAVE_ZFS_OSD */
485 vprint("not found\n");
489 /* Build fs according to type */
490 int osd_make_lustre(struct mkfs_opts *mop)
492 struct lustre_disk_data *ldd = &mop->mo_ldd;
495 switch (ldd->ldd_mount_type) {
496 #ifdef HAVE_LDISKFS_OSD
498 case LDD_MT_LDISKFS2:
499 ret = ldiskfs_make_lustre(mop);
501 #endif /* HAVE_LDISKFS_OSD */
504 ret = zfs_make_lustre(mop);
506 #endif /* HAVE_ZFS_OSD */
509 fprintf(stderr, "unknown fs type %d '%s'\n",
510 ldd->ldd_mount_type, MT_STR(ldd));
518 int osd_prepare_lustre(struct mkfs_opts *mop,
519 char *default_mountopts, int default_len,
520 char *always_mountopts, int always_len)
522 struct lustre_disk_data *ldd = &mop->mo_ldd;
525 switch (ldd->ldd_mount_type) {
526 #ifdef HAVE_LDISKFS_OSD
528 case LDD_MT_LDISKFS2:
529 ret = ldiskfs_prepare_lustre(mop,
530 default_mountopts, default_len,
531 always_mountopts, always_len);
533 #endif /* HAVE_LDISKFS_OSD */
536 ret = zfs_prepare_lustre(mop,
537 default_mountopts, default_len,
538 always_mountopts, always_len);
540 #endif /* HAVE_ZFS_OSD */
543 fprintf(stderr, "unknown fs type %d '%s'\n",
544 ldd->ldd_mount_type, MT_STR(ldd));
552 int osd_tune_lustre(char *dev, struct mount_opts *mop)
554 struct lustre_disk_data *ldd = &mop->mo_ldd;
557 switch (ldd->ldd_mount_type) {
558 #ifdef HAVE_LDISKFS_OSD
560 case LDD_MT_LDISKFS2:
561 ret = ldiskfs_tune_lustre(dev, mop);
563 #endif /* HAVE_LDISKFS_OSD */
566 ret = zfs_tune_lustre(dev, mop);
568 #endif /* HAVE_ZFS_OSD */
571 fprintf(stderr, "unknown fs type %d '%s'\n",
572 ldd->ldd_mount_type, MT_STR(ldd));
580 int osd_label_lustre(struct mount_opts *mop)
582 struct lustre_disk_data *ldd = &mop->mo_ldd;
585 switch (ldd->ldd_mount_type) {
586 #ifdef HAVE_LDISKFS_OSD
588 case LDD_MT_LDISKFS2:
589 ret = ldiskfs_label_lustre(mop);
591 #endif /* HAVE_LDISKFS_OSD */
594 ret = zfs_label_lustre(mop);
596 #endif /* HAVE_ZFS_OSD */
599 fprintf(stderr, "unknown fs type %d '%s'\n",
600 ldd->ldd_mount_type, MT_STR(ldd));
608 /* Enable quota accounting */
609 int osd_enable_quota(struct mkfs_opts *mop)
611 struct lustre_disk_data *ldd = &mop->mo_ldd;
614 switch (ldd->ldd_mount_type) {
615 #ifdef HAVE_LDISKFS_OSD
618 case LDD_MT_LDISKFS2:
619 ret = ldiskfs_enable_quota(mop);
621 #endif /* HAVE_LDISKFS_OSD */
624 fprintf(stderr, "this option is only valid for ldiskfs\n");
627 #endif /* HAVE_ZFS_OSD */
630 fprintf(stderr, "unknown fs type %d '%s'\n",
631 ldd->ldd_mount_type, MT_STR(ldd));
643 #ifdef HAVE_LDISKFS_OSD
644 ret = ldiskfs_init();
647 #endif /* HAVE_LDISKFS_OSD */
650 /* we want to be able to set up a ldiskfs-based filesystem w/o
651 * the ZFS modules installed, see ORI-425 */
654 #endif /* HAVE_ZFS_OSD */
661 #ifdef HAVE_LDISKFS_OSD
663 #endif /* HAVE_LDISKFS_OSD */
666 #endif /* HAVE_ZFS_OSD */
669 __u64 get_device_size(char* device)
674 fd = open(device, O_RDONLY);
676 fprintf(stderr, "%s: cannot open %s: %s\n",
677 progname, device, strerror(errno));
682 /* size in bytes. bz5831 */
683 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
688 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
689 size = (__u64)lsize * 512;
694 fprintf(stderr, "%s: size ioctl failed: %s\n",
695 progname, strerror(errno));
699 vprint("device size = "LPU64"MB\n", size >> 20);
700 /* return value in KB */
704 int file_create(char *path, __u64 size)
711 * Since "size" is in KB, the file offset it represents could overflow
714 size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
715 if (size >= size_max) {
716 fprintf(stderr, "%s: "LPU64" KB: Backing store size must be "
717 "smaller than "LPU64" KB\n", progname, size, size_max);
721 ret = access(path, F_OK);
728 fd = creat(path, S_IRUSR|S_IWUSR);
731 fprintf(stderr, "%s: Unable to create backing store: %s\n",
732 progname, strerror(errno));
736 ret = ftruncate(fd, size * 1024);
740 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
741 progname, strerror(errno));