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 check and load any optional modules specified in the config
31
32. ${BACKEND}/functions.sh
33. ${BACKEND}/functions-parse.sh
34
35copy_component()
36{
37  COMPONENT="$1"
38  FAILED="0"
39  CFILES=""
40
41  # Check the type, and set the components subdir properly
42  TYPE="`grep 'type:' ${COMPDIR}/${COMPONENT}/component.cfg | cut -d ' ' -f 2`"
43  if [ "${TYPE}" = "PBI" ]
44  then
45    SUBDIR="PBI"
46  else
47    SUBDIR="components"
48  fi
49
50  # Lets start by downloading / copying the files this component needs
51  while read line
52  do
53    CFILE="`echo $line | cut -d ':' -f 1`"
54    CFILEMD5="`echo $line | cut -d ':' -f 2`"
55    CFILE2MD5="`echo $line | cut -d ':' -f 3`"
56
57    case ${INSTALLMEDIUM} in
58      dvd|usb)
59        # On both dvd / usb, we can just copy the file
60        cp ${CDMNT}/${COMPFILEDIR}/${SUBDIR}/${CFILE} \
61		  ${FSMNT}/${COMPTMPDIR} >>${LOGOUT} 2>>${LOGOUT}
62	    RESULT="$?"
63        ;;
64
65      ftp)
66        get_value_from_cfg ftpPath
67        if [ -z "$VAL" ]
68        then
69          exit_err "ERROR: Install medium was set to ftp, but no ftpPath was provided!"
70        fi
71        FTPPATH="${VAL}"
72
73        fetch_file "${FTPPATH}/${COMPFILEDIR}/${SUBDIR}/${CFILE}" "${FSMNT}/${COMPTMPDIR}/${CFILE}" "0"
74        RESULT="$?"
75       ;;
76    local)
77        get_value_from_cfg localPath
78        if [ -z "$VAL" ]; then
79          exit_err "Install medium was set to local, but no localPath was provided!"
80        fi
81        LOCALPATH=$VAL
82        cp ${LOCALPATH}/${COMPFILEDIR}/${SUBDIR}/${CFILE} \
83		  ${FSMNT}/${COMPTMPDIR} >>${LOGOUT} 2>>${LOGOUT}
84	RESULT="$?"
85       ;;
86    esac
87
88    if [ "${RESULT}" != "0" ]
89    then
90      echo_log "WARNING: Failed to copy ${CFILE}"
91      FAILED="1"
92    else
93      # Now lets check the MD5 to confirm the file is valid
94      CHECKMD5=`md5 -q ${FSMNT}/${COMPTMPDIR}/${CFILE}`
95      if [ "${CHECKMD5}" != "${CFILEMD5}" -a "${CHECKMD5}" != "${CFILE2MD5}" ]
96      then
97        echo_log "WARNING: ${CFILE} failed md5 checksum"
98        FAILED="1"
99      else
100        if [ -z "${CFILES}" ]
101        then
102          CFILES="${CFILE}"
103        else
104          CFILES="${CFILES},${CFILE}"
105        fi
106      fi
107    fi
108
109
110  done < ${COMPDIR}/${COMPONENT}/distfiles
111
112  if [ "${FAILED}" = "0" ]
113  then
114    # Now install the component
115    run_component_install ${COMPONENT} ${CFILES}
116  fi
117
118};
119
120run_component_install()
121{
122  COMPONENT="$1"
123  CFILES="$1"
124
125  # Lets install this component now
126  # Start by making a wrapper script which sets the variables
127  # for the component to use
128  echo "#!/bin/sh
129COMPTMPDIR=\"${COMPTMPDIR}\"
130export COMPTMPDIR
131CFILE=\"${CFILE}\"
132export CFILE
133mount -t devfs devfs /dev
134
135sh ${COMPTMPDIR}/install.sh
136
137umount /dev
138" >${FSMNT}/.componentwrapper.sh
139  chmod 755 ${FSMNT}/.componentwrapper.sh
140
141  # Copy over the install script for this component
142  cp ${COMPDIR}/${COMPONENT}/install.sh ${FSMNT}/${COMPTMPDIR}/
143
144  echo_log "INSTALL COMPONENT: ${i}"
145  chroot ${FSMNT} /.componentwrapper.sh >>${LOGOUT} 2>>${LOGOUT}
146  rm ${FSMNT}/.componentwrapper.sh
147
148};
149
150# Check for any modules specified, and begin loading them
151install_components()
152{
153  # First, lets check and see if we even have any optional modules
154  get_value_from_cfg installComponents
155  if [ -n "${VAL}" ]
156  then
157    # Lets start by cleaning up the string and getting it ready to parse
158    strip_white_space ${VAL}
159    COMPONENTS=`echo ${VAL} | sed -e "s|,| |g"`
160    for i in $COMPONENTS
161    do
162      if [ ! -e "${COMPDIR}/${i}/install.sh" -o ! -e "${COMPDIR}/${i}/distfiles" ]
163      then
164        echo_log "WARNING: Component ${i} doesn't seem to exist"
165      else
166
167        # Make the tmpdir on the disk
168        mkdir -p ${FSMNT}/${COMPTMPDIR} >>${LOGOUT} 2>>${LOGOUT}
169
170        # Start by grabbing the component files
171        copy_component ${i}
172
173        # Remove the tmpdir now
174        rm -rf ${FSMNT}/${COMPTMPDIR} >>${LOGOUT} 2>>${LOGOUT}
175      fi
176    done
177  fi
178
179};
180