rc 7.89 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 25 26
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
27
	fi
28 29 30 31 32 33 34 35
	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}
36 37 38 39 40 41

	# cleanup configuration files from previous instance
	# (does not work with install media unfortunately)
	/bin/rm -rf /var/run/*
	/bin/rm -rf /var/etc/*
	/bin/rm -rf /var/tmp/*
42
else
43
	# fake a writeable environment in some subdirs
44
	for i in conf etc home root usr var; do
45 46
		mkdir -p /tmp/.cdrom/${i}
		mount_unionfs /tmp/.cdrom/${i} /${i}
47
	done
48
fi
Ad Schellevis's avatar
Ad Schellevis committed
49

50 51 52 53 54
# !!! migration code for OPNsense <= 15.1.7, do not remove !!!
if [ -d "/cf/conf" ]; then
	/bin/rm -f /conf
	/bin/mv /cf/conf /conf
	/bin/rm -rf /cf
Ad Schellevis's avatar
Ad Schellevis committed
55
fi
56
# !!! migration code for OPNsense <= 15.1.7, do not remove !!!
Ad Schellevis's avatar
Ad Schellevis committed
57

58
# mount repo if available
59 60 61 62
if [ -d /root/core ]; then
	/usr/bin/make -C /root/core mount
fi

63
# rewrite message of the day
64
/etc/rc.d/motd onestart
65

66 67 68 69 70 71
# set keyboard map if needed
/etc/rc.d/syscons onestart

# probe for a persistent core dump device
/usr/local/etc/rc.dumpon

72 73 74 75
# set up config directory structure
/bin/mkdir -p /conf/backup
/bin/mkdir -p /conf/sshd

76 77 78 79
# Bootstrap config.xml if necessary
if [ ! -f /conf/config.xml ]; then
	echo -n "Bootstrapping config.xml..."
	/bin/cp /usr/local/etc/config.xml /conf/config.xml
80 81 82 83 84 85 86 87
	echo "done."
fi

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

Ad Schellevis's avatar
Ad Schellevis committed
90
# Disable APM on ATA drives. Leaving this on will kill drives long-term, especially laptop drives, by generating excessive Load Cycles.
91 92
if [ -f /usr/local/etc/rc.disable_hdd_apm ]; then
	/usr/local/etc/rc.disable_hdd_apm
Ad Schellevis's avatar
Ad Schellevis committed
93 94 95 96 97 98 99 100 101 102 103 104 105
fi

#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

# sync pw database after mount.
rm -f /etc/spwd.db.tmp
/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd

106 107
# Enable console output if its muted.
/sbin/conscontrol mute off >/dev/null
Ad Schellevis's avatar
Ad Schellevis committed
108

109 110 111 112 113 114 115 116 117
USE_MFS_TMPVAR=`/usr/bin/grep -c use_mfs_tmpvar /conf/config.xml`
if [ ${USE_MFS_TMPVAR} -eq 0 ]; then
	mdmfs -S -M -s 4m md /var/run
else
	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"
118 119
	fi

120 121 122 123 124 125
	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
126

127
	echo -n "Setting up memory disks..."
128

129 130 131 132
	if [ ! -d /root/var/db/pkg ]; then
		/bin/mkdir -p /root/var/db
		/bin/mv /var/db/pkg /root/var/db/
	fi
133

134 135
	mdmfs -S -M -s ${tmpsize} md /tmp
	mdmfs -S -M -s ${varsize} md /var
136

137 138
	/bin/mkdir -p /var/db
	/bin/ln -s /root/var/db/pkg /var/db/pkg
139

140 141
	echo "done."
fi
142

143 144
/sbin/swapon -a
/usr/local/etc/rc.savecore
Ad Schellevis's avatar
Ad Schellevis committed
145

146 147 148 149 150
if [ -d /root/var/db/pkg ]; then
	# User must have just disabled RAM disks,
	# let's move these back into place.
	/bin/mkdir -p /var/db
	/bin/mv /root/var/db/pkg /var/db/
Ad Schellevis's avatar
Ad Schellevis committed
151 152 153
fi

# make some directories in /var
154
/bin/mkdir -p /var/run /var/log /var/etc /var/db/entropy /var/at/jobs/ /var/empty 2>/dev/null
Ad Schellevis's avatar
Ad Schellevis committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

echo -n "Creating symlinks..."

# Repair symlinks if they are broken
if [ -f /etc/newsyslog.conf ]; then
	/bin/rm -f /etc/newsyslog.conf
fi
if [ ! -L /etc/syslog.conf ]; then
	/bin/rm -rf /etc/syslog.conf
	if [ ! -f /var/etc/syslog.conf ]; then
		touch /var/etc/syslog.conf
	fi
	/bin/ln -s /var/etc/syslog.conf /etc/syslog.conf
fi

170 171
# Remove symlinks that are no longer needed
if [ -L /etc/resolv.conf ]; then rm /etc/resolv.conf; fi
172
if [ -L /etc/hosts ]; then /bin/rm /etc/hosts; fi
Ad Schellevis's avatar
Ad Schellevis committed
173 174 175 176 177

# Setup compatibility link for packages that
# have trouble overriding the PREFIX configure
# argument since we build our packages in a
# separated PREFIX area
178
# Only create if symlink does not exist.
Ad Schellevis's avatar
Ad Schellevis committed
179 180 181 182
if [ ! -h /tmp/tmp ]; then
    /bin/ln -hfs / /tmp/tmp
fi

183
/bin/rm -rf /tmp/*
Ad Schellevis's avatar
Ad Schellevis committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
/bin/chmod 1777 /tmp

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

if [ ! -d /var/tmp ]; then
	/bin/mkdir -p /var/tmp
fi

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

# Remove old nameserver resolution files
/bin/rm -f /var/etc/nameserver*

echo -n "."
201 202
DISABLESYSLOGCLOG=`/usr/bin/grep -c disablesyslogclog /conf/config.xml`
ENABLEFIFOLOG=`/usr/bin/grep -c usefifolog /conf/config.xml`
Ad Schellevis's avatar
Ad Schellevis committed
203 204 205 206 207 208 209 210
LOG_FILES="system filter dhcpd vpn pptps poes l2tps openvpn portalauth ipsec ppp relayd wireless lighttpd ntpd gateways resolver routing"

DEFAULT_LOG_FILE_SIZE=`/usr/local/bin/xmllint --xpath 'string(//pfsense/syslog/logfilesize)' /conf/config.xml`
if [ ! ${DEFAULT_LOG_FILE_SIZE} ]; then
	DEFAULT_LOG_FILE_SIZE=511488
fi

for logfile in $LOG_FILES; do
211
	if [ "$DISABLESYSLOGCLOG" -gt "0" ]; then
Ad Schellevis's avatar
Ad Schellevis committed
212
		/usr/bin/touch /var/log/$logfile.log
213
	else
Ad Schellevis's avatar
Ad Schellevis committed
214 215 216 217
		if [ ! -f /var/log/$logfile.log ]; then
			if [ "$ENABLEFIFOLOG" -gt "0" ]; then
				# generate fifolog files
				/usr/sbin/fifolog_create -s ${DEFAULT_LOG_FILE_SIZE} /var/log/$logfile.log
218
			else
Ad Schellevis's avatar
Ad Schellevis committed
219 220 221
				/usr/local/sbin/clog -i -s ${DEFAULT_LOG_FILE_SIZE} /var/log/$logfile.log
			fi
		fi
222
	fi
Ad Schellevis's avatar
Ad Schellevis committed
223 224 225 226 227 228 229 230 231
done

# change permissions on newly created fifolog files.
/bin/chmod 0600 /var/log/*.log

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

232 233 234 235 236 237 238
# 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
239 240 241 242 243 244

echo "done."

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

245
# Set up the correct php.ini content
246
/usr/local/etc/rc.php_ini_setup
Ad Schellevis's avatar
Ad Schellevis committed
247

248 249
# startup configd
/usr/local/etc/rc.d/configd start
Ad Schellevis's avatar
Ad Schellevis committed
250 251 252

# let the PHP-based configuration subsystem set up the system now
echo -n "Launching the init system..."
253
/bin/rm -f /conf/backup/backup.cache
Ad Schellevis's avatar
Ad Schellevis committed
254
/bin/rm -f /root/lighttpd*
255
/usr/bin/touch /var/run/booting
256
/usr/local/etc/rc.bootup
Ad Schellevis's avatar
Ad Schellevis committed
257

Ad Schellevis's avatar
Ad Schellevis committed
258
# end of boot process, remove status file
259
/bin/rm /var/run/booting
Ad Schellevis's avatar
Ad Schellevis committed
260

261
# If a shell was selected from recovery
Ad Schellevis's avatar
Ad Schellevis committed
262 263 264 265 266 267 268 269 270 271
# 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."

272 273 274
# starting standard rc scripts
/usr/local/etc/rc.opnsense start

Ad Schellevis's avatar
Ad Schellevis committed
275
# Start ping handler every 240 seconds
276
/usr/local/bin/minicron 240 /var/run/ping_hosts.pid /usr/local/bin/ping_hosts.sh
Ad Schellevis's avatar
Ad Schellevis committed
277 278

# Start account expire handler every hour
279
/usr/local/bin/minicron 3600 /var/run/expire_accounts.pid /usr/local/etc/rc.expireaccounts
Ad Schellevis's avatar
Ad Schellevis committed
280 281

# Start alias url updater every 24 hours
282
/usr/local/bin/minicron 86400 /var/run/update_alias_url_data.pid /usr/local/etc/rc.update_alias_url_data
Ad Schellevis's avatar
Ad Schellevis committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296

/bin/chmod a+rw /tmp/.

# 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.
	/usr/bin/touch /var/run/gmirror_active
	# Setup monitoring/notifications
	/usr/local/bin/minicron 60 /var/run/gmirror_status_check.pid /usr/local/sbin/gmirror_status_check.php
fi

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

297 298
/usr/local/etc/rc.initial.banner

Ad Schellevis's avatar
Ad Schellevis committed
299
exit 0