aboutsummaryrefslogtreecommitdiffstats
path: root/package/block-mount/files/10-swap
blob: 2837d2763e82e314f3563247dd986d2a5051c3b6 (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
70
71
72
73
74
75
76
77
78
#!/bin/sh
# Copyright (C) 2009-2010 OpenWrt.org
# Copyright (C) 2010 Vertical Communications
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

. /lib/functions/block.sh

blkdev=`dirname $DEVPATH`

if [ `basename $blkdev` != "block" ]; then

    device=`basename $DEVPATH`


    case "$ACTION" in
	add)
		local autoswap_from_fstab
		local automount_from_fstab
		local from_fstab
		local anon_mount
		local anon_swap
		local anon_fsck
		local mds_mount_device
		local sds_swap_device
		local sds_swap_enabled
		local use_device
		local do_swap=0

	    mount_dev_section_cb() {
			mds_mount_device="$3"
	    }

	    swap_dev_section_cb() { 
			sds_swap_device="$2"
			sds_swap_enabled="$3"
			return 0
	    }

		config_get_automount
		automount_from_fstab="$from_fstab"
		
		[ "$automount_from_fstab" -eq 1 ] && {
			config_get_mount_section_by_device "/dev/$device"
		}
		
		# skip trying swap if this device is defined as a mount point
		[ -z "$mds_mount_device" ] && {		
			config_get_autoswap
			autoswap_from_fstab="$from_fstab"
		
			[ "$autoswap_from_fstab" -eq 1 ] && {
				config_get_swap_section_by_device "/dev/$device"
				use_device="$sds_swap_device" 
				do_swap="$sds_swap_enabled"
			}

			[ -z "$use_device" ] && [ "$anon_swap" -eq 1 ] && {
				use_device="/dev/$device" && do_swap=1
			}
		}

		[ -n "$use_device" ] && [ "$do_swap" -eq 1 ] && {
			grep -q "$use_device" /proc/swaps || grep -q "$use_device" /proc/mounts || {
				swapon "$use_device"
			}
		}
		reset_dev_section_cb
		;;
	remove)
		grep -q "/dev/$device" /proc/swaps && {
			swapoff "/dev/$device"
		}
		;;
    esac	
fi