Project

General

Profile

Download (14.2 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
# pfSense-rc
4
#
5
# part of pfSense (https://www.pfsense.org)
6
# Copyright (c) 2004-2013 BSD Perimeter
7
# Copyright (c) 2013-2016 Electric Sheep Fencing
8
# Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
# All rights reserved.
10
#
11
# originally based on m0n0wall (http://neon1.net/m0n0wall)
12
# Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
# All rights reserved.
14
#
15
# Licensed under the Apache License, Version 2.0 (the "License");
16
# you may not use this file except in compliance with the License.
17
# You may obtain a copy of the License at
18
#
19
# http://www.apache.org/licenses/LICENSE-2.0
20
#
21
# Unless required by applicable law or agreed to in writing, software
22
# distributed under the License is distributed on an "AS IS" BASIS,
23
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
# See the License for the specific language governing permissions and
25
# limitations under the License.
26

    
27
#/bin/stty status '^T'
28
#/bin/stty susp '^-' intr '^-' quit '^-'
29

    
30
#trap : 2
31
#trap : 3
32

    
33
HOME=/
34
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
35
export HOME PATH
36

    
37
get_version ()
38
{
39
	# Set our current version
40
	version=`/bin/cat /etc/version`
41

    
42
	# Version patch
43
	version_patch="0"
44
	if [ -f /etc/version.patch ]; then
45
		version_patch=`/bin/cat /etc/version.patch`
46
	fi
47

    
48
	if [ "${version_patch}" = "0" ]; then
49
		version_patch=""
50
	else
51
		version_patch=" (Patch ${version_patch})"
52
	fi
53
}
54

    
55
get_version
56

    
57
# Setup dumpdev/ddb/savecore"
58
echo "Configuring crash dumps..."
59
/etc/rc.dumpon
60

    
61
if [ -e /root/force_growfs ]; then
62
	/etc/rc.d/growfs onestart
63
fi
64

    
65
#
66
# The file system needs to be unmounted to guarantee a clean operation of fsck.
67
# Pending changes can keep the file system dirty until all the cached data is
68
# flushed to the disk.
69
#
70
/sbin/mount -ur /
71

    
72
fsck_forced_iterations=`/bin/kenv -q pfsense.fsck.force`
73
if [ ! -z "${fsck_forced_iterations}" ]; then
74
	echo "Forcing filesystem check (${fsck_forced_iterations} times)..."
75
	while [ ${fsck_forced_iterations} -gt 0 ]; do
76
		/sbin/fsck -y -t ufs
77
		fsck_forced_iterations=$((fsck_forced_iterations - 1))
78
	done
79
fi
80

    
81
fsck_fix_flag_file="/.fix_for_SA-19-10.ufs"
82

    
83
# Apply fix for https://www.freebsd.org/security/advisories/FreeBSD-SA-19:10.ufs.asc
84
unset fsck_fix_applied
85
unset skip_fsck_fix
86
unset fsck_fix_count
87
if [ -f ${fsck_fix_flag_file} ]; then
88
	if ! awk '{print $3}' /etc/fstab | grep -q ufs; then
89
		echo "Fix for FreeBSD-SA-19:10.ufs is not needed in this system"
90
		skip_fsck_fix=1
91
	else
92
		fsck_fix_count=$(cat ${fsck_fix_flag_file})
93
		echo "Applying fix for FreeBSD-SA-19:10.ufs"
94
		/sbin/fsck -t ufs -f -p -T ufs:-z >/dev/null 2>&1
95
		if [ $? -eq 0 ]; then
96
			fsck_fix_applied=1
97
		fi
98
	fi
99
fi
100

    
101
# Set it to 0 if it's empty
102
fsck_fix_count=${fsck_fix_count:-0}
103

    
104
FSCK_ACTION_NEEDED=0
105
/sbin/fsck -p
106
case $? in
107
0)
108
	echo "Filesystems are clean, continuing..."
109
	echo "Mounting filesystems..."
110
	;;
111
8|16)
112
	echo "Preen mode recommended running a check that will be performed now."
113
	FSCK_ACTION_NEEDED=1
114
	;;
115
*)
116
	echo "Stopping boot is recommended because filesystem manual action is needed, nevertheless automated repair of the filesystem will be attempted."
117
	FSCK_ACTION_NEEDED=1
118
	;;
119
esac
120

    
121
if [ ${FSCK_ACTION_NEEDED} = 1 ]; then
122
	echo "WARNING: Trying to recover filesystem from inconsistency..."
