/[livecd-tools]/trunk/livecd-functions.sh
Gentoo

Contents of /trunk/livecd-functions.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 75 - (show annotations) (download) (as text)
Tue Aug 16 19:02:33 2005 UTC (9 years ago) by wolf31o2
File MIME type: text/x-sh
File size: 12172 byte(s)
Fix writing wireless config, made dhcpcd use -n, and made inittab munging create a backup first.

1 #!/bin/bash
2 # Copyright 1999-2005 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Header: /var/cvsroot/gentoo/src/livecd-tools/livecd-functions.sh,v 1.13 2005/08/16 19:02:33 wolf31o2 Exp $
5
6 # Global Variables:
7 # CDBOOT -- is booting off CD
8 # LIVECD_CONSOLE -- console that is specified to kernel commandline
9 # -- (ttyS0, tty1, etc). Only defined if passed to kernel
10 # LIVECD_CONSOLE_BAUD -- console baudrate specified
11 # LIVECD_CONSOLE_PARITY -- console parity specified
12 # LIVECD_CONSOLE_DATABITS -- console databits specified
13
14 livecd_parse_opt() {
15 case "$1" in
16 *\=*)
17 echo "$1" | cut -f2 -d=
18 ;;
19 esac
20 }
21
22 livecd_check_root() {
23 if [ "$(whoami)" != "root" ]
24 then
25 echo "ERROR: must be root to continue"
26 return 1
27 fi
28 }
29
30 livecd_get_cmdline() {
31 echo "0" > /proc/sys/kernel/printk
32 CMDLINE="$(cat /proc/cmdline)"
33 export CMDLINE
34 }
35
36 no_gl() {
37 echo "No OpenGL-capable card found."
38 GLTYPE=xorg-x11
39 }
40
41 ati_gl() {
42 echo "ATI card detected."
43 GLTYPE=ati
44 }
45
46 nv_gl() {
47 echo "NVIDIA card detected."
48 GLTYPE=nvidia
49 }
50
51 get_video_cards() {
52 VIDEO_CARDS="$(/sbin/lspci | grep VGA)"
53 NUM_CARDS="$(echo ${VIDEO_CARDS} | wc -l)"
54 if [ ${NUM_CARDS} -eq 1 ]; then
55 NVIDIA=$(echo ${VIDEO_CARDS} | grep "nVidia Corporation")
56 ATI=$(echo ${VIDEO_CARDS} | grep "ATI Technologies")
57 if [ -n "${NVIDIA}" ]; then
58 NVIDIA_CARD=$(echo ${NVIDIA} | awk 'BEGIN {RS=" "} /NV[0-9]+/ {print $1}')
59 if [ -n "${NVIDIA_CARD}" ]; then
60 if [ $(echo ${NVIDIA_CARD} | cut -dV -f2) -ge 4 ]; then
61 nv_gl
62 else
63 no_gl
64 fi
65 else
66 no_gl
67 fi
68 elif [ -n "${ATI}" ]; then
69 ATI_CARD=$(echo ${ATI} | awk 'BEGIN {RS=" "} /(R|RV|RS)[0-9]+/ {print $1}')
70 if [ $(echo ${ATI_CARD} | grep S) ]; then
71 ATI_CARD_S=$(echo ${ATI_CARD} | cut -dS -f2)
72 elif [ $(echo ${ATI_CARD} | grep V) ]; then
73 ATI_CARD_V=$(echo ${ATI_CARD} | cut -dV -f2)
74 else
75 ATI_CARD=$(echo ${ATI_CARD} | cut -dR -f2)
76 fi
77 if [ -n "${ATI_CARD_S}" ] && [ ${ATI_CARD_S} -ge 350 ]; then
78 ati_gl
79 elif [ -n "${ATI_CARD_V}" ] && [ ${ATI_CARD_V} -ge 250 ]; then
80 ati_gl
81 elif [ -n "${ATI_CARD}" ] && [ ${ATI_CARD} -ge 200 ]; then
82 ati_gl
83 else
84 no_gl
85 fi
86 else
87 no_gl
88 fi
89 fi
90 }
91
92 livecd_config_wireless() {
93 cd /tmp/setup.opts
94 dialog --title "SSID" --inputbox "Please enter your SSID, or leave blank for selecting the nearest open network" 20 50 2> ${iface}.SSID
95 SSID="$(cat ${iface}.SSID)"
96 if [ -n "${SSID}" ]
97 then
98 dialog --title "WEP (Part 1)" --menu "Does your network use encryption?" 20 60 7 1 "Yes" 2 "No" 2> ${iface}.WEP
99 WEP="$(cat ${iface}.WEP)"
100 case ${WEP} in
101 1)
102 dialog --title "WEP (Part 2)" --menu "Are you entering your WEP key in HEX or ASCII?" 20 60 7 1 "HEX" 2 "ASCII" 2> ${iface}.WEPTYPE
103 WEP_TYPE="$(cat ${iface}.WEPTYPE)"
104 case ${WEP_TYPE} in
105 1)
106 dialog --title "WEP (Part 3)" --inputbox "Please enter your WEP key in the form of XXXX-XXXX-XX for 64-bit or XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XX for 128-bit" 20 50 2> ${iface}.WEPKEY
107 WEP_KEY="$(cat ${iface}.WEPKEY)"
108 if [ -n "${WEP_KEY}" -a -x /usr/sbin/iwconfig ]
109 then
110 /usr/sbin/iwconfig ${iface} essid "${SSID}"
111 /usr/sbin/iwconfig ${iface} key "${WEP_KEY}"
112 fi
113 ;;
114 2)
115 dialog --title "WEP (Part 3)" --inputbox "Please enter your WEP key in ASCII form. This should be 5 or 13 characters for either 64-bit or 128-bit encryption, repectively" 20 50 2> ${iface}.WEPKEY
116 WEP_KEY="$(cat ${iface}.WEPKEY)"
117 if [ -n "${WEP_KEY}" -a -x /usr/sbin/iwconfig ]
118 then
119 /usr/sbin/iwconfig ${iface} essid "${SSID}"
120 /usr/sbin/iwconfig ${iface} key "s:${WEP_KEY}"
121 fi
122 ;;
123 esac
124 ;;
125 2)
126 /usr/sbin/iwconfig ${iface} essid "${SSID}"
127 /usr/sbin/iwconfig ${iface} key off
128 ;;
129 esac
130 fi
131 }
132
133 livecd_write_wireless_conf() {
134 cd /tmp/setup.opts
135 SSID="$(cat ${iface}.SSID)"
136 if [ -n "${SSID}" ]
137 then
138 echo "# This wireless configuration file was built by net-setup" > /etc/conf.d/wireless
139 WEP="$(cat ${iface}.WEPTYPE)"
140 case ${WEP} in
141 1)
142 WEP_TYPE="$(cat ${iface}.WEPTYPE)"
143 if [ -n "${WEP_TYPE}" ]
144 then
145 WEP_KEY="$(cat ${iface}.WEPKEY)"
146 if [ -n "${WEP_KEY}" ]
147 then
148 SSID_TRANS="$(echo ${SSID//[![:word:]]/_})"
149 case ${WEP_TYPE} in
150 1)
151 echo "key_${SSID_TRANS}=\"${WEP_KEY} enc open\"" >> /etc/conf.d/wireless
152 ;;
153 2)
154 echo "key_${SSID_TRANS}=\"s:${WEP_KEY} enc open\"" >> /etc/conf.d/wireless
155 ;;
156 esac
157 fi
158 fi
159 ;;
160 2)
161 :
162 ;;
163 esac
164 echo "preferred_aps=( \"${SSID}\" )" >> /etc/conf.d/wireless
165 echo "associate_order=\"forcepreferredonly\"" >> /etc/conf.d/wireless
166 fi
167 }
168
169 livecd_config_ip() {
170 cd /tmp/setup.opts
171 dialog --title "TCP/IP setup" --menu "You can use DHCP to automatically configure a network interface or you can specify an IP and related settings manually. Choose one option:" 20 60 7 1 "Use DHCP to auto-detect my network settings" 2 "Specify an IP address manually" 2> ${iface}.DHCP
172 DHCP="$(cat ${iface}.DHCP)"
173 case ${DHCP} in
174 1)
175 /sbin/dhcpcd -n -t 10 -h $(hostname) ${iface} &
176 ;;
177 2)
178 dialog --title "IP address" --inputbox "Please enter an IP address for ${iface}:" 20 50 "192.168.1.1" 2> ${iface}.IP
179 IP="$(cat ${iface}.IP)"
180 BC_TEMP="$(echo $IP|cut -d . -f 1).$(echo $IP|cut -d . -f 2).$(echo $IP|cut -d . -f 3).255"
181 dialog --title "Broadcast address" --inputbox "Please enter a Broadcast address for ${iface}:" 20 50 "${BC_TEMP}" 2> ${iface}.BC
182 BROADCAST="$(cat ${iface}.BC)"
183 dialog --title "Network mask" --inputbox "Please enter a Network Mask for ${iface}:" 20 50 "255.255.255.0" 2> ${iface}.NM
184 NETMASK="$(cat ${iface}.NM)"
185 dialog --title "Gateway" --inputbox "Please enter a Gateway for ${iface} (hit enter for none:)" 20 50 2> ${iface}.GW
186 GATEWAY="$(cat ${iface}.GW)"
187 dialog --title "DNS server" --inputbox "Please enter a name server to use (hit enter for none:)" 20 50 2> ${iface}.DNS
188 DNS="$(cat ${iface}.DNS)"
189 /sbin/ifconfig ${iface} ${IP} broadcast ${BROADCAST} netmask ${NETMASK}
190 if [ -n "${GATEWAY}" ]
191 then
192 /sbin/route add default gw ${GATEWAY} dev ${iface} netmask 0.0.0.0 metric 1
193 fi
194 if [ -n "${DNS}" ]
195 then
196 dialog --title "DNS Search Suffix" --inputbox "Please enter any domains which you would like to search on DNS queries (hit enter for none:)" 20 50 2> ${iface}.SUFFIX
197 SUFFIX="$(cat ${iface}.SUFFIX)"
198 echo "nameserver ${DNS}" > /etc/resolv.conf
199 if [ -n "${SUFFIX}" ]
200 then
201 echo "search ${SUFFIX}" >> /etc/resolv.conf
202 fi
203 fi
204 ;;
205 esac
206 }
207
208 livecd_write_net_conf() {
209 cd /tmp/setup.opts
210 echo "# This network configuration was written by net-setup" > /etc/conf.d/net
211 DHCP="$(cat ${iface}.DHCP)"
212 case ${DHCP} in
213 1)
214 echo "iface_${iface}=\"dhcp\"" >> /etc/conf.d/net
215 ;;
216 2)
217 IP="$(cat ${iface}.IP)"
218 BROADCAST="$(cat ${iface}.BC)"
219 NETMASK="$(cat ${iface}.NM)"
220 GATEWAY="$(cat ${iface}.GW)"
221 if [ -n "${IP}" -a -n "${BROADCAST}" -a -n "${NETMASK}" ]
222 then
223 echo "iface_eth0=\"${IP} broadcast ${BROADCAST} netmask ${NETMASK}\"" >> /etc/conf.d/net
224 if [ -n "${GATEWAY}" ]
225 then
226 echo "gateway=\"${iface}/${GATEWAY}\"" >> /etc/conf.d/net
227 fi
228 fi
229 ;;
230 esac
231 }
232
233 livecd_console_settings() {
234 # scan for a valid baud rate
235 case "$1" in
236 300*)
237 LIVECD_CONSOLE_BAUD=300
238 ;;
239 600*)
240 LIVECD_CONSOLE_BAUD=600
241 ;;
242 1200*)
243 LIVECD_CONSOLE_BAUD=1200
244 ;;
245 2400*)
246 LIVECD_CONSOLE_BAUD=2400
247 ;;
248 4800*)
249 LIVECD_CONSOLE_BAUD=4800
250 ;;
251 9600*)
252 LIVECD_CONSOLE_BAUD=9600
253 ;;
254 14400*)
255 LIVECD_CONSOLE_BAUD=14400
256 ;;
257 19200*)
258 LIVECD_CONSOLE_BAUD=19200
259 ;;
260 28800*)
261 LIVECD_CONSOLE_BAUD=28800
262 ;;
263 38400*)
264 LIVECD_CONSOLE_BAUD=38400
265 ;;
266 57600*)
267 LIVECD_CONSOLE_BAUD=57600
268 ;;
269 115200*)
270 LIVECD_CONSOLE_BAUD=115200
271 ;;
272 esac
273 if [ "${LIVECD_CONSOLE_BAUD}" = "" ]
274 then
275 # If it's a virtual console, set baud to 38400, if it's a serial
276 # console, set it to 9600 (by default anyhow)
277 case ${LIVECD_CONSOLE} in
278 tty[0-9])
279 LIVECD_CONSOLE_BAUD=38400
280 ;;
281 *)
282 LIVECD_CONSOLE_BAUD=9600
283 ;;
284 esac
285 fi
286 export LIVECD_CONSOLE_BAUD
287
288 # scan for a valid parity
289 # If the second to last byte is a [n,e,o] set parity
290 local parity
291 parity=$(echo $1 | rev | cut -b 2-2)
292 case "$parity" in
293 [neo])
294 LIVECD_CONSOLE_PARITY=$parity
295 ;;
296 esac
297 export LIVECD_CONSOLE_PARITY
298
299 # scan for databits
300 # Only set databits if second to last character is parity
301 if [ "${LIVECD_CONSOLE_PARITY}" != "" ]
302 then
303 LIVECD_CONSOLE_DATABITS=$(echo $1 | rev | cut -b 1)
304 fi
305 export LIVECD_CONSOLE_DATABITS
306 return 0
307 }
308
309 livecd_read_commandline() {
310 livecd_get_cmdline || return 1
311
312 for x in ${CMDLINE}
313 do
314 case "${x}" in
315 cdroot)
316 CDBOOT="yes"
317 export CDBOOT
318 ;;
319 cdroot\=*)
320 CDBOOT="yes"
321 export CDBOOT
322 ;;
323 console\=*)
324 local live_console
325 live_console=$(livecd_parse_opt "${x}")
326
327 # Parse the console line. No options specified if
328 # no comma
329 LIVECD_CONSOLE=$(echo ${live_console} | cut -f1 -d,)
330 if [ "${LIVECD_CONSOLE}" = "" ]
331 then
332 # no options specified
333 LIVECD_CONSOLE=${live_console}
334 else
335 # there are options, we need to parse them
336 local livecd_console_opts
337 livecd_console_opts=$(echo ${live_console} | cut -f2 -d,)
338 livecd_console_settings ${livecd_console_opts}
339 fi
340 export LIVECD_CONSOLE
341 ;;
342 esac
343 done
344 return 0
345 }
346
347 livecd_fix_inittab() {
348 if [ "${CDBOOT}" = "" ]
349 then
350 return 1
351 fi
352
353 # Create a backup
354 cp -f /etc/inittab /etc/inittab.old
355
356 # Comment out current getty settings
357 sed -i -e '/^c[0-9]/ s/^/#/' /etc/inittab
358
359 # SPARC & HPPA console magic
360 if [ "${HOSTTYPE}" = "sparc" -o "${HOSTTYPE}" = "hppa" -o "${HOSTTYPE}" = "ppc64" ]
361 then
362 # Mount openprom tree for user debugging purposes
363 if [ "${HOSTTYPE}" = "sparc" ]
364 then
365 mount -t openpromfs none /proc/openprom
366 fi
367
368 # SPARC serial port A, HPPA mux / serial
369 if [ -c "/dev/tts/0" ]
370 then
371 LIVECD_CONSOLE_BAUD=$(stty -F /dev/tts/0 speed)
372 echo "s0:12345:respawn:/sbin/agetty -nl /bin/bashlogin ${LIVECD_CONSOLE_BAUD} tts/0 vt100" >> /etc/inittab
373 fi
374 # HPPA software PDC console (K-models)
375 if [ "${LIVECD_CONSOLE}" = "ttyB0" ]
376 then
377 mknod /dev/ttyB0 c 11 0
378 LIVECD_CONSOLE_BAUD=$(stty -F /dev/ttyB0 speed)
379 echo "b0:12345:respawn:/sbin/agetty -nl /bin/bashlogin ${LIVECD_CONSOLE_BAUD} ttyB0 vt100" >> /etc/inittab
380 fi
381 # FB / STI console
382 if [ -c "/dev/vc/1" ]
383 then
384 MODEL_NAME=$(cat /proc/cpuinfo |grep "model name"|sed 's/.*: //')
385 if [ "${MODEL_NAME}" = "UML" ]
386 then
387 for x in 0 1 2 3 4 5 6
388 do
389 echo "c${x}:12345:respawn:/sbin/mingetty --noclear --autologin root tty${x}" >> /etc/inittab
390 done
391 else
392 for x in 1 2 3 4 5 6
393 do
394 echo "c${x}:12345:respawn:/sbin/mingetty --noclear --autologin root tty${x}" >> /etc/inittab
395 done
396 fi
397 fi
398 if [ -c "/dev/hvc0" ]
399 then
400 einfo "Adding hvc console to inittab"
401 echo "s0:12345:respawn:/sbin/agetty -nl /bin/bashlogin 9600 hvc0 vt320" >> /etc/inittab
402 fi
403
404
405 # The rest...
406 else
407 if [ "${LIVECD_CONSOLE}" = "tty0" -o "${LIVECD_CONSOLE}" = "" ]
408 then
409 for x in 1 2 3 4 5 6
410 do
411 echo "c${x}:12345:respawn:/sbin/agetty -nl /bin/bashlogin 38400 tty${x} linux" >> /etc/inittab
412 done
413 else
414 einfo "Adding ${LIVECD_CONSOLE} console to inittab"
415 echo "s0:12345:respawn:/sbin/agetty -nl /bin/bashlogin ${LIVECD_CONSOLE_BAUD} ${LIVECD_CONSOLE} vt100" >> /etc/inittab
416 fi
417 fi
418
419 # EFI-based machines should automatically hook up their console lines
420 if dmesg | grep -q '^Adding console on'
421 then
422 dmesg | grep '^Adding console on' | while read x; do
423 line=`echo "$x" | cut -d' ' -f4`
424 id=e`echo "$line" | grep -o '.\{1,3\}$'`
425 [ "${line}" = "${LIVECD_CONSOLE}" ] && continue # already setup above
426 case "$x" in
427 *options\ \'[0-9]*) speed=`echo "$x" | sed "s/.*options '//; s/[^0-9].*//"` ;;
428 *) speed=9600 ;; # choose a default, only matters if it is serial
429 esac
430 echo "$id:12345:respawn:/sbin/agetty -nl /bin/bashlogin ${speed} ${line} vt100" >> /etc/inittab
431 done
432 fi
433
434 # force reread of inittab
435 kill -HUP 1
436 return 0
437 }

Properties

Name Value
svn:eol-style native
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20