Project

General

Profile

Download (15.5 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
# Read product_name from $g, defaults to pfSense
58
# Use php -n here because we are not ready to load extensions yet
59
product_name=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var product_name pfSense)
60
product_label=$(/usr/local/bin/php -n /usr/local/sbin/read_global_var product_label pfSense)
61

    
62
# Setup dumpdev/ddb/savecore"
63
echo "Configuring crash dumps..."
64
/etc/rc.dumpon
65

    
66
if [ -e /root/force_growfs ]; then
67
	/sbin/zpool status pfSense | grep -q mmcsd0 > /dev/null
68
	if [ $? -eq 0 ]; then
69
		echo "Clearing ZFS label before expanding"
70
		/sbin/zpool labelclear /dev/mmcsd0
71
	fi
72
	/etc/rc.d/growfs onestart
73
fi
74

    
75
#
76
# The file system needs to be unmounted to guarantee a clean operation of fsck.
77
# Pending changes can keep the file system dirty until all the cached data is
78
# flushed to the disk.
79
#
80
/sbin/mount -ur /
81

    
82
fsck_forced_iterations=`/bin/kenv -q pfsense.fsck.force`
83
if [ ! -z "${fsck_forced_iterations}" ]; then
84
	echo "Forcing filesystem check (${fsck_forced_iterations} times)..."
85
	while [ ${fsck_forced_iterations} -gt 0 ]; do
86
		/sbin/fsck -fy -t ufs
87
		fsck_forced_iterations=$((fsck_forced_iterations - 1))
88
	done
89
fi
90

    
91
fsck_fix_flag_file="/.fix_for_SA-19-10.ufs"
92

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

    
111
# Set it to 0 if it's empty
112
fsck_fix_count=${fsck_fix_count:-0}
113

    
114
FSCK_ACTION_NEEDED=0
115
/sbin/fsck -p
116
case $? in
117
0)
118
	echo "Filesystems are clean, continuing..."
119
	echo "Mounting filesystems..."
120
	;;
121
8|16)
122
	echo "Preen mode recommended running a check that will be performed now."
123
	FSCK_ACTION_NEEDED=1
124
	;;
125
*)
126
	echo "Stopping boot is recommended because filesystem manual action is needed, nevertheless automated repair of the filesystem will be attempted."
127
	FSCK_ACTION_NEEDED=1
128
	;;
129
esac
130

    
131
if [ ${FSCK_ACTION_NEEDED} = 1 ]; then
132
	echo "WARNING: Trying to recover filesystem from inconsistency..."
133
	/sbin/fsck -fy -t ufs
134
fi
135

    
136
/sbin/mount -a 2>/dev/null
137
mount_rc=$?
138
attempts=0
139
while [ ${mount_rc} -ne 0 -a ${attempts} -lt 10 ]; do
140
	/sbin/fsck -fy -t ufs
141
	/sbin/mount -a 2>/dev/null
142
	mount_rc=$?
143
	attempts=$((attempts+1))
144
done
145

    
146
if [ ${mount_rc} -ne 0 ]; then
147
	echo "ERROR: Impossible to mount filesystem, use interactive shell to attempt to recover it"
148
	/bin/sh
149
	/sbin/reboot
150
fi
151

    
152
. /etc/rc.ramdisk_functions.sh
153

    
154
# Handle ZFS read-only case
155
unset USE_ZFS
156
if /sbin/kldstat -qm zfs; then
157
	ZFSFSAVAILABLE=$(/sbin/zfs mount 2>/dev/null | wc -l)
158
	if [ $ZFSFSAVAILABLE -eq 0 ]; then
159
		/sbin/kldunload zfs
160
	else
161
		USE_ZFS=1
162
		ZFSROOT=$(/sbin/zfs mount | /usr/bin/awk '$2 == "/" {print $1}')
163
		if [ -n "$ZFSROOT" ]; then
164
			/sbin/zfs set readonly=off $ZFSROOT
165
		fi
166
		/sbin/zfs mount -a
167
		# If /bootpool is present, then there is an additional zfs pool to import
168
		# See https://redmine.pfsense.org/issues/8063
169
		if [ -d /bootpool ]; then
170
			/sbin/zpool import -f bootpool
