rc 6.21 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
	echo "done."
fi

93 94 95 96 97 98 99 100 101 102 103 104 105 106
# 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
107 108 109 110 111 112 113 114

#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

115 116
# Enable console output if its muted.
/sbin/conscontrol mute off >/dev/null
Ad Schellevis's avatar
Ad Schellevis committed
117

118
USE_MFS_TMPVAR=`/usr/bin/grep -c use_mfs_tmpvar /conf/config.xml`
119
if [ ${USE_MFS_TMPVAR} -ne 0 ]; then
120
	echo -n "Setting up memory disks..."
121

122
	if [ ! -d /root/var/db/pkg ]; then
123
		mkdir -p /root/var/db
124
		mv /var/db/pkg /root/var/db
125 126 127
		# create a symlink underneath as well
		# to fix early boot pkg(8) issues:
		ln -s /root/var/db/pkg /var/db/pkg
128
	fi
129

130 131
	mount -t tmpfs -o mode=01777 tmpfs /tmp
	mount -t tmpfs tmpfs /var
132

133 134
	mkdir -p /var/db
	ln -s /root/var/db/pkg /var/db/pkg
135

136
	echo "done."
Ad Schellevis's avatar
Ad Schellevis committed
137
elif [ -d /root/var/db/pkg ]; then
138 139
	# User must have just disabled RAM disks,
	# let's move these back into place.
140
	mkdir -p /var/db
141 142 143
	# reverse the recovery symlink before moving
	# back the original database:
	rm -f /var/db/pkg
144
	mv /root/var/db/pkg /var/db/
Ad Schellevis's avatar
Ad Schellevis committed
145 146 147
fi

# make some directories in /var
148 149
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
150

151 152 153
# work around the fact that shutdown(8) doesn't clean up this file
rm -f /var/run/nologin

154 155 156
# write /var/run/dmesg.boot
/etc/rc.d/dmesg onestart

157 158
rm -rf /tmp/*
chmod 1777 /tmp
Ad Schellevis's avatar
Ad Schellevis committed
159 160

echo -n "."
161
DISABLESYSLOGCLOG=`/usr/bin/grep -c disablesyslogclog /conf/config.xml`
162
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
163

164
DEFAULT_LOG_FILE_SIZE=`/usr/local/bin/xmllint --xpath 'string(//opnsense/syslog/logfilesize)' /conf/config.xml`
Ad Schellevis's avatar
Ad Schellevis committed
165 166 167 168 169
if [ ! ${DEFAULT_LOG_FILE_SIZE} ]; then
	DEFAULT_LOG_FILE_SIZE=511488
fi

for logfile in $LOG_FILES; do
170
	if [ "$DISABLESYSLOGCLOG" -gt "0" ]; then
171
		touch /var/log/$logfile.log
172
	else
Ad Schellevis's avatar
Ad Schellevis committed
173
		if [ ! -f /var/log/$logfile.log ]; then
174
			clog -i -s ${DEFAULT_LOG_FILE_SIZE} /var/log/$logfile.log
Ad Schellevis's avatar
Ad Schellevis committed
175
		fi
176
	fi
177
	chmod 0600 /var/log/$logfile.log
Ad Schellevis's avatar
Ad Schellevis committed
178 179 180 181 182 183
done

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

184 185 186 187 188 189 190
# 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
191 192 193 194 195 196

echo "done."

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

197
# Set up the correct php.ini content
198
/usr/local/etc/rc.php_ini_setup
Ad Schellevis's avatar
Ad Schellevis committed
199

200 201
# startup configd
/usr/local/etc/rc.d/configd start
Ad Schellevis's avatar
Ad Schellevis committed
202 203 204

# let the PHP-based configuration subsystem set up the system now
echo -n "Launching the init system..."
205 206
rm -f /root/lighttpd*
touch /var/run/booting
207
/usr/local/etc/rc.bootup
208
rm /var/run/booting
Ad Schellevis's avatar
Ad Schellevis committed
209

210
# If a shell was selected from recovery
Ad Schellevis's avatar
Ad Schellevis committed
211 212 213 214 215 216 217 218 219 220
# 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."

221 222 223
# starting standard rc scripts
/usr/local/etc/rc.opnsense start

Ad Schellevis's avatar
Ad Schellevis committed
224
# Start ping handler every 240 seconds
225
minicron 240 /var/run/ping_hosts.pid /usr/local/sbin/ping_hosts.sh
Ad Schellevis's avatar
Ad Schellevis committed
226 227

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

# Start alias url updater every 24 hours
231
minicron 86400 /var/run/update_alias_url_data.pid /usr/local/etc/rc.update_alias_url_data
Ad Schellevis's avatar
Ad Schellevis committed
232 233 234 235 236

# 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.
237
	touch /var/run/gmirror_active
Ad Schellevis's avatar
Ad Schellevis committed
238
	# Setup monitoring/notifications
239
	minicron 60 /var/run/gmirror_status_check.pid /usr/local/sbin/gmirror_status_check.php
Ad Schellevis's avatar
Ad Schellevis committed
240 241
fi

242
/usr/local/sbin/beep.sh start
Ad Schellevis's avatar
Ad Schellevis committed
243

244 245
/usr/local/etc/rc.initial.banner

Ad Schellevis's avatar
Ad Schellevis committed
246
exit 0