Project

General

Profile

Download (6.92 KB) Statistics
| Branch: | Tag: | Revision:
1 82bf21fc jim-p
#!/bin/sh
2
#
3
# rc.ramdisk_functions.sh
4
#
5
# part of pfSense (https://www.pfsense.org)
6 a68f7a3d Luiz Otavio O Souza
# Copyright (c) 2020-2024 Rubicon Communications, LLC (Netgate)
7 82bf21fc jim-p
# 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 a1ccf0db Christian McDonald
# Replacement for /etc/rc.d/zfsbe onestart
30
_be_remount_ds() {
31
	local _dataset="${1}"
32
33
	/sbin/zfs list -rH -o mountpoint,name,canmount,mounted -s mountpoint -t filesystem "${_dataset}" | \
34
	while read _mp _name _canmount _mounted ; do
35
		# skip filesystems that must *not* be mounted
36
		[ "${_canmount}" = "off" -o "${_mp}" = "/" ] && continue
37
		# unmount the dataset if mounted...
38
		[ "$_mounted" = "yes" ] && /sbin/umount -f "${_name}"
39
		# mount the dataset
40
		/sbin/zfs mount "${_name}"
41
	done
42
}
43
44
# Replacement for /etc/rc.d/zfsbe onestart
45
_be_mount_zfs() {
46
	echo -n "Mounting ZFS boot environment..."
47
	/sbin/mount -p | while read _dev _mp _type _rest; do
48
		[ "${_mp}"  = "/" ] || continue
49
		if [ "${_type}" = "zfs" ] ; then
50
			_be_remount_ds "${_dev}"
51
		fi
52
		break
53
	done
54
	echo " done."
55
}
56
57 82bf21fc jim-p
# Check if RAM disks are enabled in config.xml
58
ramdisk_check_enabled () {
59
	[ "$(/usr/local/sbin/read_xml_tag.sh boolean system/use_mfs_tmpvar)" = "true" ]
60
	return $?
61
}
62
63
# Checks that RAM disks are both enabled and that they have not failed
64
ramdisk_is_active () {
65
	ramdisk_check_enabled && [ ! -e ${RAMDISK_FLAG_FILE} ]
66
	return $?
67
}
68
69
# Checks if RAM disk setup failed
70
ramdisk_failed () {
71
	[ -e ${RAMDISK_FLAG_FILE} ]
72
	return $?
73
}
74
75
# Resets the RAM disk failure status
76
ramdisk_reset_status () {
77
	if [ -f ${RAMDISK_FLAG_FILE} ]; then
78
		rm -f ${RAMDISK_FLAG_FILE}
79
	fi
80
}
81
82
# Checks if RAM disks were active on a previous boot (or active now)
83
ramdisk_was_active() {
84
	# If /var is on a memory disk, then RAM disks are active now or were active and recently disabled
85
	DISK_NAME=`/bin/df /var/db/rrd | /usr/bin/tail -1 | /usr/bin/awk '{print $1;}'`
86
	DISK_TYPE=`/usr/bin/basename ${DISK_NAME} | /usr/bin/cut -c1-2`
87
	[ "${DISK_TYPE}" = "md" ]
88
	return $?
89
}
90
91
# Echos the effective size of the given RAM disk (var or tmp)
92
# If set, use that value. If unset, use the default value
93
# Usage example:
94
#   tmpsize = $( ramdisk_get_size tmp )
95
#   varsize = $( ramdisk_get_size var )
96
ramdisk_get_size () {
97
	NAME=${1}
98
	DEFAULT_SIZE=$(eval echo \${RAMDISK_DEFAULT_SIZE_${NAME}})
99
100
	SIZE=$(/usr/local/sbin/read_xml_tag.sh string system/use_mfs_${NAME}_size)
101
	if [ -n "${SIZE}" ] && [ ${SIZE} -gt 0 ]; then
102
		echo ${SIZE}
103
	else
104
		echo ${DEFAULT_SIZE}
105
	fi
106
	return 0
107
}
108
109
# Tests if the current total RAM disk size can fit in free kernel memory
110
ramdisk_check_size () {
111
	tmpsize=$( ramdisk_get_size tmp )
112
	varsize=$( ramdisk_get_size var )
113 d566427f jim-p
	# Check available RAM
114
	PAGES_FREE=$( /sbin/sysctl -n vm.stats.vm.v_free_count )
115
	PAGE_SIZE=$( /sbin/sysctl -n vm.stats.vm.v_page_size )
116
	MEM_FREE=$( /bin/expr ${PAGES_FREE} \* ${PAGE_SIZE} )
117 82bf21fc jim-p
	# Convert to MB
118 1c0fa041 jim-p
	MEM_FREE=$( /bin/expr ${MEM_FREE} / 1024 / 1024 )
119 82bf21fc jim-p
	# Total size of desired RAM disks
120 d566427f jim-p
	MEM_NEED=$( /bin/expr ${tmpsize} + ${varsize} )
121
	[ ${MEM_FREE} -gt ${MEM_NEED} ]
122 82bf21fc jim-p
	return $?
123
}
124
125
# Attempt to mount the given RAM disk (var or tmp)
126
# Usage:
127
#   ramdisk_try_mount tmp
128
#   ramdisk_try_mount var
129
ramdisk_try_mount () {
130
	NAME=$1
131
	if [ ramdisk_check_size ]; then
132
		SIZE=$(eval echo \${${NAME}size})m
133 7d87d7cb jim-p
		if [ "${NAME}" = "tmp" ]; then
134
			MODE="1777"
135
		else
136
			MODE="1755"
137
		fi
138
		/sbin/mount -o rw,size=${SIZE},mode=${MODE} -t tmpfs tmpfs /${NAME}
139 db6e63dd Christian McDonald
		return $?
140 82bf21fc jim-p
	else
141
		return 1;
142
	fi
143
}
144
145
# If the install has RAM disks, or if the full install _was_ using RAM disks, make a backup.
146
ramdisk_make_backup () {
147
	if ramdisk_is_active || ramdisk_was_active; then
148
		echo "Backing up RAM disk contents"
149
		/etc/rc.backup_aliastables.sh
150
		/etc/rc.backup_rrd.sh
151
		/etc/rc.backup_dhcpleases.sh
152
		/etc/rc.backup_logs.sh
153 75f2aba5 Mark Silinio
		/etc/rc.backup_captiveportal.sh
154 382f76bc Christian McDonald
		# /etc/rc.backup_voucher.sh
155 82bf21fc jim-p
	fi
156
}
157
158
# Relocate the pkg database to a specific given location, either disk (/var) or
159
#   to its safe location for use with RAM disks (/root/var)
160
# Usage:
161
#   ramdisk_relocate_pkgdb disk
162
#   ramdisk_relocate_pkgdb ram
163
ramdisk_relocate_pkgdb () {
164
	if [ ${1} = "disk" ]; then
165
		local SRC=/root
166
		local DST=
167
	else
168
		local SRC=
169
		local DST=/root
170
	fi
171
172
	echo "Moving pkg database for ${1} storage"
173
	if [ -d ${SRC}/var/db/pkg ]; then
174
		echo "Clearing ${DST}/var/db/pkg"
175
		rm -rf ${DST}/var/db/pkg 2>/dev/null
176
		/bin/mkdir -p ${DST}/var/db
177
		echo "Moving ${SRC}/var/db/pkg to ${DST}/var/db/"
178
		mv -f ${SRC}/var/db/pkg ${DST}/var/db
179
	fi
180
	if [ -d ${SRC}/var/cache/pkg ]; then
181
		echo "Clearing ${DST}/var/cache/pkg"
182
		rm -rf ${DST}/var/cache/pkg 2>/dev/null
183
		/bin/mkdir -p ${DST}/var/cache
184
		echo "Moving ${SRC}/var/cache/pkg to ${DST}/var/cache/"
185
		mv -f ${SRC}/var/cache/pkg ${DST}/var/cache
186
	fi
187
}
188
189
# Relocate the pkg database as needed based on RAM disk options and status
190
ramdisk_relocate_pkgdb_all () {
191
	unset MOVE_PKG_DATA
192
	unset USE_RAMDISK
193
	if ramdisk_check_enabled; then
194
		USE_RAMDISK=true
195
	fi
196
	if [ -z "${USE_RAMDISK}" -a -f /root/var/db/pkg/local.sqlite ]; then
197
		if ramdisk_failed; then
198
			echo "Not relocating pkg db due to previous RAM disk failure."
199
			return 1
200
		fi
201
		# If RAM disks are disabled, move files back into place
202
		MOVE_PKG_DATA=1
203
		ramdisk_relocate_pkgdb disk
204
	elif [ -n "${USE_RAMDISK}" -a -f /var/db/pkg/local.sqlite ]; then
205
		# If RAM disks are enabled, move files to a safe place
206
		MOVE_PKG_DATA=1
207
		ramdisk_relocate_pkgdb ram
208
	fi
209
}
210
211
# Setup symbolic links for the pkg database
212
ramdisk_link_pkgdb () {
213
	/bin/mkdir -p /var/db /var/cache
214
	if [ ! -e /var/db/pkg ]; then
215
		echo "Creating pkg db symlink"
216
		ln -sf ../../root/var/db/pkg /var/db/pkg
217
	fi
218
	if [ ! -e /var/cache/pkg ]; then
219
		echo "Creating pkg cache symlink"
220
		ln -sf ../../root/var/cache/pkg /var/cache/pkg
221
	fi
222
}
223
224 a1ccf0db Christian McDonald
# Unmounts parent and subordinate datasets
225
ramdisk_zfs_deep_unmount() {
226
	local _path="${1}"
227
228
	/sbin/zfs list -rH -o name,mountpoint -S mountpoint -t filesystem "${_path}" | \
229
	while read _name _mp; do
230
		echo -n "Unmounting ZFS volume ${_name} at ${_mp} for RAM disk..."
231
		/sbin/zfs unmount -f "${_name}" 1>/dev/null 2>&1
232
		echo " done."
233
	done
234
}
235
236
# Mounts ZFS datasets and BE datasets
237
ramdisk_fixup_zfs_mount() {
238
	echo -n "Mounting ZFS volumes..."
239
	/sbin/zfs mount -a 1>/dev/null 2>&1
240
	_be_mount_zfs
241
	echo " done."
242
}
243
244
# Unmounts ZFS datasets and remounts BE datasets
245
ramdisk_fixup_zfs_unmount() {
246
	ramdisk_zfs_deep_unmount "/tmp"
247
	ramdisk_zfs_deep_unmount "/var"
248
	_be_mount_zfs
249 8f585441 Luiz Souza
}