#!/bin/sh
#
# Copyright C 2002-2013 by Isilon Systems LLC. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# LLC.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems, LLC. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# LLC.  Legion is a trademark of Isilon Systems, LLC.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

# PROVIDE: isi_bootdisk_init fsck
# REQUIRE: isi_module_init
# BEFORE: FILESYSTEMS initrandom root

. /etc/rc.subr

name="isi_bootdisk_init"
start_cmd="isi_bootdisk_init_start"
BASE_UZIP_MNT=/base
BASE_UZIP=/base.uzip

isi_bootdisk_init_start()
{
	isi_export_ld_library_path
	bootdisk_type=`/usr/sbin/isi_bootdisk_type 2> /dev/null`

	case "${bootdisk_type}" in
	flash)
		# Leave isi_bootdisk_init, isi_flashdisk_init starts after
		# NETWORKING
		exit 0
		;;
	gmirror)
		echo "Executing GEOM bootdisk startup..."
		/usr/libexec/isilon/isi_gmirror_start 2>/dev/null || \
		    /usr/sbin/isi_stop_boot "GEOM start failed" || \
		    return
		/bin/rm -rf /var/log/isi_bootdisk_info.log
		/bin/rm -rf /tmp/doing_flash_reimage
		;;
	*)
		/usr/sbin/isi_stop_boot "Bootdisk type not recognized: ${bootdisk_type}" || \
		    return
	esac

	# Clean out /var/run which should only contain things created since boot.
	if [ -d /var/run ]; then
		/bin/rm -rf /var/run/*
		force_depend ldconfig || \
		    /usr/sbin/isi_stop_boot "Failed to generate runtime linker (ldconfig) cache" || \
		    return
	fi

	# Immediately after mounting, make sure we weren't resetting.
	if [ -f /etc/isi_resetting ]; then
		echo "Previous node reformat failed, trying again..."
		isi_reformat_node -f
		sleep 60
	fi

	# Mount the read-only uzip fs.
	if [ -f ${BASE_UZIP} ] && ! [ -d ${BASE_UZIP_MNT}/usr ]; then
		load_kld -m g_uzip geom_uzip || \
		    /usr/sbin/isi_stop_boot "Failed to load geom_uzip" || \
		    return
		mkdir -p ${BASE_UZIP_MNT}
		mdmfs -P -F ${BASE_UZIP} -o ro md.uzip ${BASE_UZIP_MNT} || \
			/usr/sbin/isi_stop_boot "Failed to mount base uzip" || \
			return
	fi

	# Specific to Gen5/6 platforms. Relies on BMC sensor monitoring
	# (S1400FP, S2600CP, 110-365 (Infinity DE), 110-385 (Infinity EP))
	/usr/local/sbin/dmidecode -t baseboard | \
	    grep -iE "S1400FP|S2600CP|110-365|110-385" > /dev/null
	if [ $? -eq 0 ]; then
		# Mount a ramdisk for the IPMI command cache
		if [ ! -d "/tmp/ipmi" ]; then
			mkdir /tmp/ipmi
		fi
		/sbin/mount | grep "/tmp/ipmi" > /dev/null
		if [ ! $? -eq 0 ]; then
			/sbin/mdmfs -M -S -s4M -o noatime md /tmp/ipmi
			if [ $? -eq 0 ]; then
				echo "Mounted ramdisk for IPMI command cache"
			else
				echo "Failed to mount ramdisk for IPMI command cache"
				rm -rf /tmp/ipmi
			fi
		else
			echo "Ramdisk for IPMI command cache already mounted"
		fi
	fi

	# Specific to Gen5 platforms (with S1400FP and S2600CP baseboards)
	/usr/local/sbin/dmidecode -t baseboard | grep -e S1400FP -e S2600CP > /dev/null
	if [ $? -eq 0 ]; then
		# Enable IPMB bridge message throttle to prevent overloading the BMC
		/sbin/sysctl hw.ipmi.throttle_time=40
	fi
}

load_rc_config $name
run_rc_command "$1"
