1 /*	$NetBSD: wr_fstab.c,v 1.1.1.2 2009/03/20 20:26:55 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-2009 Erez Zadok
5  * Copyright (c) 1989 Jan-Simon Pendry
6  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
7  * Copyright (c) 1989 The Regents of the University of California.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to Berkeley by
11  * Jan-Simon Pendry at Imperial College, London.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgment:
23  *      This product includes software developed by the University of
24  *      California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *
42  * File: am-utils/fsinfo/wr_fstab.c
43  *
44  */
45 
46 #ifdef HAVE_CONFIG_H
47 # include <config.h>
48 #endif /* HAVE_CONFIG_H */
49 #include <am_defs.h>
50 #include <fsi_data.h>
51 #include <fsinfo.h>
52 
53 #define	GENERIC_OS_NAME "generic"
54 
55 /* forward definitions */
56 static void write_aix1_dkfstab(FILE *ef, disk_fs *dp);
57 static void write_aix1_dkrmount(FILE *ef, char *hn, fsmount *fp);
58 static void write_aix3_dkfstab(FILE *ef, disk_fs *dp);
59 static void write_aix3_dkrmount(FILE *ef, char *hn, fsmount *fp);
60 static int  write_dkfstab(FILE *ef, qelem *q, void (*output) (FILE *, disk_fs *));
61 static int write_dkrmount(FILE *ef, qelem *q, char *hn, void (*output) (FILE *, char *, fsmount *));
62 static void write_generic_dkfstab(FILE *ef, disk_fs *dp);
63 static void write_generic_dkrmount(FILE *ef, char *hn, fsmount *fp);
64 static void write_ultrix_dkfstab(FILE *ef, disk_fs *dp);
65 static void write_ultrix_dkrmount(FILE *ef, char *hn, fsmount *fp);
66 
67 /* ----------------------------------------------- */
68 
69 static struct os_fstab_type {
70   char *os_name;
71   void (*op_fstab) (FILE *ef, disk_fs *dp);
72   void (*op_mount) (FILE *ef, char *hn, fsmount *fp);
73 } os_tabs[] = {
74 
75   {
76     "aix1", write_aix1_dkfstab, write_aix1_dkrmount
77   },				/* AIX 1 */
78   {
79     "aix3", write_aix3_dkfstab, write_aix3_dkrmount
80   },				/* AIX 3 */
81   {
82     "generic", write_generic_dkfstab, write_generic_dkrmount
83   },				/* Generic */
84   {
85     "u2_0", write_ultrix_dkfstab, write_ultrix_dkrmount
86   },				/* Ultrix */
87   {
88     "u3_0", write_ultrix_dkfstab, write_ultrix_dkrmount
89   },				/* Ultrix */
90   {
91     "u4_0", write_ultrix_dkfstab, write_ultrix_dkrmount
92   },				/* Ultrix */
93   {
94     NULL, NULL, NULL
95   }
96 };
97 
98 
99 /* ---------- AIX 1 ------------------------------ */
100 
101 /*
102  * AIX 1 format
103  */
104 static void
105 write_aix1_dkfstab(FILE *ef, disk_fs *dp)
106 {
107   char *hp = strdup(dp->d_host->h_hostname);
108   char *p = strchr(hp, '.');
109 
110   if (p)
111     *p = '\0';
112 
113   fprintf(ef, "\n%s:\n\tdev = %s\n\tvfs = %s\n\ttype = %s\n\tlog = %s\n\tvol = %s\n\topts = %s\n\tmount = true\n\tcheck = true\n\tfree = false\n",
114 	  dp->d_mountpt,
115 	  dp->d_dev,
116 	  dp->d_fstype,
117 	  dp->d_fstype,
118 	  dp->d_log,
119 	  dp->d_mountpt,
120 	  dp->d_opts);
121   XFREE(hp);
122 }
123 
124 
125 static void
126 write_aix1_dkrmount(FILE *ef, char *hn, fsmount *fp)
127 {
128   char *h = strdup(fp->f_ref->m_dk->d_host->h_hostname);
129   char *hp = strdup(h);
130   char *p = strchr(hp, '.');
131 
132   if (p)
133     *p = '\0';
134   domain_strip(h, hn);
135   fprintf(ef, "\n%s:\n\tsite = %s\n\tdev = %s:%s\n\tvfs = %s\n\ttype = %s\n\tvol = %s\n\topts = %s\n\tmount = true\n\tcheck = true\n\tfree = false\n",
136 	  fp->f_localname,
137 	  hp,
138 	  h,
139 	  fp->f_volname,
140 	  fp->f_fstype,
141 	  fp->f_fstype,
142 	  fp->f_localname,
143 	  fp->f_opts);
144 
145   XFREE(hp);
146   XFREE(h);
147 }
148 
149 
150 /* ---------- AIX 3 ------------------------------ */
151 
152 /*
153  * AIX 3 format
154  */
155 static void
156 write_aix3_dkfstab(FILE *ef, disk_fs *dp)
157 {
158   if (STREQ(dp->d_fstype, "jfs") &&
159       NSTREQ(dp->d_dev, "/dev/", 5) &&
160       !dp->d_log)
161     error("aix 3 needs a log device for journalled filesystem (jfs) mounts");
162 
163   fprintf(ef, "\n%s:\n\tdev = %s\n\tvfs = %s\n\ttype = %s\n\tlog = %s\n\tvol = %s\n\topts = %s\n\tmount = true\n\tcheck = true\n\tfree = false\n",
164 	  dp->d_mountpt,
165 	  dp->d_dev,
166 	  dp->d_fstype,
167 	  dp->d_fstype,
168 	  dp->d_log,
169 	  dp->d_mountpt,
170 	  dp->d_opts);
171 }
172 
173 
174 static void
175 write_aix3_dkrmount(FILE *ef, char *hn, fsmount *fp)
176 {
177   char *h = strdup(fp->f_ref->m_dk->d_host->h_hostname);
178 
179   domain_strip(h, hn);
180   fprintf(ef, "\n%s:\n\tdev = %s:%s\n\tvfs = %s\n\ttype = %s\n\tvol = %s\n\topts = %s\n\tmount = true\n\tcheck = true\n\tfree = false\n",
181 	  fp->f_localname,
182 	  h,
183 	  fp->f_volname,
184 	  fp->f_fstype,
185 	  fp->f_fstype,
186 	  fp->f_localname,
187 	  fp->f_opts);
188 
189   XFREE(h);
190 }
191 
192 
193 /* ---------- Ultrix ----------------------------- */
194 
195 static void
196 write_ultrix_dkfstab(FILE *ef, disk_fs *dp)
197 {
198   fprintf(ef, "%s:%s:%s:%s:%d:%d\n",
199 	  dp->d_dev,
200 	  dp->d_mountpt,
201 	  dp->d_fstype,
202 	  dp->d_opts,
203 	  dp->d_freq,
204 	  dp->d_passno);
205 }
206 
207 
208 static void
209 write_ultrix_dkrmount(FILE *ef, char *hn, fsmount *fp)
210 {
211   char *h = strdup(fp->f_ref->m_dk->d_host->h_hostname);
212 
213   domain_strip(h, hn);
214   fprintf(ef, "%s@%s:%s:%s:%s:0:0\n",
215 	  fp->f_volname,
216 	  h,
217 	  fp->f_localname,
218 	  fp->f_fstype,
219 	  fp->f_opts);
220   XFREE(h);
221 }
222 
223 
224 /* ---------- Generic ---------------------------- */
225 
226 /*
227  * Generic (BSD, SunOS, HPUX) format
228  */
229 static void
230 write_generic_dkfstab(FILE *ef, disk_fs *dp)
231 {
232   fprintf(ef, "%s %s %s %s %d %d\n",
233 	  dp->d_dev,
234 	  dp->d_mountpt,
235 	  dp->d_fstype,
236 	  dp->d_opts,
237 	  dp->d_freq,
238 	  dp->d_passno);
239 }
240 
241 
242 static void
243 write_generic_dkrmount(FILE *ef, char *hn, fsmount *fp)
244 {
245   char *h;
246 
247   if (fp->f_ref) {
248     h = strdup(fp->f_ref->m_dk->d_host->h_hostname);
249   } else {
250     h = strdup(fp->f_from);
251   }
252   domain_strip(h, hn);
253   fprintf(ef, "%s:%s %s %s %s 0 0\n",
254 	  h,
255 	  fp->f_volname,
256 	  fp->f_localname,
257 	  fp->f_fstype,
258 	  fp->f_opts);
259   XFREE(h);
260 }
261 
262 
263 static struct os_fstab_type *
264 find_fstab_type(host *hp)
265 {
266   struct os_fstab_type *op = NULL;
267   char *os_name = NULL;
268 
269 again:;
270   if (os_name == 0) {
271     if (ISSET(hp->h_mask, HF_OS))
272       os_name = hp->h_os;
273     else
274       os_name = GENERIC_OS_NAME;
275   }
276   for (op = os_tabs; op->os_name; op++)
277     if (STREQ(os_name, op->os_name))
278       return op;
279 
280   os_name = GENERIC_OS_NAME;
281   goto again;
282 }
283 
284 
285 static int
286 write_dkfstab(FILE *ef, qelem *q, void (*output) (FILE *, disk_fs *))
287 {
288   int errors = 0;
289   disk_fs *dp;
290 
291   ITER(dp, disk_fs, q)
292   if (!STREQ(dp->d_fstype, "export"))
293       (*output) (ef, dp);
294 
295   return errors;
296 }
297 
298 
299 static int
300 write_dkrmount(FILE *ef, qelem *q, char *hn, void (*output) (FILE *, char *, fsmount *))
301 {
302   int errors = 0;
303   fsmount *fp;
304 
305   ITER(fp, fsmount, q)
306     (*output) (ef, hn, fp);
307 
308   return errors;
309 }
310 
311 
312 int
313 write_fstab(qelem *q)
314 {
315   int errors = 0;
316 
317   if (fstab_pref) {
318     host *hp;
319 
320     show_area_being_processed("write fstab", 4);
321     ITER(hp, host, q) {
322       if (hp->h_disk_fs || hp->h_mount) {
323 	FILE *ef = pref_open(fstab_pref, hp->h_hostname, gen_hdr, hp->h_hostname);
324 	if (ef) {
325 	  struct os_fstab_type *op = find_fstab_type(hp);
326 	  show_new(hp->h_hostname);
327 	  if (hp->h_disk_fs)
328 	    errors += write_dkfstab(ef, hp->h_disk_fs, op->op_fstab);
329 	  else
330 	    fsi_log("No local disk mounts on %s", hp->h_hostname);
331 
332 	  if (hp->h_mount)
333 	    errors += write_dkrmount(ef, hp->h_mount, hp->h_hostname, op->op_mount);
334 
335 	  pref_close(ef);
336 	}
337       } else {
338 	error("no disk mounts on %s", hp->h_hostname);
339       }
340     }
341   }
342   return errors;
343 }
344