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