summaryrefslogtreecommitdiffstats
path: root/sbin/sysinstall/stage3.c
blob: 5b7a1a1b481ba2ab57327cc15a595a974c58e029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
 * ----------------------------------------------------------------------------
 *
 * $Id$
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <dialog.h>
#include <ncurses.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <sys/param.h>
#include <sys/mount.h>
#include <sys/stat.h>

#if 0
#include <sys/types.h>
#include <sys/disklabel.h>
#include <sys/ioctl.h>
#include <sys/reboot.h>
#include <sys/wait.h>
#include "bootarea.h"
#include <ufs/ffs/fs.h>
#endif

#include "sysinstall.h"

void
stage3()
{
	char *diskname;
	struct ufs_args ufsargs;
	char           *s;
	int             i;

	/*
	 * Extract the disk-name from /etc/fstab, we wrote it ourselves,
	 * so we know how to read it :-)
	 * XXX: Multidisk installs.  We need to mount all partitions.
	 */

	i = open("/etc/fstab", O_RDONLY);
	if (i < 0) {
		fatal("Couldn't open /etc/fstab");
	}
	read(i, scratch, 100);
	for (s = scratch; *s != ' ' && *s != '\t'; s++);
	s--;
	*s = '\0';
	s = scratch + 5;
	diskname = malloc(strlen(s) + 1);
	if (!diskname) {
		fatal("malloc failed");
	}
	strcpy(diskname, s);
	close(i);

	sprintf(scratch, "mount -u /dev/%sa /", diskname);
	TellEm(scratch);
	sprintf(scratch, "/dev/%sa", diskname);
	ufsargs.fspec = scratch;
	if (mount(MOUNT_UFS, "/", MNT_UPDATE, (caddr_t) & ufsargs) == -1) {
		sprintf(errmsg, "Failed to mount root read/write: %s\n%s", strerror(errno), ufsargs.fspec);
		fatal(errmsg);
	}
	sprintf(scratch, "mount /dev/%se /usr", diskname);
	TellEm(scratch);
	sprintf(scratch, "/dev/%se", diskname);
	ufsargs.fspec = scratch;
	if (mount(MOUNT_UFS, "/usr", 0, (caddr_t) & ufsargs) == -1) {
		sprintf(errmsg, "Failed to mount /usr: %s\n%s", strerror(errno), ufsargs.fspec);
		fatal(errmsg);
	}
	TellEm("mkdir /proc");
	if (mkdir("/proc", S_IRWXU) == -1) {
		sprintf(errmsg, "Couldn't create directory /proc: %s\n",
			strerror(errno));
		fatal(errmsg);
	}
	TellEm("mkdir /root");
	if (mkdir("/root", S_IRWXU) == -1) {
		sprintf(errmsg, "Couldn't create directory /root: %s\n",
			strerror(errno));
		fatal(errmsg);
	}
	TellEm("mkdir /var");
	if (mkdir("/var", S_IRWXU) == -1) {
		sprintf(errmsg, "Couldn't create directory /var: %s\n",
			strerror(errno));
		fatal(errmsg);
	}
	TellEm("mkdir /var/run");
	if (mkdir("/var/run", S_IRWXU) == -1) {
		sprintf(errmsg, "Couldn't create directory /var/run: %s\n",
			strerror(errno));
		fatal(errmsg);
	}
	sprintf(scratch, "Insert CPIO floppy in floppy drive 0\n");
	dialog_msgbox("Stage 2 installation", scratch, 6, 75, 1);
	ufsargs.fspec = "/dev/fd0a";
	if (mount(MOUNT_UFS, "/mnt", MNT_RDONLY, (caddr_t) & ufsargs) == -1) {
		sprintf(errmsg, "Failed to mount /mnt: %s\n%s", strerror(errno), ufsargs.fspec);
		fatal(errmsg);
	}
	TellEm("sh -c 'cd / ; gunzip < /mnt/inst2.cpio.gz | cpio -idum'");
	if (exec("/bin/sh", "/bin/sh", "-e", "-c",
		 "cd / ; gunzip < /mnt/inst2.cpio.gz | cpio -idum", 0) == -1)
		fatal(errmsg);

	TellEm("sh -c 'cd /mnt ; ls install magic | cpio -dump /'");
	if (exec("/bin/sh", "/bin/sh", "-e", "-c",
		 "cd /mnt ; ls magic | cpio -dump /", 0) == -1)
		fatal(errmsg);

	TellEm("unmount /mnt");
	if (unmount("/mnt", 0) == -1) {
		sprintf(errmsg, "Error unmounting /mnt: %s\n", strerror(errno));
		fatal(errmsg);
	}
	TellEm("sh -c 'cd /dev ; sh MAKEDEV all'");
	if (exec("/bin/sh", "/bin/sh", "-e", "-c",
		 "PATH=/bin:/sbin:/usr/bin:/usr/sbin; export PATH ; cd /dev ; sh MAKEDEV all", 0) == -1)
		fatal(errmsg);

	TellEm("unlink /sbin/oinit");
	unlink("/sbin/oinit");
	TellEm("link /stand/sysinstall /sbin/init");
	link("/stand/sysinstall", "/sbin/init");
	clear();
	refresh();
	endwin();
	close(0);
	close(1);
	close(2);
	execl("/sbin/init", "init", 0);
}
OpenPOWER on IntegriCloud