Project

General

Profile

Download (5.92 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-2022 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 RAM
86
	PAGES_FREE=$( /sbin/sysctl -n vm.stats.vm.v_free_count )
87
	PAGE_SIZE=$( /sbin/sysctl -n vm.stats.vm.v_page_size )
88
	MEM_FREE=$( /bin/expr ${PAGES_FREE} \* ${PAGE_SIZE} )
89
	# Convert to MB
90
	MEM_FREE=$( /bin/expr ${MEM_FREE} / 1024 / 1024 )
91
	# Total size of desired RAM disks
92
	MEM_NEED=$( /bin/expr ${tmpsize} + ${varsize} )
93
	[ ${MEM_FREE} -gt ${MEM_NEED} ]
94
	return $?
95
}
96

    
97
# Attempt to mount the given RAM disk (var or tmp)
98
# Usage:
99
#   ramdisk_try_mount tmp
100
#   ramdisk_try_mount var
101
ramdisk_try_mount () {
102
	NAME=$1
103
	if [ ramdisk_check_size ]; then
104
		SIZE=$(eval echo \${${NAME}size})m
105
		/sbin/mount -o rw,size=${SIZE},mode=1777 -t tmpfs tmpfs /${NAME}
106
		return $?
107
	else
108
		return 1;
109
	fi
110
}
111

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

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

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

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

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

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