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 int end = sizeof(((struct lustre_disk_data *)0)->ldd_params);
107 int start = strlen(buf);
111 keylen = strlen(key);
112 if (start + 1 + keylen + strlen(val) >= end) {
113 fprintf(stderr, "%s: params are too long-\n%s %s%s\n",
114 progname, buf, key ? key : "", val);
118 sprintf(buf + start, " %s%s", key ? key : "", val);
122 int get_param(char *buf, char *key, char **val)
124 int i, key_len = strlen(key);
127 ptr = strstr(buf, key);
129 *val = strdup(ptr + key_len);
133 for (i = 0; i < strlen(*val); i++)
134 if (((*val)[i] == ' ') || ((*val)[i] == '\0'))
144 char *strscat(char *dst, char *src, int buflen)
147 if (strlen(dst) + strlen(src) >= buflen) {
148 fprintf(stderr, "string buffer overflow (max %d): '%s' + '%s'"
149 "\n", buflen, dst, src);
152 return strcat(dst, src);
155 char *strscpy(char *dst, char *src, int buflen)
158 return strscat(dst, src, buflen);
161 int check_mtab_entry(char *spec1, char *spec2, char *mtpt, char *type)
166 fp = setmntent(MOUNTED, "r");
170 while ((mnt = getmntent(fp)) != NULL) {
171 if ((strcmp(mnt->mnt_fsname, spec1) == 0 ||
172 strcmp(mnt->mnt_fsname, spec2) == 0) &&
173 (mtpt == NULL || strcmp(mnt->mnt_dir, mtpt) == 0) &&
174 (type == NULL || strcmp(mnt->mnt_type, type) == 0)) {
184 #define PROC_DIR "/proc/"
185 static int mtab_is_proc(const char *mtab)
189 if (readlink(mtab, path, sizeof(path)) < 0)
192 if (strncmp(path, PROC_DIR, strlen(PROC_DIR)))
198 int update_mtab_entry(char *spec, char *mtpt, char *type, char *opts,
199 int flags, int freq, int pass)
205 /* Don't update mtab if it is linked to any file in /proc direcotry.*/
206 if (mtab_is_proc(MOUNTED))
209 mnt.mnt_fsname = spec;
212 mnt.mnt_opts = opts ? opts : "";
214 mnt.mnt_passno = pass;
216 fp = setmntent(MOUNTED, "a+");
218 fprintf(stderr, "%s: setmntent(%s): %s:",
219 progname, MOUNTED, strerror (errno));
222 if ((addmntent(fp, &mnt)) == 1) {
223 fprintf(stderr, "%s: addmntent: %s:",
224 progname, strerror (errno));
233 /* Search for opt in mntlist, returning true if found.
235 static int in_mntlist(char *opt, char *mntlist)
237 char *ml, *mlp, *item, *ctx = NULL;
239 if (!(ml = strdup(mntlist))) {
240 fprintf(stderr, "%s: out of memory\n", progname);
244 while ((item = strtok_r(mlp, ",", &ctx))) {
245 if (!strcmp(opt, item))
250 return (item != NULL);
253 /* Issue a message on stderr for every item in wanted_mountopts that is not
254 * present in mountopts. The justwarn boolean toggles between error and
255 * warning message. Return an error count.
257 int check_mountfsoptions(char *mountopts, char *wanted_mountopts,
260 char *ml, *mlp, *item, *ctx = NULL;
263 if (!(ml = strdup(wanted_mountopts))) {
264 fprintf(stderr, "%s: out of memory\n", progname);
268 while ((item = strtok_r(mlp, ",", &ctx))) {
269 if (!in_mntlist(item, mountopts)) {
270 fprintf(stderr, "%s: %s mount option `%s' is missing\n",
271 progname, justwarn ? "Warning: default"
272 : "Error: mandatory", item);
281 /* Trim embedded white space, leading and trailing commas from string s.
283 void trim_mountfsoptions(char *s)
289 memmove(p, p + 1, strlen(p + 1) + 1);
296 memmove(&s[0], &s[1], strlen(&s[1]) + 1);
298 p = s + strlen(s) - 1;
299 while (p >= s && *p == ',')
303 /* Setup a file in the first unused loop_device */
304 int loop_setup(struct mkfs_opts *mop)
310 /* Figure out the loop device names */
311 if (!access("/dev/loop0", F_OK | R_OK) ||
312 !access("/dev/loop-control", F_OK | R_OK)) {
313 strcpy(loop_base, "/dev/loop\0");
314 } else if (!access("/dev/loop/0", F_OK | R_OK)) {
315 strcpy(loop_base, "/dev/loop/\0");
317 fprintf(stderr, "%s: can't access loop devices\n", progname);
321 /* Find unused loop device */
322 for (i = 0; i < MAX_LOOP_DEVICES; i++) {
324 int cmdsz = sizeof(cmd);
326 #ifdef LOOP_CTL_GET_FREE
327 ret = open("/dev/loop-control", O_RDWR);
329 fprintf(stderr, "%s: can't access loop control\n", progname);
332 /* find or allocate a free loop device to use */
333 i = ioctl(ret, LOOP_CTL_GET_FREE);
335 fprintf(stderr, "%s: access loop control error\n", progname);
338 sprintf(l_device, "%s%d", loop_base, i);
340 sprintf(l_device, "%s%d", loop_base, i);
341 if (access(l_device, F_OK | R_OK))
344 snprintf(cmd, cmdsz, "losetup %s > /dev/null 2>&1", l_device);
347 /* losetup gets 1 (ret=256) for non-set-up device */
349 /* Set up a loopback device to our file */
350 snprintf(cmd, cmdsz, "losetup %s %s", l_device,
352 ret = run_command(cmd, cmdsz);
354 /* someone else picked up this loop device
358 fprintf(stderr, "%s: error %d on losetup: %s\n",
360 ret >= 0 ? strerror(ret) : "");
363 strscpy(mop->mo_loopdev, l_device,
364 sizeof(mop->mo_loopdev));
369 fprintf(stderr, "%s: out of loop devices!\n", progname);
373 int loop_cleanup(struct mkfs_opts *mop)
378 if ((mop->mo_flags & MO_IS_LOOP) && *mop->mo_loopdev) {
381 sprintf(cmd, "losetup -d %s", mop->mo_loopdev);
382 for (tries = 0; tries < 3; tries++) {
383 ret = run_command(cmd, sizeof(cmd));
391 fprintf(stderr, "cannot cleanup %s: rc = %d\n",
392 mop->mo_loopdev, ret);
396 int loop_format(struct mkfs_opts *mop)
400 if (mop->mo_device_kb == 0) {
402 fprintf(stderr, "loop device requires a --device-size= "
407 fd = creat(mop->mo_device, S_IRUSR|S_IWUSR);
410 fprintf(stderr, "%s: Unable to create backing store: %s\n",
411 progname, strerror(errno));
415 if (ftruncate(fd, mop->mo_device_kb * 1024) != 0) {
418 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
419 progname, strerror(errno));
427 /* Write the server config files */
428 int osd_write_ldd(struct mkfs_opts *mop)
430 struct lustre_disk_data *ldd = &mop->mo_ldd;
433 switch (ldd->ldd_mount_type) {
434 #ifdef HAVE_LDISKFS_OSD
436 case LDD_MT_LDISKFS2:
437 ret = ldiskfs_write_ldd(mop);
439 #endif /* HAVE_LDISKFS_OSD */
442 ret = zfs_write_ldd(mop);
444 #endif /* HAVE_ZFS_OSD */
447 fprintf(stderr, "unknown fs type %d '%s'\n",
448 ldd->ldd_mount_type, MT_STR(ldd));
456 /* Read the server config files */
457 int osd_read_ldd(char *dev, struct lustre_disk_data *ldd)
461 switch (ldd->ldd_mount_type) {
462 #ifdef HAVE_LDISKFS_OSD
464 case LDD_MT_LDISKFS2:
465 ret = ldiskfs_read_ldd(dev, ldd);
467 #endif /* HAVE_LDISKFS_OSD */
470 ret = zfs_read_ldd(dev, ldd);
472 #endif /* HAVE_ZFS_OSD */
475 fprintf(stderr, "unknown fs type %d '%s'\n",
476 ldd->ldd_mount_type, MT_STR(ldd));
484 /* Was this device formatted for Lustre */
485 int osd_is_lustre(char *dev, unsigned *mount_type)
487 vprint("checking for existing Lustre data: ");
489 #ifdef HAVE_LDISKFS_OSD
490 if (ldiskfs_is_lustre(dev, mount_type)) {
494 #endif /* HAVE_LDISKFS_OSD */
496 if (zfs_is_lustre(dev, mount_type)) {
500 #endif /* HAVE_ZFS_OSD */
502 vprint("not found\n");
506 /* Build fs according to type */
507 int osd_make_lustre(struct mkfs_opts *mop)
509 struct lustre_disk_data *ldd = &mop->mo_ldd;
512 switch (ldd->ldd_mount_type) {
513 #ifdef HAVE_LDISKFS_OSD
515 case LDD_MT_LDISKFS2:
516 ret = ldiskfs_make_lustre(mop);
518 #endif /* HAVE_LDISKFS_OSD */
521 ret = zfs_make_lustre(mop);
523 #endif /* HAVE_ZFS_OSD */
526 fprintf(stderr, "unknown fs type %d '%s'\n",
527 ldd->ldd_mount_type, MT_STR(ldd));
535 int osd_prepare_lustre(struct mkfs_opts *mop,
536 char *default_mountopts, int default_len,
537 char *always_mountopts, int always_len)
539 struct lustre_disk_data *ldd = &mop->mo_ldd;
542 switch (ldd->ldd_mount_type) {
543 #ifdef HAVE_LDISKFS_OSD
545 case LDD_MT_LDISKFS2:
546 ret = ldiskfs_prepare_lustre(mop,
547 default_mountopts, default_len,
548 always_mountopts, always_len);
550 #endif /* HAVE_LDISKFS_OSD */
553 ret = zfs_prepare_lustre(mop,
554 default_mountopts, default_len,
555 always_mountopts, always_len);
557 #endif /* HAVE_ZFS_OSD */
560 fprintf(stderr, "unknown fs type %d '%s'\n",
561 ldd->ldd_mount_type, MT_STR(ldd));
569 int osd_tune_lustre(char *dev, struct mount_opts *mop)
571 struct lustre_disk_data *ldd = &mop->mo_ldd;
574 switch (ldd->ldd_mount_type) {
575 #ifdef HAVE_LDISKFS_OSD
577 case LDD_MT_LDISKFS2:
578 ret = ldiskfs_tune_lustre(dev, mop);
580 #endif /* HAVE_LDISKFS_OSD */
583 ret = zfs_tune_lustre(dev, mop);
585 #endif /* HAVE_ZFS_OSD */
588 fprintf(stderr, "unknown fs type %d '%s'\n",
589 ldd->ldd_mount_type, MT_STR(ldd));
597 int osd_label_lustre(struct mount_opts *mop)
599 struct lustre_disk_data *ldd = &mop->mo_ldd;
602 switch (ldd->ldd_mount_type) {
603 #ifdef HAVE_LDISKFS_OSD
605 case LDD_MT_LDISKFS2:
606 ret = ldiskfs_label_lustre(mop);
608 #endif /* HAVE_LDISKFS_OSD */
611 ret = zfs_label_lustre(mop);
613 #endif /* HAVE_ZFS_OSD */
616 fprintf(stderr, "unknown fs type %d '%s'\n",
617 ldd->ldd_mount_type, MT_STR(ldd));
625 /* Enable quota accounting */
626 int osd_enable_quota(struct mkfs_opts *mop)
628 struct lustre_disk_data *ldd = &mop->mo_ldd;
631 switch (ldd->ldd_mount_type) {
632 #ifdef HAVE_LDISKFS_OSD
635 case LDD_MT_LDISKFS2:
636 ret = ldiskfs_enable_quota(mop);
638 #endif /* HAVE_LDISKFS_OSD */
641 fprintf(stderr, "this option is only valid for ldiskfs\n");
644 #endif /* HAVE_ZFS_OSD */
647 fprintf(stderr, "unknown fs type %d '%s'\n",
648 ldd->ldd_mount_type, MT_STR(ldd));
660 #ifdef HAVE_LDISKFS_OSD
661 ret = ldiskfs_init();
664 #endif /* HAVE_LDISKFS_OSD */
667 /* we want to be able to set up a ldiskfs-based filesystem w/o
668 * the ZFS modules installed, see ORI-425 */
671 #endif /* HAVE_ZFS_OSD */
678 #ifdef HAVE_LDISKFS_OSD
680 #endif /* HAVE_LDISKFS_OSD */
683 #endif /* HAVE_ZFS_OSD */
686 __u64 get_device_size(char* device)
691 fd = open(device, O_RDONLY);
693 fprintf(stderr, "%s: cannot open %s: %s\n",
694 progname, device, strerror(errno));
699 /* size in bytes. bz5831 */
700 ret = ioctl(fd, BLKGETSIZE64, (void*)&size);
705 ret = ioctl(fd, BLKGETSIZE, (void*)&lsize);
706 size = (__u64)lsize * 512;
711 fprintf(stderr, "%s: size ioctl failed: %s\n",
712 progname, strerror(errno));
716 vprint("device size = "LPU64"MB\n", size >> 20);
717 /* return value in KB */
721 int file_create(char *path, __u64 size)
728 * Since "size" is in KB, the file offset it represents could overflow
731 size_max = (off_t)1 << (_FILE_OFFSET_BITS - 1 - 10);
732 if (size >= size_max) {
733 fprintf(stderr, "%s: "LPU64" KB: Backing store size must be "
734 "smaller than "LPU64" KB\n", progname, size, size_max);
738 ret = access(path, F_OK);
745 fd = creat(path, S_IRUSR|S_IWUSR);
748 fprintf(stderr, "%s: Unable to create backing store: %s\n",
749 progname, strerror(errno));
753 ret = ftruncate(fd, size * 1024);
757 fprintf(stderr, "%s: Unable to truncate backing store: %s\n",
758 progname, strerror(errno));