Project

General

Profile

Download (12.9 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-2018 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_ACTION_NEEDED=0
80
/sbin/fsck -p
81
case $? in
82
0)
83
	echo "Filesystems are clean, continuing..."
84
	echo "Mounting filesystems..."
85
	;;
86
8|16)
87
	echo "Preen mode recommended running a check that will be performed now."
88
	FSCK_ACTION_NEEDED=1
89
	;;
90
*)
91
	echo "Stopping boot is recommended because filesystem manual action is needed, nevertheless automated repair of the filesystem will be attempted."
92
	FSCK_ACTION_NEEDED=1
93
	;;
94
esac
95

    
96
if [ ${FSCK_ACTION_NEEDED} = 1 ]; then
97
	echo "WARNING: Trying to recover filesystem from inconsistency..."
98
	/sbin/fsck -y -t ufs
99
fi
100

    
101
/sbin/mount -a 2>/dev/null
102
mount_rc=$?
103
attempts=0
104
while [ ${mount_rc} -ne 0 -a ${attempts} -lt 10 ]; do
105
	/sbin/fsck -y -t ufs
106
	/sbin/mount -a 2>/dev/null
107
	mount_rc=$?
108
	attempts=$((attempts+1))
109
done
110

    
111
if [ ${mount_rc} -ne 0 ]; then
112
	echo "ERROR: Impossible to mount filesystem, use interactive shell to attempt to recover it"
113
	/bin/sh
114
	/sbin/reboot
115
fi
116

    
117
# Handle ZFS read-only case
118
unset USE_ZFS
119
if /sbin/kldstat -qm zfs; then
120
	ZFSFSAVAILABLE=$(/sbin/zfs mount 2>/dev/null | wc -l)
121
	if [ $ZFSFSAVAILABLE -eq 0 ]; then
122
		/sbin/kldunload zfs
123
	else
124
		USE_ZFS=1
125
		ZFSROOT=$(/sbin/zfs mount | /usr/bin/awk '$2 == "/" {print $1}')
126
		if [ -n "$ZFSROOT" ]; then
127
			/sbin/zfs set readonly=off $ZFSROOT
128
		fi
129
		/sbin/zfs mount -a
130
		# If /bootpool is present, then there is an additional zfs pool to import
131
		# See https://redmine.pfsense.org/issues/8063
132
		if [ -d /bootpool ]; then
133
			/sbin/zpool import -f bootpool
134
		fi
135
	fi
136
fi
137

    
138
# If /conf is a directory, convert it to a symlink to /cf/conf
139
if [ -d "/conf" ]; then
140
	# If item is not a symlink then rm and recreate
141
	CONFPOINTSTO=`readlink /conf`
142
	if ! test "x$CONFPOINTSTO" = "x/cf/conf"; then
143
		/bin/rm -rf /conf
144
		/bin/ln -s /cf/conf /conf
145
	fi
146
fi
147

    
148
USE_MFS_TMPVAR=$(/usr/local/sbin/read_xml_tag.sh boolean system/use_mfs_tmpvar)
149

    
150
unset MOVE_PKG_DATA
151
# If use MFS var is disabled, move files back to place
152
if [ "${USE_MFS_TMPVAR}" != "true" -a -f /root/var/db/pkg/local.sqlite ]; then
153
	MOVE_PKG_DATA=1
154
	rm -rf /var/db/pkg 2>/dev/null
155
	rm -rf /var/cache/pkg 2>/dev/null
156
	mv -f /root/var/db/pkg /var/db
157
	mv -f /root/var/cache/pkg /var/cache
158
# If use MFS var is enabled, move files to a safe place
159
elif [ "${USE_MFS_TMPVAR}" = "true" -a -f /var/db/pkg/local.sqlite ]; then
160
	MOVE_PKG_DATA=1
161
	rm -rf /root/var/db/pkg 2>/dev/null
162
	rm -rf /root/var/cache/pkg 2>/dev/null
163
	/bin/mkdir -p /root/var/db /root/var/cache
164
	mv -f /var/db/pkg /root/var/db
165
	mv -f /var/cache/pkg /root/var/cache
166
fi
167

    
168
# Mount /var and /tmp on ZFS filesystems when it's necessary
169
if [ -n "${USE_ZFS}" -a "${USE_MFS_TMPVAR}" = "true" ]; then
170
	zfs list -H -o name,mountpoint |
