#!/bin/sh
#  Copyright (C) 2015, Parallels, Inc. All rights reserved.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# This script is run in host context right before container start.
# Currently it loads kernel modules that might be needed for a CT.
#
# Parameters are passed in environment variables.
# Required parameters:
#   VEID	- container ID

. /etc/vz/vz.conf
. /usr/libexec/vzctl/scripts/vps-functions

vzcheckvar VEID

load_modules() {
	local mod msg modules

	eval $(. /etc/vz/conf/${VEID}.conf && \
		echo DEVNODES=\"$DEVNODES\" FEATURES=\"$FEATURES\")

	if echo $DEVNODES | grep -Fq 'net/tun:rw'; then
		modules="$modules tun"
	fi

	if echo $FEATURES | grep -Fwq 'ppp:on'; then
		modules="$modules ppp_generic"
	fi

	if echo $FEATURES | grep -Fwq 'bridge:on'; then
		modules="$modules veth bridge"
	fi

	for mod in nfs nfsd; do
		if echo $FEATURES | grep -Fwq "${mod}:on"; then
			modules="$modules $mod"
		fi
	done

	for mod in ${modules}; do
		lsmod | grep -qw $mod && continue
		echo "Preloading kernel module: $mod"
		msg=$(modprobe $mod >/dev/null 2>&1) || \
			echo "Warning: can't load $mod: $msg" 1>&2
	done
}

load_modules

exit 0
