Annotation of /trunk/installer-simple/functions/hwdetection.sh
Parent Directory | Revision Log
Revision 2491 -
(hide annotations)
(download)
(as text)
Wed Jan 8 12:22:26 2014 UTC (10 years, 8 months ago) by niro
File MIME type: application/x-sh
File size: 5693 byte(s)
Wed Jan 8 12:22:26 2014 UTC (10 years, 8 months ago) by niro
File MIME type: application/x-sh
File size: 5693 byte(s)
-refactor hdd_size_below_256mb() as device_minimum_size()
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 | 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 | niro | 2488 | blkid -o value -s "${variable}" "${method}" "${partition}" |
186 | niro | 2335 | } |
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 | niro | 2408 | is_mounted() |
219 | { | ||
220 | local item | ||
221 | local mount | ||
222 | local method | ||
223 | method="$1" | ||
224 | item="$2" | ||
225 | |||
226 | [ -e /proc/mounts ] || return 1 | ||
227 | |||
228 | case ${method} in | ||
229 | --location) mount="$(grep "[[:space:]]${item}[[:space:]]" /proc/mounts | cut -d ' ' -f2)" ;; | ||
230 | --device) mount="$(grep "^${item}[[:space:]]" /proc/mounts | cut -d ' ' -f1)" ;; | ||
231 | --filesystem) mount="$(grep "[[:space:]]${item}[[:space:]]" /proc/mounts | cut -d ' ' -f3)" ;; | ||
232 | *) echo "unknown method '${method}'"; return 1 ;; | ||
233 | esac | ||
234 | [[ ${mount} != ${item} ]] && return 1 | ||
235 | |||
236 | return 0 | ||
237 | } | ||
238 | |||
239 | niro | 2466 | iface_has_link() |
240 | { | ||
241 | local interface="$1" | ||
242 | local flags | ||
243 | |||
244 | [[ -n ${interface} ]] || return 2 | ||
245 | interface="/sys/class/net/${interface}" | ||
246 | [[ -d ${interface} ]] || return 2 | ||
247 | flags=$(cat ${interface}/flags) | ||
248 | echo $((${flags}|0x41)) > ${interface}/flags # 0x41: IFF_UP|IFF_RUNNING | ||
249 | [[ $(cat ${interface}/carrier) = 1 ]] || return 1 | ||
250 | } | ||
251 | |||
252 | niro | 2491 | # device_minimum_size [drive] [size-in-mb] |
253 | device_minimum_size() | ||
254 | niro | 2476 | { |
255 | niro | 2491 | local device="$1" |
256 | local minimum_size="$2" | ||
257 | niro | 2476 | local size |
258 | |||
259 | niro | 2491 | [[ -z ${device} ]] && dialog_die "Error: device_minimum_size() no \$device given!" |
260 | [[ -z ${minimum_size} ]] && dialog_die "Error: device_minimum_size() no \$minium_size given!" | ||
261 | niro | 2476 | |
262 | niro | 2491 | # convert to bytes |
263 | minimum_size=$(( ${minimum_size}*1024*1024 )) | ||
264 | |||
265 | size=$(fdisk -l ${device} | grep "Disk.*${device}" | sed 's:.*,\ \(.*\)\ byte.*:\1:') | ||
266 | [[ ${size} -ge ${minimum_size} ]] || return 1 | ||
267 | |||
268 | return 0 | ||
269 | niro | 2476 | } |
270 | |||
271 | niro | 2335 | chrooted() |
272 | { | ||
273 | local cmd="$@" | ||
274 | |||
275 | niro | 2408 | is_mounted --location ${INSTALLROOT}/sys || mount -t sysfs sysfs ${INSTALLROOT}/sys |
276 | is_mounted --location ${INSTALLROOT}/proc || mount -t proc proc ${INSTALLROOT}/proc | ||
277 | is_mounted --location ${INSTALLROOT}/dev || mount -o bind /dev ${INSTALLROOT}/dev | ||
278 | niro | 2335 | |
279 | niro | 2488 | chroot ${INSTALLROOT} ${cmd} |
280 | niro | 2335 | |
281 | niro | 2408 | is_mounted --location ${INSTALLROOT}/dev && umount ${INSTALLROOT}/dev |
282 | is_mounted --location ${INSTALLROOT}/proc && umount ${INSTALLROOT}/proc | ||
283 | is_mounted --location ${INSTALLROOT}/sys && umount ${INSTALLROOT}/sys | ||
284 | niro | 2335 | } |