171
		fi
172
		# We need to handle ZFS boot environments here
173
		_be_mount_zfs
174
	fi
175
fi
176

    
177
# If /conf is a directory, convert it to a symlink to /cf/conf
178
if [ -d "/conf" ]; then
179
	# If item is not a symlink then rm and recreate
180
	CONFPOINTSTO=`readlink /conf`
181
	if ! test "x$CONFPOINTSTO" = "x/cf/conf"; then
182
		/bin/rm -rf /conf
183
		/bin/ln -s /cf/conf /conf
184
	fi
185
fi
186

    
187
# Sanity check the clock
188
/etc/rc.checkclock
189

    
190
# Check if RAM disks are enabled, store for repeated use
191
if ramdisk_check_enabled; then
192
	USE_RAMDISK=true
193
fi
194

    
195
# Relocate pkgdb on UFS based on desired RAM disk settings
196
if [ -z "${USE_ZFS}" ]; then
197
	ramdisk_relocate_pkgdb_all
198
fi
199

    
200
# Dismount /tmp and /var on ZFS if using RAM disks and they are separate volumes
201
if [ -n "${USE_ZFS}" -a -n "${USE_RAMDISK}" ]; then
202
	ramdisk_fixup_zfs_unmount
203
fi
204

    
205
# Attempt to create and mount RAM disks
206
if [ -n "${USE_RAMDISK}" ]; then
207
	/etc/rc.embedded
208
	# Remount the correct subordinate ZFS datasets to ensure they are used properly
209
	if [ -n "${USE_ZFS}" ]; then
210
		_be_mount_zfs
211
	fi
212
fi
213

    
214
# If RAM disks are active, make symlinks for pkg database on UFS
215
if [ -n "${USE_RAMDISK}" -o -n "${MOVE_PKG_DATA}" ]; then
216
	if [ -z "${USE_ZFS}" ]; then
217
		ramdisk_link_pkgdb
218
	fi
219
fi
220

    
221
# If activating RAM disks failed, then undo some of the above actions
222
if [ -n "${USE_RAMDISK}" ] && ramdisk_failed; then
223
	ramdisk_fixup_zfs_mount
224
	if [ -z "${USE_ZFS}" ]; then
225
		ramdisk_relocate_pkgdb disk
226
	fi
227
else
228
	ramdisk_reset_status
229
fi
230

    
231
# Setup ddb on all platforms.
232
if [ ! -z "`sysctl -Nq debug.ddb.scripting.scripts`" ]; then
233
	/sbin/ddb /etc/${product_name}-ddb.conf
234
fi
235

    
236
# Restore contents of the RAM disk store
237
/etc/rc.restore_ramdisk_store
238

    
239
# Make sure /home exists
240
[ -d /home ] \
241
	|| mkdir /home
242

    
243
/bin/rm -f /root/force_fsck
244
/bin/rm -f /root/force_growfs
245
/bin/rm -f /root/TRIM_set
246
/bin/rm -f /root/TRIM_unset
247

    
248
# Disable APM on ATA drives. Leaving this on will kill drives long-term, especially laptop drives, by generating excessive Load Cycles.
249
if [ -f /etc/rc.disable_hdd_apm ]; then
250
	/etc/rc.disable_hdd_apm
251
fi
252

    
253
# Eject CD devices on 3G modems
254
MANUFACTURER="huawei|zte"
255
CDDEVICE=`dmesg |egrep -ie "($MANUFACTURER)" | awk -F: '/cd/ {print $1}'`
256
if [ "$CDDEVICE" != "" ]; then
257
	cdcontrol -f /dev/"$CDDEVICE" eject
258
fi
259

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

    
263
if ! ramdisk_is_active; then
264
	/sbin/mount -o rw,size=4m,mode=1777 -t tmpfs tmpfs $varrunpath
265
fi
266

    
267
echo
268
cat /usr/local/share/pfSense/ascii-art/pfsense-logo-small.txt
269
echo
270
echo
271
echo "Welcome to ${product_label} ${version}${version_patch}..."
272
echo
273

    
274
/sbin/conscontrol mute off >/dev/null
275

    
276
SWAPDEVICE=`/bin/cat /etc/fstab | /usr/bin/grep swap | /usr/bin/cut -f1 | /usr/bin/head -n 1`
277
if [ -n "${SWAPDEVICE}" ]; then
278
	/bin/rm -f /tmp/fstab.swap
