slitaz-tools view tinyutils/tazhw @ rev 312

tazctrlbox: improved date and time setting + users management
author Christophe Lincoln <pankso@slitaz.org>
date Tue Mar 03 23:17:19 2009 +0100 (2009-03-03)
parents 67d086ba0452
children af65458ca488
line source
1 #!/bin/sh
2 #
3 # SliTaz Hardware configuration tool. Auto-detect and configure in a
4 # simple way all PCI, PCMCIA and USB devices. Some Wireless Adapaters
5 # need non-free firmware not installed by default, users must use the
6 # option --get-firmware to force installation. GUI use GTKdialog, is
7 # called by arg such as all box functions.
8 #
9 # (c) 2009 SliTaz GNU/Linux - GNU gpl v3
10 #
12 usage()
13 {
14 echo -e "\nSliTaz Hardware configuration\n
15 \033[1mUsage: \033[0m `basename $0` [command] [--option]
16 \033[1mCommands: \033[0m\n
17 usage Print this short usage.
18 box Start in graphical mode.
19 init Used at boot time to configure devices.
20 detect-pci Detect all PCI devices.
21 detect-usb Detect all USB devices.
22 detected-modules List all detected Kernel modules.
24 \033[1mOptions: \033[0m\n
25 --get-firmware Get and install non-free firmware (PCI and USB).\n"
26 }
28 # Check if user is root to install, or remove packages.
29 check_root()
30 {
31 if test $(id -u) != 0 ; then
32 echo -e "\nYou must be root to run `basename $0` with this option."
33 echo -e "Please use 'su' and root password to become super-user.\n"
34 exit 0
35 fi
36 }
37 box_check_root()
38 {
39 if test $(id -u) != 0 ; then
40 exec subox tazhw box
41 exit 0
42 fi
43 }
45 check_firmware()
46 {
47 if [ -x /usr/bin/get-$mod-firmware ]; then
48 if [ ! -d /var/lib/tazpkg/installed/$mod-firmware ]; then
49 # We need and active connection to install firmware and we
50 # only install firmware if specified from cmdline.
51 if ifconfig | grep -q "inet addr"; then
52 # Ensure module is not loaded and get files.
53 if [ "$firmware" == "get" ]; then
54 rmmod $mod 2>/dev/null
55 get-$mod-firmware
56 else
57 echo "* Use --get-firmware option to install missing files."
58 fi
59 else
60 echo "* No active connection to get and install firmware."
61 fi
62 else
63 echo "> Firmware in use: $mod-firmware"
64 fi
65 fi
66 }
68 load_module()
69 {
70 if ! lsmod | grep -q "^$mod"; then
71 # lsmod and some modules use underscore, other use dash.
72 # The real modprobe can handle both but not busybox version
73 modprobe $mod 2>/dev/null && \
74 echo "* Loaded module: $mod" || \
75 echo "! Missing module: $mod"
76 else
77 echo "> Module in use: $mod"
78 fi
79 # Add module to rcS.conf and avoid duplication.
80 . /etc/rcS.conf
81 if ! echo "$LOAD_MODULES" | grep -q "$mod"; then
82 sed -i s/"LOAD_MODULES=\"$LOAD_MODULES\""/"LOAD_MODULES=\"$LOAD_MODULES $mod\""/ \
83 /etc/rcS.conf
84 fi
85 }
87 # Retry a Ethernet connection with DHCP after detection (used at boot).
88 # TODO: try a wireless connection but stop eth0 to avoid conflict.
89 start_network()
90 {
91 if ifconfig -a | grep -q "eth0"; then
92 if [ ! -f /var/run/udhcpc.eth0.pid ]; then
93 echo "Starting udhcpc client on: eth0... "
94 /sbin/udhcpc -b -i eth0 -p /var/run/udhcpc.eth0.pid
95 fi
96 fi
97 }
99 # Detect PCI devices and load kernel module only at first boot,
100 # in LiveCD mode or with the command 'detect-pci'.
101 detect_pci_devices()
102 {
103 if [ ! -s /var/lib/detected-modules ]; then
104 . /etc/rcS.conf
105 # We need module_name to match output of lsmod.
106 list=`lspci -k | grep "modules" | cut -d ":" -f 2 | sed s/-/_/g`
107 echo "$list" > /var/lib/detected-modules
108 for mod in $list
109 do
110 check_firmware
111 load_module
112 done
113 # yenta_socket = laptop
114 if `lsmod | grep -q "yenta_socket"`; then
115 for mod in ac battery
116 do
117 load_module
118 echo " $mod" >> /var/lib/detected-modules
119 done
120 sed -i 's/= cpu/= batt\n}\n\nPlugin {\n type = cpu/' \
121 /etc/lxpanel/default/panels/panel 2> /dev/null
122 fi
123 fi
124 }
126 # Detect all USB devices.
127 detect_usb_devices()
128 {
129 if [ ! -s /var/lib/detected-usb-modules ]; then
130 rm -f /var/lib/detected-usb-modules
131 cat /proc/bus/usb/devices | grep "Vendor" | while read line ; do
132 ID=`echo "$line" | awk '{ print $2,$3 }' | sed 's/ / /' | \
133 sed 's/Vendor=/0x/' | sed 's/ProdID=/0x/'`
134 if grep -q "$ID" /lib/modules/*-slitaz/modules.usbmap; then
135 mod=`grep "$ID" /lib/modules/*-slitaz/modules.usbmap | \
136 awk '{ print $1 }'`
137 prod=`grep -A 2 "$line" /proc/bus/usb/devices | grep Product | \
138 cut -d "=" -f 2`
139 echo "$prod"
140 check_firmware
141 load_module
142 echo " $mod" >> /var/lib/detected-usb-modules
143 fi
144 done
145 fi
146 }
148 display_line()
149 {
150 echo "================================================================================"
151 }
153 # Box functions and dialog
155 box_list()
156 {
157 for mod in `cat /var/lib/detected-modules /var/lib/detected-usb-modules 2>/dev/null`
158 do
159 desc=`modinfo $mod | grep ^description | cut -d ":" -f 2`
160 [ -z "$desc" ] && desc="N/A"
161 echo "$mod | $desc"
162 done
163 }
165 box_detect_devices()
166 {
167 if [ $INSTALL_FIRMARE != true ]; then
168 xterm -T "Detect devices" \
169 -geometry 80x24 \
170 -e "$0 detect-pci; $0 detect-usb; \
171 echo -e \"----\nPress ENTER to close...\"; \
172 read i; exit 0"
173 else
174 xterm -T "Detect and get firmware" \
175 -geometry 80x24 \
176 -e "$0 detect-pci --get-firmware; \
177 $0 detect-usb --get-firmware; \
178 echo -e \"----\nPress ENTER to close...\"; \
179 read i; exit 0"
180 fi
181 }
183 box()
184 {
185 export BIN=$0
186 export MAIN_DIALOG='
187 <window title="Tazhw Box" icon-name="computer">
188 <vbox>
190 <text width-chars="54" use-markup="true">
191 <label>"
192 <b>Hardware auto-detection and configuration tool</b>
193 "</label>
194 </text>
195 <tree>
196 <width>520</width><height>180</height>
197 <variable>MODULE</variable>
198 <label>Kernel Module|Description</label>
199 <input>$BIN box_list</input>
200 <action>refresh:MODULE</action>
201 </tree>
202 <hbox>
203 <checkbox>
204 <label>Auto install non-free Firmware</label>
205 <variable>INSTALL_FIRMARE</variable>
206 <default>false</default>
207 </checkbox>
208 <button>
209 <label>Detect PCI/USB devices</label>
210 <input file icon="forward"></input>
211 <action>$BIN box_detect_devices</action>
212 <action>refresh:MODULE</action>
213 </button>
214 <button>
215 <label>Exit</label>
216 <input file icon="exit"></input>
217 <action type="exit">Exit</action>
218 </button>
219 </hbox>
220 </vbox>
221 </window>'
222 gtkdialog --center --program=MAIN_DIALOG >/dev/null
223 }
225 # Get firmware used by check_firmware()
226 if [ "$2" == "--get-firmware" ]; then
227 firmware='get'
228 fi
230 # What to do.
231 case "$1" in
232 init)
233 check_root
234 echo "Detecting PCI devices Kernel modules..."
235 detect_pci_devices
236 echo "Detecting USB devices Kernel modules..."
237 detect_usb_devices
238 start_network ;;
239 detect-pci)
240 check_root
241 echo -e "\nDetected PCI devices Kernel modules" && display_line
242 rm -f /var/lib/detected-modules
243 detect_pci_devices
244 display_line && echo "" ;;
245 detect-usb)
246 check_root
247 echo -e "\nDetected USB devices Kernel modules" && display_line
248 rm -f /var/lib/detected-usb-modules
249 detect_usb_devices
250 display_line && echo "" ;;
251 box*)
252 box_check_root
253 $1 ;;
254 detected-modules)
255 echo -e "\nDetected PCI and USB modules" && display_line
256 cat /var/lib/detected-modules
257 cat /var/lib/detected-usb-modules 2>/dev/null
258 display_line && echo "" ;;
259 *)
260 usage ;;
261 esac
263 exit 0