Project

General

Profile

Download (14 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-2020 Rubicon Communications, LLC (Netgate)
7
# All rights reserved.
8
#
9
# originally based on m0n0wall (http://neon1.net/m0n0wall)
10
# Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
# All rights reserved.
12
#
13
# Licensed under the Apache License, Version 2.0 (the "License");
14
# you may not use this file except in compliance with the License.
15
# You may obtain a copy of the License at
16
#
17
# http://www.apache.org/licenses/LICENSE-2.0
18
#
19
# Unless required by applicable law or agreed to in writing, software
20
# distributed under the License is distributed on an "AS IS" BASIS,
21
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
# See the License for the specific language governing permissions and
23
# limitations under the License.
24

    
25
#/bin/stty status '^T'
26
#/bin/stty susp '^-' intr '^-' quit '^-'
27

    
28
#trap : 2
29
#trap : 3
30

    
31
HOME=/
32
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
33
export HOME PATH
34

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

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

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

    
53
get_version
54

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

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

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

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

    
79
fsck_fix_flag_file="/.fix_for_SA-19-10.ufs"
80

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

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

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

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

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

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

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

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

    
171
. /etc/rc.ramdisk_functions.sh
172

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

    
178
# Relocate pkgdb based on desired RAM disk settings
179
ramdisk_relocate_pkgdb_all
180

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

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

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

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

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

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

    
213
# Restore contents of the RAM disk store
214
/etc/rc.restore_ramdisk_store
215

    
216
# Make sure /home exists
217
[ -d /home ] \
218
	|| mkdir /home
219

    
220
/bin/rm -f /root/force_fsck
221
/bin/rm -f /root/force_growfs
222
/bin/rm -f /root/TRIM_set
223
/bin/rm -f /root/TRIM_unset
224

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

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

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

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

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

    
251
/sbin/conscontrol mute off >/dev/null
252

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

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

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

    
280
/bin/rm -rf $varrunpath/*
281

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

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

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

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

    
311
# Make sure our /tmp is 777 + Sticky
312
/bin/chmod 1777 /tmp
313

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

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

    
324
set -T
325
trap "echo 'Reboot interrupted'; exit 1" 3
326

    
327
echo -n "."
328
DISABLESYSLOGCLOG=$(/usr/local/sbin/read_xml_tag.sh boolean system/disablesyslogclog)
329
LOG_FILES="system filter dhcpd vpn poes l2tps openvpn portalauth ipsec ppp relayd wireless nginx ntpd gateways resolver routing"
330

    
331
DEFAULT_LOG_FILE_SIZE=$(/usr/local/sbin/read_xml_tag.sh string syslog/logfilesize)
332
DEFAULT_LOG_FILE_SIZE=${DEFAULT_LOG_FILE_SIZE:-"511488"}
333

    
334
for logfile in $LOG_FILES; do
335
	if [ "$DISABLESYSLOGCLOG" = "true" ]; then
336
		/usr/bin/touch /var/log/$logfile.log
337
	else
338
		if [ ! -f /var/log/$logfile.log ]; then
339
			/usr/local/sbin/clog -i -s ${DEFAULT_LOG_FILE_SIZE} /var/log/$logfile.log
340
		fi
341
	fi
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

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

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

    
520
exit 0
(13-13/83)