Contents of /tags/installer-0_1_0/include/hwdetection.sh
Parent Directory | Revision Log
Revision 1035 -
(show annotations)
(download)
(as text)
Sun May 30 18:37:54 2010 UTC (14 years, 4 months ago) by niro
Original Path: trunk/installer/include/hwdetection.sh
File MIME type: application/x-sh
File size: 6462 byte(s)
Sun May 30 18:37:54 2010 UTC (14 years, 4 months ago) by niro
Original Path: trunk/installer/include/hwdetection.sh
File MIME type: application/x-sh
File size: 6462 byte(s)
-code cleanup
1 | #!/bin/bash |
2 | # $Id$ |
3 | |
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 | local i |
79 | |
80 | for i in $(hwinfo --"${item}" | grep "Driver Modules:" | cut -d: -f2 ) |
81 | do |
82 | eval $(echo "${i}" | sed -e "s:\":modules=\"\${modules}\ :" -e "s:,:\ :") |
83 | done |
84 | remove_duplicates "${modules}" |
85 | } |
86 | |
87 | # remove_duplicates $list |
88 | remove_duplicates() |
89 | { |
90 | local list="$@" |
91 | local fixed_list=":" |
92 | local i |
93 | |
94 | for i in ${list} |
95 | do |
96 | if [[ -z $(echo "${fixed_list}" | fgrep ":${i}:") ]] |
97 | then |
98 | fixed_list="${fixed_list}${i}:" |
99 | fi |
100 | done |
101 | |
102 | unset list |
103 | |
104 | # remove all ':' and show the cleaned list |
105 | # this way fixes double spaces also |
106 | local counter |
107 | declare -i counter=0 |
108 | for i in $(echo ${fixed_list} | sed "s|:| |g") |
109 | do |
110 | (( counter++ )) |
111 | if [[ ${counter} -eq 1 ]] |
112 | then |
113 | list="${i}" |
114 | else |
115 | list+=" ${i}" |
116 | fi |
117 | done |
118 | |
119 | echo "${list}" |
120 | } |
121 | |
122 | get_x11_driver_modules() |
123 | { |
124 | local modules |
125 | modules="$(hwinfo --gfxcard | grep 'XFree86 v4 Server Module:' | sed 's/.*Module:\ \(.*\)/\1/')" |
126 | |
127 | # remove duplicates from list and show it |
128 | remove_duplicates "${modules}" |
129 | } |
130 | |
131 | get_evdev_device_path() |
132 | { |
133 | local device="$1" |
134 | local path |
135 | |
136 | case ${device} in |
137 | mouse|keyboard) true;; |
138 | *) die "unkown device";; |
139 | esac |
140 | |
141 | path="$(hwinfo --${device} | grep 'Device Files:' | sed 's:.*\(/dev/input/event[0-5]\).*:\1:')" |
142 | echo "${path}" |
143 | } |
144 | |
145 | # get_netcard_driver_modules device |
146 | # e.g. get_netcard_driver_modules eth0 |
147 | get_netcard_driver_modules() |
148 | { |
149 | local device="$1" |
150 | local modules |
151 | |
152 | if [[ -z ${device} ]] |
153 | then |
154 | echo "Error: get_netcard_driver_module(): no device given" |
155 | return 1 |
156 | fi |
157 | |
158 | modules=$(hwinfo --netcard | grep -B1 "Device File: ${device}" | sed 's/.*Modules: \"\(.*\)\"/\1/;q') |
159 | remove_duplicates "${modules}" |
160 | } |
161 | |
162 | get_blkid_information() |
163 | { |
164 | local partition="$1" |
165 | local variable="$2" |
166 | local method |
167 | |
168 | case $1 in |
169 | --uuid|-u) shift; method="-U"; partition="$1"; variable="$2" ;; |
170 | --label|-l) shift; method="-L"; partition="$1"; variable="$2" ;; |
171 | esac |
172 | |
173 | if [[ -z ${partition} ]] |
174 | then |
175 | echo "Error: get_get_blkid_information()(): no partition given" |
176 | return 1 |
177 | fi |
178 | |
179 | if [[ -z ${variable} ]] |
180 | then |
181 | echo "Error: get_get_blkid_information()(): no variable given" |
182 | return 1 |
183 | fi |
184 | |
185 | blkid -o value -s "${variable}" "${method}" "${partition}" |
186 | } |
187 | |
188 | get_fstype() |
189 | { |
190 | local partition="$1" |
191 | if [[ -z ${partition} ]] |
192 | then |
193 | echo "Error: get_fstype(): no partition given" |
194 | return 1 |
195 | fi |
196 | |
197 | # does not work with uuids |
198 | #mount -f --guess-fstype ${partition} |
199 | |
200 | get_blkid_information ${partition} TYPE |
201 | } |
202 | |
203 | get_uuid() |
204 | { |
205 | local partition="$1" |
206 | if [[ -z ${partition} ]] |
207 | then |
208 | echo "Error: get_uuid(): no partition given" |
209 | return 1 |
210 | fi |
211 | |
212 | # does not work with uuids |
213 | #mount -f --guess-fstype ${partition} |
214 | |
215 | get_blkid_information ${partition} UUID |
216 | } |
217 | |
218 | # create_initrd {/path/to/initrd kernelversion </path/to/config>} |
219 | # when nothing given then /boot/initrd-$(uname -r).img $(uname -r) will be used |
220 | # default config path is /etc/conf.d/mkinitrd |
221 | create_initrd() |
222 | { |
223 | local initrd |
224 | local kernel |
225 | local config |
226 | local chroot |
227 | #local root |
228 | local modules |
229 | # enabled framebuffer modules as default |
230 | local framebuffer=1 |
231 | local uvesafb_config="/etc/modprobe.d/uvesafb.conf" |
232 | |
233 | # very basic getops |
234 | for i in $* |
235 | do |
236 | case $1 in |
237 | --initrd|-i) shift; initrd="$1" ;; |
238 | --config|-c) shift; config="$1" ;; |
239 | --kernelversion|-v) shift; kernel="$1" ;; |
240 | --nofb) shift; framebuffer=0 |
241 | esac |
242 | shift |
243 | done |
244 | |
245 | [[ -z ${initrd} ]] && initrd="/boot/initrd-$(uname -r).img" |
246 | [[ -z ${kernel} ]] && kernel="$(uname -r)" |
247 | [[ -z ${config} ]] && config="/etc/conf.d/mkinitrd" |
248 | |
249 | if [[ ! -z ${INSTALL_ROOT} ]] |
250 | then |
251 | config="${INSTALL_ROOT}/${config}" |
252 | uvesafb_config="${INSTALL_ROOT}/${uvesafb_config}" |
253 | chroot="chrooted" |
254 | fi |
255 | |
256 | # get various modules needed to boot |
257 | modules="$(get_driver_modules disk)" |
258 | modules+=" $(get_driver_modules scsi)" |
259 | modules+=" $(get_driver_modules cdrom)" |
260 | |
261 | # check for special ide_disk drivers (ata support) |
262 | if [[ ! -z $(echo ${modules} | grep ide_disk) ]] |
263 | then |
264 | modules+=" $(grep ide_disk /proc/modules | cut -d' ' -f4 | sed '/-/d;s:,:\ :g')" |
265 | fi |
266 | |
267 | # add some generic modules |
268 | modules+=" sg_mod sg loop sr_mod sd_mod ide-cd ide-cd_mod ide-disk" |
269 | |
270 | # add generic framebuffer modules |
271 | if [[ ${framebuffer} = 1 ]] |
272 | then |
273 | modules+=" uvesafb" |
274 | |
275 | # setup modprobe conf too |
276 | [[ ! -d $(dirname ${uvesafb_config}) ]] && install -d $(dirname ${uvesafb_config}) |
277 | cat > ${uvesafb_config} << EOF |
278 | # This file sets the parameters for uvesafb module. |
279 | # The following format should be used: |
280 | # options uvesafb mode_option=<xres>x<yres>[-<bpp>][@<refresh>] scroll=<ywrap|ypan|redraw> ... |
281 | # |
282 | # For more details see: |
283 | # http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=Documentation/fb/uvesafb.txt |
284 | # |
285 | EOF |
286 | # default config 1024x768 and 60 HZ |
287 | echo "options uvesafb mode_option=1024x768-32@60 scroll=ywrap" >> ${uvesafb_config} |
288 | fi |
289 | |
290 | # remove all duplicate modules |
291 | modules="$(remove_duplicates ${modules})" |
292 | |
293 | # create the config and an initrd |
294 | echo "# autogenerated config file" > ${config} |
295 | echo "MODULES=\"${modules}\"" >> ${config} |
296 | echo "IMAGE_TYPE=\"initramfs\"" >> ${config} |
297 | |
298 | ${chroot} mkinitrd -f ${initrd} ${kernel} |
299 | } |
300 | |
301 | chrooted() |
302 | { |
303 | local cmd="$@" |
304 | |
305 | mount -t sysfs sysfs ${INSTALL_ROOT}/sys |
306 | mount -t proc proc ${INSTALL_ROOT}/proc |
307 | mount -o bind /dev ${INSTALL_ROOT}/dev |
308 | |
309 | chroot ${INSTALL_ROOT} ${cmd} |
310 | |
311 | umount ${INSTALL_ROOT}/dev |
312 | umount ${INSTALL_ROOT}/proc |
313 | umount ${INSTALL_ROOT}/sys |
314 | } |