#!/bin/bash # $Id$ ## hwdetection, needs >= sys-apps/hwinfo # hwinfo wrapper to use hwinfo with sudo if requested hwinfo() { local use_sudo="" case ${SUDO} in yes|y|true|1) use_sudo=sudo ;; esac LC_ALL=C ${use_sudo} /usr/sbin/hwinfo $@ } # get_hwinfo $hw_item get_hwinfo() { local enable_desc="0" if [[ $1 = --with-description ]] || [[ $1 = -d ]] then enable_desc=1 shift fi local item="$1" local all local i # handle special items case ${item} in memory) get_hwinfo_memory; return ;; smp) get_hwinfo_smp; return ;; esac all=$(hwinfo --short --"${item}") declare -i i=0 while read device description do # skip the first line (( i++ )) [ ${i} -eq 1 ] && continue if [[ ${enable_desc} = 1 ]] then echo "${device};${description}" else echo "${device}" fi done <<< "${all}" } # special memory case get_hwinfo_memory() { local memory=$(hwinfo --memory | grep "Memory Size" | sed 's:.*\:\ \(.*\):\1:') echo "${memory}" } # special smp case get_hwinfo_smp() { local smp=$(hwinfo --smp | grep "SMP support:" | sed 's:.*\:\ \(.*\):\1:') echo "${smp}" } # get_driver_modules $hw_item get_driver_modules() { local item="$1" local modules local i for i in $(hwinfo --"${item}" | grep "Driver Modules:" | cut -d: -f2 ) do eval $(echo "${i}" | sed -e "s:\":modules=\"\${modules}\ :" -e "s:,:\ :") done remove_duplicates "${modules}" } # remove_duplicates $list remove_duplicates() { local list="$@" local fixed_list=":" local i for i in ${list} do if [[ -z $(echo "${fixed_list}" | fgrep ":${i}:") ]] then fixed_list="${fixed_list}${i}:" fi done unset list # remove all ':' and show the cleaned list # this way fixes double spaces also local counter declare -i counter=0 for i in $(echo ${fixed_list} | sed "s|:| |g") do (( counter++ )) if [[ ${counter} -eq 1 ]] then list="${i}" else list+=" ${i}" fi done echo "${list}" } get_x11_driver_modules() { local modules modules="$(hwinfo --gfxcard | grep 'XFree86 v4 Server Module:' | sed 's/.*Module:\ \(.*\)/\1/')" # remove duplicates from list and show it remove_duplicates "${modules}" } get_evdev_device_path() { local device="$1" local path case ${device} in mouse|keyboard) true;; *) die "unkown device";; esac path="$(hwinfo --${device} | grep 'Device Files:' | sed 's:.*\(/dev/input/event[0-5]\).*:\1:')" echo "${path}" } # get_netcard_driver_modules device # e.g. get_netcard_driver_modules eth0 get_netcard_driver_modules() { local device="$1" local modules if [[ -z ${device} ]] then echo "Error: get_netcard_driver_module(): no device given" return 1 fi modules=$(hwinfo --netcard | grep -B1 "Device File: ${device}" | sed 's/.*Modules: \"\(.*\)\"/\1/;q') remove_duplicates "${modules}" } get_blkid_information() { local partition="$1" local variable="$2" local method local use_sudo="" case ${SUDO} in yes|y|true|1) use_sudo=sudo ;; esac case $1 in --uuid|-u) shift; method="-U"; partition="$1"; variable="$2" ;; --label|-l) shift; method="-L"; partition="$1"; variable="$2" ;; esac if [[ -z ${partition} ]] then echo "Error: get_get_blkid_information()(): no partition given" return 1 fi if [[ -z ${variable} ]] then echo "Error: get_get_blkid_information()(): no variable given" return 1 fi LC_ALL=C ${use_sudo} blkid -o value -s "${variable}" "${method}" "${partition}" } get_fstype() { local partition="$1" if [[ -z ${partition} ]] then echo "Error: get_fstype(): no partition given" return 1 fi # does not work with uuids #mount -f --guess-fstype ${partition} get_blkid_information ${partition} TYPE } get_uuid() { local partition="$1" if [[ -z ${partition} ]] then echo "Error: get_uuid(): no partition given" return 1 fi # does not work with uuids #mount -f --guess-fstype ${partition} get_blkid_information ${partition} UUID } is_mounted() { local item local mount local method method="$1" item="$2" [ -e /proc/mounts ] || return 1 case ${method} in --location) mount="$(grep "[[:space:]]${item}[[:space:]]" /proc/mounts | cut -d ' ' -f2)" ;; --device) mount="$(grep "^${item}[[:space:]]" /proc/mounts | cut -d ' ' -f1)" ;; --filesystem) mount="$(grep "[[:space:]]${item}[[:space:]]" /proc/mounts | cut -d ' ' -f3)" ;; *) echo "unknown method '${method}'"; return 1 ;; esac [[ ${mount} != ${item} ]] && return 1 return 0 } iface_has_link() { local interface="$1" local flags [[ -n ${interface} ]] || return 2 interface="/sys/class/net/${interface}" [[ -d ${interface} ]] || return 2 flags=$(cat ${interface}/flags) echo $((${flags}|0x41)) > ${interface}/flags # 0x41: IFF_UP|IFF_RUNNING [[ $(cat ${interface}/carrier) = 1 ]] || return 1 } # device_minimum_size [drive] [size-in-mb] device_minimum_size() { local device="$1" local minimum_size="$2" local size local use_sudo="" case ${SUDO} in yes|y|true|1) use_sudo=sudo ;; esac [[ -z ${device} ]] && dialog_die "Error: device_minimum_size() no \$device given!" [[ -z ${minimum_size} ]] && dialog_die "Error: device_minimum_size() no \$minium_size given!" # convert to bytes minimum_size=$(( ${minimum_size}*1024*1024 )) size=$(LC_ALL=C ${use_sudo} fdisk -l ${device} | grep "Disk.*${device}" | sed 's:.*,\ \(.*\)\ byte.*:\1:') [[ ${size} -ge ${minimum_size} ]] || return 1 return 0 } chrooted() { local cmd="$@" local use_sudo="" case ${SUDO} in yes|y|true|1) use_sudo=sudo ;; esac is_mounted --location ${INSTALLROOT}/sys || mount -t sysfs sysfs ${INSTALLROOT}/sys is_mounted --location ${INSTALLROOT}/proc || mount -t proc proc ${INSTALLROOT}/proc is_mounted --location ${INSTALLROOT}/dev || mount -o bind /dev ${INSTALLROOT}/dev LC_ALL=C ${use_sudo} chroot ${INSTALLROOT} ${cmd} is_mounted --location ${INSTALLROOT}/dev && umount ${INSTALLROOT}/dev is_mounted --location ${INSTALLROOT}/proc && umount ${INSTALLROOT}/proc is_mounted --location ${INSTALLROOT}/sys && umount ${INSTALLROOT}/sys }