slitaz-tools view tinyutils/tazhw @ rev 435

improve firewall and iptables_rules (thanks gokhlayeh)
author Rohit Joshi <jozee@slitaz.org>
date Fri Mar 12 12:01:54 2010 +0000 (2010-03-12)
parents f8bb0d243acf
children f3b7d1f4c8ba
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 GTKdialog, and is
7 # called by args 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 # Detect PCI devices and load kernel module only at first boot,
88 # in LiveCD mode or with the command 'detect-pci'.
89 detect_pci_devices()
90 {
91 if [ ! -s /var/lib/detected-modules ]; then
92 . /etc/rcS.conf
93 # We need module_name to match output of lsmod.
94 list=`lspci -k | grep "modules" | cut -d ":" -f 2 | sed s/-/_/g`
95 echo "$list" > /var/lib/detected-modules
96 for mod in $list
97 do
98 check_firmware
99 load_module
100 done
101 # yenta_socket = laptop
102 if `lsmod | grep -q "yenta_socket"`; then
103 grep -qs batt /etc/lxpanel/default/panels/panel ||
104 sed -i 's/= cpu/= batt\n}\n\nPlugin {\n type = cpu/' \
105 /etc/lxpanel/default/panels/panel 2> /dev/null
106 fi
107 fi
108 }
110 # Detect all USB devices.
111 detect_usb_devices()
112 {
113 if [ -f /proc/bus/usb/devices -a ! -s /var/lib/detected-usb-modules ]; then
114 rm -f /var/lib/detected-usb-modules
115 cat /proc/bus/usb/devices | grep "Vendor" | while read line ; do
116 ID=`echo "$line" | awk '{ print $2,$3 }' | sed 's/ / /' | \
117 sed 's/Vendor=/0x/' | sed 's/ProdID=/0x/'`
118 if grep -q "$ID" /lib/modules/`uname -r`/modules.usbmap; then
119 mod=`grep "$ID" /lib/modules/$(uname -r)/modules.usbmap | \
120 awk '{ print $1 }'`
121 prod=`grep -A 2 "$line" /proc/bus/usb/devices | grep Product | \
122 cut -d "=" -f 2`
123 echo "$prod"
124 check_firmware
125 load_module
126 echo " $mod" >> /var/lib/detected-usb-modules
127 fi
128 done
129 fi
130 }
132 display_line()
133 {
134 echo "================================================================================"
135 }
137 # Box functions and dialog
139 box_list()
140 {
141 for mod in `cat /var/lib/detected-modules /var/lib/detected-usb-modules 2>/dev/null`
142 do
143 desc=`modinfo $mod | grep ^description | cut -d ":" -f 2`
144 [ -z "$desc" ] && desc="N/A"
145 echo "$mod | $desc"
146 done
147 }
149 box_detect_devices()
150 {
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()
168 {
169 export BIN=$0
170 export MAIN_DIALOG='
171 <window title="Tazhw Box" icon-name="computer">
172 <vbox>
174 <text width-chars="54" use-markup="true">
175 <label>"
176 <b>Hardware auto-detection and configuration tool</b>
177 "</label>
178 </text>
179 <tree>
180 <width>520</width><height>180</height>
181 <variable>MODULE</variable>
182 <label>Kernel Module|Description</label>
183 <input>$BIN box_list</input>
184 <action>refresh:MODULE</action>
185 </tree>
186 <hbox>
187 <checkbox>
188 <label>Auto install non-free Firmware</label>
189 <variable>INSTALL_FIRMARE</variable>
190 <default>true</default>
191 </checkbox>
192 <button>
193 <label>Detect PCI/USB devices</label>
194 <input file icon="forward"></input>
195 <action>$BIN box_detect_devices</action>
196 <action>refresh:MODULE</action>
197 </button>
198 <button>
199 <label>Exit</label>
200 <input file icon="exit"></input>
201 <action type="exit">Exit</action>
202 </button>
203 </hbox>
204 </vbox>
205 </window>'
206 gtkdialog --center --program=MAIN_DIALOG >/dev/null
207 }
209 # Get firmware used by check_firmware()
210 if [ "$2" == "--get-firmware" ]; then
211 firmware='get'
212 fi
214 # What to do.
215 case "$1" in
216 init)
217 check_root
218 echo "Detecting PCI devices Kernel modules..."
219 detect_pci_devices
220 echo "Detecting USB devices Kernel modules..."
221 detect_usb_devices ;;
222 detect-pci)
223 check_root
224 echo -e "\nDetected PCI devices Kernel modules" && display_line
225 rm -f /var/lib/detected-modules
226 detect_pci_devices
227 display_line && echo "" ;;
228 detect-usb)
229 check_root
230 echo -e "\nDetected USB devices Kernel modules" && display_line
231 rm -f /var/lib/detected-usb-modules
232 detect_usb_devices
233 display_line && echo "" ;;
234 box*)
235 box_check_root
236 $1 ;;
237 detected-modules)
238 echo -e "\nDetected PCI and USB modules" && display_line
239 cat /var/lib/detected-modules
240 cat /var/lib/detected-usb-modules 2>/dev/null
241 display_line && echo "" ;;
242 *)
243 usage ;;
244 esac
246 exit 0