Menu

[r3313]: / trunk / etc / rc.d / swap  Maximize  Restore  History

Download this file

104 lines (81 with data), 1.9 kB

#!/bin/sh
# Copyright (c) 2007-2008 Volker Theile (votdev@gmx.de)
# All rights reserved.
#
# Create a swap file. Usefull for embedded systems.
#

# PROVIDE: swap
# REQUIRE: FILESYSTEMS
# KEYWORD: nojail shutdown
# XQUERY: -i "count(//system/swap_enable) > 0" -o "0" -b
# RCVAR: swap

. /etc/rc.subr
. /etc/configxml.subr

name="swap"
rcvar=`set_rcvar`

# Custom commands
extra_commands="status"
start_cmd="start_cmd"
stop_cmd="stop_cmd"
status_cmd="status_cmd"

# Defaults
swap_enable="${swap_enable:=NO}"
mdidfile="/var/run/${name}.mdid"
swap_mountname=`configxml_get "//system/swap_mountname"`
swap_file="/mnt/${swap_mountname}/swap_file"

status_cmd()
{
	local _mdid

	if [ -e ${mdidfile} ]; then
		_mdid=`cat ${mdidfile}`
		echo "${name} is running using mdid ${_mdid}."
	else
		echo "${name} is not running."
		return 1
	fi
}

start_cmd()
{
	local _swap_size _mdid

	# Check if already running?
	if [ -e ${mdidfile} ]; then
		_mdid=`cat ${mdidfile}`
		echo "${name} already running? (mdid=${_mdid})."
		return 0
	fi

	echo "Starting ${name}."

	# Create swap file if necessary
	if [ ! -e ${swap_file} ]; then
		_swap_size=`configxml_get "//system/swap_size"`
		/bin/dd if=/dev/zero of=${swap_file} bs=1024k count=${_swap_size} >/dev/null 2>&1
		[ 0 != $? ] && return 1
		/bin/chmod 0600 ${swap_file}
	fi

	# Create block device
	_mdid=`/sbin/mdconfig -a -t vnode -f ${swap_file}`
	echo ${_mdid} > ${mdidfile}

	# Enable swap
	/sbin/swapon "/dev/${_mdid}"
}

stop_cmd()
{
	local _mdid

	# Check if running?
	if [ ! -e ${mdidfile} ]; then
		echo "${name} not running? (check ${mdidfile})."
		return 0
	fi

	echo "Stopping ${name}."

	# Get memory disk id
	_mdid=`cat ${mdidfile}`

	# Stop swap
	/sbin/swapoff "/dev/${_mdid}"

	# Destroy block device
	/sbin/mdconfig -d -u ${_mdid}

	# Delete swap file
	rm -f ${swap_file}
	
	# Cleanup
	rm -f ${mdidfile}
}

load_rc_config ${name}
run_rc_command "$1"
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.