]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/sade/install.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / sade / install.c
1 /*
2  * $FreeBSD$
3  *
4  * Copyright (c) 1995
5  *      Jordan Hubbard.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    verbatim and that no modifications are made prior to this
13  *    point in the file.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31
32 #include "sade.h"
33 #include <ctype.h>
34 #include <sys/consio.h>
35 #include <sys/disklabel.h>
36 #include <sys/errno.h>
37 #include <sys/ioctl.h>
38 #include <sys/fcntl.h>
39 #include <sys/wait.h>
40 #include <sys/uio.h>
41 #include <sys/param.h>
42 #define MSDOSFS
43 #include <sys/mount.h>
44 #include <ufs/ufs/ufsmount.h>
45 #include <fs/msdosfs/msdosfsmount.h>
46 #undef MSDOSFS
47 #include <sys/stat.h>
48 #include <sys/sysctl.h>
49 #include <libdisk.h>
50 #include <limits.h>
51 #include <unistd.h>
52 #include <termios.h>
53
54 #define TERMCAP_FILE    "/usr/share/misc/termcap"
55
56 Boolean
57 checkLabels(Boolean whinge)
58 {
59     Boolean status;
60
61     /* Don't allow whinging if noWarn is set */
62     if (variable_get(VAR_NO_WARN))
63         whinge = FALSE;
64
65     status = TRUE;
66     HomeChunk = RootChunk = SwapChunk = NULL;
67     TmpChunk = UsrChunk = VarChunk = NULL;
68 #ifdef __ia64__
69     EfiChunk = NULL;
70 #endif
71
72     /* We don't need to worry about root/usr/swap if we're already multiuser */
73     return status;
74 }
75
76 #define QUEUE_YES       1
77 #define QUEUE_NO        0
78 static int
79 performNewfs(PartInfo *pi, char *dname, int queue)
80 {
81         char buffer[LINE_MAX];
82
83         if (pi->do_newfs) {
84                 switch(pi->newfs_type) {
85                 case NEWFS_UFS:
86                         snprintf(buffer, LINE_MAX, "%s %s %s %s %s",
87                             NEWFS_UFS_CMD,
88                             pi->newfs_data.newfs_ufs.softupdates ?  "-U" : "",
89                             pi->newfs_data.newfs_ufs.ufs1 ? "-O1" : "-O2",
90                             pi->newfs_data.newfs_ufs.user_options,
91                             dname);
92                         break;
93
94                 case NEWFS_MSDOS:
95                         snprintf(buffer, LINE_MAX, "%s %s", NEWFS_MSDOS_CMD,
96                             dname);
97                         break;
98
99                 case NEWFS_CUSTOM:
100                         snprintf(buffer, LINE_MAX, "%s %s",
101                             pi->newfs_data.newfs_custom.command, dname);
102                         break;
103                 }
104
105                 if (queue == QUEUE_YES) {
106                         command_shell_add(pi->mountpoint, "%s", buffer);
107                         return (0);
108                 } else
109                         return (vsystem("%s", buffer));
110         }
111         return (0);
112 }
113
114 /* Go newfs and/or mount all the filesystems we've been asked to */
115 int
116 installFilesystems(dialogMenuItem *self)
117 {
118     int i;
119     Disk *disk;
120     Chunk *c1, *c2;
121     Device **devs;
122     PartInfo *root;
123     char dname[80];
124     Boolean upgrade = FALSE;
125
126     /* If we've already done this, bail out */
127     if (!variable_cmp(DISK_LABELLED, "written"))
128         return DITEM_SUCCESS;
129
130     upgrade = !variable_cmp(SYSTEM_STATE, "upgrade");
131     if (!checkLabels(TRUE))
132         return DITEM_FAILURE;
133
134     root = (RootChunk != NULL) ? (PartInfo *)RootChunk->private_data : NULL;
135
136     command_clear();
137
138     /* Now buzz through the rest of the partitions and mount them too */
139     devs = deviceFind(NULL, DEVICE_TYPE_DISK);
140     for (i = 0; devs[i]; i++) {
141         if (!devs[i]->enabled)
142             continue;
143
144         disk = (Disk *)devs[i]->private;
145         if (!disk->chunks) {
146             msgConfirm("No chunk list found for %s!", disk->name);
147             return DITEM_FAILURE | DITEM_RESTORE;
148         }
149         for (c1 = disk->chunks->part; c1; c1 = c1->next) {
150 #ifdef __ia64__
151         if (c1->type == part) {
152                 c2 = c1;
153                 {
154 #elif defined(__powerpc__)
155             if (c1->type == apple) {
156                 for (c2 = c1->part; c2; c2 = c2->next) {
157 #else
158             if (c1->type == freebsd) {
159                 for (c2 = c1->part; c2; c2 = c2->next) {
160 #endif
161                     if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
162                         PartInfo *tmp = (PartInfo *)c2->private_data;
163
164                         /* Already did root */
165                         if (c2 == RootChunk)
166                             continue;
167
168                         sprintf(dname, "/dev/%s", c2->name);
169
170                         if (tmp->do_newfs && (!upgrade ||
171                             !msgNoYes("You are upgrading - are you SURE you"
172                             " want to newfs /dev/%s?", c2->name)))
173                                 performNewfs(tmp, dname, QUEUE_YES);
174                         else
175                             command_shell_add(tmp->mountpoint,
176                                 "fsck_ffs -y /dev/%s", c2->name);
177                         command_func_add(tmp->mountpoint, Mount, c2->name);
178                     }
179                     else if (c2->type == part && c2->subtype == FS_SWAP) {
180                         char fname[80];
181                         int i;
182
183                         if (c2 == SwapChunk)
184                             continue;
185                         sprintf(fname, "/dev/%s", c2->name);
186                         i = (Fake || swapon(fname));
187                         if (!i) {
188                             dialog_clear_norefresh();
189                             msgNotify("Added %s as an additional swap device", fname);
190                         }
191                         else {
192                             msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
193                         }
194                     }
195                 }
196             }
197             else if (c1->type == fat && c1->private_data &&
198                 (root->do_newfs || upgrade)) {
199                 char name[FILENAME_MAX];
200
201                 sprintf(name, "/%s", ((PartInfo *)c1->private_data)->mountpoint);
202                 Mkdir(name);
203             }
204 #if defined(__ia64__)
205             else if (c1->type == efi && c1->private_data) {
206                 PartInfo *pi = (PartInfo *)c1->private_data;
207
208                 sprintf(dname, "/dev/%s", c1->name);
209
210                 if (pi->do_newfs && (!upgrade ||
211                     !msgNoYes("You are upgrading - are you SURE you want to "
212                     "newfs /dev/%s?", c1->name)))
213                         performNewfs(pi, dname, QUEUE_YES);
214             }
215 #endif
216         }
217     }
218
219     command_sort();
220     command_execute();
221     dialog_clear_norefresh();
222     return DITEM_SUCCESS | DITEM_RESTORE;
223 }
224
225 static char *
226 getRelname(void)
227 {
228     static char buf[64];
229     size_t sz = (sizeof buf) - 1;
230
231     if (sysctlbyname("kern.osrelease", buf, &sz, NULL, 0) != -1) {
232         buf[sz] = '\0';
233         return buf;
234     }
235     else
236         return "<unknown>";
237 }
238
239 /* Initialize various user-settable values to their defaults */
240 int
241 installVarDefaults(dialogMenuItem *self)
242 {
243
244     /* Set default startup options */
245     variable_set2(VAR_RELNAME,                  getRelname(), 0);
246     variable_set2(SYSTEM_STATE,         "update", 0);
247     variable_set2(VAR_NEWFS_ARGS,               "-b 16384 -f 2048", 0);
248     variable_set2(VAR_CONSTERM,                 "NO", 0);
249     return DITEM_SUCCESS;
250 }
251
252 /* Load the environment up from various system configuration files */
253 void
254 installEnvironment(void)
255 {
256 }
257