rc 6.81 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2
#!/bin/sh

3 4 5
# Copyright (c) 2014-2015 Franco Fichtner <franco@opnsense.org>
# Copyright (c) 2004-2010 Scott Ullrich <sullrich@gmail.com>
# Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>
Ad Schellevis's avatar
Ad Schellevis committed
6 7
# All rights reserved.

8
stty status '^T' 2> /dev/null
Ad Schellevis's avatar
Ad Schellevis committed
9

10 11 12 13 14
# Set shell to ignore SIGINT (2), but not children;
# shell catches SIGQUIT (3) and returns to single user.
#
trap : 2
trap "echo 'Boot interrupted'; exit 1" 3
Ad Schellevis's avatar
Ad Schellevis committed
15 16 17 18 19 20 21

HOME=/
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
export HOME PATH

echo "Mounting filesystems..."

22 23 24
# tunefs may refuse otherwise
mount -fr /

25 26 27 28 29 30 31
while read FS_PART FS_MNT FS_TYPE FS_MORE; do
	# only tune our own file systems
	if [ "${FS_TYPE}" != "ufs" ]; then
		continue;
	fi

	# enables soft updates
32
	tunefs -n enable ${FS_MNT}
33 34 35 36 37

	# enables TRIM
	FS_DEV=$(echo ${FS_PART} | awk 'match($0, /\/dev\/([a-z]+[0-9]+)/) { print substr( $0, RSTART, RLENGTH )}')
	FS_TRIM=$(camcontrol identify ${FS_DEV} | grep TRIM | awk '{ print $5; }')
	if [ "${FS_TRIM}" = "yes" ]; then
38
		tunefs -t enable ${FS_MNT}
39 40 41
	fi
done < /etc/fstab

42 43 44 45 46
attempts=0
while [ ${attempts} -lt 3 ]; do
	if mount -a 2>/dev/null; then
		# bail if all is well
		break
Ad Schellevis's avatar
Ad Schellevis committed
47
	fi
48 49 50 51 52 53 54 55 56
	fsck -y /
	attempts=$((attempts+1))
done

# see if / is writable (aka. non-LiveCD boot)
if _tmpdir=$(mktemp -d -q /.diskless.XXXXXX); then
	# only remove the directory
	rmdir ${_tmpdir}
else
57
	# fake a writeable environment in some subdirs
58
	for i in conf etc home root usr var; do
59 60
		mkdir -p /tmp/.cdrom/${i}
		mount_unionfs /tmp/.cdrom/${i} /${i}
61
	done
62
fi
Ad Schellevis's avatar
Ad Schellevis committed
63

64 65 66 67 68 69 70 71
# mount repo if available
if [ -d /root/core ]; then
	make -C /root/core mount
fi

# regenerate groups and users
/usr/local/etc/rc.recover > /dev/null

72 73 74 75 76 77
# rewrite message of the day
/etc/rc.d/motd onestart

# set keyboard map if needed
/etc/rc.d/syscons onestart

78 79
# set up and recover a crash dump before activating swap
/usr/local/etc/rc.crashdump
80 81
swapon -a

82
# set up config directory structure
83 84
mkdir -p /conf/backup
mkdir -p /conf/sshd
85

86 87 88
# Bootstrap config.xml if necessary
if [ ! -f /conf/config.xml ]; then
	echo -n "Bootstrapping config.xml..."
89
	cp /usr/local/etc/config.xml /conf/config.xml
90 91 92 93 94 95
	echo "done."
fi

# Bootstrap openssl.cnf for port if necessary
if [ ! -f /usr/local/openssl/openssl.cnf ]; then
	echo -n "Bootstrapping openssl.cnf..."
96
	cp /etc/ssl/openssl.cnf /usr/local/openssl/openssl.cnf
97
	echo "done."
98 99
fi

