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 Whamcloud, Inc.
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 */
44 #include <lustre_disk.h>
45 #include <lustre_ver.h>
47 #include <sys/utsname.h>
49 extern char *progname;
52 #define vprint(fmt, arg...) if (verbose > 0) printf(fmt, ##arg)
53 #define verrprint(fmt, arg...) if (verbose >= 0) fprintf(stderr, fmt, ##arg)
58 fprintf(stderr, "\n%s FATAL: ", progname);
61 int run_command(char *cmd, int cmdsz)
63 char log[] = "/tmp/run_command_logXXXXXX";
66 if ((cmdsz - strlen(cmd)) < 6) {
68 fprintf(stderr, "Command buffer overflow: %.*s...\n",
74 printf("cmd: %s\n", cmd);
76 if ((fd = mkstemp(log)) >= 0) {
84 /* Can't use popen because we need the rv of the command */
86 if (rc && (fd >= 0)) {
91 while (fgets(buf, sizeof(buf), fp) != NULL) {
102 int get_mountdata(char *dev, struct lustre_disk_data *mo_ldd)
105 char tmpdir[] = "/tmp/lustre_tmp.XXXXXX";
111 int cmdsz = sizeof(cmd);
113 /* Make a temporary directory to hold Lustre data files. */
114 if (!mkdtemp(tmpdir)) {
115 verrprint("%s: Can't create temporary directory %s: %s\n",
116 progname, tmpdir, strerror(errno));
120 snprintf(cmd, cmdsz, "%s -c -R 'dump /%s %s/mountdata' %s",
121 DEBUGFS, MOUNT_DATA_FILE, tmpdir, dev);
123 ret = run_command(cmd, cmdsz);
125 verrprint("%s: Unable to dump %s dir (%d)\n",
126 progname, MOUNT_CONFIGS_DIR, ret);
130 sprintf(filepnm, "%s/mountdata", tmpdir);
131 filep = fopen(filepnm, "r");
134 vprint("Reading %s\n", MOUNT_DATA_FILE);
135 num_read = fread(mo_ldd, sizeof(*mo_ldd), 1, filep);
136 if (num_read < 1 && ferror(filep)) {
137 fprintf(stderr, "%s: Unable to read from file (%s): %s\n",
138 progname, filepnm, strerror(errno));
142 verrprint("%s: Unable to read %d.%d config %s.\n",
143 progname, LUSTRE_MAJOR, LUSTRE_MINOR, filepnm);
152 snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
153 ret2 = run_command(cmd, cmdsz);
155 verrprint("Failed to remove temp dir %s (%d)\n", tmpdir, ret2);
156 /* failure return from run_command() is more important
157 * than the failure to remove a dir */