#!/bin/sh

MIN=0
MAX=256

#Check that no loop devices are in use,
#if loop devices are in use, print warning
#and exit.
if [ -n "`mount|grep loop`" ]; then
	echo "You should not have any loop devices"
	echo "in use before starting this rocedure"
	exit 1
fi

#Create loop devices in /dev/loop if needed
if [ ! -b /dev/loop255 ]; then
	echo Creating loop devices in /dev...
	for X in `seq $MIN $MAX`; do
		mknod /dev/loop$X b 7 $X;
	done
fi

#Make sure we can have plenty of mounts
MaxCurMount=`cat /proc/sys/fs/super-max`
MinMount=300
if [ $MaxCurMount -lt $MinMount ]; then
	echo "Not enough available mounts,"
	echo "Raising /proc/sys/fs/super-max"
	echo "from $MaxCurMount to $MinMount"
	echo $MinMount >/proc/sys/fs/super-max
	echo
fi

#Make temporary working directory and cd there.
mkdir -p /tmp/looptest.tmp
cd /tmp/looptest.tmp

#Initialize MAXLOOP variable to 0
MAXLOOP=0

#Do Maxloop test, find out how many loop devices we can successfully mount
for X in `seq $MIN $MAX`; do
	dd if=/dev/zero of=$X.file bs=1k count=60 2>/dev/null
	echo y | mkfs $X.file 1>/dev/null 2>/dev/null
	mkdir $X.dir 2>/dev/null
	mount $X.file $X.dir -o loop 2>/dev/null && MAXLOOP=`expr $MAXLOOP + 1` && echo -n . && continue
	break
done
echo

#Double check max loop value, just in case we missed something.
#Lets manually count using mount and wc.
MAXLOOP2=`mount|grep loop|wc -l|sed -e 's/^ *//'`

#At this point we have all the loops mounted that we possibly can.


#Now lets clean up the mess we made.
echo Cleaning up, please wait...
echo "     Unmounting loop devices..."
umount `mount | grep loop | awk '{print $1}'`
echo "     Removing temporary files and directories..."
# An rm -rf /tmp/looptest.tmp would be faster
# but I don't feel comfortable just wacking a directory and all contents.
# What if the directory existed prior to this script...
for X in `seq $MIN $MAX`; do
	rmdir $X.dir 2>/dev/null
	rm $X.file 2>/dev/null
	echo -n .
done
echo
cd /tmp
rmdir /tmp/looptest.tmp

#Restoring mounts to original maximum amount
echo Restoring number of available mounts to $MaxCurMount
echo $MaxCurMount >/proc/sys/fs/super-max

echo
echo
#Print results to user
echo
echo
echo -----------------------------------------------------------
echo "Max valid loop devices is: $MAXLOOP"
echo "Max valid loop devices is: $MAXLOOP2 (double checked value)"
if [ "$MAXLOOP" -ne "$MAXLOOP2" ]; then echo "There seems to be an error in my logic, please check"; fi
echo -----------------------------------------------------------
echo
echo

    Source: geocities.com/rlcomp_1999/scripts

               ( geocities.com/rlcomp_1999)