rc 5.72 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
# Bootstrap openssl.cnf for port if necessary
if [ ! -f /usr/local/openssl/openssl.cnf ]; then
	echo -n "Bootstrapping openssl.cnf..."
	cp /etc/ssl/openssl.cnf /usr/local/openssl/openssl.cnf
	echo "done."
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
	echo -n "Setting up memory disks..."
128

129
	if [ ! -d /root/var/db/pkg ]; then
130
		mkdir -p /root/var/db
131
		mv /var/db/pkg /root/var/db
132 133 134
		# create a symlink underneath as well
		# to fix early boot pkg(8) issues:
		ln -s /root/var/db/pkg /var/db/pkg
135
	fi
136

137 138
	mount -t tmpfs -o mode=01777 tmpfs /tmp
	mount -t tmpfs tmpfs /var
139

140 141
	mkdir -p /var/db
	ln -s /root/var/db/pkg /var/db/pkg
142

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

# make some directories in /var
155 156
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
157

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

161 162 163
# write /var/run/dmesg.boot
/etc/rc.d/dmesg onestart

164 165
rm -rf /tmp/*
chmod 1777 /tmp
Ad Schellevis's avatar
Ad Schellevis committed
166 167 168 169 170

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

171 172 173 174 175 176 177
# 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
178 179 180 181 182 183

echo "done."

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

184
# Set up the correct php.ini content
185
/usr/local/etc/rc.php_ini_setup
Ad Schellevis's avatar
Ad Schellevis committed
186

187 188
# startup configd
/usr/local/etc/rc.d/configd start
Ad Schellevis's avatar
Ad Schellevis committed
189

190 191 192
# Execute the early syshook / plugin commands
/usr/local/etc/rc.syshook early

Ad Schellevis's avatar
Ad Schellevis committed
193 194
# let the PHP-based configuration subsystem set up the system now
echo -n "Launching the init system..."
195 196
rm -f /root/lighttpd*
touch /var/run/booting
197
/usr/local/etc/rc.bootup
198
rm /var/run/booting
Ad Schellevis's avatar
Ad Schellevis committed
199

200
# If a shell was selected from recovery
Ad Schellevis's avatar
Ad Schellevis committed
201 202 203 204 205 206
# console then just drop to the shell now.
if [ -f "/tmp/donotbootup" ]; then
	echo "Dropping to recovery shell."
	exit 0
fi

207 208 209
# Execute the normal syshook / plugin commands
/usr/local/etc/rc.syshook start

210 211 212 213
# 16.1 migration code for stale captive portal files to be removed in 16.7
rm -rf /var/db/rrd/*-concurrent.rrd
rm -rf /var/db/rrd/*-loggedin.rrd

Ad Schellevis's avatar
Ad Schellevis committed
214 215 216 217
echo -n "Starting CRON... "
cd /tmp && /usr/sbin/cron -s 2>/dev/null
echo "done."

218 219 220
# starting standard rc scripts
/usr/local/etc/rc.opnsense start

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

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

# Start alias url updater every 24 hours
228
minicron 86400 /var/run/update_alias_url_data.pid /usr/local/etc/rc.update_alias_url_data
Ad Schellevis's avatar
Ad Schellevis committed
229

230
/usr/local/sbin/beep.sh start
Ad Schellevis's avatar
Ad Schellevis committed
231

232 233
/usr/local/etc/rc.initial.banner

Ad Schellevis's avatar
Ad Schellevis committed
234
exit 0