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>
49 #include <linux/loop.h>
51 extern char *progname;
54 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
55 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
60 fprintf(stderr, "\n%s FATAL: ", progname);
63 int run_command(char *cmd, int cmdsz)
65 char log[] = "/tmp/run_command_logXXXXXX";
68 if ((cmdsz - strlen(cmd)) < 6) {
70 fprintf(stderr, "Command buffer overflow: %.*s...\n",
76 printf("cmd: %s\n", cmd);
78 if ((fd = mkstemp(log)) >= 0) {
86 /* Can't use popen because we need the rv of the command */
88 if (rc && (fd >= 0)) {
93 while (fgets(buf, sizeof(buf), fp) != NULL) {
104 int add_param(char *buf, char *key, char *val)
106 char *sub_val = NULL;
107 int buflen = strlen(buf);
108 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
113 keylen = strlen(key);
116 while ((sub_val = strsep(&val, ",")) != NULL) {
120 if (start + 1 + keylen + strlen(sub_val) >= end) {
121 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
122 progname, buf, key != NULL ? key : "", sub_val);
127 sprintf(buf + start, " %s%s", key != NULL ? key : "", sub_val);
134 int get_param(char *buf, char *key, char **val)
136 int i, key_len = strlen(key);
139 ptr = strstr(buf, key);
141 *val = strdup(ptr + key_len);
145 for (i = 0; i < strlen(*val); i++)
146 if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
156 char *strscat(char *dst, char *src, int buflen)
159 if (strlen(dst) + strlen(src) >= buflen) {
160 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
161 "\n", buflen, dst, src);
164 return strcat(dst, src);
167 char *strscpy(char *dst, char *src, int buflen)
170 return strscat(dst, src, buflen);
173 static int check_losetup(char *device, char *file_path)
175 struct loop_info64 loop_info;
179 fd = open(device, O_RDONLY);
183 rc = ioctl(fd, LOOP_GET_STATUS64, (void *)&loop_info);
188 return strcmp(file_path, (char *)(loop_info.lo_file_name));
191 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
195 char lo_dev[] = "/dev/loop";
196 int lo_dev_len = strlen(lo_dev);
199 fp = setmntent(MOUNTED, "r");
203 while ((mnt = getmntent(fp)) != NULL) {
204 char *fsname = mnt->mnt_fsname;
206 if ((strcmp(fsname, spec1) == 0 ||
207 strcmp(fsname, spec2) == 0) &&
208 (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
209 (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
213 /* Check if the loop device is already mounted */
214 if (strncmp(fsname, lo_dev, lo_dev_len) != 0)
216 if (check_losetup(fsname, spec1) == 0 ||
217 check_losetup(fsname, spec2) == 0) {
227 #define PROC_DIR "/proc/"
228 static int mtab_is_proc(const char *mtab)
232 if (readlink(mtab, path, sizeof(path)) < 0)
235 if (strncmp(path, PROC_DIR, strlen(PROC_DIR)))
241 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
242 int flags, int freq, int pass)
248 /* Don't update mtab if it is linked to any file in /proc direcotry.*/
249 if (mtab_is_proc(MOUNTED))
252 mnt.mnt_fsname = spec;
255 mnt.mnt_opts = opts ? opts : "";
257 mnt.mnt_passno = pass;
259 fp = setmntent(MOUNTED, "a+");
261 fprintf(stderr, "%s: setmntent(%s): %s:",
262 progname, MOUNTED, strerror (errno));
265 if ((addmntent(fp, &mnt)) == 1) {
266 fprintf(stderr, "%s: addmntent: %s:",
267 progname, strerror (errno));
276 /* Search for opt in mntlist, returning true if found.
278 static int in_mntlist(char *opt, char *mntlist)
280 char *ml, *mlp, *item, *ctx = NULL;
282 if (!(ml = strdup(mntlist))) {
283 fprintf(stderr, "%s: out of memory\n", progname);
287 while ((item = strtok_r(mlp, ",", &ctx))) {
288 if (!strcmp(opt, item))
293 return (item != NULL);
296 /* Issue a message on stderr for every item in wanted_mountopts that is not
297 * present in mountopts. The justwarn boolean toggles between error and
298 * warning message. Return an error count.
300 int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
303 char *ml, *mlp, *item, *ctx = NULL;
306 if (!(ml = strdup(wanted_mountopts))) {
307 fprintf(stderr, "%s: out of memory\n", progname);
311 while ((item = strtok_r(mlp, ",", &ctx))) {
312 if (!in_mntlist(item, mountopts)) {
313 fprintf(stderr, "%s: %s mount option `%s' is missing\n",
314 progname, justwarn ? "Warning: default"
315 : "Error: mandatory", item);
324 /* Trim embedded white space, leading and trailing commas from string s.
326 void trim_mountfsoptions(char *s)
332 memmove(p, p + 1, strlen(p + 1) + 1);
339 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
341 p = s + strlen(s) - 1;
342 while (p >= s && *p == ',')
346 /* Setup a file in the first unused loop_device */
347 int loop_setup(struct mkfs_opts *mop)
353 /* Figure out the loop device names */
354 if (!access("/dev/loop0", F_OK | R_OK)) {
355 strcpy(loop_base, "/dev/loop\0");
356 } else if (!access("/dev/loop/0", F_OK | R_OK)) {
357 strcpy(loop_base, "/dev/loop/\0");
359 fprintf(stderr, "%s: can't access loop devices\n", progname);
363 /* Find unused loop device */
364 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
366 int cmdsz = sizeof(cmd);
368 sprintf(l_device, "%s%d", loop_base, i);
369 if (access(l_device, F_OK | R_OK))
371 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
374 /* losetup gets 1 (ret=256) for non-set-up device */
376 /* Set up a loopback device to our file */
377 snprintf(cmd, cmdsz, "losetup %s %s", l_device,
379 ret = run_command(cmd, cmdsz);
381 /* someone else picked up this loop device
385 fprintf(stderr, "%s: error %d on losetup: %s\n",
387 ret >= 0 ? strerror(ret) : "");
390 strscpy(mop->mo_loopdev, l_device,
391 sizeof(mop->mo_loopdev));
396 fprintf(stderr, "%s: out of loop devices!\n", progname);
400 int loop_cleanup(struct mkfs_opts *mop)
404 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
405 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
406 ret = run_command(cmd, sizeof(cmd));
411 int loop_format(struct mkfs_opts *mop)
415 if (mop->mo_device_sz == 0) {
417 fprintf(stderr, "loop device requires a --device-size= "
422 fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
425 fprintf(stderr, "%s: Unable to create backing store: %s\n",
426 progname, strerror(errno));
430 if (ftruncate(fd, mop->mo_device_sz * 1024) != 0) {
433 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
434 progname, strerror(errno));
442 /* Write the server config files */
443 int osd_write_ldd(struct mkfs_opts *mop)
445 struct lustre_disk_data *ldd = &mop->mo_ldd;
448 switch (ldd->ldd_mount_type) {
449 #ifdef HAVE_LDISKFS_OSD
451 case LDD_MT_LDISKFS2:
452 ret = ldiskfs_write_ldd(mop);
454 #endif /* HAVE_LDISKFS_OSD */
457 ret = zfs_write_ldd(mop);
459 #endif /* HAVE_ZFS_OSD */
462 fprintf(stderr, "unknown fs type %d '%s'\n",
463 ldd->ldd_mount_type, MT_STR(ldd));
471 /* Read the server config files */
472 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
476 switch (ldd->ldd_mount_type) {
477 #ifdef HAVE_LDISKFS_OSD
479 case LDD_MT_LDISKFS2:
480 ret = ldiskfs_read_ldd(dev, ldd);
482 #endif /* HAVE_LDISKFS_OSD */
485 ret = zfs_read_ldd(dev, ldd);
487 #endif /* HAVE_ZFS_OSD */
490 fprintf(stderr, "unknown fs type %d '%s'\n",
491 ldd->ldd_mount_type, MT_STR(ldd));
499 /* Was this device formatted for Lustre */
500 int osd_is_lustre(char *dev, unsigned *mount_type)
502 vprint("checking for existing Lustre data: ");
504 #ifdef HAVE_LDISKFS_OSD
505 if (ldiskfs_is_lustre(dev, mount_type)) {
509 #endif /* HAVE_LDISKFS_OSD */
511 if (zfs_is_lustre(dev, mount_type)) {
515 #endif /* HAVE_ZFS_OSD */
517 vprint("not found\n");
521 /* Build fs according to type */
522 int osd_make_lustre(struct mkfs_opts *mop)
524 struct lustre_disk_data *ldd = &mop->mo_ldd;
527 switch (ldd->ldd_mount_type) {
528 #ifdef HAVE_LDISKFS_OSD
530 case LDD_MT_LDISKFS2:
531 ret = ldiskfs_make_lustre(mop);
533 #endif /* HAVE_LDISKFS_OSD */
536 ret = zfs_make_lustre(mop);
538 #endif /* HAVE_ZFS_OSD */
541 fprintf(stderr, "unknown fs type %d '%s'\n",
542 ldd->ldd_mount_type, MT_STR(ldd));
550 int osd_prepare_lustre(struct mkfs_opts *mop,
551 char *default_mountopts, int default_len,
552 char *always_mountopts, int always_len)
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_prepare_lustre(mop,
562 default_mountopts, default_len,
563 always_mountopts, always_len);
565 #endif /* HAVE_LDISKFS_OSD */
568 ret = zfs_prepare_lustre(mop,
569 default_mountopts, default_len,
570 always_mountopts, always_len);
572 #endif /* HAVE_ZFS_OSD */
575 fprintf(stderr, "unknown fs type %d '%s'\n",
576 ldd->ldd_mount_type, MT_STR(ldd));
584 int osd_tune_lustre(char *dev, struct mount_opts *mop)
586 struct lustre_disk_data *ldd = &mop->mo_ldd;
589 switch (ldd->ldd_mount_type) {
590 #ifdef HAVE_LDISKFS_OSD
592 case LDD_MT_LDISKFS2:
593 ret = ldiskfs_tune_lustre(dev, mop);
595 #endif /* HAVE_LDISKFS_OSD */
598 ret = zfs_tune_lustre(dev, mop);
600 #endif /* HAVE_ZFS_OSD */
603 fprintf(stderr, "unknown fs type %d '%s'\n",
604 ldd->ldd_mount_type, MT_STR(ldd));
612 int osd_label_lustre(struct mount_opts *mop)
614 struct lustre_disk_data *ldd = &mop->mo_ldd;
617 switch (ldd->ldd_mount_type) {
618 #ifdef HAVE_LDISKFS_OSD
620 case LDD_MT_LDISKFS2:
621 ret = ldiskfs_label_lustre(mop);
623 #endif /* HAVE_LDISKFS_OSD */
626 ret = zfs_label_lustre(mop);
628 #endif /* HAVE_ZFS_OSD */
631 fprintf(stderr, "unknown fs type %d '%s'\n",
632 ldd->ldd_mount_type, MT_STR(ldd));
640 /* Enable quota accounting */
641 int osd_enable_quota(struct mkfs_opts *mop)
643 struct lustre_disk_data *ldd = &mop->mo_ldd;
646 switch (ldd->ldd_mount_type) {
647 #ifdef HAVE_LDISKFS_OSD
650 case LDD_MT_LDISKFS2:
651 ret = ldiskfs_enable_quota(mop);
653 #endif /* HAVE_LDISKFS_OSD */
656 fprintf(stderr, "this option is only valid for ldiskfs\n");
659 #endif /* HAVE_ZFS_OSD */
662 fprintf(stderr, "unknown fs type %d '%s'\n",
663 ldd->ldd_mount_type, MT_STR(ldd));
675 #ifdef HAVE_LDISKFS_OSD
676 ret = ldiskfs_init();
679 #endif /* HAVE_LDISKFS_OSD */
682 /* we want to be able to set up a ldiskfs-based filesystem w/o
683 * the ZFS modules installed, see ORI-425 */
686 #endif /* HAVE_ZFS_OSD */
693 #ifdef HAVE_LDISKFS_OSD
695 #endif /* HAVE_LDISKFS_OSD */
698 #endif /* HAVE_ZFS_OSD */
701 __u64 get_device_size(char* device)
706 fd = open(device, O_RDONLY);
708 fprintf(stderr, "%s: cannot open %s: %s\n",
709 progname, device, strerror(errno));
714 /* size in bytes. bz5831 */
715 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
720 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
721 size = (__u64)lsize * 512;
726 fprintf(stderr, "%s: size ioctl failed: %s\n",
727 progname, strerror(errno));
731 vprint("device size = "LPU64"MB\n", size >> 20);
732 /* return value in KB */
736 int file_create(char *path, __u64 size)
743 * Since "size" is in KB, the file offset it represents could overflow
746 size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
747 if (size >= size_max) {
748 fprintf(stderr, "%s: "LPU64" KB: Backing store size must be "
749 "smaller than "LPU64" KB\n", progname, size, size_max);
753 ret = access(path, F_OK);
760 fd = creat(path, S_IRUSR|S_IWUSR);
763 fprintf(stderr, "%s: Unable to create backing store: %s\n",
764 progname, strerror(errno));
768 ret = ftruncate(fd, size * 1024);
772 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
773 progname, strerror(errno));
781 * Try to get the real path to the device, in case it is a
782 * symbolic link or dm device for instance
784 int get_realpath(char *path, char **device)
790 char real_path[PATH_MAX] = {'\0'};
792 if (realpath(path, real_path) == NULL)
795 ptr = strrchr(real_path, '/');
796 if (ptr && strncmp(ptr, "/dm-", 4) == 0 && isdigit(*(ptr + 4))) {
797 snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptr+1);
798 f = fopen(path, "r");
800 /* read "<name>\n" from sysfs */
801 if (fgets(name, sizeof(name), f) != NULL) {
805 snprintf(real_path, sizeof(real_path),
806 "/dev/mapper/%s", name);
812 *device = strdup(real_path);