1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
31 * Copyright (c) 2012 Whamcloud, Inc.
34 * This file is part of Lustre, http://www.lustre.org/
35 * Lustre is a trademark of Sun Microsystems, Inc.
40 #endif /* HAVE_CONFIG_H */
46 #include <lustre_disk.h>
47 #include <lustre_ver.h>
49 #include <sys/utsname.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 get_mountdata(char *dev, struct lustre_disk_data *mo_ldd)
107 char tmpdir[] = "/tmp/lustre_tmp.XXXXXX";
113 int cmdsz = sizeof(cmd);
115 /* Make a temporary directory to hold Lustre data files. */
116 if (!mkdtemp(tmpdir)) {
117 verrprint("%s: Can't create temporary directory %s: %s\n",
118 progname, tmpdir, strerror(errno));
122 snprintf(cmd, cmdsz, "%s -c -R 'dump /%s %s/mountdata' %s",
123 DEBUGFS, MOUNT_DATA_FILE, tmpdir, dev);
125 ret = run_command(cmd, cmdsz);
127 verrprint("%s: Unable to dump %s dir (%d)\n",
128 progname, MOUNT_CONFIGS_DIR, ret);
132 sprintf(filepnm, "%s/mountdata", tmpdir);
133 filep = fopen(filepnm, "r");
136 vprint("Reading %s\n", MOUNT_DATA_FILE);
137 num_read = fread(mo_ldd, sizeof(*mo_ldd), 1, filep);
138 if (num_read < 1 && ferror(filep)) {
139 fprintf(stderr, "%s: Unable to read from file (%s): %s\n",
140 progname, filepnm, strerror(errno));
144 verrprint("%s: Unable to read %d.%d config %s.\n",
145 progname, LUSTRE_MAJOR, LUSTRE_MINOR, filepnm);
154 snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
155 ret2 = run_command(cmd, cmdsz);
157 verrprint("Failed to remove temp dir %s (%d)\n", tmpdir, ret2);
158 /* failure return from run_command() is more important
159 * than the failure to remove a dir */