Project

General

Profile

Download (13.7 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
. /etc/rc.ramdisk_functions.sh
174

    
175
# Check if RAM disks are enabled, store for repeated use
176
if ramdisk_check_enabled; then
177
	USE_RAMDISK=true
178
fi
179

    
180
# Relocate pkgdb based on desired RAM disk settings
181
ramdisk_relocate_pkgdb_all
182

    
183
# Dismount /tmp and /var on ZFS if using RAM disks and they are separate volumes
184
if [ -n "${USE_ZFS}" -a -n "${USE_RAMDISK}" ]; then
185
	ramdisk_fixup_zfs
186
fi
187

    
188
# Attempt to create and mount RAM disks
189
if [ -n "${USE_RAMDISK}" ]; then
190
	/etc/rc.embedded
191
fi
192

    
193
# If RAM disks are active, make symlinks for pkg database
194
if [ -n "${USE_RAMDISK}" -o -n "${MOVE_PKG_DATA}" ]; then
195
	ramdisk_link_pkgdb
196
fi
197

    
198
# If activating RAM disks failed, then undo some of the above actions
199
if [ -n "${USE_RAMDISK}" ] && ramdisk_failed; then
200
	ramdisk_fixup_zfs mount
201
	ramdisk_relocate_pkgdb disk
202
else
203
	ramdisk_reset_status
204
fi
205

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

    
210
# Setup ddb on all platforms.
211
if [ ! -z "`sysctl -Nq debug.ddb.scripting.scripts`" ]; then
212
	/sbin/ddb /etc/${product}-ddb.conf
213
fi
214

    
215
# Restore contents of the RAM disk store
216
/etc/rc.restore_ramdisk_store
217

    
218
# Make sure /home exists
219
[ -d /home ] \
220
	|| mkdir /home
221

    
222
/bin/rm -f /root/force_fsck
223
/bin/rm -f /root/force_growfs
224
/bin/rm -f /root/TRIM_set
225
/bin/rm -f /root/TRIM_unset
226

    
227
# Disable APM on ATA drives. Leaving this on will kill drives long-term, especially laptop drives, by generating excessive Load Cycles.
228
if [ -f /etc/rc.disable_hdd_apm ]; then
229
	/etc/rc.disable_hdd_apm
230
fi
231

    
232
# Eject CD devices on 3G modems
233
MANUFACTURER="huawei|zte"
234
CDDEVICE=`dmesg |egrep -ie "($MANUFACTURER)" | awk -F: '/cd/ {print $1}'`
235
if [ "$CDDEVICE" != "" ]; then
236
	cdcontrol -f /dev/"$CDDEVICE" eject
237
fi
238

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

    
242
if ! ramdisk_is_active; then
243
	/sbin/mdmfs -S -M -s 4m md $varrunpath
244
fi
245

    
246
echo
247
cat /usr/local/share/pfSense/ascii-art/pfsense-logo-small.txt
248
echo
249
echo
250
echo "Welcome to ${product} ${version}${version_patch}..."
251
echo
252

    
253
/sbin/conscontrol mute off >/dev/null
254

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

    
274
# make some directories in /var
275
/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
276

    
277
# turn off the immutable flag, set /var/empty to read-only, make it immutable again
278
chflags noschg /var/empty
279
chmod 0555 /var/empty
280
chflags schg /var/empty
281

    
282
/bin/rm -rf $varrunpath/*
283

    
284
# Cleanup configuration files from previous instance
285
/bin/rm -rf /var/etc/*
286

    
287
# Workaround for ipsec symlinks, otherwise it's going to break
288
# strongswan pkg upgrade
289

    
290
if [ -L /usr/local/etc/ipsec.d ]; then
291
	rm -f /usr/local/etc/ipsec.d
292
fi
293
if [ -L /usr/local/etc/ipsec.conf ]; then
294
	rm -f /usr/local/etc/ipsec.conf
295
fi
296
if [ -L /usr/local/etc/strongswan.d ]; then
297
	rm -f /usr/local/etc/strongswan.d
298
fi
299
if [ -L /usr/local/etc/strongswan.conf ]; then
300
	rm -f /usr/local/etc/strongswan.conf
301
fi
302

    
303
# Remove deprecated symlinks - #5538
304
for f in /etc/hosts \
305
    /etc/resolv.conf \
306
    /etc/resolvconf.conf \
307
    /etc/syslog.conf; do
308
	if [ -L "${f}" ]; then
309
		rm -f ${f}
310
	fi
311
done
312

    
313
# Make sure our /tmp is 777 + Sticky
314
/bin/chmod 1777 /tmp
315

    
316
if [ ! -L /etc/dhclient.conf ]; then
317
	/bin/rm -rf /etc/dhclient.conf
318
fi
319

    
320
if [ ! -d /var/tmp ]; then
321
	/bin/mkdir -p /var/tmp
322
fi
323
# Make sure our /var/tmp is 777 + Sticky
324
/bin/chmod 1777 /var/tmp
325

    
326
set -T
327
trap "echo 'Reboot interrupted'; exit 1" 3
328

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

    
332
for logfile in $LOG_FILES; do
333
	/usr/bin/touch /var/log/${logfile}.log
334
done
335

    
336
# change permissions on newly created log files.
337
/bin/chmod 0600 /var/log/*.log
338

    
339
echo -n "."
340
DEVFS=`/sbin/mount | /usr/bin/grep devfs | /usr/bin/wc -l | /usr/bin/cut -d" " -f8`
341
if [ "$DEVFS" = "0" ]; then
342
	mount_devfs devfs /dev
343
fi
344

    
345
# Create an initial utmp file
346
cd $varrunpath && /bin/cp /dev/null utmp && /bin/chmod 644 utmp
347

    
348
echo -n "."
349
/sbin/ldconfig -elf /usr/lib /usr/local/lib /lib
350
/etc/rc.d/ldconfig start 2>/dev/null
351

    
352
# Launching kbdmux(4)
353
if [ -f "/dev/kbdmux0" ]; then
354
	echo -n "."
355
	/usr/sbin/kbdcontrol -k /dev/kbdmux0 < /dev/console
356
	[ -c "/dev/atkbd0" ] && kbdcontrol -a atkbd0 < /dev/console
357
	[ -c "/dev/ukbd0" ] && kbdcontrol -a ukbd0 < /dev/console
358
fi
359

    
360
# Fire up unionfs if mount points exist.
361
if [ -f /dist/uniondirs ]; then
362
	echo -n "."
363
	/etc/rc.d/unionfs start
364
fi
365

    
366
echo "done."
367

    
368
# Recreate capabilities DB
369
/usr/bin/cap_mkdb /etc/login.conf
370

    
371
if [ -f /cf/conf/needs_package_sync ]; then
372
	skip_packages=1
373
fi
374

    
375
# Second upgrade stage
376
[ -z "$skip_packages" ] \
377
	&& /usr/local/sbin/${product}-upgrade -y -U -b 2
378

    
379
# Copy default openssl config file
380
[ -d /etc/ssl ] \
381
	|| mkdir -p /etc/ssl
382
[ -f /usr/local/share/${product}/ssl/openssl.cnf ] \
383
	&& cp -f /usr/local/share/${product}/ssl/openssl.cnf /etc/ssl
384
mkdir -p /usr/local/openssl >/dev/null 2>&1
385
ln -sf /etc/ssl/openssl.cnf \
386
	/usr/local/openssl/openssl.cnf
387

    
388
# Run the php.ini setup file and populate
389
# /usr/local/etc/php.ini
390
/etc/rc.php_ini_setup 2>/tmp/php_errors.txt
391
/usr/local/sbin/php-fpm -c /usr/local/etc/php.ini -y /usr/local/lib/php-fpm.conf -RD 2>&1 >/dev/null
392

    
393
# Launch external configuration loader
394
/usr/local/sbin/fcgicli -f /etc/ecl.php
395

    
396
if [ -f /etc/rc.custom_boot_early ]; then
397
	/bin/echo -n "Launching /etc/rc.custom_boot_early...";
398
	/etc/rc.custom_boot_early
399
	echo "Done"
400
fi
401

    
402
export fcgipath=/var/run/php-fpm.socket
403
/usr/bin/nice -n20 /usr/local/sbin/check_reload_status
404

    
405
# let the PHP-based configuration subsystem set up the system now
406
echo -n "Launching the init system..."
407
/bin/rm -f /cf/conf/backup/backup.cache
408
/usr/bin/touch $varrunpath/booting
409

    
410
# Copy custom logo over if it's present
411
if [ -d /usr/local/share/${product}/custom_logos ]; then
412
	cp -f /usr/local/share/${product}/custom_logos/*svg \
413
		/usr/local/www
414
	cp -f /usr/local/share/${product}/custom_logos/*css \
415
		/usr/local/www/css
416
fi
417

    
418
# Apply CPU microcode update
419
[ -x /usr/local/etc/rc.d/microcode_update ] \
420
	&& /usr/local/etc/rc.d/microcode_update onestart
421

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

    
441
		# fsck fix failed, increment escape counter to avoid infinite
442
		# loop on a system with a broken filesystem
443
		fsck_fix_count=$((fsck_fix_count+1))
444

    
445
		echo "${fsck_fix_count}" > ${fsck_fix_flag_file}
446

    
447
		# fsck binary was old and didn't have -z option, then reboot
448
		# and run again
449
		echo "fsck needs to run to fix SA-10-10. Rebooting..."
450
		/etc/rc.reboot ${reroot}
451
		exit 0
452
	fi
453
fi
454

    
455
/etc/rc.bootup
456

    
457
# /etc/rc.bootup unset $g['booting'], and removes file
458
# Be sure the file is removed to not create troubles after
459
if [ -f $varrunpath/booting ]; then
460
	/bin/rm $varrunpath/booting
461
fi
462

    
463
echo -n "Starting CRON... "
464
cd /tmp && /usr/sbin/cron -s 2>/dev/null
465
echo "done."
466

    
467
/bin/rm -rf /usr/local/pkg/pf/CVS
468

    
469
# Start ping handler every 240 seconds
470
/usr/local/bin/minicron 240 $varrunpath/ping_hosts.pid /usr/local/bin/ping_hosts.sh
471

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

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

    
478
/bin/chmod a+rw /tmp/.
479

    
480
# Check for GEOM mirrors
481
GMIRROR_STATUS=`/sbin/gmirror status`
482
if [ "${GMIRROR_STATUS}" != "" ]; then
483
	# Using a flag file at bootup saves an expensive exec/check on each page load.
484
	/usr/bin/touch /var/run/gmirror_active
485
	# Setup monitoring/notifications
486
	/usr/local/bin/minicron 60 /var/run/gmirror_status_check.pid /usr/local/sbin/gmirror_status_check.php
487
fi
488

    
489
[ -z "$skip_packages" ] \
490
	&& /usr/local/sbin/${product}-upgrade -y -U -b 3
491

    
492
# Start packages
493
[ -z "$skip_packages" ] \
494
	&& /usr/local/sbin/fcgicli -f /etc/rc.start_packages
495

    
496
# Update pkg metadata
497
/etc/rc.update_pkg_metadata now
498

    
499
# Log product version to syslog
500
get_version
501
BUILDTIME=`cat /etc/version.buildtime`
502
ARCH=`uname -m`
503
echo "$product ${version}${version_patch} $ARCH $BUILDTIME"
504

    
505
echo "Bootup complete"
506
echo "Bootup complete" | /usr/bin/logger
507

    
508
/usr/local/bin/beep.sh start 2>&1 >/dev/null
509

    
510
# Reset the cache.  read-only requires this.
511
/bin/rm -f /tmp/config.cache
512

    
513
exit 0
(13-13/82)