Annotation of /trunk/installer/include/hwdetection.sh
Parent Directory | Revision Log
Revision 1027 -
(hide annotations)
(download)
(as text)
Sun May 30 17:49:14 2010 UTC (14 years, 4 months ago) by niro
File MIME type: application/x-sh
File size: 8089 byte(s)
Sun May 30 17:49:14 2010 UTC (14 years, 4 months ago) by niro
File MIME type: application/x-sh
File size: 8089 byte(s)
-fixed header -added remove_duplicates to reduce modules to one instance -fixed x11 module handling -added get_evdev_device_path to read event devices (used by kdrive loader script) -added get_netcard_driver_modules() -added get_blkid_information() for uuid/label support -better fstype guessing with new get_fstype() -added get_uuid() to convert partitions to uuids -added framebuffer support via uvesafb and use 1024x768x16@60 as standard resolution -add more generic modules to initrd
1 | niro | 773 | #!/bin/bash |
2 | niro | 1027 | # $Id$ |
3 | niro | 773 | |
4 | ## hwdetection, needs >= sys-apps/hwinfo | ||
5 | |||
6 | # hwinfo wrapper to use hwinfo with sudo if requested | ||
7 | hwinfo() | ||
8 | { | ||
9 | local use_sudo="" | ||
10 | case ${SUDO} in | ||
11 | yes|y|true|1) use_sudo=sudo ;; | ||
12 | esac | ||
13 | |||
14 | ${use_sudo} /usr/sbin/hwinfo $@ | ||
15 | } | ||
16 | |||
17 | # get_hwinfo $hw_item | ||
18 | get_hwinfo() | ||
19 | { | ||
20 | local enable_desc="0" | ||
21 | |||
22 | if [[ $1 = --with-description ]] || [[ $1 = -d ]] | ||
23 | then | ||
24 | enable_desc=1 | ||
25 | shift | ||
26 | fi | ||
27 | |||
28 | local item="$1" | ||
29 | local all | ||
30 | local i | ||
31 | |||
32 | # handle special items | ||
33 | case ${item} in | ||
34 | memory) get_hwinfo_memory; return ;; | ||
35 | smp) get_hwinfo_smp; return ;; | ||
36 | esac | ||
37 | |||
38 | all=$(hwinfo --short --"${item}") | ||
39 | |||
40 | declare -i i=0 | ||
41 | while read device description | ||
42 | do | ||
43 | # skip the first line | ||
44 | (( i++ )) | ||
45 | [ ${i} -eq 1 ] && continue | ||
46 | |||
47 | if [[ ${enable_desc} = 1 ]] | ||
48 | then | ||
49 | echo "${device};${description}" | ||
50 | else | ||
51 | echo "${device}" | ||
52 | fi | ||
53 | done <<< "${all}" | ||
54 | } | ||
55 | |||
56 | |||
57 | # special memory case | ||
58 | get_hwinfo_memory() | ||
59 | { | ||
60 | local memory=$(hwinfo --memory | grep "Memory Size" | cut -d: -f2 | sed "s:\ ::") | ||
61 | echo "${memory}" | ||
62 | } | ||
63 | |||
64 | |||
65 | # special smp case | ||
66 | get_hwinfo_smp() | ||
67 | { | ||
68 | local smp=$(hwinfo --smp | grep "SMP support:" | cut -d: -f2 | sed "s:\ ::") | ||
69 | echo "${smp}" | ||
70 | } | ||
71 | |||
72 | |||
73 | # get_driver_modules $hw_item | ||
74 | get_driver_modules() | ||
75 | { | ||
76 | local item="$1" | ||
77 | local modules | ||
78 | |||
79 | # being little tricky here :) | ||
80 | # does following: | ||
81 | # grep: | ||
82 | # -> 'Driver Modules: "via82cxxx", "ide_cd"' | ||
83 | # cut: | ||
84 | # -> ' "via82cxxx", "ide_cd"' | ||
85 | # sed1: | ||
86 | # -> ' modules="via82cxxx", "ide_cd"' | ||
87 | # sed2: | ||
88 | # -> ' modules="via82cxxx ide_cd"' | ||
89 | # then evaluate -> eval modules="via82cxxx ide_cd" | ||
90 | # and we get a variable $modules with all mods | ||
91 | #eval $(hwinfo --"${item}" | grep "Driver Modules:" | cut -d: -f2 | sed -e "s:\":modules=\":" -e "s:\",\ \":\ :") | ||
92 | |||
93 | local i | ||
94 | for i in $(hwinfo --"${item}" | grep "Driver Modules:" | cut -d: -f2 ) | ||
95 | do | ||
96 | eval $(echo "${i}" | sed -e "s:\":modules=\"\${modules}\ :" -e "s:,:\ :") | ||
97 | done | ||
98 | niro | 1027 | remove_duplicates "${modules}" |
99 | niro | 773 | } |
100 | |||
101 | # remove_duplicates $list | ||
102 | remove_duplicates() | ||
103 | { | ||
104 | local list="$@" | ||
105 | local fixed_list=":" | ||
106 | local i | ||
107 | |||
108 | for i in ${list} | ||
109 | do | ||
110 | if [[ -z $(echo "${fixed_list}" | fgrep ":${i}:") ]] | ||
111 | then | ||
112 | fixed_list="${fixed_list}${i}:" | ||
113 | fi | ||
114 | done | ||
115 | |||
116 | unset list | ||
117 | |||
118 | # remove all ':' and show the cleaned list | ||
119 | # this way fixes double spaces also | ||
120 | niro | 1027 | local counter |
121 | declare -i counter=0 | ||
122 | niro | 773 | for i in $(echo ${fixed_list} | sed "s|:| |g") |
123 | do | ||
124 | niro | 1027 | (( counter++ )) |
125 | if [[ ${counter} -eq 1 ]] | ||
126 | then | ||
127 | list="${i}" | ||
128 | else | ||
129 | list+=" ${i}" | ||
130 | fi | ||
131 | niro | 773 | done |
132 | |||
133 | echo "${list}" | ||
134 | } | ||
135 | |||
136 | get_x11_driver_modules() | ||
137 | { | ||
138 | local modules | ||
139 | niro | 1027 | modules="$(hwinfo --gfxcard | grep 'XFree86 v4 Server Module:' | sed 's/.*Module:\ \(.*\)/\1/')" |
140 | niro | 773 | |
141 | # remove duplicates from list and show it | ||
142 | remove_duplicates "${modules}" | ||
143 | } | ||
144 | |||
145 | niro | 1027 | get_evdev_device_path() |
146 | { | ||
147 | local device="$1" | ||
148 | local path | ||
149 | |||
150 | case ${device} in | ||
151 | mouse|keyboard) true;; | ||
152 | *) die "unkown device";; | ||
153 | esac | ||
154 | |||
155 | path="$(hwinfo --${device} | grep 'Device Files:' | sed 's:.*\(/dev/input/event[0-5]\).*:\1:')" | ||
156 | echo "${path}" | ||
157 | } | ||
158 | |||
159 | # get_netcard_driver_modules device | ||
160 | # e.g. get_netcard_driver_modules eth0 | ||
161 | get_netcard_driver_modules() | ||
162 | { | ||
163 | local device="$1" | ||
164 | local modules | ||
165 | |||
166 | if [[ -z ${device} ]] | ||
167 | then | ||
168 | echo "Error: get_netcard_driver_module(): no device given" | ||
169 | return 1 | ||
170 | fi | ||
171 | |||
172 | modules=$(hwinfo --netcard | grep -B1 "Device File: ${device}" | sed 's/.*Modules: \"\(.*\)\"/\1/;q') | ||
173 | remove_duplicates "${modules}" | ||
174 | } | ||
175 | |||
176 | get_blkid_information() | ||
177 | { | ||
178 | local partition="$1" | ||
179 | local variable="$2" | ||
180 | local method | ||
181 | |||
182 | case $1 in | ||
183 | --uuid|-u) shift; method="-U"; partition="$1"; variable="$2" ;; | ||
184 | --label|-l) shift; method="-L"; partition="$1"; variable="$2" ;; | ||
185 | esac | ||
186 | |||
187 | if [[ -z ${partition} ]] | ||
188 | then | ||
189 | echo "Error: get_get_blkid_information()(): no partition given" | ||
190 | return 1 | ||
191 | fi | ||
192 | |||
193 | if [[ -z ${variable} ]] | ||
194 | then | ||
195 | echo "Error: get_get_blkid_information()(): no variable given" | ||
196 | return 1 | ||
197 | fi | ||
198 | |||
199 | blkid -o value -s "${variable}" "${method}" "${partition}" | ||
200 | } | ||
201 | |||
202 | get_fstype() | ||
203 | { | ||
204 | local partition="$1" | ||
205 | if [[ -z ${partition} ]] | ||
206 | then | ||
207 | echo "Error: get_fstype(): no partition given" | ||
208 | return 1 | ||
209 | fi | ||
210 | |||
211 | # does not work with uuids | ||
212 | #mount -f --guess-fstype ${partition} | ||
213 | |||
214 | get_blkid_information ${partition} TYPE | ||
215 | } | ||
216 | |||
217 | get_uuid() | ||
218 | { | ||
219 | local partition="$1" | ||
220 | if [[ -z ${partition} ]] | ||
221 | then | ||
222 | echo "Error: get_uuid(): no partition given" | ||
223 | return 1 | ||
224 | fi | ||
225 | |||
226 | # does not work with uuids | ||
227 | #mount -f --guess-fstype ${partition} | ||
228 | |||
229 | get_blkid_information ${partition} UUID | ||
230 | } | ||
231 | |||
232 | niro | 773 | # create_initrd {/path/to/initrd kernelversion </path/to/config>} |
233 | # when nothing given then /boot/initrd-$(uname -r).img $(uname -r) will be used | ||
234 | # default config path is /etc/conf.d/mkinitrd | ||
235 | create_initrd() | ||
236 | { | ||
237 | local initrd | ||
238 | local kernel | ||
239 | local config | ||
240 | local chroot | ||
241 | #local root | ||
242 | local modules | ||
243 | niro | 1027 | # enabled framebuffer modules as default |
244 | local framebuffer=1 | ||
245 | local uvesafb_config="/etc/modprobe.d/uvesafb.conf" | ||
246 | niro | 773 | |
247 | # very basic getops | ||
248 | for i in $* | ||
249 | do | ||
250 | case $1 in | ||
251 | #--root|-r) shift; root="$1" ;; | ||
252 | --initrd|-i) shift; initrd="$1" ;; | ||
253 | --config|-c) shift; config="$1" ;; | ||
254 | --kernelversion|-v) shift; kernel="$1" ;; | ||
255 | niro | 1027 | --nofb) shift; framebuffer=0 |
256 | niro | 773 | esac |
257 | shift | ||
258 | done | ||
259 | |||
260 | [[ -z ${initrd} ]] && initrd="/boot/initrd-$(uname -r).img" | ||
261 | [[ -z ${kernel} ]] && kernel="$(uname -r)" | ||
262 | [[ -z ${config} ]] && config="/etc/conf.d/mkinitrd" | ||
263 | |||
264 | if [[ ! -z ${INSTALL_ROOT} ]] | ||
265 | then | ||
266 | config="${INSTALL_ROOT}/${config}" | ||
267 | niro | 1027 | uvesafb_config="${INSTALL_ROOT}/${uvesafb_config}" |
268 | niro | 773 | chroot="chrooted" |
269 | fi | ||
270 | |||
271 | # get various modules needed to boot | ||
272 | modules="$(get_driver_modules disk)" | ||
273 | modules+=" $(get_driver_modules scsi)" | ||
274 | modules+=" $(get_driver_modules cdrom)" | ||
275 | |||
276 | # check for special ide_disk drivers (ata support) | ||
277 | if [[ ! -z $(echo ${modules} | grep ide_disk) ]] | ||
278 | then | ||
279 | modules+=" $(grep ide_disk /proc/modules | cut -d' ' -f4 | sed '/-/d;s:,:\ :g')" | ||
280 | fi | ||
281 | |||
282 | niro | 1027 | # add some generic modules |
283 | modules+=" sg_mod sg loop sr_mod sd_mod ide-cd ide-cd_mod ide-disk" | ||
284 | |||
285 | # add generic framebuffer modules | ||
286 | if [[ ${framebuffer} = 1 ]] | ||
287 | then | ||
288 | modules+=" uvesafb" | ||
289 | |||
290 | # setup modprobe conf too | ||
291 | [[ ! -d $(dirname ${uvesafb_config}) ]] && install -d $(dirname ${uvesafb_config}) | ||
292 | cat > ${uvesafb_config} << EOF | ||
293 | # This file sets the parameters for uvesafb module. | ||
294 | # The following format should be used: | ||
295 | # options uvesafb mode_option=<xres>x<yres>[-<bpp>][@<refresh>] scroll=<ywrap|ypan|redraw> ... | ||
296 | # | ||
297 | # For more details see: | ||
298 | # http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=Documentation/fb/uvesafb.txt | ||
299 | # | ||
300 | EOF | ||
301 | # default config 1024x768 and 60 HZ | ||
302 | echo "options uvesafb mode_option=1024x768-32@60 scroll=ywrap" >> ${uvesafb_config} | ||
303 | fi | ||
304 | |||
305 | niro | 773 | # remove all duplicate modules |
306 | modules="$(remove_duplicates ${modules})" | ||
307 | |||
308 | # create the config and an initrd | ||
309 | echo "# autogenerated config file" > ${config} | ||
310 | echo "MODULES=\"${modules}\"" >> ${config} | ||
311 | echo "IMAGE_TYPE=\"initramfs\"" >> ${config} | ||
312 | |||
313 | ${chroot} mkinitrd -f ${initrd} ${kernel} | ||
314 | } | ||
315 | |||
316 | chrooted() | ||
317 | { | ||
318 | local cmd="$@" | ||
319 | |||
320 | mount -t sysfs sysfs ${INSTALL_ROOT}/sys | ||
321 | mount -t proc proc ${INSTALL_ROOT}/proc | ||
322 | mount -o bind /dev ${INSTALL_ROOT}/dev | ||
323 | |||
324 | chroot ${INSTALL_ROOT} ${cmd} | ||
325 | |||
326 | umount ${INSTALL_ROOT}/dev | ||
327 | umount ${INSTALL_ROOT}/proc | ||
328 | umount ${INSTALL_ROOT}/sys | ||
329 | } | ||
330 | |||
331 | # special: memory (size), floppy (modprobe floppy), smp (support y/n) | ||
332 | # sound (which module? -> eval $(hwinfo --sound | grep "Driver Modules:" | cut -d: -f2 | sed s:\":a=\":) ) | ||
333 | # | ||
334 | |||
335 | # for i in cdrom cpu disk floppy gfxcard keyboard memory monitor mouse | ||
336 | # netcard pppoe scsi smp \ | ||
337 | # sound wlan | ||
338 | # do | ||
339 | # echo -------- ${i} | ||
340 | # get_hwinfo ${i} | ||
341 | # echo ------------------------------------------------- | ||
342 | # echo | ||
343 | # done | ||
344 | |||
345 | # get_driver_modules sound | ||
346 | # get_driver_modules wlan | ||
347 | # get_driver_modules cdrom | ||
348 | # get_driver_modules disk | ||
349 | # get_driver_modules gfxcard | ||
350 | # get_x11_driver_modules | ||
351 | |||
352 | # # get a modules list | ||
353 | # for i in sound wlan cdrom disk gfxcard | ||
354 | # do | ||
355 | # ALL_MODULES="${ALL_MODULES} $(get_driver_modules ${i})" | ||
356 | # done | ||
357 | # ALL_MODULES="${ALL_MODULES} $(get_x11_driver_modules)" | ||
358 | # # show a clean list | ||
359 | # for i in $(remove_duplicates ${ALL_MODULES}) | ||
360 | # do | ||
361 | # echo ${i} | ||
362 | # done | ||
363 | |||
364 | #create_initrd /root/TEST/initrd | ||
365 | #get_driver_modules cdrom | ||
366 | #get_driver_modules disk | ||
367 | #get_driver_modules scsi | ||
368 | #get_driver_modules all | ||
369 | |||
370 | # network | ||
371 | #get_hwinfo --with-description netcard | ||
372 | #get_driver_modules netcard | ||
373 | |||
374 | #get_hwinfo disk | ||
375 | #get_hwinfo partition | grep /dev/hda |