100 101 102 103 104 105 106 107 108 109 110 111 112 113
# Disable APM on ATA drives.  Leaving this on will kill
# drives long-term, especially laptop drives, by generating
# excessive load cycles.
ATAIDLE=/usr/local/sbin/ataidle
for i in /dev/ad?; do
	if [ ! -e ${i} ]; then
		continue;
	fi
	SUPPORTED=`${ATAIDLE} ${i} | grep "APM Supported" | awk '{print $3;}'`
	if [ "${SUPPORTED}" = "yes" ] ; then
		echo Disabling APM on $i
		${ATAIDLE} -P 0 ${i}
	fi
done
Ad Schellevis's avatar
Ad Schellevis committed
114 115 116 117 118 119 120 121

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

122 123
# Enable console output if its muted.
/sbin/conscontrol mute off >/dev/null
Ad Schellevis's avatar
Ad Schellevis committed
124

125
USE_MFS_TMPVAR=`/usr/bin/grep -c use_mfs_tmpvar /conf/config.xml`
126
if [ ${USE_MFS_TMPVAR} -ne 0 ]; then
127 128 129 130 131
	USE_MFS_TMP_SIZE=`/usr/bin/grep use_mfs_tmp_size /conf/config.xml | /usr/bin/cut -f2 -d'>' | /usr/bin/cut -f1 -d'<'`
	if [ ! -z ${USE_MFS_TMP_SIZE} ] && [ ${USE_MFS_TMP_SIZE} -gt 0 ]; then
		tmpsize="${USE_MFS_TMP_SIZE}m"
	else
		tmpsize="40m"
132 133
	fi

134 135 136 137 138 139
	USE_MFS_VAR_SIZE=`/usr/bin/grep use_mfs_var_size /conf/config.xml | /usr/bin/cut -f2 -d'>' | /usr/bin/cut -f1 -d'<'`
	if [ ! -z ${USE_MFS_VAR_SIZE} ] && [ ${USE_MFS_VAR_SIZE} -gt 0 ]; then
		varsize="${USE_MFS_VAR_SIZE}m"
	else
		varsize="60m"
	fi
140

141
	echo -n "Setting up memory disks..."
142

143
	if [ ! -d /root/var/db/pkg ]; then
144
		mkdir -p /root/var/db
145
		mv /var/db/pkg /root/var/db
146 147 148
		# create a symlink underneath as well
		# to fix early boot pkg(8) issues:
		ln -s /root/var/db/pkg /var/db/pkg
149
	fi
150

151 152
	mdmfs -S -M -s ${tmpsize} md /tmp
	mdmfs -S -M -s ${varsize} md /var
153

154 155
	mkdir -p /var/db
	ln -s /root/var/db/pkg /var/db/pkg
156

157
	echo "done."
Ad Schellevis's avatar
Ad Schellevis committed
158
elif [ -d /root/var/db/pkg ]; then
159 160
	# User must have just disabled RAM disks,
	# let's move these back into place.
161
	mkdir -p /var/db
162 163 164
	# reverse the recovery symlink before moving
	# back the original database:
	rm -f /var/db/pkg
165
	mv /root/var/db/pkg /var/db/
Ad Schellevis's avatar
Ad Schellevis committed
166 167 168
fi

# make some directories in /var
169 170
mkdir -p /var/run /var/log /var/etc /var/db/entropy /var/at/jobs \
    /var/empty /var/tmp
Ad Schellevis's avatar
Ad Schellevis committed
171

172 173 174
# write /var/run/dmesg.boot
/etc/rc.d/dmesg onestart