171
	    while read volume mountpoint; do
172
		[ "${mountpoint}" != "/var" -a "${mountpoint}" != "/tmp" ] \
173
			&& continue
174

    
175
		/sbin/zfs umount ${volume}
176
	done
177
fi
178

    
179
if [ "${USE_MFS_TMPVAR}" = "true" ]; then
180
	/etc/rc.embedded
181
fi
182

    
183
if [ -n "${MOVE_PKG_DATA}" -o "${USE_MFS_TMPVAR}" = "true" ]; then
184
	/bin/mkdir -p /var/db /var/cache
185
	ln -sf ../../root/var/db/pkg /var/db/pkg
186
	ln -sf ../../root/var/cache/pkg /var/cache/pkg
187
fi
188

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

    
193
# Setup ddb on all platforms.
194
if [ ! -z "`sysctl -Nq debug.ddb.scripting.scripts`" ]; then
195
	/sbin/ddb /etc/${product}-ddb.conf
196
fi
197

    
198
# Restore contents of the RAM disk store
199
/etc/rc.restore_ramdisk_store
200

    
201
# Make sure /home exists
202
[ -d /home ] \
203
	|| mkdir /home
204

    
205
/bin/rm -f /root/force_fsck
206
/bin/rm -f /root/force_growfs
207
/bin/rm -f /root/TRIM_set
208
/bin/rm -f /root/TRIM_unset
209

    
210
# Disable APM on ATA drives. Leaving this on will kill drives long-term, especially laptop drives, by generating excessive Load Cycles.
211
if [ -f /etc/rc.disable_hdd_apm ]; then
212
	/etc/rc.disable_hdd_apm
213
fi
214

    
215
# Eject CD devices on 3G modems
216
MANUFACTURER="huawei|zte"
217
CDDEVICE=`dmesg |egrep -ie "($MANUFACTURER)" | awk -F: '/cd/ {print $1}'`
218
if [ "$CDDEVICE" != "" ]; then
219
	cdcontrol -f /dev/"$CDDEVICE" eject
220
fi
221

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

    
225
if [ "${USE_MFS_TMPVAR}" != "true" ]; then
226
	/sbin/mdmfs -S -M -s 4m md $varrunpath
227
fi
228

    
229
echo
230
cat /usr/local/share/pfSense/ascii-art/pfsense-logo-small.txt
231
echo
232
echo
233
echo "Welcome to ${product} ${version}${version_patch}..."
234
echo
235

    
236
/sbin/conscontrol mute off >/dev/null
237

    
238
SWAPDEVICE=`/bin/cat /etc/fstab | /usr/bin/grep swap | /usr/bin/cut -f1 | /usr/bin/head -n 1`
239
if [ -n "${SWAPDEVICE}" ]; then
240
	/bin/rm -f /tmp/fstab.swap
241
	if ! [ -c ${SWAPDEVICE} ]; then
242
		# Keep the original device, in case it is special, such as encrypted+mirrored zfs swap
243
		echo "${SWAPDEVICE}	none	swap	sw	0	0" >> /tmp/fstab.swap
244
		# The swap device in fstab does not exist, look for other valid entries and update fstab
245
		for SWAPLABEL in /dev/label/swap* /dev/mirror/swap*; do
246
			if [ -c ${SWAPLABEL} ]; then
247
				echo "${SWAPLABEL}	none	swap	sw	0	0" >> /tmp/fstab.swap
248
			fi
249
		done
250
	else
251
		/bin/cp /etc/fstab /tmp/fstab.swap
252
	fi
253
	/sbin/swapon -F /tmp/fstab.swap -a 2>/dev/null >/dev/null
254
	/etc/rc.savecore
