Whamcloud - gitweb
b=15625
[fs/lustre-release.git] / lustre / utils / mount_utils.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <string.h>
4 #include <config.h>
5 #include <lustre_disk.h>
6 #include <lustre_ver.h>
7 #include <sys/stat.h>
8 #include <sys/utsname.h>
9
10 extern char *progname;
11 extern int verbose;
12
13 #define vprint if (verbose > 0) printf
14 #define verrprint if (verbose >= 0) printf
15
16 void fatal(void)
17 {
18         verbose = 0;
19         fprintf(stderr, "\n%s FATAL: ", progname);
20 }
21
22 int run_command(char *cmd, int cmdsz)
23 {
24         char log[] = "/tmp/run_command_logXXXXXX";
25         int fd = -1, rc;
26
27         if ((cmdsz - strlen(cmd)) < 6) {
28                 fatal();
29                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
30                         cmdsz, cmd);
31                 return ENOMEM;
32         }
33
34         if (verbose > 1) {
35                 printf("cmd: %s\n", cmd);
36         } else {
37                 if ((fd = mkstemp(log)) >= 0) {
38                         close(fd);
39                         strcat(cmd, " >");
40                         strcat(cmd, log);
41                 }
42         }
43         strcat(cmd, " 2>&1");
44
45         /* Can't use popen because we need the rv of the command */
46         rc = system(cmd);
47         if (rc && (fd >= 0)) {
48                 char buf[128];
49                 FILE *fp;
50                 fp = fopen(log, "r");
51                 if (fp) {
52                         while (fgets(buf, sizeof(buf), fp) != NULL) {
53                                 printf("   %s", buf);
54                         }
55                         fclose(fp);
56                 }
57         }
58         if (fd >= 0)
59                 remove(log);
60         return rc;
61 }
62
63 int get_mountdata(char *dev, struct lustre_disk_data *mo_ldd)
64 {
65
66         char tmpdir[] = "/tmp/lustre_tmp.XXXXXX";
67         char cmd[256];
68         char filepnm[128];
69         FILE *filep;
70         int ret = 0;
71         int ret2 = 0;
72         int cmdsz = sizeof(cmd);
73
74         /* Make a temporary directory to hold Lustre data files. */
75         if (!mkdtemp(tmpdir)) {
76                 verrprint(stderr, "%s: Can't create temporary "
77                         "directory %s: %s\n",
78                         progname, tmpdir, strerror(errno));
79                 return errno;
80         }
81
82         snprintf(cmd, cmdsz, "/sbin/debugfs -c -R 'dump /%s %s/mountdata' %s",
83                  MOUNT_DATA_FILE, tmpdir, dev);
84
85         ret = run_command(cmd, cmdsz);
86         if (ret) {
87                 verrprint("%s: Unable to dump %s dir (%d)\n",
88                           progname, MOUNT_CONFIGS_DIR, ret);
89                 goto out_rmdir;
90         }
91
92         sprintf(filepnm, "%s/mountdata", tmpdir);
93         filep = fopen(filepnm, "r");
94         if (filep) {
95                 vprint("Reading %s\n", MOUNT_DATA_FILE);
96                 fread(mo_ldd, sizeof(*mo_ldd), 1, filep);
97         } else {
98                 verrprint("%s: Unable to read %d.%d config %s.\n",
99                           progname, LUSTRE_MAJOR, LUSTRE_MINOR, filepnm);
100                 goto out_close;
101         }       
102
103 out_close:
104         fclose(filep);
105
106 out_rmdir:
107         snprintf(cmd, cmdsz, "rm -rf %s", tmpdir);
108         ret2 = run_command(cmd, cmdsz);
109         if (ret2) {
110                 verrprint("Failed to remove temp dir %s (%d)\n", tmpdir, ret2);
111                 /* failure return from run_command() is more important
112                  * than the failure to remove a dir */
113                 if (!ret)
114                         ret = ret2;
115         }
116
117         return ret;
118 }
119
120 #define PARENT_URN "urn:uuid:2bb5bdbf-6c4b-11dc-9b8e-080020a9ed93"
121 #define PARENT_PRODUCT "Lustre"
122
123 static int stclient(char *type, char *arch)
124 {
125
126         char product[64];
127         char *urn = NULL;
128         char cmd[1024];
129         FILE *fp;
130         int i;
131
132         if (strcmp(type, "Client") == 0)
133                 urn = CLIENT_URN;
134         else if (strcmp(type, "MDS") == 0)
135                 urn = MDS_URN;
136         else if (strcmp(type, "MGS") == 0)
137                 urn = MGS_URN;
138         else if (strcmp(type, "OSS") == 0)
139                 urn = OSS_URN;
140
141         snprintf(product, 64, "Lustre %s %d.%d.%d", type, LUSTRE_MAJOR,
142                  LUSTRE_MINOR, LUSTRE_PATCH); 
143
144         /* need to see if the entry exists first */
145         snprintf(cmd, 1024,
146                  "/opt/sun/servicetag/bin/stclient -f -t '%s' ", urn);
147         fp = popen(cmd, "r");
148         if (!fp) {
149                 if (verbose)
150                         fprintf(stderr, "%s: trying to run stclient -f: %s\n",
151                                 progname, strerror(errno));
152                 return 0;
153         }
154
155         i = fread(cmd, 1, sizeof(cmd), fp);
156         if (i) {
157                 cmd[i] = 0;
158                 if (strcmp(cmd, "Record not found\n") != 0) {
159                         /* exists, just return */
160                         pclose(fp);
161                         return 0;
162                 }
163         }
164         pclose(fp);
165
166         snprintf(cmd, 1024, "/opt/sun/servicetag/bin/stclient -a -p '%s' "
167                "-e %d.%d.%d -t '%s' -S mount -F '%s' -P '%s' -m SUN "
168                "-A %s -z global", product, LUSTRE_MAJOR, LUSTRE_MINOR,
169                LUSTRE_PATCH, urn, PARENT_URN, PARENT_PRODUCT, arch);
170
171         return(run_command(cmd, sizeof(cmd)));
172 }
173
174 void register_service_tags(char *usource, char *source, char *target)
175 {
176         struct lustre_disk_data mo_ldd;
177         struct utsname utsname_buf;
178         struct stat stat_buf;
179         char stclient_loc[] = "/opt/sun/servicetag/bin/stclient";
180         int rc;
181
182         rc = stat(stclient_loc, &stat_buf);
183
184         if (rc == 0) {
185                 /* call the service tags stclient to show that we use Lustre on
186                    this system */
187
188                 rc = uname(&utsname_buf);
189                 if (rc) {
190                         if (verbose)
191                                 fprintf(stderr,
192                                         "%s: trying to get uname failed: %s, "
193                                         "inventory tags will not be created\n",
194                                         progname, strerror(errno));
195                 } else {
196
197                         /* client or server? */
198                         if (strchr(usource, ':')) {
199                                 stclient("Client", utsname_buf.machine);
200                         } else {
201                                 /* first figure what type of device it is */
202                                 rc = get_mountdata(source, &mo_ldd);
203                                 if (rc) {
204                                         if (verbose)
205                                                 fprintf(stderr,
206                                                         "%s: trying to read mountdata from %s "
207                                                         "failed: %s, inventory tags will not "
208                                                         "be created\n",
209                                                         progname, target, strerror(errno));
210                                 } else {
211
212                                         if (IS_MDT(&mo_ldd))
213                                                 stclient("MDS", utsname_buf.machine);
214
215                                         if (IS_MGS(&mo_ldd))
216                                                 stclient("MGS", utsname_buf.machine);
217
218                                         if (IS_OST(&mo_ldd))
219                                                 stclient("OSS", utsname_buf.machine);
220                                 }
221                         }
222                 }
223         } else {
224                 if (errno != ENOENT && verbose) {
225                         fprintf(stderr,
226                                 "%s: trying to stat stclient failed: %s\n",
227                                 progname, strerror(errno));
228                 }
229         }
230 }