Project

General

Profile

Download (15 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-2022 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 -fy -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 -fy -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 -fy -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
# Sanity check the clock
174
/etc/rc.checkclock
175

    
176
. /etc/rc.ramdisk_functions.sh
177

    
178
# Check if RAM disks are enabled, store for repeated use
179
if ramdisk_check_enabled; then
180
	USE_RAMDISK=true
181
fi
182

    
183
# Relocate pkgdb based on desired RAM disk settings
184
ramdisk_relocate_pkgdb_all
185

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

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

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

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

    
209
# Read product_name from $g, defaults to pfSense
210
# Use php -n here because we are not ready to load extensions yet
211
product_name=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var product_name pfSense)
212
product_label=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var product_label pfSense)
213

    
214
# Setup ddb on all platforms.
215
if [ ! -z "`sysctl -Nq debug.ddb.scripting.scripts`" ]; then
216
	/sbin/ddb /etc/${product_name}-ddb.conf
217
fi
218

    
219
# Restore contents of the RAM disk store
220
/etc/rc.restore_ramdisk_store
221

    
222
# Make sure /home exists
223
[ -d /home ] \
224
	|| mkdir /home
225

    
226
/bin/rm -f /root/force_fsck
227
/bin/rm -f /root/force_growfs
228
/bin/rm -f /root/TRIM_set
229
/bin/rm -f /root/TRIM_unset
230

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

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

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

    
246
if ! ramdisk_is_active; then
247
	/sbin/mount -o rw,size=4m,mode=1777 -t tmpfs tmpfs $varrunpath