123
	/sbin/fsck -y -t ufs
124
fi
125

    
126
/sbin/mount -a 2>/dev/null
127
mount_rc=$?
128
attempts=0
129
while [ ${mount_rc} -ne 0 -a ${attempts} -lt 10 ]; do
130
	/sbin/fsck -y -t ufs
131
	/sbin/mount -a 2>/dev/null
132
	mount_rc=$?
133
	attempts=$((attempts+1))
134
done
135

    
136
if [ ${mount_rc} -ne 0 ]; then
137
	echo "ERROR: Impossible to mount filesystem, use interactive shell to attempt to recover it"
138
	/bin/sh
139
	/sbin/reboot
140
fi
141

    
142
# Handle ZFS read-only case
143
unset USE_ZFS
144
if /sbin/kldstat -qm zfs; then
145
	ZFSFSAVAILABLE=$(/sbin/zfs mount 2>/dev/null | wc -l)
146
	if [ $ZFSFSAVAILABLE -eq 0 ]; then
147
		/sbin/kldunload zfs
148
	else
149
		USE_ZFS=1
150
		ZFSROOT=$(/sbin/zfs mount | /usr/bin/awk '$2 == "/" {print $1}')
151
		if [ -n "$ZFSROOT" ]; then
152
			/sbin/zfs set readonly=off $ZFSROOT
153
		fi
154
		/sbin/zfs mount -a
155
		# If /bootpool is present, then there is an additional zfs pool to import
156
		# See https://redmine.pfsense.org/issues/8063
157
		if [ -d /bootpool ]; then
158
			/sbin/zpool import -f bootpool
159
		fi
160
	fi
161
fi
162

    
163
# If /conf is a directory, convert it to a symlink to /cf/conf
164
if [ -d "/conf" ]; then
165
	# If item is not a symlink then rm and recreate
166
	CONFPOINTSTO=`readlink /conf`
167
	if ! test "x$CONFPOINTSTO" = "x/cf/conf"; then
168
		/bin/rm -rf /conf
169
		/bin/ln -s /cf/conf /conf
170
	fi
171
fi
172

    
173
USE_MFS_TMPVAR=$(/usr/local/sbin/read_xml_tag.sh boolean system/use_mfs_tmpvar)
174

    
175
unset MOVE_PKG_DATA
176
# If use MFS var is disabled, move files back to place
177
if [ "${USE_MFS_TMPVAR}" != "true" -a -f /root/var/db/pkg/local.sqlite ]; then
178
	MOVE_PKG_DATA=1
179
	rm -rf /var/db/pkg 2>/dev/null
180
	rm -rf /var/cache/pkg 2>/dev/null
181
	mv -f /root/var/db/pkg /var/db
182
	mv -f /root/var/cache/pkg /var/cache
183
# If use MFS var is enabled, move files to a safe place
184
elif [ "${USE_MFS_TMPVAR}" = "true" -a -f /var/db/pkg/local.sqlite ]; then
185
	MOVE_PKG_DATA=1
186
	rm -rf /root/var/db/pkg 2>/dev/null
187
	rm -rf /root/var/cache/pkg 2>/dev/null
188
	/bin/mkdir -p /root/var/db /root/var/cache
189
	mv -f /var/db/pkg /root/var/db
190
	mv -f /var/cache/pkg /root/var/cache
191
fi
192

    
193
# Mount /var and /tmp on ZFS filesystems when it's necessary
194
if [ -n "${USE_ZFS}" -a "${USE_MFS_TMPVAR}" = "true" ]; then
195
	zfs list -H -o name,mountpoint |
196
	    while read volume mountpoint; do
197
		[ "${mountpoint}" != "/var" -a "${mountpoint}" != "/tmp" ] \
198
			&& continue
199

    
200
		/sbin/zfs umount ${volume}
201
	done
202
fi
203

    
204
if [ "${USE_MFS_TMPVAR}" = "true" ]; then
205
	/etc/rc.embedded
206
fi
207

    
208
if [ -n "${MOVE_PKG_DATA}" -o "${USE_MFS_TMPVAR}" = "true" ]; then
209
	/bin/mkdir -p /var/db /var/cache
210
	ln -sf ../../root/var/db/pkg /var/db/pkg
211
	ln -sf ../../root/var/cache/pkg /var/cache/pkg
212
fi
213

    
214
# Read product_name from $g, defaults to pfSense
215
# Use php -n here because we are not ready to load extensions yet
216
product=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var product_name pfSense)
217

    
218
# Setup ddb on all platforms.
219
if [ ! -z "`sysctl -Nq debug.ddb.scripting.scripts`" ]; then
220
	/sbin/ddb /etc/${product}-ddb.conf