255
fi
256

    
257
# make some directories in /var
258
/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
259

    
260
# turn off the immutable flag, set /var/empty to read-only, make it immutable again
261
chflags noschg /var/empty
262
chmod 0555 /var/empty
263
chflags schg /var/empty
264

    
265
/bin/rm -rf $varrunpath/*
266

    
267
# Cleanup configuration files from previous instance
268
/bin/rm -rf /var/etc/*
269

    
270
# Workaround for ipsec symlinks, otherwise it's going to break
271
# strongswan pkg upgrade
272

    
273
if [ -L /usr/local/etc/ipsec.d ]; then
274
	rm -f /usr/local/etc/ipsec.d
275
fi
276
if [ -L /usr/local/etc/ipsec.conf ]; then
277
	rm -f /usr/local/etc/ipsec.conf
278
fi
279
if [ -L /usr/local/etc/strongswan.d ]; then
280
	rm -f /usr/local/etc/strongswan.d
281
fi
282
if [ -L /usr/local/etc/strongswan.conf ]; then
283
	rm -f /usr/local/etc/strongswan.conf
284
fi
285

    
286
# Remove deprecated symlinks - #5538
287
for f in /etc/hosts \
288
    /etc/resolv.conf \
289
    /etc/resolvconf.conf \
290
    /etc/syslog.conf; do
291
	if [ -L "${f}" ]; then
292
		rm -f ${f}
293
	fi
294
done
295

    
296
# Make sure our /tmp is 777 + Sticky
297
/bin/chmod 1777 /tmp
298

    
299
if [ ! -L /etc/dhclient.conf ]; then
300
	/bin/rm -rf /etc/dhclient.conf
301
fi
302

    
303
if [ ! -d /var/tmp ]; then
304
	/bin/mkdir -p /var/tmp
305
fi
306
# Make sure our /var/tmp is 777 + Sticky
307
/bin/chmod 1777 /var/tmp
308

    
309
set -T
310
trap "echo 'Reboot interrupted'; exit 1" 3
311

    
312
echo -n "."
313
DISABLESYSLOGCLOG=$(/usr/local/sbin/read_xml_tag.sh boolean system/disablesyslogclog)
314
LOG_FILES="system filter dhcpd vpn poes l2tps openvpn portalauth ipsec ppp wireless nginx ntpd gateways resolver routing"
315

    
316
DEFAULT_LOG_FILE_SIZE=$(/usr/local/sbin/read_xml_tag.sh string syslog/logfilesize)
317
DEFAULT_LOG_FILE_SIZE=${DEFAULT_LOG_FILE_SIZE:-"511488"}
318

    
319
for logfile in $LOG_FILES; do
320
	if [ "$DISABLESYSLOGCLOG" = "true" ]; then
321
		/usr/bin/touch /var/log/$logfile.log
322
	else
323
		if [ ! -f /var/log/$logfile.log ]; then
324
			/usr/local/sbin/clog -i -s ${DEFAULT_LOG_FILE_SIZE} /var/log/$logfile.log
325
		fi
326
	fi
327
done
328

    
329
# change permissions on newly created log files.
330
/bin/chmod 0600 /var/log/*.log
331

    
332
echo -n "."
333
DEVFS=`/sbin/mount | /usr/bin/grep devfs | /usr/bin/wc -l | /usr/bin/cut -d" " -f8`
334
if [ "$DEVFS" = "0" ]; then
335
	mount_devfs devfs /dev
336
fi
337

    
338
# Create an initial utmp file
339
cd $varrunpath && /bin/cp /dev/null utmp && /bin/chmod 644 utmp
340

    
341
echo -n "."
342
/sbin/ldconfig -elf /usr/lib /usr/local/lib /lib
343
/etc/rc.d/ldconfig start 2>/dev/null
344

    
345
# Launching kbdmux(4)
346
if [ -f "/dev/kbdmux0" ]; then
347
	echo -n "."
348
	/usr/sbin/kbdcontrol -k /dev/kbdmux0 < /dev/console
349
	[ -c "/dev/atkbd0" ] && kbdcontrol -a atkbd0 < /dev/console
350
	[ -c "/dev/ukbd0" ] && kbdcontrol -a ukbd0 < /dev/console
351
fi
352

    
353
# Fire up unionfs if mount points exist.
354
if [ -f /dist/uniondirs ]; then
355
	echo -n "."
356
	/etc/rc.d/unionfs start
357
fi
358

    
359
echo "done."
360

    
361
# Recreate capabilities DB
362
/usr/bin/cap_mkdb /etc/login.conf
363

    
364
if [ -f /cf/conf/needs_package_sync ]; then
365
	skip_packages=1
366
fi
367

    
368
# Second upgrade stage
369
[ -z "$skip_packages" ] \
370
	&& /usr/local/sbin/${product}-upgrade -y -U -b 2
371

    
372
# Copy default openssl config file
373
[ -d /etc/ssl ] \
374
	|| mkdir -p /etc/ssl
375
[ -f /usr/local/share/${product}/ssl/openssl.cnf ] \
376
	&& cp -f /usr/local/share/${product}/ssl/openssl.cnf /etc/ssl
377
mkdir -p /usr/local/openssl >/dev/null 2>&1
378
ln -sf /etc/ssl/openssl.cnf \
379
	/usr/local/openssl/openssl.cnf
380

    
381
# Run the php.ini setup file and populate
382
# /usr/local/etc/php.ini
383
/etc/rc.php_ini_setup 2>/tmp/php_errors.txt
384
/usr/local/sbin/php-fpm -c /usr/local/etc/php.ini -y /usr/local/lib/php-fpm.conf -RD 2>&1 >/dev/null
385

    
386
# Launch external configuration loader
387
/usr/local/sbin/fcgicli -f /etc/ecl.php
388

    
389
if [ -f /etc/rc.custom_boot_early ]; then
390
	/bin/echo -n "Launching /etc/rc.custom_boot_early...";
391
	/etc/rc.custom_boot_early
392
	echo "Done"
393
fi
394

    
395
export fcgipath=/var/run/php-fpm.socket
396
/usr/bin/nice -n20 /usr/local/sbin/check_reload_status
397

    
398
# let the PHP-based configuration subsystem set up the system now
399
echo -n "Launching the init system..."
400
/bin/rm -f /cf/conf/backup/backup.cache
401
/usr/bin/touch $varrunpath/booting
402

    
403
# Copy custom logo over if it's present
404
if [ -d /usr/local/share/${product}/custom_logos ]; then
405
	cp -f /usr/local/share/${product}/custom_logos/*svg \
406
		/usr/local/www
407
	cp -f /usr/local/share/${product}/custom_logos/*css \
408
		/usr/local/www/css
409
fi
410

    
411
# Apply CPU microcode update
412
[ -x /usr/local/etc/rc.d/microcode_update ] \
413
	&& /usr/local/etc/rc.d/microcode_update onestart
414

    
415
/etc/rc.bootup
416

    
417
# /etc/rc.bootup unset $g['booting'], and removes file
418
# Be sure the file is removed to not create troubles after
419
if [ -f $varrunpath/booting ]; then
420
	/bin/rm $varrunpath/booting
421
fi
422

    
423
echo -n "Starting CRON... "
424
cd /tmp && /usr/sbin/cron -s 2>/dev/null
425
echo "done."
426

    
427
/bin/rm -rf /usr/local/pkg/pf/CVS
428

    
429
# Start ping handler every 240 seconds
430
/usr/local/bin/minicron 240 $varrunpath/ping_hosts.pid /usr/local/bin/ping_hosts.sh
431

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

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

    
438
/bin/chmod a+rw /tmp/.
439

    
440
# Check for GEOM mirrors
441
GMIRROR_STATUS=`/sbin/gmirror status`
442
if [ "${GMIRROR_STATUS}" != "" ]; then
443
	# Using a flag file at bootup saves an expensive exec/check on each page load.
444
	/usr/bin/touch /var/run/gmirror_active
445
	# Setup monitoring/notifications
446
	/usr/local/bin/minicron 60 /var/run/gmirror_status_check.pid /usr/local/sbin/gmirror_status_check.php
447
fi
448

    
449
[ -z "$skip_packages" ] \
450
	&& /usr/local/sbin/${product}-upgrade -y -U -b 3
451

    
452
# Start packages
453
[ -z "$skip_packages" ] \
454
	&& /usr/local/sbin/fcgicli -f /etc/rc.start_packages
455

    
456
# Update pkg metadata
457
/etc/rc.update_pkg_metadata now
458

    
459
# Log product version to syslog
460
get_version
461
BUILDTIME=`cat /etc/version.buildtime`
462
ARCH=`uname -m`
463
echo "$product ${version}${version_patch} $ARCH $BUILDTIME"
464

    
465
echo "Bootup complete"
466

    
467
/usr/local/bin/beep.sh start 2>&1 >/dev/null
468

    
469
# Reset the cache.  read-only requires this.
470
/bin/rm -f /tmp/config.cache
471

    
472
exit 0
(13-13/80)