slitaz-tools view tinyutils/tazhw @ rev 813

Current state, features stabilized and open for bugfixes and translations.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon Sep 09 03:27:04 2013 +0300 (2013-09-09)
parents 72c2ef5c57e7
children c79e656b37a5
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 Adapters
5 # need non-free firmware not installed by default. Users must use the
6 # option --get-firmware to force installation. GUI uses Yad, and is
7 # called by args such as all box functions.
8 #
9 # (c) 2009-2013 SliTaz GNU/Linux - GNU GPL v3
10 #
11 # Authors: Christophe Lincoln <pankso@slitaz.org>
12 # Rohit Joshi <jozee@slitaz.org>
13 #
14 . /lib/libtaz.sh
15 export TEXTDOMAIN='slitaz-boxes' #i18n
17 usage() {
18 cat << EOT
20 $(_n 'SliTaz Hardware configuration')
22 $(boldify "$(_n 'Usage:')") $(basename $0) [$(_n 'command')] [--$(_n 'option')]
24 $(boldify "$(_n 'Commands:')")
25 usage $(_n 'Print this short usage.')
26 box $(_n 'Start in graphical mode.')
27 init $(_n 'Used at boot time to configure devices.')
28 setup $(_n 'Setup hardware devices.')
29 detect-pci $(_n 'Detect all PCI devices.')
30 detect-usb $(_n 'Detect all USB devices.')
31 detected-modules $(_n 'List all detected Kernel modules.')
33 $(boldify "$(_n 'Options:')")
34 --get-firmware $(_n 'Get and install non-free firmware (PCI and USB).')
36 EOT
37 }
39 # Check if user is root to install, or remove packages.
40 check_root() {
41 if test $(id -u) != 0 ; then
42 echo -e "\nYou must be root to run `basename $0` with this option."
43 echo -e "Please use 'su' and root password to become super-user.\n"
44 exit 0
45 fi
46 }
48 box_check_root() {
49 if test $(id -u) != 0 ; then
50 exec subox tazhw box
51 exit 0
52 fi
53 }
55 check_firmware() {
56 if [ -x /usr/bin/get-$mod-firmware ]; then
57 if [ ! -d /var/lib/tazpkg/installed/$mod-firmware ]; then
58 # We need an active connection to install firmware and we
59 # only install firmware if specified from cmdline.
60 if ifconfig | grep -q "inet addr"; then
61 # Ensure module is not loaded and get files.
62 if [ "$firmware" == "get" ]; then
63 rmmod $mod 2>/dev/null
64 get-$mod-firmware
65 else
66 _ '* Use --get-firmware option to install missing files.'
67 fi
68 else
69 _ '* No active connection to get and install firmware.'
70 fi
71 else
72 _ '> Firmware in use: $mod-firmware'
73 fi
74 fi
75 }
77 load_module() {
78 if ! lsmod | grep -q "^$mod"; then
80 # lsmod and some modules use underscore, others use dash.
81 # The real modprobe can handle both but not busybox version
82 modprobe $mod 2>/dev/null && \
83 _ '* Loaded module: $mod' || \
84 _ '! Missing module: $mod'
86 # Add it to load automatically at next boot.
87 if ! echo "$LOAD_MODULES" | grep -q "$mod"; then
88 sed -i s/"LOAD_MODULES=\"$LOAD_MODULES\""/"LOAD_MODULES=\"$LOAD_MODULES $mod\""/ \
89 /etc/rcS.conf
90 fi
91 . /etc/rcS.conf
93 else
94 _ '> Module in use: $mod'
95 fi
96 }
98 # Detect PCI devices and load kernel module only at first boot,
99 # in LiveCD mode or with the command 'detect-pci'.
100 detect_pci_devices() {
101 if [ ! -s /var/lib/detected-modules ]; then
102 . /etc/rcS.conf
103 # We need module_name to match output of lsmod.
104 list=$(lspci -k | grep "modules" | cut -d ":" -f 2 | sed s/-/_/g)
105 echo "$list" > /var/lib/detected-modules
106 for mod in $list; do
107 check_firmware
108 load_module
109 done
110 # yenta_socket = laptop
111 if $(lsmod | grep -q "yenta_socket"); then
112 grep -qs batt /etc/lxpanel/default/panels/panel ||
113 sed -i 's/= cpu/= batt\n}\n\nPlugin {\n type = cpu/' \
114 /etc/lxpanel/default/panels/panel 2> /dev/null
115 fi
116 fi
117 }
119 # Detect all USB devices.
120 detect_usb_devices() {
121 if [ -f /proc/bus/usb/devices -a ! -s /var/lib/detected-usb-modules ]; then
122 rm -f /var/lib/detected-usb-modules
123 cat /proc/bus/usb/devices | grep "Vendor" | while read line ; do
124 ID=$(echo "$line" | awk '{ print $2,$3 }' | sed 's/ / /' | \
125 sed 's/Vendor=/0x/' | sed 's/ProdID=/0x/')
126 if grep -q "$ID" /lib/modules/$(uname -r)/modules.usbmap; then
127 mod=$(grep "$ID" /lib/modules/$(uname -r)/modules.usbmap | \
128 awk '{ print $1 }')
129 prod=$(grep -A 2 "$line" /proc/bus/usb/devices | grep Product | \
130 cut -d"=" -f2)
131 echo "$prod"
132 check_firmware
133 load_module
134 echo " $mod" >> /var/lib/detected-usb-modules
135 fi
136 done
137 fi
138 }
140 # Box functions and dialog
142 box_list() {
143 for mod in $(cat /var/lib/detected-modules /var/lib/detected-usb-modules 2>/dev/null); do
144 desc=$(modinfo $mod | grep ^description | cut -d":" -f2)
145 [ -z "$desc" ] && desc="N/A"
146 echo "$mod | $desc"
147 done
148 }
150 box_detect_devices() {
151 if [ $INSTALL_FIRMARE != true ]; then
152 xterm -T "Detect devices" \
153 -geometry 80x24 \
154 -e "$0 detect-pci; $0 detect-usb; \
155 echo -e \"----\nPress ENTER to close...\"; \
156 read i; exit 0"
157 else
158 xterm -T "Detect and get firmware" \
159 -geometry 80x24 \
160 -e "$0 detect-pci --get-firmware; \
161 $0 detect-usb --get-firmware; \
162 echo -e \"----\nPress ENTER to close...\"; \
163 read i; exit 0"
164 fi
165 }
167 box_setup_devices() {
168 SETUP_OPTIONS=""
169 if [ $INSTALL_NON_FREE == true ]; then
170 SETUP_OPTIONS="$SETUP_OPTIONS --non-free"
171 fi
172 if [ $AUTO_INSTALL_SUGGESTED == true ]; then
173 SETUP_OPTIONS="$SETUP_OPTIONS --suggested"
174 elif [ $CONFIRM_INSTALL_SUGGESTED == true ]; then
175 SETUP_OPTIONS="$SETUP_OPTIONS --confirm"
176 fi
177 xterm -geometry 80x24 -T "$DEVICE Setup" \
178 -e "$0 setup $DEVICE $SETUP_OPTIONS; \
179 echo -e \"----\nPress ENTER to close...\"; \
180 read i; exit 0"
181 }
183 box() {
184 export BIN=$0
185 export MAIN_DIALOG='
186 <window title="Tazhw box" icon-name="computer">
187 <vbox>
188 <text width-chars="54" use-markup="true">
189 <label>"<b>Hardware auto-detection and configuration tool</b>"</label>
190 </text>
191 <frame Loaded Kernel Modules>
192 <vbox>
193 <tree>
194 <width>470</width>
195 <height>180</height>
196 <variable>MODULE</variable>
197 <label>Module|Description</label>
198 <input>$BIN box_list</input>
199 <action>refresh:MODULE</action>
200 </tree>
201 <hbox>
202 <checkbox>
203 <label>Auto install non-free Firmware </label>
204 <variable>INSTALL_FIRMARE</variable>
205 <default>false</default>
206 </checkbox>
207 <button>
208 <label>Discover PCI/USB devices</label>
209 <input file icon="folder-saved-search"></input>
210 <action>$BIN box_detect_devices</action>
211 <action>refresh:MODULE</action>
212 </button>
213 </hbox>
214 </vbox>
215 </frame>
216 <frame Hardware Setup>
217 <hbox>
218 <button>
219 <label>Printer</label>
220 <input file icon="printer"></input>
221 <action>export DEVICE="printer";$BIN box_setup_devices</action>
222 </button>
223 <button>
224 <label>Scanner</label>
225 <input file icon="x-office-document"></input>
226 <action>export DEVICE="scanner";$BIN box_setup_devices</action>
227 </button>
228 <button>
229 <label>3G-Modem</label>
230 <input file icon="network-transmit-receive"></input>
231 <action>export DEVICE="3g-modem";$BIN box_setup_devices</action>
232 </button>
233 <button>
234 <label>Nvidia</label>
235 <input file icon="video-display"></input>
236 <action>export DEVICE="nvidia";$BIN box_setup_devices</action>
237 </button>
238 <button>
239 <label>ATI Radeon</label>
240 <input file icon="video-display"></input>
241 <action>export DEVICE="ati";$BIN box_setup_devices</action>
242 </button>
243 </hbox>
244 <hbox>
245 <button>
246 <label>Webcam </label>
247 <input file icon="camera-video"></input>
248 <action>export DEVICE="webcam";$BIN box_setup_devices</action>
249 </button>
250 <button>
251 <label>Digital Camera </label>
252 <input file icon="camera-photo"></input>
253 <action>export DEVICE="camera";$BIN box_setup_devices</action>
254 </button>
255 <button>
256 <label>Bluetooth </label>
257 <input file icon="preferences-desktop-multimedia"></input>
258 <action>export DEVICE="bluetooth";$BIN box_setup_devices</action>
259 </button>
260 <button>
261 <label>Firewall </label>
262 <input file icon="gtk-network"></input>
263 <action>export DEVICE="firewall";$BIN box_setup_devices</action>
264 </button>
265 </hbox>
266 <hbox>
267 <checkbox>
268 <label>Install non-free packages </label>
269 <variable>INSTALL_NON_FREE</variable>
270 <default>true</default>
271 </checkbox>
272 <checkbox>
273 <label>Auto-install all optional packages </label>
274 <variable>AUTO_INSTALL_SUGGESTED</variable>
275 <default>false</default>
276 </checkbox>
277 </hbox>
278 <hbox>
279 <checkbox>
280 <label>Choose optional packages to install</label>
281 <variable>CONFIRM_INSTALL_SUGGESTED</variable>
282 <default>false</default>
283 </checkbox>
284 </hbox>
285 </frame>
286 <hbox>
287 <button>
288 <label>Exit</label>
289 <input file icon="exit"></input>
290 <action type="exit">Exit</action>
291 </button>
292 </hbox>
293 </vbox>
294 </window>'
296 gtkdialog --center --program=MAIN_DIALOG >/dev/null
297 }
299 # Get firmware used by check_firmware()
300 if [ "$2" == "--get-firmware" ]; then
301 firmware='get'
302 fi
304 # What to do.
305 case "$1" in
306 init)
307 check_root
308 _ 'Detecting PCI devices Kernel modules...'
309 detect_pci_devices
310 _ 'Detecting USB devices Kernel modules...'
311 detect_usb_devices ;;
312 detect-pci)
313 check_root
314 newline; _ 'Detected PCI devices Kernel modules'; separator
315 rm -f /var/lib/detected-modules
316 detect_pci_devices
317 separator; newline ;;
318 detect-usb)
319 check_root
320 newline; _ 'Detected USB devices Kernel modules'; separator
321 rm -f /var/lib/detected-usb-modules
322 detect_usb_devices
323 separator; newline ;;
324 setup)
325 SETUP_OPTIONS=$(echo "$@" | sed 's/setup//')
326 hwsetup $SETUP_OPTIONS ;;
327 box*)
328 box_check_root
329 $1 ;;
330 detected-modules)
331 newline; _ 'Detected PCI and USB modules'; separator
332 cat /var/lib/detected-modules
333 cat /var/lib/detected-usb-modules 2>/dev/null
334 separator; newline ;;
335 *)
336 usage ;;
337 esac
339 exit 0