#!/bin/bash CONFIG="/etc/crypto/crypto.conf" NR="`grep NR_OF_DISKS $CONFIG |sed 's/NR_OF_DISKS=//'`" USBISIN="NO" dm=/sbin/dmsetup cr=/sbin/cryptsetup for (( i =1 ; i <= "$NR" ; i++ )) do ## all variables should go here usb_vendor=`sed -n "/<$i/,/>$i/p" "$CONFIG" | grep "usb_vendor=" | sed 's/usb_vendor=//'` usbmountpoint=`sed -n "/<$i/,/>$i/p" "$CONFIG" | grep "usbmountpoint=" | sed 's/usbmountpoint=//'` usbfs=`sed -n "/<$i/,/>$i/p" "$CONFIG" | grep "usbfs=" | sed 's/usbfs=//'` cryptohd=`sed -n "/<$i/,/>$i/p" "$CONFIG" | grep "cryptohd=" | sed 's/cryptohd=//'` fsocd=`sed -n "/<$i/,/>$i/p" "$CONFIG" | grep "fsocd=" | sed 's/fsocd=//'` mappername=`sed -n "/<$i/,/>$i/p" "$CONFIG" | grep "mappername=" | sed 's/mappername=//'` mapperdev=`sed -n "/<$i/,/>$i/p" "$CONFIG" | grep "mapperdev=" | sed 's/mapperdev=//'` hdmountpoint=`sed -n "/<$i/,/>$i/p" "$CONFIG" | grep "hdmountpoint=" | sed 's/hdmountpoint=//'` nameof_keyfile=`sed -n "/<$i/,/>$i/p" "$CONFIG" | grep "nameof_keyfile=" | sed 's/nameof_keyfile=//'` chiper=`sed -n "/<$i/,/>$i/p" "$CONFIG" | grep "chiper=" | sed 's/chiper=//'` if [[ "$USBISIN" = "NO" ]] then echo "RUNNING USBCHECK" while (x=0) do let x++ sleep 1 ## well we will wait no more than 20 sec.. if not pluged in by then exit.. if [[ "$x" == "25" ]] then echo -e "\n * you better find that usbstick.." && exit 0 fi ## when this returns 1 the kernel has read the device.. ## therfor this must be in the while loop so the variable ## usbdev can be updated on each loop.. ## this can |must be changed if you got a static device.. i dont not.. usbdev="`egrep "(sda1|sdb1|sdc1|sdd1)" /proc/partitions |awk '{print $4}'`" ## A little prechek.. check if the usb device is in mtab but not pluged in.. i can happen :) ## trust me just rip it out and its still be shown as mounted.. just try yourself.. look in df if [[ `df |grep -c "$usbmountpoint"` -eq "1" ]] && [[ $(lsusb | grep -c "${usb_vendor}") -eq "0" ]] ## just remove it and lets continue df then umount -f ${usbmountpoint} ; break fi ## Lets see if the usb device is pluged in,, read by the kernel and mounted already.. if [ $usbdev > /dev/null ] && [ $(lsusb |grep -c "${usb_vendor}") == "1" ] && [[ "`df |grep -c "$usbmountpoint"`" -eq "1" ]] then USBISIN="YES" break fi ## Lets see if the usb device is pluged in,, read by the kernel and not mounted.. if [ $usbdev > /dev/null ] && [ $(lsusb |grep -c "${usb_vendor}") == "1" ] && [[ "`df |grep -c "$usbmountpoint"`" -eq "0" ]] then mount -t "$usbfs" "/dev/$usbdev" "$usbmountpoint" USBISIN="YES" break fi done fi ## Alright.. now we are ready to make the mappers.. if this is true if [[ "$USBISIN" == "YES" ]] && [[ -f "$usbmountpoint/$nameof_keyfile" ]] then if [[ ! -e ${mapperdev} ]] && [[ `df | grep -c "${mapperdev}"` == "0" ]] #make the mapper then echo "making mapper $mapperdev" $cr -c $chiper -d $usbmountpoint/$nameof_keyfile create $mappername $cryptohd && fsck -t $fsocd -T -C -y "$mapperdev" if [[ "$?" != "0" ]] || [[ "$?" != "1" ]] then exit 1 ; fi mount -t "${fsocd}" "${mapperdev}" "${hdmountpoint}" ## If no mapper in dev but mounted ; bad stuff can happen elif [[ ! -e ${mapperdev} ]] && [[ `df |grep -c "${mapperdev}"` == "1" ]] then echo "umounting and making new mapper + mounting mapper" umount -l -r -f ${mountpoint} || echo "crap.. try lsof ${hdmountpoint} and kill all thouse pid's" & exit 1; $cr -c $chiper -d $usbmountpoint/$nameof_keyfile create $mappername $cryptohd & fsck -t $fsocd -T -C -y "$mapperdev" if [[ "$?" != "0" ]] || [[ "$?" != "1" ]] then exit 1 ; fi mount -t "${fsocd}" "${mapperdev}" "${hdmountpoint}" ## If mapper exsits but its not mounted.. remove it and make a new.. elif [[ -e "${mapperdev}" ]] && [[ `df |grep -c "${mapperdev}"` == "0" ]] then echo "removing old mapper.. making new mapper + mounting $mapperdev" ${dm} remove ${mappername} && $cr -c $chiper -d $usbmountpoint/$nameof_keyfile create $mappername $cryptohd && fsck -t $fsocd -T -C -y "$mapperdev" if [[ "$?" != "0" ]] || [[ "$?" != "1" ]] then exit 1 ; fi mount -t "${fsocd}" "${mapperdev}" "${hdmountpoint}" ## if mounted and all well just exit,, elif [[ -e "${mapperdev}" ]] && [[ `df |grep -c "${mapperdev}"` == "1" ]] then echo " *** DEVICE $mapperdev Is already mounted" fi fi done