Project

General

Profile

Download (5.77 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
# rc.ramdisk_functions.sh
4
#
5
# part of pfSense (https://www.pfsense.org)
6
# Copyright (c) 2020 Rubicon Communications, LLC (Netgate)
7
# All rights reserved.
8
#
9
# Licensed under the Apache License, Version 2.0 (the "License");
10
# you may not use this file except in compliance with the License.
11
# You may obtain a copy of the License at
12
#
13
# http://www.apache.org/licenses/LICENSE-2.0
14
#
15
# Unless required by applicable law or agreed to in writing, software
16
# distributed under the License is distributed on an "AS IS" BASIS,
17
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
# See the License for the specific language governing permissions and
19
# limitations under the License.
20

    
21
# Source like so:
22
#     . /etc/rc.ramdisk_functions.sh
23
# Then use these variables and functions wherever RAM disk operations are needed
24

    
25
RAMDISK_FLAG_FILE=/conf/ram_disks_failed
26
RAMDISK_DEFAULT_SIZE_tmp=40
27
RAMDISK_DEFAULT_SIZE_var=60
28

    
29
# Check if RAM disks are enabled in config.xml
30
ramdisk_check_enabled () {
31
	[ "$(/usr/local/sbin/read_xml_tag.sh boolean system/use_mfs_tmpvar)" = "true" ]
32
	return $?
33
}
34

    
35
# Checks that RAM disks are both enabled and that they have not failed
36
ramdisk_is_active () {
37
	ramdisk_check_enabled && [ ! -e ${RAMDISK_FLAG_FILE} ]
38
	return $?
39
}
40

    
41
# Checks if RAM disk setup failed
42
ramdisk_failed () {
43
	[ -e ${RAMDISK_FLAG_FILE} ]
44
	return $?
45
}
46

    
47
# Resets the RAM disk failure status
48
ramdisk_reset_status () {
49
	if [ -f ${RAMDISK_FLAG_FILE} ]; then
50
		rm -f ${RAMDISK_FLAG_FILE}
51
	fi
52
}
53

    
54
# Checks if RAM disks were active on a previous boot (or active now)
55
ramdisk_was_active() {
56
	# If /var is on a memory disk, then RAM disks are active now or were active and recently disabled
57
	DISK_NAME=`/bin/df /var/db/rrd | /usr/bin/tail -1 | /usr/bin/awk '{print $1;}'`
58
	DISK_TYPE=`/usr/bin/basename ${DISK_NAME} | /usr/bin/cut -c1-2`
59
	[ "${DISK_TYPE}" = "md" ]
60
	return $?
61
}
62

    
63
# Echos the effective size of the given RAM disk (var or tmp)
64
# If set, use that value. If unset, use the default value
65
# Usage example:
66
#   tmpsize = $( ramdisk_get_size tmp )
67
#   varsize = $( ramdisk_get_size var )
68
ramdisk_get_size () {
69
	NAME=${1}
70
	DEFAULT_SIZE=$(eval echo \${RAMDISK_DEFAULT_SIZE_${NAME}})
71

    
72
	SIZE=$(/usr/local/sbin/read_xml_tag.sh string system/use_mfs_${NAME}_size)
73
	if [ -n "${SIZE}" ] && [ ${SIZE} -gt 0 ]; then
74
		echo ${SIZE}
75
	else
76
		echo ${DEFAULT_SIZE}
77
	fi
78
	return 0
79
}
80

    
81
# Tests if the current total RAM disk size can fit in free kernel memory
82
ramdisk_check_size () {
83
	tmpsize=$( ramdisk_get_size tmp )
84
	varsize=$( ramdisk_get_size var )
85
	# Check available kmem
86
	KMEM_FREE=$( /sbin/sysctl -n vm.kmem_map_free )
87
	# Convert to MB
88
	KMEM_FREE=$( /bin/expr ${KMEM_FREE} / 1024 / 1024 )
89
	# Total size of desired RAM disks
90
	KMEM_NEED=$( /bin/expr ${tmpsize} + ${varsize} )
91
	# echo "FREE: ${KMEM_FREE} NEED: ${KMEM_NEED}"
92
	[ ${KMEM_FREE} -gt ${KMEM_NEED} ]
93
	return $?
94
}
95

    
96
# Attempt to mount the given RAM disk (var or tmp)
97
# Usage:
98
#   ramdisk_try_mount tmp
99
#   ramdisk_try_mount var
100
ramdisk_try_mount () {
101
	NAME=$1
102
	if [ ramdisk_check_size ]; then
103
		SIZE=$(eval echo \${${NAME}size})m
104
		/sbin/mdmfs -S -M -s ${SIZE} md /${NAME}
105
		return $?
106
	else
107
		return 1;
108
	fi
109
}
110

    
111
# If the install has RAM disks, or if the full install _was_ using RAM disks, make a backup.
112
ramdisk_make_backup () {
113
	if ramdisk_is_active || ramdisk_was_active; then
114
		echo "Backing up RAM disk contents"
115
		/etc/rc.backup_aliastables.sh
116
		/etc/rc.backup_rrd.sh
117
		/etc/rc.backup_dhcpleases.sh
118
		/etc/rc.backup_logs.sh
119
	fi
120
}
121

    
122
# Relocate the pkg database to a specific given location, either disk (/var) or
123
#   to its safe location for use with RAM disks (/root/var)
124
# Usage:
125
#   ramdisk_relocate_pkgdb disk
126
#   ramdisk_relocate_pkgdb ram
127
ramdisk_relocate_pkgdb () {
128
	if [ ${1} = "disk" ]; then
129
		local SRC=/root
130
		local DST=
131
	else
132
		local SRC=
133
		local DST=/root
134
	fi
135

    
136
	echo "Moving pkg database for ${1} storage"
137
	if [ -d ${SRC}/var/db/pkg ]; then
138
		echo "Clearing ${DST}/var/db/pkg"
139
		rm -rf ${DST}/var/db/pkg 2>/dev/null
140
		/bin/mkdir -p ${DST}/var/db
141
		echo "Moving ${SRC}/var/db/pkg to ${DST}/var/db/"
142
		mv -f ${SRC}/var/db/pkg ${DST}/var/db
143
	fi
144
	if [ -d ${SRC}/var/cache/pkg ]; then
145
		echo "Clearing ${DST}/var/cache/pkg"
146
		rm -rf ${DST}/var/cache/pkg 2>/dev/null
147
		/bin/mkdir -p ${DST}/var/cache
148
		echo "Moving ${SRC}/var/cache/pkg to ${DST}/var/cache/"
149
		mv -f ${SRC}/var/cache/pkg ${DST}/var/cache
150
	fi
151
}
152

    
153
# Relocate the pkg database as needed based on RAM disk options and status
154
ramdisk_relocate_pkgdb_all () {
155
	unset MOVE_PKG_DATA
156
	unset USE_RAMDISK
157
	if ramdisk_check_enabled; then
158
		USE_RAMDISK=true
159
	fi
160
	if [ -z "${USE_RAMDISK}" -a -f /root/var/db/pkg/local.sqlite ]; then
161
		if ramdisk_failed; then
162
			echo "Not relocating pkg db due to previous RAM disk failure."
163
			return 1
164
		fi
165
		# If RAM disks are disabled, move files back into place
166
		MOVE_PKG_DATA=1
167
		ramdisk_relocate_pkgdb disk
168
	elif [ -n "${USE_RAMDISK}" -a -f /var/db/pkg/local.sqlite ]; then
169
		# If RAM disks are enabled, move files to a safe place
170
		MOVE_PKG_DATA=1
171
		ramdisk_relocate_pkgdb ram
172
	fi
173
}
174

    
175
# Setup symbolic links for the pkg database
176
ramdisk_link_pkgdb () {
177
	/bin/mkdir -p /var/db /var/cache
178
	if [ ! -e /var/db/pkg ]; then
179
		echo "Creating pkg db symlink"
180
		ln -sf ../../root/var/db/pkg /var/db/pkg
181
	fi
182
	if [ ! -e /var/cache/pkg ]; then
183
		echo "Creating pkg cache symlink"
184
		ln -sf ../../root/var/cache/pkg /var/cache/pkg
185
	fi
186
}
187

    
188
# Either unmount or mount /var and /tmp in ZFS as needed to avoid conflicts with
189
#   active RAM disk options.
190
ramdisk_fixup_zfs () {
191
	# Mount /var and /tmp on ZFS filesystems when necessary
192
	if [ ${1} = "mount" ]; then
193
		echo "Remounting ZFS volumes"
194
		zfs mount -a
195
	else
196
		zfs list -H -o name,mountpoint |
197
		    while read volume mountpoint; do
198
			[ "${mountpoint}" != "/var" -a "${mountpoint}" != "/tmp" ] \
199
				&& continue
200
			echo "Dismounting ZFS volume ${volume} for RAM disk"
201
			/sbin/zfs umount ${volume}
202
		done
203
	fi
204
}
(65-65/82)