3 # Copyright (c) 2010 iXsystems, Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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
28 # Functions which perform the extraction / installation of system to disk
30 . ${BACKEND}/functions-mountoptical.sh
32 # Performs the extraction of data to disk from a uzip or tar archive
33 start_extract_uzip_tar()
37 exit_err "ERROR: Called extraction with no install file set!"
40 # Check if we have a .count file, and echo it out for a front-end to use in progress bars
41 if [ -e "${INSFILE}.count" ]
43 echo "INSTALLCOUNT: `cat ${INSFILE}.count`"
46 # Check if we are doing an upgrade, and if so use our exclude list
47 if [ "${INSTALLMODE}" = "upgrade" ]
49 TAROPTS="-X ${PROGDIR}/conf/exclude-from-upgrade"
54 echo_log "pc-sysinstall: Starting Extraction"
56 case ${PACKAGETYPE} in
58 if ! kldstat -v | grep -q "geom_uzip" ; then
59 exit_err "Kernel module geom_uzip not loaded"
62 # Start by mounting the uzip image
63 MDDEVICE=`mdconfig -a -t vnode -o readonly -f ${INSFILE}`
64 mkdir -p ${FSMNT}.uzip
65 mount -r /dev/${MDDEVICE}.uzip ${FSMNT}.uzip
68 exit_err "ERROR: Failed mounting the ${INSFILE}"
72 # Copy over all the files now!
73 tar cvf - . 2>/dev/null | tar -xpv -C ${FSMNT} ${TAROPTS} -f - 2>&1 | tee -a ${FSMNT}/.tar-extract.log
77 echo "TAR failure occurred:" >>${LOGOUT}
78 cat ${FSMNT}/.tar-extract.log | grep "tar:" >>${LOGOUT}
80 mdconfig -d -u ${MDDEVICE}
81 exit_err "ERROR: Failed extracting the tar image"
84 # All finished, now lets umount and cleanup
87 mdconfig -d -u ${MDDEVICE}
90 tar -xpv -C ${FSMNT} -f ${INSFILE} ${TAROPTS} >&1 2>&1
93 exit_err "ERROR: Failed extracting the tar image"
98 # Check if this was a FTP download and clean it up now
99 if [ "${INSTALLMEDIUM}" = "ftp" ]
101 echo_log "Cleaning up downloaded archive"
103 rm ${INSFILE}.count >/dev/null 2>/dev/null
104 rm ${INSFILE}.md5 >/dev/null 2>/dev/null
107 echo_log "pc-sysinstall: Extraction Finished"
111 # Performs the extraction of data to disk from a directory with split files
112 start_extract_split()
114 if [ -z "${INSDIR}" ]
116 exit_err "ERROR: Called extraction with no install directory set!"
119 echo_log "pc-sysinstall: Starting Extraction"
126 DIRS=`ls -d ${INSDIR}/*|grep -Ev '(uzip|kernels|src)'`
130 if [ -f "install.sh" ]
132 echo_log "Extracting" `basename ${dir}`
133 echo "y" | sh install.sh >/dev/null
136 exit_err "ERROR: Failed extracting ${dir}"
139 exit_err "ERROR: ${dir}/install.sh does not exist"
144 KERNELS=`ls -d ${INSDIR}/*|grep kernels`
146 if [ -f "install.sh" ]
148 echo_log "Extracting" `basename ${KERNELS}`
149 echo "y" | sh install.sh generic >/dev/null
152 exit_err "ERROR: Failed extracting ${KERNELS}"
154 rm -rf "${FSMNT}/boot/kernel"
155 mv "${FSMNT}/boot/GENERIC" "${FSMNT}/boot/kernel"
157 exit_err "ERROR: ${KERNELS}/install.sh does not exist"
161 SOURCE=`ls -d ${INSDIR}/*|grep src`
163 if [ -f "install.sh" ]
165 echo_log "Extracting" `basename ${SOURCE}`
166 echo "y" | sh install.sh all >/dev/null
169 exit_err "ERROR: Failed extracting ${SOURCE}"
172 exit_err "ERROR: ${SOURCE}/install.sh does not exist"
176 echo_log "pc-sysinstall: Extraction Finished"
179 # Function which will attempt to fetch the install file before we start
183 get_value_from_cfg ftpPath
186 exit_err "ERROR: Install medium was set to ftp, but no ftpPath was provided!"
191 # Check if we have a /usr partition to save the download
192 if [ -d "${FSMNT}/usr" ]
194 OUTFILE="${FSMNT}/usr/.fetch-${INSFILE}"
196 OUTFILE="${FSMNT}/.fetch-${INSFILE}"
199 # Do the fetch of the archive now
200 fetch_file "${FTPPATH}/${INSFILE}" "${OUTFILE}" "1"
202 # Check to see if there is a .count file for this install
203 fetch_file "${FTPPATH}/${INSFILE}.count" "${OUTFILE}.count" "0"
205 # Check to see if there is a .md5 file for this install
206 fetch_file "${FTPPATH}/${INSFILE}.md5" "${OUTFILE}.md5" "0"
208 # Done fetching, now reset the INSFILE to our downloaded archived
209 export INSFILE="${OUTFILE}"
213 # Function which will download freebsd install files
219 exit_err "ERROR: Install medium was set to ftp, but no ftpHost was provided!"
226 exit_err "ERROR: Install medium was set to ftp, but no ftpDir was provided!"
230 # Check if we have a /usr partition to save the download
231 if [ -d "${FSMNT}/usr" ]
233 OUTFILE="${FSMNT}/usr/.fetch-${INSFILE}"
235 OUTFILE="${FSMNT}/.fetch-${INSFILE}"
238 DIRS="base catpages dict doc games info manpages proflibs kernels src"
239 if [ "${FBSD_ARCH}" = "amd64" ]
246 mkdir -p "${OUTFILE}/${d}"
250 NETRC="${OUTFILE}/.netrc"
251 cat <<EOF >"${NETRC}"
262 cat <<EOF >>"${NETRC}"
269 cat <<EOF >>"${NETRC}"
275 # Fetch the files via ftp
276 echo "$ INSTALL" | ftp -N "${NETRC}" "${FTPHOST}"
278 # Done fetching, now reset the INSFILE to our downloaded archived
279 export INSFILE="${OUTFILE}"
282 # Function which does the rsync download from the server specified in cfg
285 # Load our rsync config values
286 get_value_from_cfg rsyncPath
287 if [ -z "${VAL}" ]; then
288 exit_err "ERROR: rsyncPath is unset! Please check your config and try again."
290 export RSYNCPATH="${VAL}"
292 get_value_from_cfg rsyncHost
293 if [ -z "${VAL}" ]; then
294 exit_err "ERROR: rsyncHost is unset! Please check your config and try again."
296 export RSYNCHOST="${VAL}"
298 get_value_from_cfg rsyncUser
299 if [ -z "${VAL}" ]; then
300 exit_err "ERROR: rsyncUser is unset! Please check your config and try again."
302 export RSYNCUSER="${VAL}"
304 get_value_from_cfg rsyncPort
305 if [ -z "${VAL}" ]; then
306 exit_err "ERROR: rsyncPort is unset! Please check your config and try again."
308 export RSYNCPORT="${VAL}"
314 if [ ${COUNT} -gt ${RSYNCTRIES} ]
316 exit_err "ERROR: Failed rsync command!"
321 --rsync-path="rsync --fake-super" \
322 -e "ssh -p ${RSYNCPORT}" \
323 ${RSYNCUSER}@${RSYNCHOST}:${RSYNCPATH}/./ ${FSMNT}
326 echo "Rsync failed! Tries: ${COUNT}"
336 start_image_install()
338 if [ -z "${IMAGE_FILE}" ]
340 exit_err "ERROR: installMedium set to image but no image file specified!"
343 # We are ready to start mounting, lets read the config and do it
346 echo $line | grep -q "^disk0=" 2>/dev/null
349 # Found a disk= entry, lets get the disk we are working on
350 get_value_from_string "${line}"
351 strip_white_space "$VAL"
355 echo $line | grep -q "^commitDiskPart" 2>/dev/null
358 # Found our flag to commit this disk setup / lets do sanity check and do it
363 write_image "${IMAGE_FILE}" "${DISK}"
365 # Increment our disk counter to look for next disk and unset
370 exit_err "ERROR: commitDiskPart was called without procceding disk<num>= and partition= entries!!!"
377 # Entrance function, which starts the installation process
380 # Figure out what file we are using to install from via the config
381 get_value_from_cfg installFile
385 export INSFILE="${VAL}"
387 # If no installFile specified, try our defaults
388 if [ "$INSTALLTYPE" = "FreeBSD" ]
391 uzip) INSFILE="${FBSD_UZIP_FILE}" ;;
392 tar) INSFILE="${FBSD_TAR_FILE}" ;;
394 INSDIR="${FBSD_BRANCH_DIR}"
396 # This is to trick opt_mount into not failing
402 uzip) INSFILE="${UZIP_FILE}" ;;
403 tar) INSFILE="${TAR_FILE}" ;;
409 # Lets start by figuring out what medium we are using
410 case ${INSTALLMEDIUM} in
412 # Lets start by mounting the disk
414 if [ -n "${INSDIR}" ]
416 INSDIR="${CDMNT}/${INSDIR}" ; export INSDIR
420 INSFILE="${CDMNT}/${INSFILE}" ; export INSFILE
421 start_extract_uzip_tar
426 if [ "$PACKAGETYPE" = "split" ]
430 INSDIR="${INSFILE}" ; export INSDIR
434 start_extract_uzip_tar
440 rsync) start_rsync_copy ;;
441 image) start_image_install ;;
443 get_value_from_cfg localPath
446 exit_err "Install medium was set to local, but no localPath was provided!"
449 INSFILE="${LOCALPATH}/${INSFILE}" ; export INSFILE
450 start_extract_uzip_tar
452 *) exit_err "ERROR: Unknown install medium" ;;