Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


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