1#!/bin/sh
2
3# installer - start installer frontend and backend as per pfi config.
4# $Id: installer,v 1.20 2005/04/13 03:32:16 cpressey Exp $
5
6### SUBS ###
7cleanup()
8{
9	PIDS=`ps aux | grep -e dfuife_curses -e dfuibe_installer | awk '{print $1}'`
10	for PID in $PIDS; do
11		# DFUI still running, kill it
12		kill -9 $PID
13	done
14}
15
16background_backend()
17{
18	RENDEZVOUS=$1
19	TRANSPORT=$2
20	$pfi_backend \
21	    -o $SOURCE_DIR \
22	    -r $RENDEZVOUS \
23	    -t $TRANSPORT \
24	    >/dev/null 2>&1
25	RESULT=$?
26	case "$RESULT" in
27	0)
28		;;
29	5)
30		$pfi_shutdown_command
31		;;
32	*)
33		;;
34	esac
35}
36
37is_serial()
38{
39	# Detect if we are currently connected via a serial console
40	if [ "X`/usr/bin/kenv console`" == "Xcomconsole" ]; then
41		return 0 # return success
42	fi
43	return 1
44}
45
46setup_term()
47{
48	# If TERM has not been set manually (ie: still 'dialup' or from /etc/ttyd), we ask the user what they want to use
49	if [ "X`tty |cut -c6-9`" == "Xttyd" ]; then
50		newterm=${TERM}
51		if [ "X`/usr/bin/kenv smbios.bios.vendor`" == "XSeaBIOS" ]; then
52			# installation on a virtial machine uses this type of simulated bios often, so we can do better than vt100 (eg:vt220-co, vt320-co, cons50-w)
53			newterm="xterm"
54		elif [ "X${TERM}" == "Xdialup" ]; then
55			newterm="vt100"
56		fi
57		echo ""
58		echo -n "What is your terminal type (provide value termcap name)? [${newterm}]: "
59		read input
60		[ "${input}" = '' ] && input=$newterm
61		export TERM="${input}"
62		echo "set new TERM=$TERM"
63	fi
64	TTY_BAUD=`stty speed`
65	if [ $TTY_BAUD -lt 38400 ]; then
66		echo -n "Your serial connection is quite slow ($TTY_BAUD), causing installer slow down. Continue Anyway ? [Y/n]: "
67		read input
68		[ "${input}" == "N" ] || [ "${input}" == "n" ] && exit 0
69	fi
70}
71
72installer_start()
73{
74	# Console start sequence:
75	# - Backend (and all other logging) goes to console (ttyv0)
76	# - curses frontend starts on ttyv1.
77	# - Uses vidcontrol -s 2 to switch to ttyv1 once the frontend is up.
78
79	echo -n "Starting installer.  "
80
81	if [ -r /etc/defaults/pfi.conf ]; then
82		. /etc/defaults/pfi.conf
83	fi
84
85	if [ -r /etc/pfi.conf ]; then
86		echo "Reading /etc/pfi.conf ..."
87		. /etc/pfi.conf
88	else
89		echo "/etc/pfi.conf not found, starting interactive install."
90	fi
91
92	# We can set up any install variables and such
93	# here by examining pfi_* variables.
94
95	if [ "X$pfi_run" != "X" ]; then
96		pfi_frontend=none
97		$pfi_run
98	fi
99
100	case "X$pfi_dfui_transport" in
101	Xnpipe)
102		RENDEZVOUS="installer"
103		;;
104	Xtcp)
105		RENDEZVOUS="9999"
106		;;
107	*)
108		echo "Unsupported DFUI transport '$pfi_dfui_transport'."
109		return
110		;;
111	esac
112
113	if [ "X$pfi_frontend" = "Xauto" ]; then
114		if [ "X$TTY_INST" = "X" ]; then
115			if $(is_serial); then
116				setup_term
117				RENDEZVOUS="installer"
118				pfi_dfui_transport="npipe"
119				TTY=$(tty)
120				pfi_frontend="curseslog"
121			else
122				if $(is_installmedia); then
123					TTY=/dev/ttyv1
124					pfi_frontend="cursesvty"
125				else
126					TTY=$(tty)
127					pfi_frontend="curseslog"
128				fi
129			fi
130		else
131			pfi_frontend="cursesx11"
132		fi
133	fi
134
135	case "X$pfi_frontend" in
136	Xqt)
137		$pfi_backend \
138		    -o $SOURCE_DIR \
139		    -r $RENDEZVOUS \
140		    -t $pfi_dfui_transport
141		RESULT=$?
142		;;
143	Xcgi)
144		$pfi_backend \
145		    -o $SOURCE_DIR \
146		    -r $RENDEZVOUS \
147		    -t $pfi_dfui_transport
148		RESULT=$?
149		;;
150	Xcursesvty)
151		ps auwwwxxx > /tmp/ps.txt
152		if grep -q dfuife_curses /tmp/ps.txt; then
153			# Frontend is already running.
154		else
155			ESCDELAY=$pfi_curses_escdelay \
156			    /usr/sbin/dfuife_curses \
157				-r $RENDEZVOUS \
158				-t $pfi_dfui_transport \
159				-b /usr/share/installer/fred.txt \
160			    2>/dev/ttyv0 <$TTY >$TTY &
161		fi
162		rm -f /tmp/ps.txt
163		sleep 1
164		vidcontrol -s 2 </dev/ttyv0
165		$pfi_backend \
166		    -o $SOURCE_DIR \
167		    -r $RENDEZVOUS \
168		    -t $pfi_dfui_transport
169		RESULT=$?
170		sleep 1
171		killall dfuife_curses
172		vidcontrol -s 1 </dev/ttyv0
173		;;
174	Xcurseslog)
175		ps auwwwxxx > /tmp/ps.txt
176		if grep -q dfuife_curses /tmp/ps.txt; then
177			# Frontend is already running.
178		else
179			ESCDELAY=$pfi_curses_escdelay \
180			    /usr/sbin/dfuife_curses \
181				-r $RENDEZVOUS \
182				-t $pfi_dfui_transport \
183				-b /usr/share/installer/fred.txt \
184			    2>/tmp/dfuife_curses.log <$TTY >$TTY &
185		fi
186		rm -f /tmp/ps.txt
187		sleep 1
188		$pfi_backend \
189		    -o $SOURCE_DIR \
190		    -r $RENDEZVOUS \
191		    -t $pfi_dfui_transport \
192		    >/dev/null 2>&1
193		RESULT=$?
194		sleep 1
195		killall -q dfuife_curses
196		;;
197	Xcursesx11)
198		ps auwwwxxx > /tmp/ps.txt
199		if grep -q dfuife_curses /tmp/ps.txt; then
200			echo "Frontend is already running"
201		else
202			ESCDELAY=$pfi_curses_escdelay \
203			/usr/sbin/dfuife_curses \
204			-r $RENDEZVOUS \
205			-t $pfi_dfui_transport \
206			-b /usr/share/installer/fred.txt \
207			>$TTY_INST <$TTY_INST 2>&1 &
208		fi
209		rm -f /tmp/ps.txt
210		sleep 1
211		$pfi_backend \
212		    -o $SOURCE_DIR \
213		    -r $RENDEZVOUS \
214		    -t $pfi_dfui_transport
215		RESULT=$?
216		sleep 1
217		killall dfuife_curses
218		;;
219	Xnone)
220		RESULT=0
221		;;
222	*)
223		echo "Unknown installer frontend '$pfi_frontend'."
224		return
225		;;
226	esac
227
228	case "$RESULT" in
229	0)
230		;;
231	5)
232		$pfi_shutdown_command
233		;;
234	*)
235		;;
236	esac
237}
238
239is_installmedia()
240{
241    local _ttyv1=$(grep -w "^ttyv1" /etc/ttys)
242    local guest=$(sysctl -n kern.vmm_guest)
243
244    #
245    # ttyv1 isn't configured for the install media so use
246    # that as a clue for now. Vkernels will be forced
247    # to use 'curseslog' to avoid polluting its only
248    # terminal.
249    #
250    [ "${guest}" = "vkernel" ] && return 1;
251
252    if [ -z "${_ttyv1}" ]; then
253	return 0	# Return success, it's a USB image, ISO etc.
254    else
255	return 1
256    fi
257}
258
259### MAIN ###
260
261if [ $# -gt 1 ]; then
262	echo "usage: installer [source_directory]"
263	exit 1
264elif [ $# = 1 -a ! -d $1 ]; then
265	echo "source_directory does not exist or is no directory"
266	exit 1
267fi
268
269trap cleanup EXIT SIGTERM SIGINT
270
271#
272# Source directory for the installation
273#
274if [ $# = 1 ]; then
275	SOURCE_DIR=$1
276else
277	SOURCE_DIR=/
278fi
279
280ps auwwwxxx > /tmp/ps.txt
281if grep -q dfuibe_installer /tmp/ps.txt; then
282	# Installer is already running. Log in normally.
283	rm -f /tmp/ps.txt
284else
285	rm -f /tmp/ps.txt
286	installer_start
287fi
288
289### END of installer ###
290