functions-installcomponents.sh revision 302408
1#!/bin/sh
2#-
3# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD: stable/11/usr.sbin/pc-sysinstall/backend/functions-installcomponents.sh 240165 2012-09-06 14:59:53Z jpaetzel $
27
28# Functions which check and load any optional modules specified in the config
29
30. ${BACKEND}/functions.sh
31. ${BACKEND}/functions-parse.sh
32
33copy_component()
34{
35  COMPONENT="$1"
36  FAILED="0"
37  CFILES=""
38
39  # Check the type, and set the components subdir properly
40  TYPE="`grep 'type:' ${COMPDIR}/${COMPONENT}/component.cfg | cut -d ' ' -f 2`"
41  if [ "${TYPE}" = "PBI" ]
42  then
43    SUBDIR="PBI"
44  else
45    SUBDIR="components"
46  fi
47
48  # Lets start by downloading / copying the files this component needs
49  while read line
50  do
51    CFILE="`echo $line | cut -d ':' -f 1`"
52    CFILEMD5="`echo $line | cut -d ':' -f 2`"
53    CFILE2MD5="`echo $line | cut -d ':' -f 3`"
54
55    case ${INSTALLMEDIUM} in
56      dvd|usb)
57        # On both dvd / usb, we can just copy the file
58        cp ${CDMNT}/${COMPFILEDIR}/${SUBDIR}/${CFILE} \
59		  ${FSMNT}/${COMPTMPDIR} >>${LOGOUT} 2>>${LOGOUT}
60	    RESULT="$?"
61        ;;
62
63      ftp)
64        get_value_from_cfg ftpPath
65        if [ -z "$VAL" ]
66        then
67          exit_err "ERROR: Install medium was set to ftp, but no ftpPath was provided!"
68        fi
69        FTPPATH="${VAL}" 
70
71        fetch_file "${FTPPATH}/${COMPFILEDIR}/${SUBDIR}/${CFILE}" "${FSMNT}/${COMPTMPDIR}/${CFILE}" "0"
72        RESULT="$?"
73       ;;
74    local)
75        get_value_from_cfg localPath
76        if [ -z "$VAL" ]; then
77          exit_err "Install medium was set to local, but no localPath was provided!"
78        fi
79        LOCALPATH=$VAL
80        cp ${LOCALPATH}/${COMPFILEDIR}/${SUBDIR}/${CFILE} \
81		  ${FSMNT}/${COMPTMPDIR} >>${LOGOUT} 2>>${LOGOUT}
82	RESULT="$?"
83       ;;
84    esac
85
86    if [ "${RESULT}" != "0" ]
87    then
88      echo_log "WARNING: Failed to copy ${CFILE}"
89      FAILED="1"
90    else
91      # Now lets check the MD5 to confirm the file is valid
92      CHECKMD5=`md5 -q ${FSMNT}/${COMPTMPDIR}/${CFILE}`
93      if [ "${CHECKMD5}" != "${CFILEMD5}" -a "${CHECKMD5}" != "${CFILE2MD5}" ]
94      then
95        echo_log "WARNING: ${CFILE} failed md5 checksum"
96        FAILED="1"
97      else
98        if [ -z "${CFILES}" ]
99        then
100          CFILES="${CFILE}" 
101        else
102          CFILES="${CFILES},${CFILE}"
103        fi
104      fi
105    fi
106
107
108  done < ${COMPDIR}/${COMPONENT}/distfiles
109      
110  if [ "${FAILED}" = "0" ]
111  then
112    # Now install the component
113    run_component_install ${COMPONENT} ${CFILES}
114  fi
115
116};
117
118run_component_install()
119{
120  COMPONENT="$1"
121  CFILES="$1"
122
123  # Lets install this component now 
124  # Start by making a wrapper script which sets the variables
125  # for the component to use
126  echo "#!/bin/sh
127COMPTMPDIR=\"${COMPTMPDIR}\"
128export COMPTMPDIR
129CFILE=\"${CFILE}\"
130export CFILE
131mount -t devfs devfs /dev
132
133sh ${COMPTMPDIR}/install.sh
134
135umount /dev
136" >${FSMNT}/.componentwrapper.sh
137  chmod 755 ${FSMNT}/.componentwrapper.sh
138   
139  # Copy over the install script for this component
140  cp ${COMPDIR}/${COMPONENT}/install.sh ${FSMNT}/${COMPTMPDIR}/
141
142  echo_log "INSTALL COMPONENT: ${i}"
143  chroot ${FSMNT} /.componentwrapper.sh >>${LOGOUT} 2>>${LOGOUT}
144  rm ${FSMNT}/.componentwrapper.sh
145
146};
147
148# Check for any modules specified, and begin loading them
149install_components()
150{
151  # First, lets check and see if we even have any optional modules
152  get_value_from_cfg installComponents
153  if [ -n "${VAL}" ]
154  then
155    # Lets start by cleaning up the string and getting it ready to parse
156    strip_white_space ${VAL}
157    COMPONENTS=`echo ${VAL} | sed -e "s|,| |g"`
158    for i in $COMPONENTS
159    do
160      if [ ! -e "${COMPDIR}/${i}/install.sh" -o ! -e "${COMPDIR}/${i}/distfiles" ]
161      then
162        echo_log "WARNING: Component ${i} doesn't seem to exist"
163      else
164
165        # Make the tmpdir on the disk
166        mkdir -p ${FSMNT}/${COMPTMPDIR} >>${LOGOUT} 2>>${LOGOUT}
167
168        # Start by grabbing the component files
169        copy_component ${i}
170
171        # Remove the tmpdir now
172        rm -rf ${FSMNT}/${COMPTMPDIR} >>${LOGOUT} 2>>${LOGOUT}
173      fi
174    done
175  fi
176
177};
178