1#!/bin/sh
2#-
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4#
5# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28# $FreeBSD$
29
30# Functions which runs commands on the system
31
32. ${BACKEND}/functions.sh
33. ${BACKEND}/functions-parse.sh
34
35run_chroot_cmd()
36{
37  CMD="$@"
38  echo_log "Running chroot command: ${CMD}"
39  echo "$CMD" >${FSMNT}/.runcmd.sh
40  chmod 755 ${FSMNT}/.runcmd.sh
41  chroot ${FSMNT} sh /.runcmd.sh
42  RES=$?
43
44  rm ${FSMNT}/.runcmd.sh
45  return ${RES}
46};
47
48run_chroot_script()
49{
50  SCRIPT="$@"
51  SBASE=`basename $SCRIPT`
52
53  cp ${SCRIPT} ${FSMNT}/.$SBASE
54  chmod 755 ${FSMNT}/.${SBASE}
55
56  echo_log "Running chroot script: ${SCRIPT}"
57  chroot ${FSMNT} /.${SBASE}
58  RES=$?
59
60  rm ${FSMNT}/.${SBASE}
61  return ${RES}
62};
63
64
65run_ext_cmd()
66{
67  CMD="$@"
68  # Make sure to export FSMNT, in case cmd needs it
69  export FSMNT
70  echo_log "Running external command: ${CMD}"
71  echo "${CMD}"> ${TMPDIR}/.runcmd.sh
72  chmod 755 ${TMPDIR}/.runcmd.sh
73  sh ${TMPDIR}/.runcmd.sh
74  RES=$?
75
76  rm ${TMPDIR}/.runcmd.sh
77  return ${RES}
78};
79
80
81# Starts the user setup
82run_commands()
83{
84  while read line
85  do
86    # Check if we need to run any chroot command
87    echo $line | grep -q ^runCommand=  2>/dev/null
88    if [ $? -eq 0 ]
89    then
90      get_value_from_string "$line"
91      run_chroot_cmd "$VAL"
92    fi
93
94    # Check if we need to run any chroot script
95    echo $line | grep -q ^runScript= 2>/dev/null
96    if [ $? -eq 0 ]
97    then
98      get_value_from_string "$line"
99      run_chroot_script "$VAL"
100    fi
101
102    # Check if we need to run any chroot command
103    echo $line | grep -q ^runExtCommand= 2>/dev/null
104    if [ $? -eq 0 ]
105    then
106      get_value_from_string "$line"
107      run_ext_cmd "$VAL"
108    fi
109
110  done <${CFGF}
111
112};
113