#! /bin/bash
#
# Copyright (c) 2002 SuSE Linux AG
#
# The GNU Public License applies
#
# Author:  Susanne Oberhauser <froh@suse.de>, 2002
#
#

# abort on error
set -eu

# to preserve the script from doing anything real, call it with
# CONDOM=echo
: ${CONDOM=}

# create devices here
: ${DEV_ROOT="/dev"}
: ${DEV_OWNER="root:disk"}
: ${DEV_MODE="u=rw,g=rw,o="}

# list all partitions of the active dasds
join -a1 <(
    tr -s "[:punct:][:blank:]" " " < /proc/dasd/devices |
    while read address type at major minor is device state dummy
      do
      if test "$state" = "active"
	  then echo "$device $major $minor $address"
      fi
    done |
    sort
) \
    <(
    while read major minor dummy name
      do
      echo ${name%[0-9]} $name $major $minor
    done < /proc/partitions |
    sort
) |
while read raw_name raw_major raw_minor address name major minor
  do

  # if the entry only exists in /proc/dasd/devices, but not in
  # /proc/partitions, the device node is created:

  if test "$name" = ""
  then
      name=$raw_name
      major=$raw_major
      minor=$raw_minor
  fi

  file=$DEV_ROOT/$name

  # if the node does not exist yet, create it
  if test -e $file
  then
      # this is missing some sanity checks (is it a block device, do
      # major and minor match?)
      ls -l $file
  else
      $CONDOM mknod --mode="$DEV_MODE" $DEV_ROOT/$name b $major $minor
      $CONDOM chown "$DEV_OWNER" $DEV_ROOT/$name
  fi
  # create symlinks address->device
  if test "$raw_major" = "$major" -a "$raw_minor" = "$minor"
  then
      $CONDOM ln -snf $name $DEV_ROOT/dasd${address}disk
  else
      $CONDOM ln -snf $name $DEV_ROOT/dasd${address}partition${name#$raw_name}
  fi

done
# EOF