221
fi
222

    
223
# Restore contents of the RAM disk store
224
/etc/rc.restore_ramdisk_store
225

    
226
# Make sure /home exists
227
[ -d /home ] \
228
	|| mkdir /home
229

    
230
/bin/rm -f /root/force_fsck
231
/bin/rm -f /root/force_growfs
232
/bin/rm -f /root/TRIM_set
233
/bin/rm -f /root/TRIM_unset
234

    
235
# Disable APM on ATA drives. Leaving this on will kill drives long-term, especially laptop drives, by generating excessive Load Cycles.
236
if [ -f /etc/rc.disable_hdd_apm ]; then
237
	/etc/rc.disable_hdd_apm
238
fi
239

    
240
# Eject CD devices on 3G modems
241
MANUFACTURER="huawei|zte"
242
CDDEVICE=`dmesg |egrep -ie "($MANUFACTURER)" | awk -F: '/cd/ {print $1}'`
243
if [ "$CDDEVICE" != "" ]; then
244
	cdcontrol -f /dev/"$CDDEVICE" eject
245
fi
246

    
247
# Use php -n here because we are not ready to load extensions yet
248
varrunpath=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var varrun_path "/var/run")
249

    
250
if [ "${USE_MFS_TMPVAR}" != "true" ]; then
251
	/sbin/mdmfs -S -M -s 4m md $varrunpath
252
fi
253

    
254
echo
255
cat /usr/local/share/pfSense/ascii-art/pfsense-logo-small.txt
256
echo
257
echo
258
echo "Welcome to ${product} ${version}${version_patch}..."
259
echo
260

    
261
/sbin/conscontrol mute off >/dev/null
262

    
263
SWAPDEVICE=`/bin/cat /etc/fstab | /usr/bin/grep swap | /usr/bin/cut -f1 | /usr/bin/head -n 1`
264
if [ -n "${SWAPDEVICE}" ]; then
265
	/bin/rm -f /tmp/fstab.swap
266
	if ! [ -c ${SWAPDEVICE} ]; then
267
		# Keep the original device, in case it is special, such as encrypted+mirrored zfs swap
268
		echo "${SWAPDEVICE}	none	swap	sw	0	0" >> /tmp/fstab.swap
269
		# The swap device in fstab does not exist, look for other valid entries and update fstab
270
		for SWAPLABEL in /dev/label/swap* /dev/mirror/swap*; do
271
			if [ -c ${SWAPLABEL} ]; then
272
				echo "${SWAPLABEL}	none	swap	sw	0	0" >> /tmp/fstab.swap
273
			fi
274
		done
275
	else
276
		/bin/cp /etc/fstab /tmp/fstab.swap
277
	fi
278
	/sbin/swapon -F /tmp/fstab.swap -a 2>/dev/null >/dev/null
279
	/etc/rc.savecore
