1209513Simp#!/bin/sh
2209513Simp#-
3209552Simp# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4209513Simp#
5209513Simp# Redistribution and use in source and binary forms, with or without
6209513Simp# modification, are permitted provided that the following conditions
7209513Simp# are met:
8209513Simp# 1. Redistributions of source code must retain the above copyright
9209513Simp#    notice, this list of conditions and the following disclaimer.
10209513Simp# 2. Redistributions in binary form must reproduce the above copyright
11209513Simp#    notice, this list of conditions and the following disclaimer in the
12209513Simp#    documentation and/or other materials provided with the distribution.
13209513Simp#
14209513Simp# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15209513Simp# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16209513Simp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17209513Simp# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18209513Simp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19209513Simp# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20209513Simp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21209513Simp# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22209513Simp# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23209513Simp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24209513Simp# SUCH DAMAGE.
25209513Simp#
26209513Simp# $FreeBSD$
27209513Simp
28209513Simp# Functions which runs commands on the system
29209513Simp
30209513Simp. ${BACKEND}/functions.sh
31209513Simp. ${BACKEND}/functions-parse.sh
32209513Simp
33209513Simprun_chroot_cmd()
34209513Simp{
35209513Simp  CMD="$@"
36209513Simp  echo_log "Running chroot command: ${CMD}"
37209513Simp  echo "$CMD" >${FSMNT}/.runcmd.sh
38209513Simp  chmod 755 ${FSMNT}/.runcmd.sh
39209513Simp  chroot ${FSMNT} sh /.runcmd.sh
40211485Simp  RES=$?
41211485Simp
42209513Simp  rm ${FSMNT}/.runcmd.sh
43211485Simp  return ${RES}
44209513Simp};
45209513Simp
46209513Simprun_chroot_script()
47209513Simp{
48209513Simp  SCRIPT="$@"
49209513Simp  SBASE=`basename $SCRIPT`
50209513Simp
51209513Simp  cp ${SCRIPT} ${FSMNT}/.$SBASE
52209513Simp  chmod 755 ${FSMNT}/.${SBASE}
53209513Simp
54209513Simp  echo_log "Running chroot script: ${SCRIPT}"
55209513Simp  chroot ${FSMNT} /.${SBASE}
56211485Simp  RES=$?
57209513Simp
58209513Simp  rm ${FSMNT}/.${SBASE}
59211485Simp  return ${RES}
60209513Simp};
61209513Simp
62209513Simp
63209513Simprun_ext_cmd()
64209513Simp{
65209513Simp  CMD="$@"
66209513Simp  # Make sure to export FSMNT, in case cmd needs it
67209513Simp  export FSMNT
68209513Simp  echo_log "Running external command: ${CMD}"
69209513Simp  echo "${CMD}"> ${TMPDIR}/.runcmd.sh
70209513Simp  chmod 755 ${TMPDIR}/.runcmd.sh
71209513Simp  sh ${TMPDIR}/.runcmd.sh
72211485Simp  RES=$?
73211485Simp
74209513Simp  rm ${TMPDIR}/.runcmd.sh
75211485Simp  return ${RES}
76209513Simp};
77209513Simp
78209513Simp
79209513Simp# Starts the user setup
80209513Simprun_commands()
81209513Simp{
82209513Simp  while read line
83209513Simp  do
84209513Simp    # Check if we need to run any chroot command
85220059Sjpaetzel    echo $line | grep -q ^runCommand=  2>/dev/null
86220059Sjpaetzel    if [ $? -eq 0 ]
87209513Simp    then
88209513Simp      get_value_from_string "$line"
89209513Simp      run_chroot_cmd "$VAL"
90209513Simp    fi
91209513Simp
92209513Simp    # Check if we need to run any chroot script
93220059Sjpaetzel    echo $line | grep -q ^runScript= 2>/dev/null
94220059Sjpaetzel    if [ $? -eq 0 ]
95209513Simp    then
96209513Simp      get_value_from_string "$line"
97209513Simp      run_chroot_script "$VAL"
98209513Simp    fi
99209513Simp
100209513Simp    # Check if we need to run any chroot command
101220059Sjpaetzel    echo $line | grep -q ^runExtCommand= 2>/dev/null
102220059Sjpaetzel    if [ $? -eq 0 ]
103209513Simp    then
104209513Simp      get_value_from_string "$line"
105209513Simp      run_ext_cmd "$VAL"
106209513Simp    fi
107209513Simp
108209513Simp  done <${CFGF}
109209513Simp
110209513Simp};
111