Project

General

Profile

Download (14.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-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
USE_MFS_TMPVAR=$(/usr/local/sbin/read_xml_tag.sh boolean system/use_mfs_tmpvar)
172

    
173
unset MOVE_PKG_DATA
174
# If use MFS var is disabled, move files back to place
175
if [ "${USE_MFS_TMPVAR}" != "true" -a -f /root/var/db/pkg/local.sqlite ]; then
176
	MOVE_PKG_DATA=1
177
	rm -rf /var/db/pkg 2>/dev/null
178
	rm -rf /var/cache/pkg 2>/dev/null
179
	mv -f /root/var/db/pkg /var/db
180
	mv -f /root/var/cache/pkg /var/cache
181
# If use MFS var is enabled, move files to a safe place
182
elif [ "${USE_MFS_TMPVAR}" = "true" -a -f /var/db/pkg/local.sqlite ]; then
183
	MOVE_PKG_DATA=1
184
	rm -rf /root/var/db/pkg 2>/dev/null
185
	rm -rf /root/var/cache/pkg 2>/dev/null
186
	/bin/mkdir -p /root/var/db /root/var/cache
187
	mv -f /var/db/pkg /root/var/db
188
	mv -f /var/cache/pkg /root/var/cache
189
fi
190

    
191
# Mount /var and /tmp on ZFS filesystems when it's necessary
192
if [ -n "${USE_ZFS}" -a "${USE_MFS_TMPVAR}" = "true" ]; then
193
	zfs list -H -o name,mountpoint |
194
	    while read volume mountpoint; do
195
		[ "${mountpoint}" != "/var" -a "${mountpoint}" != "/tmp" ] \
196
			&& continue
197

    
198
		/sbin/zfs umount ${volume}
199
	done
200
fi
201

    
202
if [ "${USE_MFS_TMPVAR}" = "true" ]; then
203
	/etc/rc.embedded
204
fi
205

    
206
if [ -n "${MOVE_PKG_DATA}" -o "${USE_MFS_TMPVAR}" = "true" ]; then
207
	/bin/mkdir -p /var/db /var/cache
208
	ln -sf ../../root/var/db/pkg /var/db/pkg
209
	ln -sf ../../root/var/cache/pkg /var/cache/pkg
210
fi
211

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

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

    
221
# Restore contents of the RAM disk store
222
/etc/rc.restore_ramdisk_store
223

    
224
# Make sure /home exists
225
[ -d /home ] \
226
	|| mkdir /home
227

    
228
/bin/rm -f /root/force_fsck
229
/bin/rm -f /root/force_growfs
230
/bin/rm -f /root/TRIM_set
231
/bin/rm -f /root/TRIM_unset
232

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

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

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

    
248
if [ "${USE_MFS_TMPVAR}" != "true" ]; then
249
	/sbin/mdmfs -S -M -s 4m md $varrunpath
250
fi
251

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

    
259
/sbin/conscontrol mute off >/dev/null
260

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

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

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

    
288
/bin/rm -rf $varrunpath/*
289

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

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

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

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

    
319
# Make sure our /tmp is 777 + Sticky
320
/bin/chmod 1777 /tmp
321

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

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

    
332
set -T
333
trap "echo 'Reboot interrupted'; exit 1" 3
334

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

    
339
DEFAULT_LOG_FILE_SIZE=$(/usr/local/sbin/read_xml_tag.sh string syslog/logfilesize)
340
DEFAULT_LOG_FILE_SIZE=${DEFAULT_LOG_FILE_SIZE:-"511488"}
341

    
342
for logfile in $LOG_FILES; do
343
	if [ "$DISABLESYSLOGCLOG" = "true" ]; then
344
		/usr/bin/touch /var/log/$logfile.log
345
	else
346
		if [ ! -f /var/log/$logfile.log ]; then
347
			/usr/local/sbin/clog -i -s ${DEFAULT_LOG_FILE_SIZE} /var/log/$logfile.log
348
		fi
349
	fi
350
done
351

    
352
# change permissions on newly created log files.
353
/bin/chmod 0600 /var/log/*.log
354

    
355
echo -n "."
356
DEVFS=`/sbin/mount | /usr/bin/grep devfs | /usr/bin/wc -l | /usr/bin/cut -d" " -f8`
357
if [ "$DEVFS" = "0" ]; then
358
	mount_devfs devfs /dev
359
fi
360

    
361
# Create an initial utmp file
362
cd $varrunpath && /bin/cp /dev/null utmp && /bin/chmod 644 utmp
363

    
364
echo -n "."
365
/sbin/ldconfig -elf /usr/lib /usr/local/lib /lib
366
/etc/rc.d/ldconfig start 2>/dev/null
367

    
368
# Launching kbdmux(4)
369
if [ -f "/dev/kbdmux0" ]; then
370
	echo -n "."
371
	/usr/sbin/kbdcontrol -k /dev/kbdmux0 < /dev/console
372
	[ -c "/dev/atkbd0" ] && kbdcontrol -a atkbd0 < /dev/console
373
	[ -c "/dev/ukbd0" ] && kbdcontrol -a ukbd0 < /dev/console
374
fi
375

    
376
# Fire up unionfs if mount points exist.
377
if [ -f /dist/uniondirs ]; then
378
	echo -n "."
379
	/etc/rc.d/unionfs start
380
fi
381

    
382
echo "done."
383

    
384
# Recreate capabilities DB
385
/usr/bin/cap_mkdb /etc/login.conf
386

    
387
if [ -f /cf/conf/needs_package_sync ]; then
388
	skip_packages=1
389
fi
390

    
391
# Second upgrade stage
392
[ -z "$skip_packages" ] \
393
	&& /usr/local/sbin/${product}-upgrade -y -U -b 2
394

    
395
# Copy default openssl config file
396
[ -d /etc/ssl ] \
397
	|| mkdir -p /etc/ssl
398
[ -f /usr/local/share/${product}/ssl/openssl.cnf ] \
399
	&& cp -f /usr/local/share/${product}/ssl/openssl.cnf /etc/ssl
400
mkdir -p /usr/local/openssl >/dev/null 2>&1
401
ln -sf /etc/ssl/openssl.cnf \
402
	/usr/local/openssl/openssl.cnf
403

    
404
# Run the php.ini setup file and populate
405
# /usr/local/etc/php.ini
406
/etc/rc.php_ini_setup 2>/tmp/php_errors.txt
407
/usr/local/sbin/php-fpm -c /usr/local/etc/php.ini -y /usr/local/lib/php-fpm.conf -RD 2>&1 >/dev/null
408

    
409
# Launch external configuration loader
410
/usr/local/sbin/fcgicli -f /etc/ecl.php
411

    
412
if [ -f /etc/rc.custom_boot_early ]; then
413
	/bin/echo -n "Launching /etc/rc.custom_boot_early...";
414
	/etc/rc.custom_boot_early
415
	echo "Done"
416
fi
417

    
418
export fcgipath=/var/run/php-fpm.socket
419
/usr/bin/nice -n20 /usr/local/sbin/check_reload_status
420

    
421
# let the PHP-based configuration subsystem set up the system now
422
echo -n "Launching the init system..."
423
/bin/rm -f /cf/conf/backup/backup.cache
424
/usr/bin/touch $varrunpath/booting
425

    
426
# Copy custom logo over if it's present
427
if [ -d /usr/local/share/${product}/custom_logos ]; then
428
	cp -f /usr/local/share/${product}/custom_logos/*svg \
429
		/usr/local/www
430
	cp -f /usr/local/share/${product}/custom_logos/*css \
431
		/usr/local/www/css
432
fi
433

    
434
# Apply CPU microcode update
435
[ -x /usr/local/etc/rc.d/microcode_update ] \
436
	&& /usr/local/etc/rc.d/microcode_update onestart
437

    
438
/etc/rc.bootup
439

    
440
# /etc/rc.bootup unset $g['booting'], and removes file
441
# Be sure the file is removed to not create troubles after
442
if [ -f $varrunpath/booting ]; then
443
	/bin/rm $varrunpath/booting
444
fi
445

    
446
echo -n "Starting CRON... "
447
cd /tmp && /usr/sbin/cron -s 2>/dev/null
448
echo "done."
449

    
450
/bin/rm -rf /usr/local/pkg/pf/CVS
451

    
452
# Start ping handler every 240 seconds
453
/usr/local/bin/minicron 240 $varrunpath/ping_hosts.pid /usr/local/bin/ping_hosts.sh
454

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

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

    
461
/bin/chmod a+rw /tmp/.
462

    
463
# Check for GEOM mirrors
464
GMIRROR_STATUS=`/sbin/gmirror status`
465
if [ "${GMIRROR_STATUS}" != "" ]; then
466
	# Using a flag file at bootup saves an expensive exec/check on each page load.
467
	/usr/bin/touch /var/run/gmirror_active
468
	# Setup monitoring/notifications
469
	/usr/local/bin/minicron 60 /var/run/gmirror_status_check.pid /usr/local/sbin/gmirror_status_check.php
470
fi
471

    
472
[ -z "$skip_packages" ] \
473
	&& /usr/local/sbin/${product}-upgrade -y -U -b 3
474

    
475
if [ -n "${skip_fsck_fix}" ]; then
476
	rm -f ${fsck_fix_flag_file}
477
elif [ -f ${fsck_fix_flag_file} ]; then
478
	# fsck fix already applied
479
	if [ -n "${fsck_fix_applied}" ]; then
480
		touch /cf/conf/applied_fix_for_SA-19-10.ufs
481
		rm -f ${fsck_fix_flag_file}
482
	elif [ ${fsck_fix_count} -ge 3 ]; then
483
		echo "ERROR: fsck fix for SA-19-10 failed to apply..."
484
		sleep 5
485
		rm -f ${fsck_fix_flag_file}
486
	else
487
		# if / is UFS, reroot instead of reboot
488
		root_fstype=$(mount -p / | awk '{print $3}')
489
		unset reroot
490
		if [ "${root_fstype}" = "ufs" ]; then
491
			reroot="-r"
492
		fi
493

    
494
		# fsck fix failed, increment escape counter to avoid infinite
495
		# loop on a system with a broken filesystem
496
		fsck_fix_count=$((fsck_fix_count+1))
497

    
498
		echo "${fsck_fix_count}" > ${fsck_fix_flag_file}
499

    
500
		# fsck binary was old and didn't have -z option, then reboot
501
		# and run again
502
		echo "fsck needs to run to fix SA-10-10. Rebooting..."
503
		/etc/rc.reboot ${reroot}
504
		exit 0
505
	fi
506
fi
507

    
508
# Start packages
509
[ -z "$skip_packages" ] \
510
	&& /usr/local/sbin/fcgicli -f /etc/rc.start_packages
511

    
512
# Update pkg metadata
513
/etc/rc.update_pkg_metadata now
514

    
515
# Log product version to syslog
516
get_version
517
BUILDTIME=`cat /etc/version.buildtime`
518
ARCH=`uname -m`
519
echo "$product ${version}${version_patch} $ARCH $BUILDTIME"
520

    
521
echo "Bootup complete"
522

    
523
/usr/local/bin/beep.sh start 2>&1 >/dev/null
524

    
525
# Reset the cache.  read-only requires this.
526
/bin/rm -f /tmp/config.cache
527

    
528
exit 0
(13-13/82)