Magellan Linux

Contents of /mcore-src/trunk/mcore-tools/daemon/include/hwdetection.global.class

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1265 - (show annotations) (download)
Fri Feb 4 20:13:51 2011 UTC (13 years, 2 months ago) by niro
File size: 5994 byte(s)
-the hwdetecion provides hwdetection ;)


1 # $Id$
2
3 provide hwdetection
4
5 ## hwdetection, needs >= sys-apps/hwinfo
6
7 # hwinfo wrapper to use hwinfo with sudo if requested
8 hwinfo()
9 {
10 local use_sudo=""
11 case ${SUDO} in
12 yes|y|true|1) use_sudo=sudo ;;
13 esac
14
15 ${use_sudo} /usr/sbin/hwinfo $@
16 }
17
18 # get_hwinfo $hw_item
19 get_hwinfo()
20 {
21 local enable_desc="0"
22
23 if [[ $1 = --with-description ]] || [[ $1 = -d ]]
24 then
25 enable_desc=1
26 shift
27 fi
28
29 local item="$1"
30 local all
31 local i
32
33 # handle special items
34 case ${item} in
35 memory) get_hwinfo_memory; return ;;
36 smp) get_hwinfo_smp; return ;;
37 esac
38
39 all=$(hwinfo --short --"${item}")
40
41 declare -i i=0
42 while read device description
43 do
44 # skip the first line
45 (( i++ ))
46 [ ${i} -eq 1 ] && continue
47
48 if [[ ${enable_desc} = 1 ]]
49 then
50 echo "${device};${description}"
51 else
52 echo "${device}"
53 fi
54 done <<< "${all}"
55 }
56
57
58 # special memory case
59 get_hwinfo_memory()
60 {
61 local memory=$(hwinfo --memory | grep "Memory Size" | cut -d: -f2 | sed "s:\ ::")
62 echo "${memory}"
63 }
64
65
66 # special smp case
67 get_hwinfo_smp()
68 {
69 local smp=$(hwinfo --smp | grep "SMP support:" | cut -d: -f2 | sed "s:\ ::")
70 echo "${smp}"
71 }
72
73
74 # get_driver_modules $hw_item
75 get_driver_modules()
76 {
77 local item="$1"
78 local modules
79
80 # being little tricky here :)
81 # does following:
82 # grep:
83 # -> 'Driver Modules: "via82cxxx", "ide_cd"'
84 # cut:
85 # -> ' "via82cxxx", "ide_cd"'
86 # sed1:
87 # -> ' modules="via82cxxx", "ide_cd"'
88 # sed2:
89 # -> ' modules="via82cxxx ide_cd"'
90 # then evaluate -> eval modules="via82cxxx ide_cd"
91 # and we get a variable $modules with all mods
92 #eval $(hwinfo --"${item}" | grep "Driver Modules:" | cut -d: -f2 | sed -e "s:\":modules=\":" -e "s:\",\ \":\ :")
93
94 local i
95 for i in $(hwinfo --"${item}" | grep "Driver Modules:" | cut -d: -f2 )
96 do
97 eval $(echo "${i}" | sed -e "s:\":modules=\"\${modules}\ :" -e "s:,:\ :")
98 done
99 remove_duplicates "${modules}"
100 }
101
102 # remove_duplicates $list
103 remove_duplicates()
104 {
105 local list="$@"
106 local fixed_list=":"
107 local i
108
109 for i in ${list}
110 do
111 if [[ -z $(echo "${fixed_list}" | fgrep ":${i}:") ]]
112 then
113 fixed_list="${fixed_list}${i}:"
114 fi
115 done
116
117 unset list
118
119 # remove all ':' and show the cleaned list
120 # this way fixes double spaces also
121 local counter
122 declare -i counter=0
123 for i in $(echo ${fixed_list} | sed "s|:| |g")
124 do
125 (( counter++ ))
126 if [[ ${counter} -eq 1 ]]
127 then
128 list="${i}"
129 else
130 list+=" ${i}"
131 fi
132 done
133
134 echo "${list}"
135 }
136
137 get_x11_driver_modules()
138 {
139 local modules
140 modules="$(hwinfo --gfxcard | grep 'XFree86 v4 Server Module:' | sed 's/.*Module:\ \(.*\)/\1/')"
141
142 # remove duplicates from list and show it
143 remove_duplicates "${modules}"
144 }
145
146 # get_netcard_driver_modules device
147 # e.g. get_netcard_driver_modules eth0
148 get_netcard_driver_modules()
149 {
150 local device="$1"
151 local modules
152
153 if [[ -z ${device} ]]
154 then
155 echo "Error: get_netcard_driver_module(): no device given"
156 return 1
157 fi
158
159 modules=$(hwinfo --netcard | grep -B1 "Device File: ${device}" | sed 's/.*Modules: \"\(.*\)\"/\1/;q')
160 remove_duplicates "${modules}"
161 }
162
163 get_blkid_information()
164 {
165 local partition="$1"
166 local variable="$2"
167 local method
168
169 case $1 in
170 --uuid|-u) shift; method="-U"; partition="$1"; variable="$2" ;;
171 --label|-l) shift; method="-L"; partition="$1"; variable="$2" ;;
172 esac
173
174 if [[ -z ${partition} ]]
175 then
176 echo "Error: get_get_blkid_information()(): no partition given"
177 return 1
178 fi
179
180 if [[ -z ${variable} ]]
181 then
182 echo "Error: get_get_blkid_information()(): no variable given"
183 return 1
184 fi
185
186 blkid -o value -s "${variable}" "${method}" "${partition}"
187 }
188
189 get_fstype()
190 {
191 local partition="$1"
192 if [[ -z ${partition} ]]
193 then
194 echo "Error: get_fstype(): no partition given"
195 return 1
196 fi
197
198 # does not work with uuids
199 #mount -f --guess-fstype ${partition}
200
201 get_blkid_information ${partition} TYPE
202 }
203
204 get_uuid()
205 {
206 local partition="$1"
207 if [[ -z ${partition} ]]
208 then
209 echo "Error: get_uuid(): no partition given"
210 return 1
211 fi
212
213 # does not work with uuids
214 #mount -f --guess-fstype ${partition}
215
216 get_blkid_information ${partition} UUID
217 }
218
219 # create_initrd {/path/to/initrd kernelversion </path/to/config>}
220 # when nothing given then /boot/initrd-$(uname -r).img $(uname -r) will be used
221 # default config path is /etc/conf.d/mkinitrd
222 create_initrd()
223 {
224 local initrd
225 local kernel
226 local config
227 local chroot
228 #local root
229 local modules
230 # enabled framebuffer modules as default
231 local framebuffer=1
232
233 # very basic getops
234 for i in $*
235 do
236 case $1 in
237 #--root|-r) shift; root="$1" ;;
238 --initrd|-i) shift; initrd="$1" ;;
239 --config|-c) shift; config="$1" ;;
240 --kernelversion|-v) shift; kernel="$1" ;;
241 --nofb) shift; framebuffer=0
242 esac
243 shift
244 done
245
246 [[ -z ${initrd} ]] && initrd="/boot/initrd-$(uname -r).img"
247 [[ -z ${kernel} ]] && kernel="$(uname -r)"
248 [[ -z ${config} ]] && config="/etc/conf.d/mkinitrd"
249
250 if [[ ! -z ${INSTALL_ROOT} ]]
251 then
252 config="${INSTALL_ROOT}/${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 [[ ${framebuffer} = 1 ]] && modules+=" uvesafb"
272
273 # remove all duplicate modules
274 modules="$(remove_duplicates ${modules})"
275
276 # create the config and an initrd
277 echo "# autogenerated config file" > ${config}
278 echo "MODULES=\"${modules}\"" >> ${config}
279 echo "IMAGE_TYPE=\"initramfs\"" >> ${config}
280
281 ${chroot} mkinitrd -f ${initrd} ${kernel}
282 }
283
284 chrooted()
285 {
286 local cmd="$@"
287
288 mount -t sysfs sysfs ${INSTALL_ROOT}/sys
289 mount -t proc proc ${INSTALL_ROOT}/proc
290 mount -o bind /dev ${INSTALL_ROOT}/dev
291
292 chroot ${INSTALL_ROOT} ${cmd}
293
294 umount ${INSTALL_ROOT}/dev
295 umount ${INSTALL_ROOT}/proc
296 umount ${INSTALL_ROOT}/sys
297 }