[Pm-utils] [PATCH] unload modules recursively

Holger Macht hmacht at suse.de
Wed Oct 18 14:53:19 PDT 2006


Hi,

any comments on this? I think it's some kind of mandatory for us to have
such a module unload logic.

Thanks,
	holger

On Fri 06. Oct - 09:05:15, Stefan Seyfried wrote:
> On Thu, Oct 05, 2006 at 06:05:46PM +0200, Stefan Seyfried wrote:
> > Hi.
> > 
> > Sometimes it is usefull to unload a complete stack of modules.
> > This patch implements that:
> > 
> > Index: pm/functions
> > ===================================================================
> > RCS file: /cvs/pm-utils/pm-utils/pm/functions,v
> > retrieving revision 1.20
> > diff -u -p -r1.20 functions
> > --- pm/functions	28 Sep 2006 21:25:36 -0000	1.20
> > +++ pm/functions	5 Oct 2006 16:00:39 -0000
> > @@ -113,13 +113,46 @@ pm_main()
> >  	return 0
> >  }
> >  
> > +# this recursively unloads the given modules and all that depend on it
> > +# first parameter is the module to be unloaded
> >  modunload()
> >  {
> > -	/sbin/lsmod 2>/dev/null | grep -q "$1"
> > -	if [ "$?" == "0" ]; then
> > -		echo "export ${1}_MODULE_LOAD=yes" >> /var/run/pm-suspend
> > -		/sbin/modprobe -r "$1" >/dev/null 2>&1
> > -	fi
> > +	local MOD D C USED MODS I
> > +	local UNL=$1 RET=1
> > +	# RET is the return code. If at least one module was unloaded, return 0.
> > +	#     if the module was not loaded, also return 0 since this is no error.
> > +	#     if no module was unloaded successfully, return 1
> > +	while read MOD D C USED D; do
> > +		[ "$MOD" != "$UNL" ] && continue
> > +		if [ "$USED" == "-" ]; then
> > +			if [ $C -eq 0 ]; then
> > +				if rmmod $UNL; then
> > +					echo "export ${UNL}_MODULE_LOAD=yes" >> /var/run/pm-suspend
> > +					RET=0
> > +				else
> > +					echo "## could not unload '$UNL', usage count was 0" \
> > +						>> /var/run/pm-suspend
> > +				fi
> > +			else
> > +				echo "## could not unload '$UNL', usage count: $C" >> \
> > +					/var/run/pm-suspend
> > +			fi
> > +		else
> > +			USED=${USED//,/ }
> > +			MODS=($USED)
> > +			# it seems slightly more likely to rmmod in one pass, if we try backwards.
> > +			for I in `seq $[${#MODS[@]}-1] -1 0`; do
> > +				MOD=${MODS[$I]}
> > +				modunload $MOD && RET=0
> > +			done
> > +			# if we unloaded at least one module, then let's try again!
> > +			[ $RET -eq 0 ] && modunload $UNL
> > +			RET=$?
> > +		fi
> > +		return $RET
> > +	done < /proc/modules
> > +	# if we came this far, there was nothing to do, the module is no longer loaded.
> > +	return 0
> >  }
> >  
> >  modreload()
> 
> 
> ok, this might actually unload more modules than it will be reloading during
> resume. How about:
> 
>     if rmmod $UNL; then
>         echo "export RESUME_MODULES=\"$UNL \$RESUME_MODULES\"" >> /var/pm/pm-suspend
>         RET=0
>     else
> 
> .....
> 
> and then use RESUME_MODULES in 50modules like this:
> 
> resume_modules()
> {
> 	for x in $RESUME_MODULES; do
> 		modprobe $x > /dev/null 2>&1
> 	done
> }


More information about the Pm-utils mailing list