#!/bin/bash
#
#%stage: device
#
if [ "$(echo $blockdev | grep dasd)" ]; then
	root_dasd=1
fi

save_var root_dasd

if use_script dasd; then
    for dasd in $blockdev; do
    	update_blockdev $dasd
	if [ "$blockdriver" = "dasd" ]; then
	    sysdev=$(majorminor2blockdev $blockmajor $blockminor)
	    dir=/sys/block/${sysdev##/dev/}
	    # dir should contain the correct directory now
	    if [ ! -d "$dir" ] || [ ! -d ${dir}/device ] ; then
	    	error 1 "dasd device $dasd not found in sysfs!"
	    else
		diag_module_clause=""
		dir=$(cd -P $dir/device; echo $PWD)
		ccw=${dir##*/}
		if [ -r "$dir/discipline" ]; then
		    read type < $dir/discipline
		    
		    case $type in
			ECKD)
			    drv="dasd-eckd"
			    discipline=0
			    ;;
			FBA)
			    drv="dasd-fba"
			    discipline=1
			    ;;
			DIAG)
			    dasd_modules="dasd_diag_mod"
			    drv="dasd-eckd"
			    discipline=2
			    diag_module_clause="dasd_diag_mod"
			    ;;
			*)
			    ;;
		    esac
		fi
		cat > $tmp_mnt/etc/udev/rules.d/51-dasd-${ccw}.rules <<EOF
ACTION=="add", SUBSYSTEM=="ccw", KERNEL=="$ccw", IMPORT{program}="collect $ccw %k ${ccw} ${diag_module_clause} $drv"
ACTION=="add", SUBSYSTEM=="drivers", KERNEL=="$drv", IMPORT{program}="collect $ccw %k ${ccw} ${diag_module_clause} $drv"
EOF
		if [ "$discipline" -eq 2 ] ; then
		    cat >> $tmp_mnt/etc/udev/rules.d/51-dasd-${ccw}.rules <<EOF
ACTION=="add", SUBSYSTEM=="module", KERNEL=="$diag_module_clause", IMPORT{program}="collect $ccw %k ${ccw} ${diag_module_clause} $drv"
ACTION=="add", ENV{COLLECT_$ccw}=="0", ATTR{[ccw/$ccw]use_diag}="1"
EOF
		fi
		cat >> $tmp_mnt/etc/udev/rules.d/51-dasd-${ccw}.rules <<EOF
ACTION=="add", ENV{COLLECT_$ccw}=="0", ATTR{[ccw/$ccw]online}="1"
EOF
	        verbose "[DASD] $sysdev -> ${ccw} ($type)"
	    fi
	fi
    done
fi

save_var dasd_modules