175 176
rm -rf /tmp/*
chmod 1777 /tmp
Ad Schellevis's avatar
Ad Schellevis committed
177 178

echo -n "."
179
DISABLESYSLOGCLOG=`/usr/bin/grep -c disablesyslogclog /conf/config.xml`
180
LOG_FILES="system filter dhcpd vpn pptps poes l2tps openvpn portalauth ipsec ppps relayd wireless lighttpd ntpd gateways resolver routing"
Ad Schellevis's avatar
Ad Schellevis committed
181

182
DEFAULT_LOG_FILE_SIZE=`/usr/local/bin/xmllint --xpath 'string(//opnsense/syslog/logfilesize)' /conf/config.xml`
Ad Schellevis's avatar
Ad Schellevis committed
183 184 185 186 187
if [ ! ${DEFAULT_LOG_FILE_SIZE} ]; then
	DEFAULT_LOG_FILE_SIZE=511488
fi

for logfile in $LOG_FILES; do
188
	if [ "$DISABLESYSLOGCLOG" -gt "0" ]; then
189
		touch /var/log/$logfile.log
190
	else
Ad Schellevis's avatar
Ad Schellevis committed
191
		if [ ! -f /var/log/$logfile.log ]; then
192
			clog -i -s ${DEFAULT_LOG_FILE_SIZE} /var/log/$logfile.log
Ad Schellevis's avatar
Ad Schellevis committed
193
		fi
194
	fi
195
	chmod 0600 /var/log/$logfile.log
Ad Schellevis's avatar
Ad Schellevis committed
196 197 198 199 200 201
done

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

202 203 204 205 206 207 208
# Launching kbdmux(4)
if [ -f "/dev/kbdmux0" ]; then
	echo -n "."
	/usr/sbin/kbdcontrol -k /dev/kbdmux0 < /dev/console
	[ -c "/dev/atkbd0" ] && kbdcontrol -a atkbd0 < /dev/console
	[ -c "/dev/ukbd0" ] && kbdcontrol -a ukbd0 < /dev/console
fi
Ad Schellevis's avatar
Ad Schellevis committed
209 210 211 212 213 214

echo "done."

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

215
# Set up the correct php.ini content
216
/usr/local/etc/rc.php_ini_setup
Ad Schellevis's avatar
Ad Schellevis committed
217

218 219
# startup configd
/usr/local/etc/rc.d/configd start
Ad Schellevis's avatar
Ad Schellevis committed
220 221 222

# let the PHP-based configuration subsystem set up the system now
echo -n "Launching the init system..."
223 224
rm -f /root/lighttpd*
touch /var/run/booting
225
/usr/local/etc/rc.bootup
226
rm /var/run/booting
Ad Schellevis's avatar
Ad Schellevis committed
227

228
# If a shell was selected from recovery
Ad Schellevis's avatar
Ad Schellevis committed
229 230 231 232 233 234 235 236 237 238
# console then just drop to the shell now.
if [ -f "/tmp/donotbootup" ]; then
	echo "Dropping to recovery shell."
	exit 0
fi

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

239 240 241
# starting standard rc scripts
/usr/local/etc/rc.opnsense start

Ad Schellevis's avatar
Ad Schellevis committed
242
# Start ping handler every 240 seconds
243
minicron 240 /var/run/ping_hosts.pid /usr/local/sbin/ping_hosts.sh
Ad Schellevis's avatar
Ad Schellevis committed
244 245

# Start account expire handler every hour
246
minicron 3600 /var/run/expire_accounts.pid /usr/local/etc/rc.expireaccounts
Ad Schellevis's avatar
Ad Schellevis committed
247 248

# Start alias url updater every 24 hours
249
minicron 86400 /var/run/update_alias_url_data.pid /usr/local/etc/rc.update_alias_url_data
Ad Schellevis's avatar
Ad Schellevis committed
250 251 252 253 254

# Check for GEOM mirrors
GMIRROR_STATUS=`/sbin/gmirror status`
if [ "${GMIRROR_STATUS}" != "" ]; then
	# Using a flag file at bootup saves an expensive exec/check on each page load.
255
	touch /var/run/gmirror_active
Ad Schellevis's avatar
Ad Schellevis committed
256
	# Setup monitoring/notifications
257
	minicron 60 /var/run/gmirror_status_check.pid /usr/local/sbin/gmirror_status_check.php
Ad Schellevis's avatar
Ad Schellevis committed
258 259
fi

260
/usr/local/sbin/beep.sh start
Ad Schellevis's avatar
Ad Schellevis committed
261

262 263
/usr/local/etc/rc.initial.banner

Ad Schellevis's avatar
Ad Schellevis committed
264
exit 0