1210036Simp#!/bin/sh 2210036Simp#- 3210036Simp# Copyright (c) 2010 iXsystems, Inc. All rights reserved. 4210036Simp# 5210036Simp# Redistribution and use in source and binary forms, with or without 6210036Simp# modification, are permitted provided that the following conditions 7210036Simp# are met: 8210036Simp# 1. Redistributions of source code must retain the above copyright 9210036Simp# notice, this list of conditions and the following disclaimer. 10210036Simp# 2. Redistributions in binary form must reproduce the above copyright 11210036Simp# notice, this list of conditions and the following disclaimer in the 12210036Simp# documentation and/or other materials provided with the distribution. 13210036Simp# 14210036Simp# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15210036Simp# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16210036Simp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17210036Simp# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18210036Simp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19210036Simp# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20210036Simp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21210036Simp# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22210036Simp# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23210036Simp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24210036Simp# SUCH DAMAGE. 25210036Simp# 26210036Simp# $FreeBSD: releng/10.2/usr.sbin/pc-sysinstall/backend/functions-packages.sh 247734 2013-03-03 23:01:46Z jpaetzel $ 27210036Simp 28210036Simp# Functions which runs commands on the system 29210036Simp 30210036Simp. ${BACKEND}/functions.sh 31210036Simp. ${BACKEND}/functions-parse.sh 32210036Simp. ${BACKEND}/functions-ftp.sh 33210036Simp 34210036Simp 35211486Simpget_package_index_by_ftp() 36210036Simp{ 37211730Simp local INDEX_FILE 38211730Simp local FTP_SERVER 39211486Simp 40211730Simp FTP_SERVER="${1}" 41211730Simp INDEX_FILE="INDEX" 42211730Simp USE_BZIP2=0 43210036Simp 44211730Simp if [ -f "/usr/bin/bzip2" ] 45211730Simp then 46211730Simp INDEX_FILE="${INDEX_FILE}.bz2" 47211730Simp USE_BZIP2=1 48247734Sjpaetzel INDEX_PATH="${INDEXFILE}.bz2" 49247734Sjpaetzel else 50247734Sjpaetzel INDEX_PATH="${INDEXFILE}" 51211730Simp fi 52210036Simp 53211730Simp fetch_file "${FTP_SERVER}/${INDEX_FILE}" "${INDEX_PATH}" "1" 54211730Simp if [ -f "${INDEX_PATH}" ] && [ "${USE_BZIP2}" -eq "1" ] 55211730Simp then 56211730Simp bzip2 -d "${INDEX_PATH}" 57211730Simp fi 58211486Simp}; 59211485Simp 60211486Simpget_package_index_by_fs() 61211486Simp{ 62247734Sjpaetzel if [ "$INSTALLMEDIUM" = "local" ] ; then 63247734Sjpaetzel INDEXFILE="${LOCALPATH}/packages/INDEX" 64247734Sjpaetzel else 65247734Sjpaetzel INDEXFILE="${CDMNT}/packages/INDEX" 66247734Sjpaetzel fi 67211486Simp}; 68211486Simp 69211487Simpget_package_index_size() 70211487Simp{ 71247734Sjpaetzel if [ -f "${INDEXFILE}" ] 72211730Simp then 73247734Sjpaetzel SIZE=`ls -l ${INDEXFILE} | awk '{ print $5 }'` 74211730Simp else 75211730Simp get_ftp_mirror 76211730Simp FTPHOST="${VAL}" 77211487Simp 78211730Simp FTPDIR="/pub/FreeBSD/releases/${FBSD_ARCH}/${FBSD_BRANCH}" 79211730Simp FTPPATH="ftp://${FTPHOST}${FTPDIR}/packages" 80211487Simp 81211730Simp fetch -s "${FTPPATH}/INDEX.bz2" 82211730Simp fi 83211487Simp}; 84211487Simp 85211486Simpget_package_index() 86211486Simp{ 87211730Simp RES=0 88211486Simp 89211730Simp if [ -z "${INSTALLMODE}" ] 90211730Simp then 91211730Simp get_ftp_mirror 92211730Simp FTPHOST="${VAL}" 93211486Simp 94211730Simp FTPDIR="/pub/FreeBSD/releases/${FBSD_ARCH}/${FBSD_BRANCH}" 95211730Simp FTPPATH="ftp://${FTPHOST}${FTPDIR}/packages" 96211486Simp 97211730Simp get_package_index_by_ftp "${FTPPATH}" 98211486Simp 99211730Simp else 100211486Simp 101247734Sjpaetzel case "${INSTALLMEDIUM}" in 102247734Sjpaetzel usb|dvd|local) get_package_index_by_fs ;; 103247734Sjpaetzel ftp) get_value_from_cfg ftpHost 104247734Sjpaetzel if [ -z "$VAL" ]; then 105247734Sjpaetzel exit_err "ERROR: Install medium was set to ftp, but no ftpHost was provided!" 106247734Sjpaetzel fi 107247734Sjpaetzel FTPHOST="${VAL}" 108211486Simp 109247734Sjpaetzel get_value_from_cfg ftpDir 110247734Sjpaetzel if [ -z "$VAL" ]; then 111247734Sjpaetzel exit_err "ERROR: Install medium was set to ftp, but no ftpDir was provided!" 112247734Sjpaetzel fi 113247734Sjpaetzel FTPDIR="${VAL}" 114247734Sjpaetzel FTPPATH="ftp://${FTPHOST}${FTPDIR}" 115247734Sjpaetzel get_package_index_by_ftp "${FTPPATH}" ;; 116247734Sjpaetzel sftp) ;; 117247734Sjpaetzel *) RES=1 ;; 118211730Simp esac 119211486Simp 120211730Simp fi 121211730Simp 122211730Simp return ${RES} 123211485Simp}; 124210036Simp 125210036Simpparse_package_index() 126210036Simp{ 127247734Sjpaetzel echo_log "Building package dep list.. Please wait.." 128211730Simp INDEX_FILE="${PKGDIR}/INDEX" 129210036Simp 130211730Simp exec 3<&0 131247734Sjpaetzel exec 0<"${INDEXFILE}" 132210036Simp 133211730Simp while read -r line 134211730Simp do 135211730Simp PKGNAME="" 136211730Simp CATEGORY="" 137211730Simp PACKAGE="" 138211730Simp DESC="" 139211730Simp DEPS="" 140211730Simp i=0 141210036Simp 142211730Simp SAVE_IFS="${IFS}" 143211730Simp IFS="|" 144210036Simp 145211730Simp for part in ${line} 146211730Simp do 147220059Sjpaetzel if [ ${i} -eq 0 ] 148211730Simp then 149211730Simp PKGNAME="${part}" 150211485Simp 151220059Sjpaetzel elif [ ${i} -eq 1 ] 152211730Simp then 153211730Simp PACKAGE=`basename "${part}"` 154210036Simp 155220059Sjpaetzel elif [ ${i} -eq 3 ] 156211730Simp then 157211730Simp DESC="${part}" 158210036Simp 159220059Sjpaetzel elif [ ${i} -eq 6 ] 160211730Simp then 161211730Simp CATEGORY=`echo "${part}" | cut -f1 -d' '` 162211485Simp 163220059Sjpaetzel elif [ ${i} -eq 8 ] 164211730Simp then 165211730Simp DEPS="${part}" 166211730Simp fi 167210036Simp 168211730Simp i=$((i+1)) 169211730Simp done 170210036Simp 171211730Simp echo "${CATEGORY}|${PACKAGE}|${DESC}" >> "${INDEX_FILE}.parsed" 172211730Simp echo "${PACKAGE}|${PKGNAME}|${DEPS}" >> "${INDEX_FILE}.deps" 173211485Simp 174211730Simp IFS="${SAVE_IFS}" 175211730Simp done 176210036Simp 177211730Simp exec 0<&3 178211485Simp}; 179210036Simp 180210036Simpshow_package_file() 181210036Simp{ 182211730Simp PKGFILE="${1}" 183210036Simp 184211730Simp echo "Available Packages:" 185211486Simp 186211730Simp exec 3<&0 187211730Simp exec 0<"${PKGFILE}" 188210036Simp 189211730Simp while read -r line 190211730Simp do 191211730Simp CATEGORY=`echo "${line}" | cut -f1 -d'|'` 192211730Simp PACKAGE=`echo "${line}" | cut -f2 -d'|'` 193211730Simp DESC=`echo "${line}" | cut -f3 -d'|'` 194210036Simp 195211730Simp echo "${CATEGORY}/${PACKAGE}:${DESC}" 196211730Simp done 197210036Simp 198211730Simp exec 0<&3 199211485Simp}; 200210036Simp 201210036Simpshow_packages_by_category() 202210036Simp{ 203211730Simp CATEGORY="${1}" 204211730Simp INDEX_FILE="${PKGDIR}/INDEX.parsed" 205211730Simp TMPFILE="/tmp/.pkg.cat" 206210036Simp 207211730Simp grep "^${CATEGORY}|" "${INDEX_FILE}" > "${TMPFILE}" 208211730Simp show_package_file "${TMPFILE}" 209211730Simp rm "${TMPFILE}" 210211485Simp}; 211210036Simp 212210036Simpshow_package_by_name() 213210036Simp{ 214211730Simp CATEGORY="${1}" 215211730Simp PACKAGE="${2}" 216211730Simp INDEX_FILE="${PKGDIR}/INDEX.parsed" 217211730Simp TMPFILE="/tmp/.pkg.cat.pak" 218210036Simp 219211730Simp grep "^${CATEGORY}|${PACKAGE}" "${INDEX_FILE}" > "${TMPFILE}" 220211730Simp show_package_file "${TMPFILE}" 221211730Simp rm "${TMPFILE}" 222211485Simp}; 223210036Simp 224210036Simpshow_packages() 225210036Simp{ 226211730Simp show_package_file "${PKGDIR}/INDEX.parsed" 227211485Simp}; 228211485Simp 229211485Simpget_package_dependencies() 230211485Simp{ 231211730Simp PACKAGE="${1}" 232211730Simp LONG="${2:-0}" 233211730Simp RES=0 234211485Simp 235211730Simp INDEX_FILE="${PKGDIR}/INDEX.deps" 236211730Simp REGEX="^${PACKAGE}|" 237211485Simp 238220059Sjpaetzel if [ ${LONG} -ne 0 ] 239211730Simp then 240211730Simp REGEX="^.*|${PACKAGE}|" 241211730Simp fi 242211485Simp 243211730Simp LINE=`grep "${REGEX}" "${INDEX_FILE}" 2>/dev/null` 244211730Simp DEPS=`echo "${LINE}"|cut -f3 -d'|'` 245211485Simp 246220059Sjpaetzel export VAL="${DEPS}" 247211485Simp 248211730Simp if [ -z "${VAL}" ] 249211730Simp then 250211730Simp RES=1 251211730Simp fi 252211485Simp 253211730Simp return ${RES} 254211485Simp}; 255211485Simp 256211485Simpget_package_name() 257211485Simp{ 258211730Simp PACKAGE="${1}" 259211730Simp RES=0 260247734Sjpaetzel local PKGPTH 261211485Simp 262247734Sjpaetzel # If we are on a local medium, we can parse the Latest/ directory 263247734Sjpaetzel if [ "${INSTALLMEDIUM}" != "ftp" ] ; then 264247734Sjpaetzel case "${INSTALLMEDIUM}" in 265247734Sjpaetzel usb|dvd) PKGPTH="${CDMNT}/packages" ;; 266247734Sjpaetzel *) PKGPTH="${LOCALPATH}/packages" ;; 267247734Sjpaetzel esac 268247734Sjpaetzel 269247734Sjpaetzel # Check the /Latest dir for generic names, then look for specific version in All/ 270247734Sjpaetzel if [ -e "${PKGPTH}/Latest/${PACKAGE}.${PKGEXT}" ] ; then 271247734Sjpaetzel NAME=`ls -al ${PKGPTH}/Latest/${PACKAGE}.${PKGEXT} 2>/dev/null | cut -d '>' -f 2 | rev | cut -f1 -d'/' | rev | tr -s ' '` 272247734Sjpaetzel else 273247734Sjpaetzel NAME=`ls -al ${PKGPTH}/All/${PACKAGE}.${PKGEXT} 2>/dev/null | cut -d '>' -f 2 | rev | cut -f1 -d'/' | rev | tr -s ' '` 274247734Sjpaetzel fi 275247734Sjpaetzel export VAL="${NAME}" 276247734Sjpaetzel else 277247734Sjpaetzel # Doing remote fetch, we we will look up, but some generic names like 278247734Sjpaetzel # "perl" wont work, since we don't know the default version 279247734Sjpaetzel INDEX_FILE="${PKGDIR}/INDEX.deps" 280247734Sjpaetzel REGEX="^${PACKAGE}|" 281211485Simp 282247734Sjpaetzel LINE=`grep "${REGEX}" "${INDEX_FILE}" 2>/dev/null` 283247734Sjpaetzel NAME=`echo "${LINE}"|cut -f2 -d'|'` 284211485Simp 285247734Sjpaetzel export VAL="${NAME}" 286247734Sjpaetzel fi 287211485Simp 288211730Simp if [ -z "${VAL}" ] 289211730Simp then 290211730Simp RES=1 291211730Simp fi 292211730Simp return ${RES} 293211485Simp}; 294211485Simp 295211485Simpget_package_short_name() 296211485Simp{ 297211730Simp PACKAGE="${1}" 298211730Simp RES=0 299211485Simp 300211730Simp INDEX_FILE="${PKGDIR}/INDEX.deps" 301211730Simp REGEX="^.*|${PACKAGE}|" 302211485Simp 303211730Simp LINE=`grep "${REGEX}" "${INDEX_FILE}" 2>/dev/null` 304211730Simp NAME=`echo "${LINE}"|cut -f1 -d'|'` 305211485Simp 306220059Sjpaetzel export VAL="${NAME}" 307211485Simp 308211730Simp if [ -z "${VAL}" ] 309211730Simp then 310211730Simp RES=1 311211730Simp fi 312211485Simp 313211730Simp return ${RES} 314211485Simp}; 315211485Simp 316211485Simpget_package_category() 317211485Simp{ 318211730Simp PACKAGE="${1}" 319211730Simp INDEX_FILE="${PKGDIR}/INDEX.parsed" 320211730Simp RES=0 321211485Simp 322211730Simp LINE=`grep "|${PACKAGE}|" "${INDEX_FILE}" 2>/dev/null` 323211730Simp NAME=`echo "${LINE}"|cut -f1 -d'|'` 324211485Simp 325220059Sjpaetzel export VAL="${NAME}" 326211485Simp 327211730Simp if [ -z "${VAL}" ] 328211730Simp then 329211730Simp RES=1 330211730Simp fi 331211485Simp 332211730Simp return ${RES} 333211485Simp}; 334211485Simp 335211486Simpfetch_package_by_ftp() 336211485Simp{ 337211730Simp CATEGORY="${1}" 338211730Simp PACKAGE="${2}" 339211730Simp SAVEDIR="${3}" 340211485Simp 341211730Simp get_value_from_cfg ftpHost 342211730Simp if [ -z "$VAL" ] 343211730Simp then 344211730Simp exit_err "ERROR: Install medium was set to ftp, but no ftpHost was provided!" 345211730Simp fi 346211730Simp FTPHOST="${VAL}" 347211485Simp 348211730Simp get_value_from_cfg ftpDir 349211730Simp if [ -z "$VAL" ] 350211730Simp then 351211730Simp exit_err "ERROR: Install medium was set to ftp, but no ftpDir was provided!" 352211730Simp fi 353211730Simp FTPDIR="${VAL}" 354211486Simp 355247734Sjpaetzel PACKAGE="${PACKAGE}.${PKGEXT}" 356211730Simp FTP_SERVER="ftp://${FTPHOST}${FTPDIR}" 357211486Simp 358211730Simp if [ ! -f "${SAVEDIR}/${PACKAGE}" ] 359211730Simp then 360211730Simp PKGPATH="${CATEGORY}/${PACKAGE}" 361211730Simp FTP_PATH="${FTP_HOST}/packages/${PKGPATH}" 362211730Simp fetch_file "${FTP_PATH}" "${SAVEDIR}/" "0" 363211730Simp fi 364211485Simp}; 365211486Simp 366247734Sjpaetzelfetch_package() 367211486Simp{ 368211730Simp CATEGORY="${1}" 369211730Simp PACKAGE="${2}" 370211730Simp SAVEDIR="${3}" 371211486Simp 372247734Sjpaetzel # Fetch package, but skip if installing from local media 373247734Sjpaetzel case "${INSTALLMEDIUM}" in 374247734Sjpaetzel usb|dvd|local) return ;; 375247734Sjpaetzel ftp) fetch_package_by_ftp "${CATEGORY}" "${PACKAGE}" "${SAVEDIR}" ;; 376247734Sjpaetzel sftp) ;; 377247734Sjpaetzel esac 378211486Simp}; 379211486Simp 380247734Sjpaetzelbootstrap_pkgng() 381211486Simp{ 382247734Sjpaetzel # Check if we need to boot-strap pkgng 383247734Sjpaetzel if run_chroot_cmd "which pkg-static" >/dev/null 2>/dev/null 384247734Sjpaetzel then 385247734Sjpaetzel return 386247734Sjpaetzel fi 387247734Sjpaetzel local PKGPTH 388211486Simp 389247734Sjpaetzel # Ok, lets boot-strap this sucker 390247734Sjpaetzel echo_log "Bootstraping pkgng.." 391247734Sjpaetzel fetch_package "Latest" "pkg" "${PKGDLDIR}" 392247734Sjpaetzel 393247734Sjpaetzel # Figure out real location of "pkg" package 394211730Simp case "${INSTALLMEDIUM}" in 395247734Sjpaetzel usb|dvd|local) PKGPTH="${PKGTMPDIR}/Latest/pkg.${PKGEXT}" ;; 396247734Sjpaetzel *) PKGPTH="${PKGTMPDIR}/pkg.${PKGEXT}" ;; 397211730Simp esac 398247734Sjpaetzel rc_halt "pkg -c ${FSMNT} add ${PKGPTH}" ; run_chroot_cmd "pkg2ng" 399247734Sjpaetzel} 400247734Sjpaetzel 401247734Sjpaetzelget_package_location() 402247734Sjpaetzel{ 403247734Sjpaetzel case "${INSTALLMEDIUM}" in 404247734Sjpaetzel usb|dvd) rc_halt "mount_nullfs ${CDMNT}/packages ${FSMNT}${PKGTMPDIR}" 405247734Sjpaetzel PKGDLDIR="${FSMNT}${PKGTMPDIR}/All" ;; 406247734Sjpaetzel local) rc_halt "mount_nullfs ${LOCALPATH}/packages ${FSMNT}${PKGTMPDIR}" 407247734Sjpaetzel PKGDLDIR="${FSMNT}${PKGTMPDIR}/All" ;; 408247734Sjpaetzel *) PKGDLDIR="${FSMNT}${PKGTMPDIR}" ;; 409247734Sjpaetzel esac 410247734Sjpaetzel export PKGDLDIR 411247734Sjpaetzel} 412