install.c revision 161100
1232922Stheraven/*
2232922Stheraven * $FreeBSD: head/usr.sbin/sade/install.c 161100 2006-08-08 14:28:03Z delphij $
3232922Stheraven *
4232922Stheraven * Copyright (c) 1995
5232922Stheraven *	Jordan Hubbard.  All rights reserved.
6232922Stheraven *
7232922Stheraven * Redistribution and use in source and binary forms, with or without
8232922Stheraven * modification, are permitted provided that the following conditions
9232922Stheraven * are met:
10232922Stheraven * 1. Redistributions of source code must retain the above copyright
11232922Stheraven *    notice, this list of conditions and the following disclaimer,
12232922Stheraven *    verbatim and that no modifications are made prior to this
13232922Stheraven *    point in the file.
14232922Stheraven * 2. Redistributions in binary form must reproduce the above copyright
15232922Stheraven *    notice, this list of conditions and the following disclaimer in the
16232922Stheraven *    documentation and/or other materials provided with the distribution.
17232922Stheraven *
18232922Stheraven * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
19232922Stheraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20232922Stheraven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21232922Stheraven * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
22232922Stheraven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23232922Stheraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24232922Stheraven * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
25232922Stheraven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26232922Stheraven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27227825Stheraven * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28227825Stheraven * SUCH DAMAGE.
29227825Stheraven *
30227825Stheraven */
31227825Stheraven
32233233Stheraven#include "sade.h"
33233233Stheraven#include <ctype.h>
34233233Stheraven#include <sys/consio.h>
35233233Stheraven#include <sys/disklabel.h>
36227825Stheraven#include <sys/errno.h>
37233233Stheraven#include <sys/ioctl.h>
38233233Stheraven#include <sys/fcntl.h>
39233233Stheraven#include <sys/wait.h>
40233233Stheraven#include <sys/uio.h>
41233233Stheraven#include <sys/param.h>
42233233Stheraven#define MSDOSFS
43233233Stheraven#include <sys/mount.h>
44233233Stheraven#include <ufs/ufs/ufsmount.h>
45233233Stheraven#include <fs/msdosfs/msdosfsmount.h>
46233233Stheraven#undef MSDOSFS
47233233Stheraven#include <sys/stat.h>
48233233Stheraven#include <sys/sysctl.h>
49233233Stheraven#include <libdisk.h>
50233233Stheraven#include <limits.h>
51233233Stheraven#include <unistd.h>
52233233Stheraven#include <termios.h>
53233233Stheraven
54233233Stheraven#define TERMCAP_FILE	"/usr/share/misc/termcap"
55233233Stheraven
56233233StheravenBoolean
57233233StheravencheckLabels(Boolean whinge)
58233233Stheraven{
59233233Stheraven    Boolean status;
60233233Stheraven
61233233Stheraven    /* Don't allow whinging if noWarn is set */
62233233Stheraven    if (variable_get(VAR_NO_WARN))
63233233Stheraven	whinge = FALSE;
64233233Stheraven
65233233Stheraven    status = TRUE;
66233233Stheraven    HomeChunk = RootChunk = SwapChunk = NULL;
67233233Stheraven    TmpChunk = UsrChunk = VarChunk = NULL;
68233233Stheraven#ifdef __ia64__
69233233Stheraven    EfiChunk = NULL;
70233233Stheraven#endif
71233233Stheraven
72233233Stheraven    /* We don't need to worry about root/usr/swap if we're already multiuser */
73245302Stheraven    return status;
74245302Stheraven}
75245302Stheraven
76245302Stheraven#define	QUEUE_YES	1
77245302Stheraven#define	QUEUE_NO	0
78245302Stheravenstatic int
79245302StheravenperformNewfs(PartInfo *pi, char *dname, int queue)
80245302Stheraven{
81233233Stheraven	char buffer[LINE_MAX];
82233233Stheraven
83233233Stheraven	if (pi->do_newfs) {
84233233Stheraven		switch(pi->newfs_type) {
85233233Stheraven		case NEWFS_UFS:
86233233Stheraven			snprintf(buffer, LINE_MAX, "%s %s %s %s %s",
87233233Stheraven			    NEWFS_UFS_CMD,
88233233Stheraven			    pi->newfs_data.newfs_ufs.softupdates ?  "-U" : "",
89233233Stheraven			    pi->newfs_data.newfs_ufs.ufs1 ? "-O1" : "-O2",
90233233Stheraven			    pi->newfs_data.newfs_ufs.user_options,
91233233Stheraven			    dname);
92233233Stheraven			break;
93233233Stheraven
94233233Stheraven		case NEWFS_MSDOS:
95233233Stheraven			snprintf(buffer, LINE_MAX, "%s %s", NEWFS_MSDOS_CMD,
96233233Stheraven			    dname);
97233233Stheraven			break;
98233233Stheraven
99233233Stheraven		case NEWFS_CUSTOM:
100233233Stheraven			snprintf(buffer, LINE_MAX, "%s %s",
101233233Stheraven			    pi->newfs_data.newfs_custom.command, dname);
102233233Stheraven			break;
103233233Stheraven		}
104233233Stheraven
105233233Stheraven		if (queue == QUEUE_YES) {
106233233Stheraven			command_shell_add(pi->mountpoint, buffer);
107233233Stheraven			return (0);
108233233Stheraven		} else
109233233Stheraven			return (vsystem(buffer));
110233233Stheraven	}
111233233Stheraven	return (0);
112233233Stheraven}
113227825Stheraven
114227825Stheraven/* Go newfs and/or mount all the filesystems we've been asked to */
115227825Stheravenint
116227825StheraveninstallFilesystems(dialogMenuItem *self)
117227825Stheraven{
118227825Stheraven    int i;
119227825Stheraven    Disk *disk;
120227825Stheraven    Chunk *c1, *c2;
121227825Stheraven    Device **devs;
122227825Stheraven    PartInfo *root;
123227825Stheraven    char dname[80];
124227825Stheraven    Boolean upgrade = FALSE;
125227825Stheraven
126227825Stheraven    /* If we've already done this, bail out */
127227825Stheraven    if (!variable_cmp(DISK_LABELLED, "written"))
128227825Stheraven	return DITEM_SUCCESS;
129227825Stheraven
130227825Stheraven    upgrade = !variable_cmp(SYSTEM_STATE, "upgrade");
131227825Stheraven    if (!checkLabels(TRUE))
132233233Stheraven	return DITEM_FAILURE;
133227825Stheraven
134227825Stheraven    root = (RootChunk != NULL) ? (PartInfo *)RootChunk->private_data : NULL;
135227825Stheraven
136227825Stheraven    command_clear();
137227825Stheraven
138227825Stheraven    /* Now buzz through the rest of the partitions and mount them too */
139227825Stheraven    devs = deviceFind(NULL, DEVICE_TYPE_DISK);
140227825Stheraven    for (i = 0; devs[i]; i++) {
141227825Stheraven	if (!devs[i]->enabled)
142227825Stheraven	    continue;
143227825Stheraven
144227825Stheraven	disk = (Disk *)devs[i]->private;
145227825Stheraven	if (!disk->chunks) {
146227825Stheraven	    msgConfirm("No chunk list found for %s!", disk->name);
147227825Stheraven	    return DITEM_FAILURE | DITEM_RESTORE;
148227825Stheraven	}
149227825Stheraven	for (c1 = disk->chunks->part; c1; c1 = c1->next) {
150227825Stheraven#ifdef __ia64__
151233233Stheraven	if (c1->type == part) {
152233233Stheraven		c2 = c1;
153233233Stheraven		{
154233233Stheraven#elif defined(__powerpc__)
155233233Stheraven	    if (c1->type == apple) {
156233233Stheraven		for (c2 = c1->part; c2; c2 = c2->next) {
157227825Stheraven#else
158227825Stheraven	    if (c1->type == freebsd) {
159227825Stheraven		for (c2 = c1->part; c2; c2 = c2->next) {
160227825Stheraven#endif
161227825Stheraven		    if (c2->type == part && c2->subtype != FS_SWAP && c2->private_data) {
162227825Stheraven			PartInfo *tmp = (PartInfo *)c2->private_data;
163227825Stheraven
164227825Stheraven			/* Already did root */
165227825Stheraven			if (c2 == RootChunk)
166227825Stheraven			    continue;
167233233Stheraven
168233233Stheraven			sprintf(dname, "/dev/%s", c2->name);
169233233Stheraven
170227825Stheraven			if (tmp->do_newfs && (!upgrade ||
171227825Stheraven			    !msgNoYes("You are upgrading - are you SURE you"
172227825Stheraven			    " want to newfs /dev/%s?", c2->name)))
173227825Stheraven				performNewfs(tmp, dname, QUEUE_YES);
174227825Stheraven			else
175227825Stheraven			    command_shell_add(tmp->mountpoint,
176227825Stheraven				"fsck_ffs -y /dev/%s", c2->name);
177227825Stheraven			command_func_add(tmp->mountpoint, Mount, c2->name);
178227825Stheraven		    }
179227825Stheraven		    else if (c2->type == part && c2->subtype == FS_SWAP) {
180227825Stheraven			char fname[80];
181227825Stheraven			int i;
182227825Stheraven
183227825Stheraven			if (c2 == SwapChunk)
184227825Stheraven			    continue;
185227825Stheraven			sprintf(fname, "/dev/%s", c2->name);
186227825Stheraven			i = (Fake || swapon(fname));
187227825Stheraven			if (!i) {
188227825Stheraven			    dialog_clear_norefresh();
189227825Stheraven			    msgNotify("Added %s as an additional swap device", fname);
190227825Stheraven			}
191227825Stheraven			else {
192227825Stheraven			    msgConfirm("Unable to add %s as a swap device: %s", fname, strerror(errno));
193227825Stheraven			}
194227825Stheraven		    }
195227825Stheraven		}
196227825Stheraven	    }
197227825Stheraven	    else if (c1->type == fat && c1->private_data &&
198227825Stheraven		(root->do_newfs || upgrade)) {
199227825Stheraven		char name[FILENAME_MAX];
200227825Stheraven
201227825Stheraven		sprintf(name, "/%s", ((PartInfo *)c1->private_data)->mountpoint);
202227825Stheraven		Mkdir(name);
203227825Stheraven	    }
204227825Stheraven#if defined(__ia64__)
205227825Stheraven	    else if (c1->type == efi && c1->private_data) {
206227825Stheraven		PartInfo *pi = (PartInfo *)c1->private_data;
207227825Stheraven
208227825Stheraven		sprintf(dname, "/dev/%s", c1->name);
209227825Stheraven
210227825Stheraven		if (pi->do_newfs && (!upgrade ||
211227825Stheraven		    !msgNoYes("You are upgrading - are you SURE you want to "
212227825Stheraven		    "newfs /dev/%s?", c1->name)))
213227825Stheraven			performNewfs(pi, dname, QUEUE_YES);
214227825Stheraven	    }
215227825Stheraven#endif
216227825Stheraven	}
217227825Stheraven    }
218227825Stheraven
219227825Stheraven    command_sort();
220227825Stheraven    command_execute();
221227825Stheraven    dialog_clear_norefresh();
222227825Stheraven    return DITEM_SUCCESS | DITEM_RESTORE;
223227825Stheraven}
224227825Stheraven
225227825Stheravenstatic char *
226227825StheravengetRelname(void)
227227825Stheraven{
228227825Stheraven    static char buf[64];
229227825Stheraven    size_t sz = (sizeof buf) - 1;
230227825Stheraven
231227825Stheraven    if (sysctlbyname("kern.osrelease", buf, &sz, NULL, 0) != -1) {
232227825Stheraven	buf[sz] = '\0';
233227825Stheraven	return buf;
234227825Stheraven    }
235227825Stheraven    else
236227825Stheraven	return "<unknown>";
237227825Stheraven}
238227825Stheraven
239227825Stheraven/* Initialize various user-settable values to their defaults */
240227825Stheravenint
241227825StheraveninstallVarDefaults(dialogMenuItem *self)
242227825Stheraven{
243227825Stheraven
244227825Stheraven    /* Set default startup options */
245227825Stheraven    variable_set2(VAR_RELNAME,			getRelname(), 0);
246227825Stheraven    variable_set2(SYSTEM_STATE,		"update", 0);
247227825Stheraven    variable_set2(VAR_NEWFS_ARGS,		"-b 16384 -f 2048", 0);
248227825Stheraven    variable_set2(VAR_CONSTERM,                 "NO", 0);
249227825Stheraven    return DITEM_SUCCESS;
250233233Stheraven}
251233233Stheraven
252233233Stheraven/* Load the environment up from various system configuration files */
253227825Stheravenvoid
254227825StheraveninstallEnvironment(void)
255227825Stheraven{
256227825Stheraven}
257227825Stheraven
258227825Stheraven