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