Annotation of /tags/installer-simple-0_4_90_20140108_1/functions/hwdetection.sh
Parent Directory | Revision Log
Revision 2492 -
(hide annotations)
(download)
(as text)
Wed Jan 8 12:25:29 2014 UTC (10 years, 8 months ago) by niro
Original Path: trunk/installer-simple/functions/hwdetection.sh
File MIME type: application/x-sh
File size: 5958 byte(s)
Wed Jan 8 12:25:29 2014 UTC (10 years, 8 months ago) by niro
Original Path: trunk/installer-simple/functions/hwdetection.sh
File MIME type: application/x-sh
File size: 5958 byte(s)
-if requested use sudo on all commands which require root privileges
1 | niro | 2335 | #!/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 | niro | 2488 | ${use_sudo} /usr/sbin/hwinfo $@ |
15 | niro | 2335 | } |
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 | niro | 2405 | local memory=$(hwinfo --memory | grep "Memory Size" | sed 's:.*\:\ \(.*\):\1:') |
61 | niro | 2335 | echo "${memory}" |
62 | } | ||
63 | |||
64 | |||
65 | # special smp case | ||
66 | get_hwinfo_smp() | ||
67 | { | ||
68 | niro | 2406 | local smp=$(hwinfo --smp | grep "SMP support:" | sed 's:.*\:\ \(.*\):\1:') |
69 | niro | 2335 | 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 | niro | 2492 | local use_sudo="" |
169 | case ${SUDO} in | ||
170 | yes|y|true|1) use_sudo=sudo ;; | ||
171 | esac | ||
172 | |||
173 | niro | 2335 | 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 | niro | 2492 | ${use_sudo} blkid -o value -s "${variable}" "${method}" "${partition}" |
191 | niro | 2335 | } |
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 | niro | 2408 | 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 | niro | 2466 | 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 | niro | 2491 | # device_minimum_size [drive] [size-in-mb] |
258 | device_minimum_size() | ||
259 | niro | 2476 | { |
260 | niro | 2491 | local device="$1" |
261 | local minimum_size="$2" | ||
262 | niro | 2476 | local size |
263 | |||
264 | niro | 2492 | local use_sudo="" |
265 | case ${SUDO} in | ||
266 | yes|y|true|1) use_sudo=sudo ;; | ||
267 | esac | ||
268 | |||
269 | niro | 2491 | [[ -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 | niro | 2476 | |
272 | niro | 2491 | # convert to bytes |
273 | minimum_size=$(( ${minimum_size}*1024*1024 )) | ||
274 | |||
275 | niro | 2492 | size=$(${use_sudo} fdisk -l ${device} | grep "Disk.*${device}" | sed 's:.*,\ \(.*\)\ byte.*:\1:') |
276 | niro | 2491 | [[ ${size} -ge ${minimum_size} ]] || return 1 |
277 | |||
278 | return 0 | ||
279 | niro | 2476 | } |
280 | |||
281 | niro | 2335 | chrooted() |
282 | { | ||
283 | local cmd="$@" | ||
284 | |||
285 | niro | 2492 | local use_sudo="" |
286 | case ${SUDO} in | ||
287 | yes|y|true|1) use_sudo=sudo ;; | ||
288 | esac | ||
289 | |||
290 | niro | 2408 | 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 | niro | 2335 | |
294 | niro | 2492 | ${use_sudo} chroot ${INSTALLROOT} ${cmd} |
295 | niro | 2335 | |
296 | niro | 2408 | 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 | niro | 2335 | } |