280
fi
281

    
282
# make some directories in /var
283
/bin/mkdir -p $varrunpath /var/log /var/etc /var/db/entropy /var/db/rrd /var/at/jobs/ /var/empty /var/log/nginx 2>/dev/null
284

    
285
# turn off the immutable flag, set /var/empty to read-only, make it immutable again
286
chflags noschg /var/empty
287
chmod 0555 /var/empty
288
chflags schg /var/empty
289

    
290
/bin/rm -rf $varrunpath/*
291

    
292
# Cleanup configuration files from previous instance
293
/bin/rm -rf /var/etc/*
294

    
295
# Workaround for ipsec symlinks, otherwise it's going to break
296
# strongswan pkg upgrade
297

    
298
if [ -L /usr/local/etc/ipsec.d ]; then
299
	rm -f /usr/local/etc/ipsec.d
300
fi
301
if [ -L /usr/local/etc/ipsec.conf ]; then
302
	rm -f /usr/local/etc/ipsec.conf
303
fi
304
if [ -L /usr/local/etc/strongswan.d ]; then
305
	rm -f /usr/local/etc/strongswan.d
306
fi
307
if [ -L /usr/local/etc/strongswan.conf ]; then
308
	rm -f /usr/local/etc/strongswan.conf
309
fi
310

    
311
# Remove deprecated symlinks - #5538
312
for f in /etc/hosts \
313
    /etc/resolv.conf \
314
    /etc/resolvconf.conf \
315
    /etc/syslog.conf; do
316
	if [ -L "${f}" ]; then
317
		rm -f ${f}
318
	fi
319
done
320

    
321
# Make sure our /tmp is 777 + Sticky
322
/bin/chmod 1777 /tmp
323

    
324
if [ ! -L /etc/dhclient.conf ]; then
325
	/bin/rm -rf /etc/dhclient.conf
326
fi
327

    
328
if [ ! -d /var/tmp ]; then
329
	/bin/mkdir -p /var/tmp
330
fi
331
# Make sure our /var/tmp is 777 + Sticky
332
/bin/chmod 1777 /var/tmp
333

    
334
set -T
335
trap "echo 'Reboot interrupted'; exit 1" 3
336

    
337
echo -n "."
338
LOG_FILES="system filter dhcpd vpn poes l2tps openvpn auth portalauth ipsec ppp wireless nginx ntpd gateways resolver routing"
339

    
340
for logfile in $LOG_FILES; do
341
	/usr/bin/touch /var/log/${logfile}.log
342
done
343

    
344
# change permissions on newly created log files.
345
/bin/chmod 0600 /var/log/*.log
346

    
347
echo -n "."
348
DEVFS=`/sbin/mount | /usr/bin/grep devfs | /usr/bin/wc -l | /usr/bin/cut -d" " -f8`
349
if [ "$DEVFS" = "0" ]; then
350
	mount_devfs devfs /dev
351
fi
352

    
353
# Create an initial utmp file
354
cd $varrunpath && /bin/cp /dev/null utmp && /bin/chmod 644 utmp
355

    
356
echo -n "."
357
/sbin/ldconfig -elf /usr/lib /usr/local/lib /lib
358
/etc/rc.d/ldconfig start 2>/dev/null
359

    
360
# Launching kbdmux(4)
361
if [ -f "/dev/kbdmux0" ]; then
362
	echo -n "."
363
	/usr/sbin/kbdcontrol -k /dev/kbdmux0 < /dev/console
364
	[ -c "/dev/atkbd0" ] && kbdcontrol -a atkbd0 < /dev/console
365
	[ -c "/dev/ukbd0" ] && kbdcontrol -a ukbd0 < /dev/console
366
fi
367

    
368
# Fire up unionfs if mount points exist.
369
if [ -f /dist/uniondirs ]; then
370
	echo -n "."
371
	/etc/rc.d/unionfs start
372
fi
373

    
374
echo "done."
375

    
376
# Recreate capabilities DB
377
/usr/bin/cap_mkdb /etc/login.conf
378

    
379
if [ -f /cf/conf/needs_package_sync ]; then
380
	skip_packages=1
381
fi
382

    
383
# Second upgrade stage
384
[ -z "$skip_packages" ] \
385
	&& /usr/local/sbin/${product}-upgrade -y -U -b 2
386

    
387
# Copy default openssl config file
388
[ -d /etc/ssl ] \
389
	|| mkdir -p /etc/ssl
390
[ -f /usr/local/share/${product}/ssl/openssl.cnf ] \
391
	&& cp -f /usr/local/share/${product}/ssl/openssl.cnf /etc/ssl
392
mkdir -p /usr/local/openssl >/dev/null 2>&1
393
ln -sf /etc/ssl/openssl.cnf \
394
	/usr/local/openssl/openssl.cnf
395

    
396
# Run the php.ini setup file and populate
397
# /usr/local/etc/php.ini
398
/etc/rc.php_ini_setup 2>/tmp/php_errors.txt
399
/usr/local/sbin/php-fpm -c /usr/local/etc/php.ini -y /usr/local/lib/php-fpm.conf -RD 2>&1 >/dev/null
400

    
401
# Launch external configuration loader
402
/usr/local/sbin/fcgicli -f /etc/ecl.php
403

    
404
if [ -f /etc/rc.custom_boot_early ]; then
405
	/bin/echo -n "Launching /etc/rc.custom_boot_early...";
406
	/etc/rc.custom_boot_early
407
	echo "Done"
408
fi
409

    
410
export fcgipath=/var/run/php-fpm.socket
411
/usr/bin/nice -n20 /usr/local/sbin/check_reload_status
412

    
413
# let the PHP-based configuration subsystem set up the system now
414
echo -n "Launching the init system..."
415
/bin/rm -f /cf/conf/backup/backup.cache
416
/usr/bin/touch $varrunpath/booting
417

    
418
# Copy custom logo over if it's present
419
if [ -d /usr/local/share/${product}/custom_logos ]; then
420
	cp -f /usr/local/share/${product}/custom_logos/*svg \
421
		/usr/local/www
422
	cp -f /usr/local/share/${product}/custom_logos/*css \
423
		/usr/local/www/css
424
fi
425

    
426
# Apply CPU microcode update
427
[ -x /usr/local/etc/rc.d/microcode_update ] \
428
	&& /usr/local/etc/rc.d/microcode_update onestart
429

    
430
if [ -n "${skip_fsck_fix}" ]; then
431
	rm -f ${fsck_fix_flag_file}
432
elif [ -f ${fsck_fix_flag_file} ]; then
433
	# fsck fix already applied
434
	if [ -n "${fsck_fix_applied}" ]; then
435
		touch /cf/conf/applied_fix_for_SA-19-10.ufs
436
		rm -f ${fsck_fix_flag_file}
437
	elif [ ${fsck_fix_count} -ge 3 ]; then
438
		echo "ERROR: fsck fix for SA-19-10 failed to apply..."
439
		sleep 5
440
		rm -f ${fsck_fix_flag_file}
441
	else
442
		# if / is UFS, reroot instead of reboot
443
		root_fstype=$(mount -p / | awk '{print $3}')
444
		unset reroot
445
		if [ "${root_fstype}" = "ufs" ]; then
446
			reroot="-r"
447
		fi
448

    
449
		# fsck fix failed, increment escape counter to avoid infinite
450
		# loop on a system with a broken filesystem
451
		fsck_fix_count=$((fsck_fix_count+1))
452

    
453
		echo "${fsck_fix_count}" > ${fsck_fix_flag_file}
454

    
455
		# fsck binary was old and didn't have -z option, then reboot
456
		# and run again
457
		echo "fsck needs to run to fix SA-10-10. Rebooting..."
458
		/etc/rc.reboot ${reroot}
459
		exit 0
460
	fi
461
fi
462

    
463
/etc/rc.bootup
464

    
465
# /etc/rc.bootup unset $g['booting'], and removes file
466
# Be sure the file is removed to not create troubles after
467
if [ -f $varrunpath/booting ]; then
468
	/bin/rm $varrunpath/booting
469
fi
470

    
471
echo -n "Starting CRON... "
472
cd /tmp && /usr/sbin/cron -s 2>/dev/null
473
echo "done."
474

    
475
/bin/rm -rf /usr/local/pkg/pf/CVS
476

    
477
# Start ping handler every 240 seconds
478
/usr/local/bin/minicron 240 $varrunpath/ping_hosts.pid /usr/local/bin/ping_hosts.sh
479

    
480
# Start account expire handler every hour
481
/usr/local/bin/minicron 3600 $varrunpath/expire_accounts.pid '/usr/local/sbin/fcgicli -f /etc/rc.expireaccounts'
482

    
483
# Start alias url updater every 24 hours
484
/usr/local/bin/minicron 86400 $varrunpath/update_alias_url_data.pid '/usr/local/sbin/fcgicli -f /etc/rc.update_alias_url_data'
485

    
486
/bin/chmod a+rw /tmp/.
487

    
488
# Check for GEOM mirrors
489
GMIRROR_STATUS=`/sbin/gmirror status`
490
if [ "${GMIRROR_STATUS}" != "" ]; then
491
	# Using a flag file at bootup saves an expensive exec/check on each page load.
492
	/usr/bin/touch /var/run/gmirror_active
493
	# Setup monitoring/notifications
494
	/usr/local/bin/minicron 60 /var/run/gmirror_status_check.pid /usr/local/sbin/gmirror_status_check.php
495
fi
496

    
497
[ -z "$skip_packages" ] \
498
	&& /usr/local/sbin/${product}-upgrade -y -U -b 3
499

    
500
# Start packages
501
[ -z "$skip_packages" ] \
502
	&& /usr/local/sbin/fcgicli -f /etc/rc.start_packages
503

    
504
# Update pkg metadata
505
/etc/rc.update_pkg_metadata now
506

    
507
# Log product version to syslog
508
get_version
509
BUILDTIME=`cat /etc/version.buildtime`
510
ARCH=`uname -m`
511
echo "$product ${version}${version_patch} $ARCH $BUILDTIME"
512

    
513
echo "Bootup complete"
514
echo "Bootup complete" | /usr/bin/logger
515

    
516
/usr/local/bin/beep.sh start 2>&1 >/dev/null
517

    
518
# Reset the cache.  read-only requires this.
519
/bin/rm -f /tmp/config.cache
520

    
521
exit 0
(13-13/81)