248
fi
249

    
250
echo
251
cat /usr/local/share/pfSense/ascii-art/pfsense-logo-small.txt
252
echo
253
echo
254
echo "Welcome to ${product_label} ${version}${version_patch}..."
255
echo
256

    
257
/sbin/conscontrol mute off >/dev/null
258

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

    
278
# make some directories in /var
279
/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
280

    
281
# turn off the immutable flag, set /var/empty to read-only, make it immutable again
282
chflags noschg /var/empty
283
chmod 0555 /var/empty
284
chflags schg /var/empty
285

    
286
/bin/rm -rf $varrunpath/*
287

    
288
# Cleanup configuration files from previous instance
289
/bin/rm -rf /var/etc/*
290

    
291
# Workaround for ipsec symlinks, otherwise it's going to break
292
# strongswan pkg upgrade
293

    
294
if [ -L /usr/local/etc/ipsec.d ]; then
295
	rm -f /usr/local/etc/ipsec.d
296
fi
297
if [ -L /usr/local/etc/ipsec.conf ]; then
298
	rm -f /usr/local/etc/ipsec.conf
299
fi
300
if [ -L /usr/local/etc/strongswan.d ]; then
301
	rm -f /usr/local/etc/strongswan.d
302
fi
303
if [ -L /usr/local/etc/strongswan.conf ]; then
304
	rm -f /usr/local/etc/strongswan.conf
305
fi
306

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

    
317
# Make sure our /tmp is 777 + Sticky
318
/bin/chmod 1777 /tmp
319

    
320
if [ ! -L /etc/dhclient.conf ]; then
321
	/bin/rm -rf /etc/dhclient.conf
322
fi
323

    
324
if [ ! -d /var/tmp ]; then
325
	/bin/mkdir -p /var/tmp
326
fi
327
# Make sure our /var/tmp is 777 + Sticky
328
/bin/chmod 1777 /var/tmp
329

    
330
set -T
331
trap "echo 'Reboot interrupted'; exit 1" 3
332

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

    
336
for logfile in $LOG_FILES; do
337
	/usr/bin/touch /var/log/${logfile}.log
338
done
339

    
340
# change permissions on newly created log files.
341
/bin/chmod 0600 /var/log/*.log
342

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

    
349
# Create an initial utmp file
350
cd $varrunpath && /bin/cp /dev/null utmp && /bin/chmod 644 utmp
351

    
352
echo -n "."
353
/sbin/ldconfig -elf /usr/lib /usr/local/lib /lib
354
/etc/rc.d/ldconfig start 2>/dev/null
355

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

    
364
# Fire up unionfs if mount points exist.
365
if [ -f /dist/uniondirs ]; then
366
	echo -n "."
367
	/etc/rc.d/unionfs start
368
fi
369

    
370
echo "done."
371

    
372
# Recreate capabilities DB
373
/usr/bin/cap_mkdb /etc/login.conf
374

    
375
if [ -f /cf/conf/needs_package_sync ]; then
376
	skip_packages=1
377
fi
378

    
379
# Second upgrade stage
380
[ -z "$skip_packages" ] \
381
	&& /usr/local/sbin/${product_name}-upgrade -y -U -b 2
382

    
383
# Copy default openssl config file and Netgate CA
384
[ -d /etc/ssl ] \
385
	|| mkdir -p /etc/ssl
386
[ -f /usr/local/share/${product_name}/ssl/openssl.cnf ] \
387
	&& cp -f /usr/local/share/${product_name}/ssl/openssl.cnf /etc/ssl
388
mkdir -p /usr/local/openssl >/dev/null 2>&1
389
ln -sf /etc/ssl/openssl.cnf \
390
	/usr/local/openssl/openssl.cnf
391

    
392
[ -f /usr/local/share/${product_name}/ssl/netgate-ca.pem ] \
393
	&& cp -f /usr/local/share/${product_name}/ssl/netgate-ca.pem /etc/ssl
394

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

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

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

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

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

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

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

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

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

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

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

    
462
/etc/rc.bootup
463

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

    
470
if [ -n "${USE_ZFS}" ]; then
471
	# Create ZFS reservation
472
	if [ ! -f /.no_zfs_reservation ]; then
473
		zpool list -o name pfSense > /dev/null 2>&1 && ZPOOL=pfSense
474
		zpool list -o name zroot > /dev/null 2>&1 && ZPOOL=zroot
475
		if [ -n "${ZPOOL}" ]; then
476
			zfs list -Hp -o name -t filesystem | grep -q ${ZPOOL}/reservation
477
			if [ $? -ne 0 ]; then
478
				AVAIL=$( zfs list -Hpo avail ${ZPOOL} )
479
				RESSIZE=$( zfs list -Hpo avail,used ${ZPOOL} | awk -v CONVFMT='%.0f' '{printf ( $1 + $2 ) * 0.1 "\n"}' )
480
				if [ $(( ${AVAIL} / 2 )) -gt ${RESSIZE} ]; then
481
					logger "Creating ZFS reservation of ${RESSIZE} bytes on ${ZPOOL}"
482
					zfs create -o reservation=${RESSIZE} ${ZPOOL}/reservation
483
				else
484
					logger "Not enough space to create reservation on ${ZPOOL};  ${AVAIL} / 2 is not greater than ${RESSIZE} bytes"
485
				fi
486
			fi
487
		fi
488
	fi
489
fi
490

    
491
echo -n "Starting CRON... "
492
cd /tmp && /usr/sbin/cron -s 2>/dev/null
493
echo "done."
494

    
495
/bin/rm -rf /usr/local/pkg/pf/CVS
496

    
497
# Start ping handler every 240 seconds
498
/usr/local/bin/minicron 240 $varrunpath/ping_hosts.pid /usr/local/bin/ping_hosts.sh
499

    
500
# Start IPsec keep alive handler every 300 seconds
501
/usr/local/bin/minicron 300 $varrunpath/ipsec_keepalive.pid /usr/local/bin/ipsec_keepalive.php
502

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

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

    
509
/bin/chmod a+rw /tmp/.
510

    
511
# Check for GEOM mirrors
512
GMIRROR_STATUS=`/sbin/gmirror status`
513
if [ "${GMIRROR_STATUS}" != "" ]; then
514
	# Using a flag file at bootup saves an expensive exec/check on each page load.
515
	/usr/bin/touch /var/run/gmirror_active
516
	# Setup monitoring/notifications
517
	/usr/local/bin/minicron 60 /var/run/gmirror_status_check.pid /usr/local/sbin/gmirror_status_check.php
518
fi
519

    
520
[ -z "$skip_packages" ] \
521
	&& /usr/local/sbin/${product_name}-upgrade -y -U -b 3
522

    
523
# Start packages
524
[ -z "$skip_packages" ] \
525
	&& /usr/local/sbin/fcgicli -f /etc/rc.start_packages
526

    
527
# Update pkg metadata
528
/etc/rc.update_pkg_metadata now
529

    
530
# Log product version to syslog
531
get_version
532
BUILDTIME=`cat /etc/version.buildtime`
533
ARCH=`uname -m`
534
echo "$product_label ${version}${version_patch} $ARCH $BUILDTIME"
535

    
536
echo "Bootup complete"
537
echo "Bootup complete" | /usr/bin/logger
538

    
539
/usr/local/bin/beep.sh start 2>&1 >/dev/null
540

    
541
# Reset the cache.  read-only requires this.
542
/bin/rm -f /tmp/config.cache
543

    
544
exit 0
(13-13/85)