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.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
39 #endif /* HAVE_CONFIG_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 get_mountdata(char *dev, struct lustre_disk_data *mo_ldd)
106 char tmpdir[] = "/tmp/lustre_tmp.XXXXXX";
112 int cmdsz = sizeof(cmd);
114 /* Make a temporary directory to hold Lustre data files. */
115 if (!mkdtemp(tmpdir)) {
116 verrprint("%s: Can't create temporary directory %s: %s\n",
117 progname, tmpdir, strerror(errno));
121 snprintf(cmd, cmdsz, "%s -c -R 'dump /%s %s/mountdata' %s",
122 DEBUGFS, MOUNT_DATA_FILE, tmpdir, dev);
124 ret = run_command(cmd, cmdsz);
126 verrprint("%s: Unable to dump %s dir (%d)\n",
127 progname, MOUNT_CONFIGS_DIR, ret);
131 sprintf(filepnm, "%s/mountdata", tmpdir);
132 filep = fopen(filepnm, "r");
135 vprint("Reading %s\n", MOUNT_DATA_FILE);
136 num_read = fread(mo_ldd, sizeof(*mo_ldd), 1, filep);
137 if (num_read < 1 && ferror(filep)) {
138 fprintf(stderr, "%s: Unable to read from file (%s): %s\n",
139 progname, filepnm, strerror(errno));
143 verrprint("%s: Unable to read %d.%d config %s.\n",
144 progname, LUSTRE_MAJOR, LUSTRE_MINOR, filepnm);
153 snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
154 ret2 = run_command(cmd, cmdsz);
156 verrprint("Failed to remove temp dir %s (%d)\n", tmpdir, ret2);
157 /* failure return from run_command() is more important
158 * than the failure to remove a dir */
166 #define PARENT_URN "urn:uuid:2bb5bdbf-6c4b-11dc-9b8e-080020a9ed93"
167 #define PARENT_PRODUCT "Lustre"
169 static int stclient(char *type, char *arch)
178 if (strcmp(type, "Client") == 0)
180 else if (strcmp(type, "MDS") == 0)
182 else if (strcmp(type, "MGS") == 0)
184 else if (strcmp(type, "OSS") == 0)
187 snprintf(product, 64, "Lustre %s %d.%d.%d", type, LUSTRE_MAJOR,
188 LUSTRE_MINOR, LUSTRE_PATCH);
190 /* need to see if the entry exists first */
192 "/opt/sun/servicetag/bin/stclient -f -t '%s' ", urn);
193 fp = popen(cmd, "r");
196 fprintf(stderr, "%s: trying to run stclient -f: %s\n",
197 progname, strerror(errno));
201 i = fread(cmd, 1, sizeof(cmd), fp);
204 if (strcmp(cmd, "Record not found\n") != 0) {
205 /* exists, just return */
212 snprintf(cmd, 1024, "/opt/sun/servicetag/bin/stclient -a -p '%s' "
213 "-e %d.%d.%d -t '%s' -S mount -F '%s' -P '%s' -m SUN "
214 "-A %s -z global", product, LUSTRE_MAJOR, LUSTRE_MINOR,
215 LUSTRE_PATCH, urn, PARENT_URN, PARENT_PRODUCT, arch);
217 return(run_command(cmd, sizeof(cmd)));
220 void register_service_tags(char *usource, char *source, char *target)
222 struct lustre_disk_data mo_ldd;
223 struct utsname utsname_buf;
224 struct stat stat_buf;
225 char stclient_loc[] = "/opt/sun/servicetag/bin/stclient";
228 rc = stat(stclient_loc, &stat_buf);
231 if (errno != ENOENT && verbose) {
233 "%s: trying to stat stclient failed: %s\n",
234 progname, strerror(errno));
240 /* call service tags stclient to show Lustre is in use on this system */
241 rc = uname(&utsname_buf);
245 "%s: trying to get uname failed: %s, "
246 "inventory tags will not be created\n",
247 progname, strerror(errno));
251 /* client or server? */
252 if (strchr(usource, ':')) {
253 stclient("Client", utsname_buf.machine);
255 /* first figure what type of device it is */
256 rc = get_mountdata(source, &mo_ldd);
260 "%s: trying to read mountdata from %s "
261 "failed: %s, inventory tags will not "
263 progname, target, strerror(errno));
268 stclient("MDS", utsname_buf.machine);
271 stclient("MGS", utsname_buf.machine);
274 stclient("OSS", utsname_buf.machine);