Project

General

Profile

Download (15.6 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-2023 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/gpart show mmcsd0 2> /dev/null | grep -q freebsd-zfs
68
	if [ $? -eq 0 ]; then
69
		/sbin/zpool status pfSense | grep -q mmcsd0 > /dev/null
70
		if [ $? -eq 0 ]; then
71
			echo "Clearing ZFS label before expanding"
72
			/sbin/zpool labelclear /dev/mmcsd0
73
		fi
74
	fi
75
	/etc/rc.d/growfs onestart
76
fi
77

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

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

    
94
fsck_fix_flag_file="/.fix_for_SA-19-10.ufs"
95

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

    
114
# Set it to 0 if it's empty
115
fsck_fix_count=${fsck_fix_count:-0}
116

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

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

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

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

    
155
. /etc/rc.ramdisk_functions.sh
156

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

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

    
190
# Sanity check the clock
191
/etc/rc.checkclock
192

    
193
# Check if RAM disks are enabled, store for repeated use
194
if ramdisk_check_enabled; then
195
	USE_RAMDISK=true
196
fi
197

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

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

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

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

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

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

    
239
# Restore contents of the RAM disk store
240
/etc/rc.restore_ramdisk_store
241

    
242
# Make sure /home exists
243
[ -d /home ] \
244
	|| mkdir /home
245

    
246
/bin/rm -f /root/force_fsck
247
/bin/rm -f /root/force_growfs
248
/bin/rm -f /root/TRIM_set
249
/bin/rm -f /root/TRIM_unset
250

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

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

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

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

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

    
277
/sbin/conscontrol mute off >/dev/null
278

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

    
298
# make some directories in /var
299
/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
300

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

    
306
/bin/rm -rf $varrunpath/*
307

    
308
# Cleanup configuration files from previous instance
309
/bin/rm -rf /var/etc/*
310

    
311
# Workaround for ipsec symlinks, otherwise it's going to break
312
# strongswan pkg upgrade
313

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

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

    
337
# Make sure our /tmp is 777 + Sticky
338
/bin/chmod 1777 /tmp
339

    
340
if [ ! -L /etc/dhclient.conf ]; then
341
	/bin/rm -rf /etc/dhclient.conf
342
fi
343

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

    
350
set -T
351
trap "echo 'Reboot interrupted'; exit 1" 3
352

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

    
356
for logfile in $LOG_FILES; do
357
	/usr/bin/touch /var/log/${logfile}.log
358
done
359

    
360
# change permissions on newly created log files.
361
/bin/chmod 0600 /var/log/*.log
362

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

    
369
# Create an initial utmp file
370
cd $varrunpath && /bin/cp /dev/null utmp && /bin/chmod 644 utmp
371

    
372
echo -n "."
373
/sbin/ldconfig -elf /usr/lib /usr/local/lib /lib
374
/etc/rc.d/ldconfig start 2>/dev/null
375

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

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

    
390
echo "done."
391

    
392
# Recreate capabilities DB
393
/usr/bin/cap_mkdb /etc/login.conf
394

    
395
if [ -f /cf/conf/needs_package_sync ]; then
396
	skip_packages=1
397
fi
398

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

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

    
412
[ -f /usr/local/share/${product_name}/ssl/netgate-ca.pem ] \
413
	&& cp -f /usr/local/share/${product_name}/ssl/netgate-ca.pem /etc/ssl
414

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

    
420
# Launch external configuration loader
421
/etc/rc.ecl
422

    
423
if [ -f /etc/rc.custom_boot_early ]; then
424
	/bin/echo -n "Launching /etc/rc.custom_boot_early...";
425
	/etc/rc.custom_boot_early
426
	echo "Done"
427
fi
428

    
429
export fcgipath=/var/run/php-fpm.socket
430
/usr/bin/nice -n20 /usr/local/sbin/check_reload_status
431

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

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

    
445
# Apply CPU microcode update
446
[ -x /usr/local/etc/rc.d/microcode_update ] \
447
	&& /usr/local/etc/rc.d/microcode_update onestart
448

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

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

    
472
		echo "${fsck_fix_count}" > ${fsck_fix_flag_file}
473

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

    
482
/etc/rc.bootup
483

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

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

    
511
echo -n "Starting CRON... "
512
cd /tmp && /usr/sbin/cron -s 2>/dev/null
513
echo "done."
514

    
515
/bin/rm -rf /usr/local/pkg/pf/CVS
516

    
517
# Start ping handler every 240 seconds
518
/usr/local/bin/minicron 240 $varrunpath/ping_hosts.pid /usr/local/bin/ping_hosts.sh
519

    
520
# Start IPsec keep alive handler every 300 seconds
521
/usr/local/bin/minicron 300 $varrunpath/ipsec_keepalive.pid /usr/local/bin/ipsec_keepalive.php
522

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

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

    
529
/bin/chmod a+rw /tmp/.
530

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

    
540
[ -z "$skip_packages" ] \
541
	&& /usr/local/sbin/${product_name}-upgrade -y -U -b 3
542

    
543
# Start packages
544
[ -z "$skip_packages" ] \
545
	&& /usr/local/sbin/fcgicli -f /etc/rc.start_packages
546

    
547
# Update pkg metadata
548
/etc/rc.update_pkg_metadata now
549

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

    
556
echo "Bootup complete"
557
echo "Bootup complete" | /usr/bin/logger
558

    
559
/usr/local/bin/beep.sh start 2>&1 >/dev/null
560

    
561
# Reset the cache.  read-only requires this.
562
/bin/rm -f /tmp/config.cache
563

    
564
exit 0
(12-12/85)