Contents of /tags/installer-simple-0_4_90_20140108_3/functions/hwdetection.sh
Parent Directory | Revision Log
Revision 2510 -
(show annotations)
(download)
(as text)
Wed Jan 8 15:08:52 2014 UTC (10 years, 8 months ago) by niro
File MIME type: application/x-sh
File size: 5994 byte(s)
Wed Jan 8 15:08:52 2014 UTC (10 years, 8 months ago) by niro
File MIME type: application/x-sh
File size: 5994 byte(s)
tagged 'installer-simple-0_4_90_20140108_3'
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 | LC_ALL=C ${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" | sed 's:.*\:\ \(.*\):\1:') |
61 | echo "${memory}" |
62 | } |
63 | |
64 | |
65 | # special smp case |
66 | get_hwinfo_smp() |
67 | { |
68 | local smp=$(hwinfo --smp | grep "SMP support:" | sed 's:.*\:\ \(.*\):\1:') |
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 | local use_sudo="" |
169 | case ${SUDO} in |
170 | yes|y|true|1) use_sudo=sudo ;; |
171 | esac |
172 | |
173 | case $1 in |
174 | --uuid|-u) shift; method="-U"; partition="$1"; variable="$2" ;; |
175 | --label|-l) shift; method="-L"; partition="$1"; variable="$2" ;; |
176 | esac |
177 | |
178 | if [[ -z ${partition} ]] |
179 | then |
180 | echo "Error: get_get_blkid_information()(): no partition given" |
181 | return 1 |
182 | fi |
183 | |
184 | if [[ -z ${variable} ]] |
185 | then |
186 | echo "Error: get_get_blkid_information()(): no variable given" |
187 | return 1 |
188 | fi |
189 | |
190 | LC_ALL=C ${use_sudo} blkid -o value -s "${variable}" "${method}" "${partition}" |
191 | } |
192 | |
193 | get_fstype() |
194 | { |
195 | local partition="$1" |
196 | if [[ -z ${partition} ]] |
197 | then |
198 | echo "Error: get_fstype(): no partition given" |
199 | return 1 |
200 | fi |
201 | |
202 | # does not work with uuids |
203 | #mount -f --guess-fstype ${partition} |
204 | |
205 | get_blkid_information ${partition} TYPE |
206 | } |
207 | |
208 | get_uuid() |
209 | { |
210 | local partition="$1" |
211 | if [[ -z ${partition} ]] |
212 | then |
213 | echo "Error: get_uuid(): no partition given" |
214 | return 1 |
215 | fi |
216 | |
217 | # does not work with uuids |
218 | #mount -f --guess-fstype ${partition} |
219 | |
220 | get_blkid_information ${partition} UUID |
221 | } |
222 | |
223 | is_mounted() |
224 | { |
225 | local item |
226 | local mount |
227 | local method |
228 | method="$1" |
229 | item="$2" |
230 | |
231 | [ -e /proc/mounts ] || return 1 |
232 | |
233 | case ${method} in |
234 | --location) mount="$(grep "[[:space:]]${item}[[:space:]]" /proc/mounts | cut -d ' ' -f2)" ;; |
235 | --device) mount="$(grep "^${item}[[:space:]]" /proc/mounts | cut -d ' ' -f1)" ;; |
236 | --filesystem) mount="$(grep "[[:space:]]${item}[[:space:]]" /proc/mounts | cut -d ' ' -f3)" ;; |
237 | *) echo "unknown method '${method}'"; return 1 ;; |
238 | esac |
239 | [[ ${mount} != ${item} ]] && return 1 |
240 | |
241 | return 0 |
242 | } |
243 | |
244 | iface_has_link() |
245 | { |
246 | local interface="$1" |
247 | local flags |
248 | |
249 | [[ -n ${interface} ]] || return 2 |
250 | interface="/sys/class/net/${interface}" |
251 | [[ -d ${interface} ]] || return 2 |
252 | flags=$(cat ${interface}/flags) |
253 | echo $((${flags}|0x41)) > ${interface}/flags # 0x41: IFF_UP|IFF_RUNNING |
254 | [[ $(cat ${interface}/carrier) = 1 ]] || return 1 |
255 | } |
256 | |
257 | # device_minimum_size [drive] [size-in-mb] |
258 | device_minimum_size() |
259 | { |
260 | local device="$1" |
261 | local minimum_size="$2" |
262 | local size |
263 | |
264 | local use_sudo="" |
265 | case ${SUDO} in |
266 | yes|y|true|1) use_sudo=sudo ;; |
267 | esac |
268 | |
269 | [[ -z ${device} ]] && dialog_die "Error: device_minimum_size() no \$device given!" |
270 | [[ -z ${minimum_size} ]] && dialog_die "Error: device_minimum_size() no \$minium_size given!" |
271 | |
272 | # convert to bytes |
273 | minimum_size=$(( ${minimum_size}*1024*1024 )) |
274 | |
275 | size=$(LC_ALL=C ${use_sudo} fdisk -l ${device} | grep "Disk.*${device}" | sed 's:.*,\ \(.*\)\ byte.*:\1:') |
276 | [[ ${size} -ge ${minimum_size} ]] || return 1 |
277 | |
278 | return 0 |
279 | } |
280 | |
281 | chrooted() |
282 | { |
283 | local cmd="$@" |
284 | |
285 | local use_sudo="" |
286 | case ${SUDO} in |
287 | yes|y|true|1) use_sudo=sudo ;; |
288 | esac |
289 | |
290 | is_mounted --location ${INSTALLROOT}/sys || mount -t sysfs sysfs ${INSTALLROOT}/sys |
291 | is_mounted --location ${INSTALLROOT}/proc || mount -t proc proc ${INSTALLROOT}/proc |
292 | is_mounted --location ${INSTALLROOT}/dev || mount -o bind /dev ${INSTALLROOT}/dev |
293 | |
294 | LC_ALL=C ${use_sudo} chroot ${INSTALLROOT} ${cmd} |
295 | |
296 | is_mounted --location ${INSTALLROOT}/dev && umount ${INSTALLROOT}/dev |
297 | is_mounted --location ${INSTALLROOT}/proc && umount ${INSTALLROOT}/proc |
298 | is_mounted --location ${INSTALLROOT}/sys && umount ${INSTALLROOT}/sys |
299 | } |