summaryrefslogtreecommitdiffstats
path: root/etc/rc.d/addswap
blob: a6ff1879f06b6b1d15e69d8b7bea7aa307909f54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
#
# Add additional swap files
#
# $FreeBSD$
#

# PROVIDE: addswap
# REQUIRE: FILESYSTEMS kld
# BEFORE:  netif
# KEYWORD: nojail shutdown

. /etc/rc.subr

name="addswap"
start_cmd="addswap_start"
stop_cmd="addswap_stop"

addswap_start()
{
	case ${swapfile} in
	[Nn][Oo] | '')
		;;
	*)
		if [ -w "${swapfile}" ]; then
			check_startmsgs && echo "Adding ${swapfile} as additional swap"

			if [ -n "${swapfile_mdunit}" ]; then
				mdev="/dev/md${swapfile_mdunit#md}"
				mdconfig -a -t vnode -f "${swapfile}" -u ${swapfile_mdunit}
			else
				mdev="/dev/`mdconfig -a -t vnode -f "${swapfile}"`"
			fi

			if [ $? -eq 0 ]; then
				swapon ${mdev}
			else
				echo "error creating swapfile device"
			fi
		fi
		;;
	esac
}

addswap_stop()
{
	case ${swapfile} in
	[Nn][Oo] | '')
		;;
	*)
		if [ -n "${swapfile_mdunit}" ]; then
			mdev="/dev/md${swapfile_mdunit#md}"
		else
			mdev="/dev/`mdconfig -lv | grep "${swapfile}" | cut -f1`"
			swapfile_mdunit=${mdev#md}
		fi
		if [ -n "${swapfile_mdunit}" ]; then
			swapctl -l | grep -q ${mdev}
			if [ $? -eq 0 ]; then
				echo "Dismounting swapfile ${swapfile}"
				swapoff ${mdev} && mdconfig -d -u ${swapfile_mdunit}
			fi
		fi
		;;
	esac
}

load_rc_config $name
run_rc_command "$1"
OpenPOWER on IntegriCloud