home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Linux / Ubuntu_64-bit / ubuntu-11.04-desktop-amd64.iso / casper / filesystem.squashfs / bin / hw-detect < prev    next >
Text File  |  2011-04-22  |  14KB  |  556 lines

  1. #!/bin/sh
  2.  
  3. set -e
  4. . /usr/share/debconf/confmodule
  5. #set -x
  6.  
  7. if [ -z "$1" ]; then
  8.     PROGRESSBAR=hw-detect/detect_progress_step
  9. else
  10.     PROGRESSBAR=$1
  11. fi
  12.  
  13. NEWLINE="
  14. "
  15. MISSING_MODULES_LIST=""
  16. SUBARCH="$(archdetect)"
  17.  
  18. finish_install=/usr/lib/finish-install.d/30hw-detect
  19.  
  20. LOAD_IDE=""
  21. if db_get hw-detect/load-ide && [ "$RET" = true ]; then
  22.     LOAD_IDE=1
  23. fi
  24.  
  25. # Check for virtio devices
  26. if [ -d /sys/bus/pci/devices ] && \
  27.     grep -q 0x1af4 /sys/bus/pci/devices/*/vendor 2>/dev/null && \
  28.     ! grep -q ^virtio_ /proc/modules; then
  29.     anna-install virtio-modules || true
  30. fi
  31.  
  32. if [ -x /sbin/depmod ]; then
  33.     depmod -a > /dev/null 2>&1 || true
  34. fi
  35.  
  36. log () {
  37.     logger -t hw-detect "$@"
  38. }
  39.  
  40. is_not_loaded() {
  41.     ! ((cut -d" " -f1 /proc/modules | grep -q "^$1\$") || \
  42.        (cut -d" " -f1 /proc/modules | sed -e 's/_/-/g' | grep -q "^$1\$"))
  43. }
  44.  
  45. is_available () {
  46.     [ "$(modprobe -l $1)" ] || return 1
  47. }
  48.  
  49. # Module as first parameter, description of device the second.
  50. missing_module () {
  51.     if ! in_list "$1" "$MISSING_MODULES_LIST"; then
  52.         if [ -n "$MISSING_MODULES_LIST" ]; then
  53.             MISSING_MODULES_LIST="$MISSING_MODULES_LIST, "
  54.         fi
  55.         MISSING_MODULES_LIST="$MISSING_MODULES_LIST$1 ($2)"
  56.     fi
  57. }
  58.  
  59. # The list can be delimited with spaces or spaces and commas.
  60. in_list() {
  61.     echo "$2" | grep -q "\(^\| \)$1\(,\| \|$\)"
  62. }
  63.  
  64. snapshot_devs() {
  65.     echo -n `grep : /proc/net/dev | cut -d':' -f1`
  66. }
  67.  
  68. compare_devs() {
  69.     local olddevs="$1"
  70.     local devs="$2"
  71.     local dev newdevs
  72.  
  73.     newdevs=
  74.     for dev in $devs; do
  75.         if ! echo " $olddevs " | grep -q " $dev "; then
  76.             newdevs="${newdevs:+$newdevs }$dev"
  77.         fi
  78.     done
  79.     echo "$newdevs"
  80. }
  81.  
  82. load_module() {
  83.     local module="$1"
  84.     local cardname="$2"
  85.     local devs=""
  86.     local olddevs=""
  87.     local newdev=""
  88.  
  89.     old=`cat /proc/sys/kernel/printk`
  90.     echo 0 > /proc/sys/kernel/printk
  91.  
  92.     devs="$(snapshot_devs)"
  93.     if log-output -t hw-detect modprobe -v "$module"; then
  94.         olddevs="$devs"
  95.         devs="$(snapshot_devs)"
  96.         newdevs="$(compare_devs "$olddevs" "$devs")"
  97.  
  98.         # Make sure space is used as a delimiter.
  99.         IFS_SAVE="$IFS"
  100.         IFS=" "
  101.         if [ -n "$newdevs" -a -n "$cardname" ]; then
  102.             mkdir -p /etc/network
  103.             for dev in $newdevs; do
  104.                 echo "${dev}:${cardname}" >> /etc/network/devnames
  105.             done
  106.         fi
  107.         IFS="$IFS_SAVE"
  108.     else   
  109.         log "Error loading '$module'"
  110.         if [ "$module" != floppy ] && [ "$module" != ide-floppy ] && \
  111.            [ "$module" != ide-cd ] && [ "$module" != ide-generic ]; then
  112.             db_subst hw-detect/modprobe_error CMD_LINE_PARAM "modprobe -v $module"
  113.             db_input medium hw-detect/modprobe_error || [ $? -eq 30 ]
  114.             db_go
  115.         fi
  116.     fi
  117.  
  118.     echo $old > /proc/sys/kernel/printk
  119. }
  120.  
  121. # Some pci chipsets are needed or there can be DMA or other problems.
  122. get_ide_chipset_info() {
  123.     for ide_module in $(find /lib/modules/*/kernel/drivers/ide/pci/ -type f 2>/dev/null); do
  124.         if [ -e $ide_module ]; then
  125.             baseidemod=$(echo $ide_module | sed 's/\.ko$//; s/.*\///')
  126.             echo "$baseidemod:IDE chipset support"
  127.         fi
  128.     done
  129. }
  130.  
  131. # Return list of lines formatted "module:Description"
  132. get_detected_hw_info() {
  133.     if [ "${SUBARCH%%/*}" = powerpc ]; then
  134.         discover-mac-io
  135.         if [ "$SUBARCH" = powerpc/chrp_rs6k ] || \
  136.            [ "$SUBARCH" = powerpc/chrp_ibm ]; then
  137.             discover-ibm
  138.         fi
  139.     fi
  140.     if [ "${SUBARCH%%/*}" = sparc ]; then
  141.         discover-sbus
  142.     fi
  143.     if [ -d /sys/bus/usb ]; then
  144.         echo "usb-storage:USB storage"
  145.     fi
  146. }
  147.  
  148. # NewWorld PowerMacs don't want floppy or ide-floppy, and on some models
  149. # (e.g. G5s) the kernel hangs when loading the module.
  150. get_floppy_info() {
  151.     case $SUBARCH in
  152.         powerpc/powermac_newworld) ;;
  153.         *) echo "floppy:Linux Floppy" ;;
  154.     esac
  155. }
  156.  
  157. get_ide_floppy_info() {
  158.     case $SUBARCH in
  159.         powerpc/powermac_newworld) ;;
  160.         *) echo "ide-floppy:Linux IDE floppy" ;;
  161.     esac
  162. }
  163.  
  164. get_rtc_info() {
  165.     # On i386, this gets loaded by hotplug through isapnp, but that
  166.     # doesn't work on amd64.
  167.     case $SUBARCH in
  168.         amd64/*) register-module rtc ;;
  169.     esac
  170. }
  171.  
  172. # Manually load modules to enable things we can't detect.
  173. # XXX: This isn't the best way to do this; we should autodetect.
  174. # The order of these modules are important.
  175. get_manual_hw_info() {
  176.     if [ "$LOAD_IDE" ]; then
  177.         get_floppy_info
  178.         get_ide_chipset_info
  179.         echo "ide-generic:Linux IDE support"
  180.         get_ide_floppy_info
  181.         echo "ide-disk:Linux ATA DISK"
  182.         echo "ide-cd:Linux ATAPI CD-ROM"
  183.     fi
  184.     get_rtc_info
  185.  
  186.     case $SUBARCH in
  187.         powerpc/ps3)
  188.             echo "ps3rom:PS3 internal CD-ROM drive"
  189.             echo "ps3disk:PS3 internal disk drive"
  190.             echo "ps3_gelic:PS3 Gigabit Ethernet"
  191.             register-module snd_ps3
  192.         ;;
  193.     esac
  194. }
  195.  
  196. # Should be greater than the number of kernel modules we can reasonably
  197. # expect it will ever need to load.
  198. MAX_STEPS=1000
  199. OTHER_STEPS=4
  200. # Use 1/10th of the progress bar for the non-module-load steps.
  201. OTHER_STEPSIZE=$(expr $MAX_STEPS / 10 / $OTHER_STEPS)
  202. db_progress START 0 $MAX_STEPS $PROGRESSBAR
  203.  
  204. db_progress INFO hw-detect/detect_progress_step
  205.  
  206. # TODO: Can possibly be removed if udev will load yenta_socket automatically
  207. # Load yenta_socket, if hardware is available, for Cardbus cards.
  208. if [ -d /sys/bus/pci/devices ] && \
  209.     grep -q 0x060700 /sys/bus/pci/devices/*/class 2>/dev/null && \
  210.     ! grep -q ^yenta_socket /proc/modules; then
  211.     db_subst hw-detect/load_progress_step CARDNAME "Cardbus bridge"
  212.     db_subst hw-detect/load_progress_step MODULE "yenta_socket"
  213.     db_progress INFO hw-detect/load_progress_step
  214.     
  215.     log "Detected Cardbus bridge, loading yenta_socket"
  216.     load_module yenta_socket
  217.     # Ugly hack, but what's the alternative?
  218.     sleep 3 || true
  219. fi
  220.  
  221. # Load the ethernet gadget network driver (g_ether) on S3C2410/S3C2440 (Openmoko GTA01/02)
  222. if [ -d /sys/bus/platform/devices/s3c2440-usbgadget -o \
  223.     -d /sys/bus/platform/devices/s3c2410-usbgadget ] ; then
  224.     db_subst hw-detect/load_progress_step CARDNAME "S3C2410/S3C2440 SoC"
  225.     db_subst hw-detect/load_progress_step MODULE "g_ether"
  226.     db_progress INFO hw-detect/load_progress_step
  227.     
  228.     log "Detected S3C2410/S3C2440 SoC, loading g_ether"
  229.     load_module g_ether
  230.     register-module g_ether
  231. fi
  232.  
  233. # If using real hotplug, re-run the rc scripts to pick up new modules.
  234. # TODO: this just loads modules itself, rather than handing back a list
  235. # Since we've just run depmod, new modules might be available, so we
  236. # must trigger as well as settle.
  237. update-dev
  238.  
  239. ALL_HW_INFO=$(get_detected_hw_info; get_manual_hw_info)
  240. db_progress STEP $OTHER_STEPSIZE
  241.  
  242. # Remove modules that are already loaded or not available, and construct
  243. # the list for the question.
  244. LIST=""
  245. PROCESSED=""
  246. AVAIL_MODULES="$(find /lib/modules/$(uname -r)/ | sed 's!.*/!!' | cut -d . -f 1)"
  247. LOADED_MODULES="$(cut -d " " -f 1 /proc/modules) $(cut -d " " -f 1 /proc/modules | sed -e 's/_/-/g')"
  248. IFS_SAVE="$IFS"
  249. IFS="$NEWLINE"
  250. for device in $ALL_HW_INFO; do
  251.     module="${device%%:*}"
  252.     cardname="${device##*:}"
  253.     if [ "$module" != "ignore" -a "$module" != "" ] &&
  254.        ! in_list "$module" "$LOADED_MODULES" &&
  255.        ! in_list "$module" "$PROCESSED"
  256.     then
  257.         if [ -z "$cardname" ]; then
  258.             cardname="[Unknown]"
  259.         fi
  260.         
  261.         if in_list "$module" "$AVAIL_MODULES"; then
  262.             LIST="${LIST:+$LIST, }$module ($(echo "$cardname" | sed 's/,/ /g'))"
  263.             PROCESSED="$PROCESSED $module"
  264.         else
  265.             missing_module "$module" "$cardname"
  266.         fi
  267.     fi
  268. done
  269. IFS="$IFS_SAVE"
  270. db_progress STEP $OTHER_STEPSIZE
  271.  
  272. if [ "$LIST" ]; then
  273.     # Ask which modules to install.
  274.     db_subst hw-detect/select_modules list "$LIST"
  275.     db_set hw-detect/select_modules "$LIST"
  276.     db_input medium hw-detect/select_modules || true
  277.     db_go || exit 10 # back up
  278.     db_get hw-detect/select_modules
  279.     LIST="$RET"
  280. fi
  281.  
  282. list_to_lines() {
  283.     echo "$LIST" | sed 's/, /\n/g'
  284. }
  285.  
  286. # Work out amount to step per module load. expr rounds down, so 
  287. # it may not get quite to 100%, but will at least never exceed it.
  288. MODULE_STEPS=$(expr \( $MAX_STEPS - \( $OTHER_STEPS \* $OTHER_STEPSIZE \) \))
  289. if [ "$LIST" ]; then
  290.     MODULE_STEPSIZE=$(expr $MODULE_STEPS / $(list_to_lines | wc -l))
  291. fi
  292.  
  293. IFS="$NEWLINE"
  294.  
  295. for device in $(list_to_lines); do
  296.     module="${device%% *}"
  297.     cardname="`echo $device | cut -d'(' -f2 | sed 's/)$//'`"
  298.     # Restore IFS after extracting the fields.
  299.     IFS="$IFS_SAVE"
  300.  
  301.     if [ -z "$module" ] ; then module="[Unknown]" ; fi
  302.     if [ -z "$cardname" ] ; then cardname="[Unknown]" ; fi
  303.  
  304.     log "Detected module '$module' for '$cardname'"
  305.  
  306.     if is_not_loaded "$module"; then
  307.         db_subst hw-detect/load_progress_step CARDNAME "$cardname"
  308.         db_subst hw-detect/load_progress_step MODULE "$module"
  309.         db_progress INFO hw-detect/load_progress_step
  310.         if [ "$cardname" = "[Unknown]" ]; then
  311.             load_module "$module"
  312.         else
  313.             load_module "$module" "$cardname"
  314.         fi
  315.     fi
  316.  
  317.     db_progress STEP $MODULE_STEPSIZE
  318.     IFS="$NEWLINE"
  319. done
  320. IFS="$IFS_SAVE"
  321.  
  322. if [ -z "$LIST" ]; then
  323.     db_progress STEP $MODULE_STEPS
  324. fi
  325.  
  326. # Load ide-generic and check if that results in new block devices.
  327. # If so, make sure it is added to the initrd for the installed system.
  328. # Note: this may need to be done for more systems than just systems
  329. # that have an ISA bus, but that seems like a good start; it could also
  330. # be done unconditionally.
  331. if [ -z "$LOAD_IDE" ] && is_not_loaded ide-generic && \
  332.    [ -e /sys/bus/isa ] && is_available ide-generic; then
  333.     update-dev --settle
  334.     blockdev_count=$(ls /sys/block | wc -w)
  335.  
  336.     log "ISA bus detected; loading module 'ide-generic'"
  337.     load_module ide-generic
  338.     update-dev --settle
  339.     if [ $(ls /sys/block | wc -w) -gt $blockdev_count ]; then
  340.         log "New devices detected after loading ide-generic"
  341.  
  342.         # This will tell initramfs-tools to load ide-generic
  343.         kopts=
  344.         if db_get debian-installer/add-kernel-opts && [ "$RET" ]; then
  345.             kopts="$RET"
  346.         fi
  347.         if ! echo "$kopt" | grep -Eq "(^| )all_generic_ide(=1|)( |$)"; then
  348.             db_set debian-installer/add-kernel-opts \
  349.                 "${kopts:+$kopts }all_generic_ide=1"
  350.         fi
  351.     fi
  352. fi
  353.  
  354. if ! is_not_loaded ohci1394 || ! is_not_loaded firewire-ohci; then
  355.     # if firewire was found, try to enable firewire cd support
  356.     if is_not_loaded sbp2 && is_not_loaded firewire-sbp2 && \
  357.         is_available scsi_mod; then
  358.             sbp2module=
  359.         if is_available firewire-sbp2; then
  360.             sbp2module=firewire-sbp2
  361.         elif is_available sbp2; then
  362.             sbp2module=sbp2
  363.         fi
  364.         if [ -n "$sbp2module" ]; then
  365.             db_subst hw-detect/load_progress_step CARDNAME "FireWire CDROM support"
  366.             db_subst hw-detect/load_progress_step MODULE "$sbp2module"
  367.             db_progress INFO hw-detect/load_progress_step
  368.             load_module "$sbp2module"
  369.             register-module "$sbp2module"
  370.         else
  371.             missing_module firewire-sbp2 "FireWire CDROM"
  372.         fi
  373.     fi
  374.     db_progress STEP $OTHER_STEPSIZE
  375. fi
  376.  
  377. # Always load the printer driver on i386 and amd64; it's hard to autodetect.
  378. case $SUBARCH in
  379.     i386/*|amd64/*)
  380.         register-module lp
  381.         ;;
  382. esac
  383.  
  384. apply_pcmcia_resource_opts() {
  385.     local config_opts=/etc/pcmcia/config.opts
  386.     
  387.     # Idempotency
  388.     if ! [ -f ${config_opts}.orig ]; then
  389.         cp $config_opts ${config_opts}.orig
  390.     fi
  391.     cp ${config_opts}.orig $config_opts
  392.  
  393.     local mode=""
  394.     local rmode=""
  395.     local type=""
  396.     local value=""
  397.     while [ -n "$1" ] && [ -n "$2" ] && [ -n "$3" ]; do
  398.         if [ "$1" = exclude ]; then
  399.             mode=exclude
  400.             rmode=include
  401.             shift
  402.         elif [ "$1" = include ]; then
  403.             mode=include
  404.             rmode=exclude
  405.             shift
  406.         fi
  407.         type="$1"
  408.         shift
  409.         value="$1"
  410.         shift
  411.         
  412.         if grep -q "^$rmode $type $value\$" $config_opts; then
  413.             sed "s/^$rmode $type $value\$/$mode $type $value/" \
  414.                 $config_opts >${config_opts}.new
  415.             mv ${config_opts}.new $config_opts
  416.         else
  417.             echo "$mode $type $value" >>$config_opts
  418.         fi
  419.     done
  420. }
  421.  
  422. # get pcmcia running if possible
  423. PCMCIA_INIT=/etc/init.d/pcmciautils
  424. if [ -x "$PCMCIA_INIT" ]; then
  425.     if is_not_loaded pcmcia_core; then
  426.         db_input low hw-detect/pcmcia_resources || true
  427.         db_go || true
  428.  
  429.         if db_get hw-detect/pcmcia_resources && [ "$RET" ]; then
  430.             apply_pcmcia_resource_opts $RET
  431.         fi
  432.         # cdebconf doesn't set seen flags, so this would normally be
  433.         # asked again on subsequent hw-detect runs, which is
  434.         # annoying.
  435.         db_fset hw-detect/pcmcia_resources seen true || true
  436.  
  437.         db_progress INFO hw-detect/pcmcia_step
  438.         $PCMCIA_INIT start 2>&1 | log
  439.         db_progress STEP $OTHER_STEPSIZE
  440.     fi
  441. fi
  442.  
  443. have_pcmcia=0
  444. if ls /sys/class/pcmcia_socket/* >/dev/null 2>&1; then
  445.     if db_get hw-detect/start_pcmcia && [ "$RET" = false ]; then
  446.         have_pcmcia=0
  447.     else
  448.         have_pcmcia=1
  449.     fi
  450. fi
  451.  
  452. # find Cardbus network cards
  453. cardbus_check_netdev()
  454. {
  455.     local socket="$1"
  456.     local netdev="$2"
  457.     if [ -L "$netdev/device" ] && \
  458.         [ -d "$socket/device/$(basename "$(readlink "$netdev/device")")" ]; then
  459.         echo "$(basename "$netdev")" >> /etc/network/devhotplug
  460.     fi
  461. }
  462.  
  463. # Try to do this only once..
  464. if [ "$have_pcmcia" -eq 1 ] && \
  465.    ! grep -q pcmciautils /var/lib/apt-install/queue 2>/dev/null; then
  466.     log "Detected PCMCIA, installing pcmciautils."
  467.     apt-install pcmciautils || true
  468.  
  469.     for socket in /sys/class/pcmcia_socket/*; do
  470.         for netdev in /sys/class/net/*; do
  471.             cardbus_check_netdev "$socket" "$netdev"
  472.         done
  473.     done
  474.  
  475.     if db_get hw-detect/pcmcia_resources && [ -n "$RET" ]; then
  476.         echo "mkdir /target/etc/pcmcia 2>/dev/null || true" \
  477.             >>$finish_install
  478.         echo "cp /etc/pcmcia/config.opts /target/etc/pcmcia/config.opts" \
  479.             >>$finish_install
  480.     fi
  481. fi
  482.  
  483. # Install udev into target
  484. apt-install udev || true
  485.  
  486. # Install pciutils/usbutils
  487. if [ -d /sys/bus/pci ]; then
  488.     apt-install pciutils || true
  489. fi
  490.  
  491. if [ -d /sys/bus/usb ]; then
  492.     apt-install usbutils || true
  493. fi
  494.  
  495. # If hardware has support for pmu, install pbbuttonsd
  496. if [ -d /sys/class/misc/pmu/ ]; then
  497.     apt-install pbbuttonsd || true
  498. fi
  499.  
  500. # Install eject?
  501. if [ -n "$(list-devices cd; list-devices maybe-usb-floppy)" ]; then
  502.     apt-install eject || true
  503. fi
  504.  
  505. # Install optimised libc based on CPU type
  506. case "$(udpkg --print-architecture)" in
  507.     i386)
  508.     case "$(grep '^cpu family' /proc/cpuinfo | head -n1 | cut -d: -f2)" in
  509.         " 6"|" 15")
  510.         # intel 686 or Amd k6.
  511.         apt-install libc6-i686 || true
  512.                 ;;
  513.     esac
  514.     ;;
  515.     sparc)
  516.     if grep -q '^type.*: sun4u' /proc/cpuinfo ; then
  517.         # sparc v9 or v9b
  518.         if grep -q '^cpu.*: .*UltraSparc III' /proc/cpuinfo; then
  519.             apt-install libc6-sparcv9b || true
  520.         else
  521.             apt-install libc6-sparcv9 || true
  522.         fi
  523.     fi
  524.     ;;
  525. esac
  526.  
  527. # Install PS3 utilities
  528. case $SUBARCH in
  529.     powerpc/ps3)
  530.         apt-install ps3pf-utils || true
  531.         ;;
  532. esac
  533.  
  534. # Install Cell utilities
  535. case $SUBARCH in
  536.     powerpc/ps3|powerpc/cell)
  537.         apt-install elfspe2 || true
  538.         ;;
  539. esac
  540.  
  541. db_progress SET $MAX_STEPS
  542. db_progress STOP
  543.  
  544. if [ -n "$MISSING_MODULES_LIST" ]; then
  545.     log "Missing modules '$MISSING_MODULES_LIST"
  546. fi
  547.  
  548. check-missing-firmware
  549.  
  550. sysfs-update-devnames
  551.  
  552. # Let userspace /dev tools rescan the devices
  553. update-dev --settle
  554.  
  555. exit 0
  556.