home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / bin / os-prober < prev    next >
Encoding:
Text File  |  2006-08-22  |  2.4 KB  |  104 lines

  1. #!/bin/sh
  2. set -e
  3.  
  4. . /usr/share/os-prober/common.sh
  5.  
  6. partitions () {
  7.     if [ -d /dev/discs ]; then
  8.         find /dev/discs/ -follow -type b | grep /part
  9.     elif [ -d /sys/block ]; then
  10.         for part in /sys/block/*/*[0-9]; do
  11.             if [ -f "$part/start" ]; then
  12.                 name="$(echo "${part##*/}" | sed 's,[!.],/,g')"
  13.                 if [ -e "/dev/$name" ]; then
  14.                     echo "/dev/$name"
  15.                 fi
  16.             fi
  17.         done
  18.     else
  19.         echo "Cannot find list of partitions!" >&2
  20.         exit 1
  21.     fi
  22. }
  23.  
  24. parse_proc_mounts () {
  25.     while read line; do
  26.         set -- $line
  27.         echo "$(mapdevfs $1) $2 $3"
  28.     done
  29. }
  30.  
  31. parse_proc_mdstat () {
  32.     while read line; do
  33.         for word in $line; do
  34.             dev="${word%%[*}"
  35.             # TODO: factor this out to something in di-utils if
  36.             # it's needed elsewhere
  37.             if [ -d /sys/block ] && type udevinfo >/dev/null 2>&1; then
  38.                 if ! udevinfo -q path -n "/dev/$dev" | \
  39.                      grep -q '/.*/.*/'; then
  40.                     continue
  41.                 fi
  42.             elif ! echo "$dev" | grep -q "/part"; then
  43.                 continue
  44.             fi
  45.             raidpart="/dev/$dev"
  46.             echo "$(mapdevfs $raidpart)"
  47.         done
  48.     done
  49. }
  50.  
  51. # Needed for idempotency
  52. rm -f /var/lib/os-prober/labels
  53.  
  54. for prog in /usr/lib/os-probes/init/*; do
  55.     if [ -x $prog ] && [ -f $prog ]; then
  56.         $prog || true
  57.     fi
  58. done
  59.  
  60. # Partman mounts partitions on /dev/ide/ and /dev/scsi, not /dev/discs
  61. # Therefore we use mapdevfs to match partitions with mount points
  62. # and partitions used in RAID
  63. grep "^/dev/" /proc/mounts | parse_proc_mounts >/tmp/mounted-map || true
  64. : >/tmp/raided-map
  65. if [ -f /proc/mdstat ] ; then
  66.     grep "^md" /proc/mdstat | parse_proc_mdstat >/tmp/raided-map || true
  67. fi
  68.  
  69. for partition in $(partitions); do
  70.     mapped=$(mapdevfs $partition)
  71.  
  72.     # Skip partitions used in software RAID arrays
  73.     if grep -q "^$mapped" /tmp/raided-map ; then
  74.         debug "$partition: part of software raid array"
  75.         continue
  76.     fi
  77.  
  78.     if ! grep -q "^$mapped " /tmp/mounted-map ; then
  79.         for test in /usr/lib/os-probes/*; do
  80.             if [ -f $test ] && [ -x $test ]; then
  81.                 debug "running $test on $partition"
  82.                 if $test $partition; then
  83.                     debug "os detected by $test"
  84.                        break
  85.                 fi
  86.             fi
  87.         done
  88.     else
  89.         mpoint=$(grep "^$mapped " /tmp/mounted-map | cut -d " " -f 2)
  90.         if [ "$mpoint" != "/target" ] && [ "$mpoint" != "/" ]; then
  91.             type=$(grep "^$mapped " /tmp/mounted-map | cut -d " " -f 3)
  92.             for test in /usr/lib/os-probes/mounted/*; do
  93.                 if [ -f $test ] && [ -x $test ]; then
  94.                     debug "running $test on mounted $partition"
  95.                     if $test $partition $mpoint $type; then
  96.                         debug "os detected by $test"
  97.                         break
  98.                     fi
  99.                 fi
  100.             done
  101.         fi
  102.     fi
  103. done
  104.