279
	if ! [ -c ${SWAPDEVICE} ]; then
280
		# Keep the original device, in case it is special, such as encrypted+mirrored zfs swap
281
		echo "${SWAPDEVICE}	none	swap	sw	0	0" >> /tmp/fstab.swap
282
		# The swap device in fstab does not exist, look for other valid entries and update fstab
283
		for SWAPLABEL in /dev/label/swap* /dev/mirror/swap*; do
284
			if [ -c ${SWAPLABEL} ]; then
285
				echo "${SWAPLABEL}	none	swap	sw	0	0" >> /tmp/fstab.swap
286
			fi
287
		done
288
	else
289
		/bin/cp /etc/fstab /tmp/fstab.swap
290
	fi
291
	/sbin/swapon -F /tmp/fstab.swap -a 2>/dev/null >/dev/null
292
	/etc/rc.savecore
293
fi
294

    
295
# make some directories in /var
296
/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
297

    
298
# turn off the immutable flag, set /var/empty to read-only, make it immutable again
299
chflags noschg /var/empty
300
chmod 0555 /var/empty
301
chflags schg /var/empty
302

    
303
/bin/rm -rf $varrunpath/*
304

    
305
# Cleanup configuration files from previous instance
306
/bin/rm -rf /var/etc/*
307

    
308
# Workaround for ipsec symlinks, otherwise it's going to break
309
# strongswan pkg upgrade
310

    
311
if [ -L /usr/local/etc/ipsec.d ]; then
312
	rm -f /usr/local/etc/ipsec.d
313
fi
314
if [ -L /usr/local/etc/ipsec.conf ]; then
315
	rm -f /usr/local/etc/ipsec.conf
316
fi
317
if [ -L /usr/local/etc/strongswan.d ]; then
318
	rm -f /usr/local/etc/strongswan.d
319
fi
320
if [ -L /usr/local/etc/strongswan.conf ]; then
321
	rm -f /usr/local/etc/strongswan.conf
322
fi
323

    
324
# Remove deprecated symlinks - #5538
325
for f in /etc/hosts \
326
    /etc/resolv.conf \
327
    /etc/resolvconf.conf \
328
    /etc/syslog.conf; do
329
	if [ -L "${f}" ]; then
330
		rm -f ${f}
331
	fi
332
done
333

    
334
# Make sure our /tmp is 777 + Sticky
335
/bin/chmod 1777 /tmp
336

    
337
if [ ! -L /etc/dhclient.conf ]; then
338
	/bin/rm -rf /etc/dhclient.conf
339
fi
340

    
341
if [ ! -d /var/tmp ]; then
342
	/bin/mkdir -p /var/tmp
343
fi
344
# Make sure our /var/tmp is 777 + Sticky
345
/bin/chmod 1777 /var/tmp
346

    
347
set -T
348
trap "echo 'Reboot interrupted'; exit 1" 3
349

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

    
353
for logfile in $LOG_FILES; do
354
	/usr/bin/touch /var/log/${logfile}.log
355
done
356

    
357
# change permissions on newly created log files.
358
/bin/chmod 0600 /var/log/*.log
359

    
360
echo -n "."
361
DEVFS=`/sbin/mount | /usr/bin/grep devfs | /usr/bin/wc -l | /usr/bin/cut -d" " -f8`
362
if [ "$DEVFS" = "0" ]; then
363
	mount_devfs devfs /dev
364
fi
365

    
366
# Create an initial utmp file
367
cd $varrunpath && /bin/cp /dev/null utmp && /bin/chmod 644 utmp
368

    
369
echo -n "."
370
/sbin/ldconfig -elf /usr/lib /usr/local/lib /lib
371
/etc/rc.d/ldconfig start 2>/dev/null
372

    
373
# Launching kbdmux(4)
374
if [ -f "/dev/kbdmux0" ]; then
375
	echo -n "."
376
	/usr/sbin/kbdcontrol -k /dev/kbdmux0 < /dev/console
377
	[ -c "/dev/atkbd0" ] && kbdcontrol -a atkbd0 < /dev/console
378
	[ -c "/dev/ukbd0" ] && kbdcontrol -a ukbd0 < /dev/console
379
fi
380

    
381
# Fire up unionfs if mount points exist.
382
if [ -f /dist/uniondirs ]; then
383
	echo -n "."
384
	/etc/rc.d/unionfs start
385
fi
386

    
387
echo "done."
388

    
389
# Recreate capabilities DB
390
/usr/bin/cap_mkdb /etc/login.conf
391

    
392
if [ -f /cf/conf/needs_package_sync ]; then
393
	skip_packages=1
394
fi
395

    
396
# Second upgrade stage
397
[ -z "$skip_packages" ] \
398
	&& /usr/local/sbin/${product_name}-upgrade -y -U -b 2
399

    
400
# Copy default openssl config file and Netgate CA
401
[ -d /etc/ssl ] \
402
	|| mkdir -p /etc/ssl
403
[ -f /usr/local/share/${product_name}/ssl/openssl.cnf ] \
404
	&& cp -f /usr/local/share/${product_name}/ssl/openssl.cnf /etc/ssl
405
mkdir -p /usr/local/openssl >/dev/null 2>&1
406
ln -sf /etc/ssl/openssl.cnf \
407
	/usr/local/openssl/openssl.cnf
408

    
409
[ -f /usr/local/share/${product_name}/ssl/netgate-ca.pem ] \
410
	&& cp -f /usr/local/share/${product_name}/ssl/netgate-ca.pem /etc/ssl
411

    
412
# Run the php.ini setup file and populate
413
# /usr/local/etc/php.ini
414
/etc/rc.php_ini_setup 2>/tmp/php_errors.txt
415
/usr/local/sbin/php-fpm -c /usr/local/etc/php.ini -y /usr/local/lib/php-fpm.conf -RD 2>&1 >/dev/null
416

    
417
# Launch external configuration loader
418
/etc/rc.ecl
419

    
420
if [ -f /etc/rc.custom_boot_early ]; then
421
	/bin/echo -n "Launching /etc/rc.custom_boot_early...";
422
	/etc/rc.custom_boot_early
423
	echo "Done"
424
fi
425

    
426
export fcgipath=/var/run/php-fpm.socket
427
/usr/bin/nice -n20 /usr/local/sbin/check_reload_status
428

    
429
# let the PHP-based configuration subsystem set up the system now
430
echo -n "Launching the init system..."
431
/bin/rm -f /cf/conf/backup/backup.cache
432
/usr/bin/touch $varrunpath/booting
433

    
434
# Copy custom logo over if it's present
435
if [ -d /usr/local/share/${product_name}/custom_logos ]; then
436
	cp -f /usr/local/share/${product_name}/custom_logos/*svg \
437
		/usr/local/www
438
	cp -f /usr/local/share/${product_name}/custom_logos/*css \
439
		/usr/local/www/css
440
fi
441

    
442
# Apply CPU microcode update
443
[ -x /usr/local/etc/rc.d/microcode_update ] \
444
	&& /usr/local/etc/rc.d/microcode_update onestart
445

    
446
if [ -n "${skip_fsck_fix}" ]; then
447
	rm -f ${fsck_fix_flag_file}
448
elif [ -f ${fsck_fix_flag_file} ]; then
449
	# fsck fix already applied
450
	if [ -n "${fsck_fix_applied}" ]; then
451
		touch /cf/conf/applied_fix_for_SA-19-10.ufs
452
		rm -f ${fsck_fix_flag_file}
453
	elif [ ${fsck_fix_count} -ge 3 ]; then
454
		echo "ERROR: fsck fix for SA-19-10 failed to apply..."
455
		sleep 5
456
		rm -f ${fsck_fix_flag_file}
457
	else
458
		# if / is UFS, reroot instead of reboot
459
		root_fstype=$(mount -p / | awk '{print $3}')
460
		unset reroot
461
		if [ "${root_fstype}" = "ufs" ]; then
462
			reroot="-r"
463
		fi
464

    
465
		# fsck fix failed, increment escape counter to avoid infinite
466
		# loop on a system with a broken filesystem
467
		fsck_fix_count=$((fsck_fix_count+1))
468

    
469
		echo "${fsck_fix_count}" > ${fsck_fix_flag_file}
470

    
471
		# fsck binary was old and didn't have -z option, then reboot
472
		# and run again
473
		echo "fsck needs to run to fix SA-10-10. Rebooting..."
474
		/etc/rc.reboot ${reroot}
475
		exit 0
476
	fi
477
fi
478

    
479
/etc/rc.bootup
480

    
481
# /etc/rc.bootup unset $g['booting'], and removes file
482
# Be sure the file is removed to not create troubles after
483
if [ -f $varrunpath/booting ]; then
484
	/bin/rm $varrunpath/booting
485
fi
486

    
487
if [ -n "${USE_ZFS}" ]; then
488
	# Create ZFS reservation
489
	if [ ! -f /.no_zfs_reservation ]; then
490
		zpool list -o name pfSense > /dev/null 2>&1 && ZPOOL=pfSense
491
		zpool list -o name zroot > /dev/null 2>&1 && ZPOOL=zroot
492
		if [ -n "${ZPOOL}" ]; then
493
			zfs list -Hp -o name -t filesystem | grep -q ${ZPOOL}/reservation
494
			if [ $? -ne 0 ]; then
495
				AVAIL=$( zfs list -Hpo avail ${ZPOOL} )
496
				RESSIZE=$( zfs list -Hpo avail,used ${ZPOOL} | awk -v CONVFMT='%.0f' '{printf ( $1 + $2 ) * 0.1 "\n"}' )
497
				if [ $(( ${AVAIL} / 2 )) -gt ${RESSIZE} ]; then
498
					logger "Creating ZFS reservation of ${RESSIZE} bytes on ${ZPOOL}"
499
					zfs create -o reservation=${RESSIZE} ${ZPOOL}/reservation
500
				else
501
					logger "Not enough space to create reservation on ${ZPOOL};  ${AVAIL} / 2 is not greater than ${RESSIZE} bytes"
502
				fi
503
			fi
504
		fi
505
	fi
506
fi
507

    
508
echo -n "Starting CRON... "
509
cd /tmp && /usr/sbin/cron -s 2>/dev/null
510
echo "done."
511

    
512
/bin/rm -rf /usr/local/pkg/pf/CVS
513

    
514
# Start ping handler every 240 seconds
515
/usr/local/bin/minicron 240 $varrunpath/ping_hosts.pid /usr/local/bin/ping_hosts.sh
516

    
517
# Start IPsec keep alive handler every 300 seconds
518
/usr/local/bin/minicron 300 $varrunpath/ipsec_keepalive.pid /usr/local/bin/ipsec_keepalive.php
519

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

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

    
526
/bin/chmod a+rw /tmp/.
527

    
528
# Check for GEOM mirrors
529
GMIRROR_STATUS=`/sbin/gmirror status`
530
if [ "${GMIRROR_STATUS}" != "" ]; then
531
	# Using a flag file at bootup saves an expensive exec/check on each page load.
532
	/usr/bin/touch /var/run/gmirror_active
533
	# Setup monitoring/notifications
534
	/usr/local/bin/minicron 60 /var/run/gmirror_status_check.pid /usr/local/sbin/gmirror_status_check.php
535
fi
536

    
537
[ -z "$skip_packages" ] \
538
	&& /usr/local/sbin/${product_name}-upgrade -y -U -b 3
539

    
540
# Start packages
541
[ -z "$skip_packages" ] \
542
	&& /usr/local/sbin/fcgicli -f /etc/rc.start_packages
543

    
544
# Update pkg metadata
545
/etc/rc.update_pkg_metadata now
546

    
547
# Log product version to syslog
548
get_version
549
BUILDTIME=`cat /etc/version.buildtime`
550
ARCH=`uname -m`
551
echo "$product_label ${version}${version_patch} $ARCH $BUILDTIME"
552

    
553
echo "Bootup complete"
554
echo "Bootup complete" | /usr/bin/logger
555

    
556
/usr/local/bin/beep.sh start 2>&1 >/dev/null
557

    
558
# Reset the cache.  read-only requires this.
559
/bin/rm -f /tmp/config.cache
560

    
561
exit 0
(12-12/85)