1209513Simp#!/bin/sh
2209513Simp#-
3209552Simp# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4209513Simp#
5209513Simp# Redistribution and use in source and binary forms, with or without
6209513Simp# modification, are permitted provided that the following conditions
7209513Simp# are met:
8209513Simp# 1. Redistributions of source code must retain the above copyright
9209513Simp#    notice, this list of conditions and the following disclaimer.
10209513Simp# 2. Redistributions in binary form must reproduce the above copyright
11209513Simp#    notice, this list of conditions and the following disclaimer in the
12209513Simp#    documentation and/or other materials provided with the distribution.
13209513Simp#
14209513Simp# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15209513Simp# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16209513Simp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17209513Simp# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18209513Simp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19209513Simp# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20209513Simp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21209513Simp# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22209513Simp# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23209513Simp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24209513Simp# SUCH DAMAGE.
25209513Simp#
26209513Simp# Script which reads the pc-autoinstall.conf directive, and begins the install
27209513Simp#
28209513Simp# $FreeBSD: releng/11.0/usr.sbin/pc-sysinstall/backend/startautoinstall.sh 232880 2012-03-12 18:50:37Z jpaetzel $
29209513Simp
30209513Simp# Source our functions scripts
31209513Simp. ${BACKEND}/functions.sh
32209513Simp. ${BACKEND}/functions-networking.sh
33209513Simp. ${BACKEND}/functions-parse.sh
34209513Simp
35209513Simp# Check that the config file exists
36209513Simpif [ ! -e "${1}" ]
37209513Simpthen
38209513Simp  echo "ERROR: Install configuration $1 does not exist!"
39209513Simp  exit 1
40209513Simpfi
41209513Simp
42209513Simp# Set our config file variable
43209513SimpCONF=${1}
44209513SimpINSTALL_CFG="/tmp/pc-sysinstall.cfg"
45209513Simp
46209513Simp# Check if the config file is on disk as well
47209513SimpPCCFG=`grep "pc_config:" ${CONF} | grep -v "^#" | sed "s|pc_config: ||g" | sed "s|pc_config:||g"`
48209513SimpSHUTDOWN_CMD=`grep "shutdown_cmd:" ${CONF} | grep -v "^#" | sed "s|shutdown_cmd: ||g" | sed "s|shutdown_cmd:||g"`
49209513SimpCONFIRM_INS=`grep "confirm_install:" ${CONF} | grep -v "^#" | sed "s|confirm_install: ||g" | sed "s|confirm_install:||g"`
50209513Simp
51209513Simp# Check that this isn't a http / ftp file we need to fetch later
52220059Sjpaetzelecho "${PCCFG}" | grep -q -e "^http" -e "^ftp" 2>/dev/null
53220059Sjpaetzelif [ $? -ne 0 ]
54209513Simpthen
55209513Simp  # Copy over the install cfg file, if not done already
56209513Simp  if [ ! -e "${INSTALL_CFG}" ]
57209513Simp  then
58209513Simp    cp ${PCCFG} ${INSTALL_CFG}
59209513Simp  fi
60209513Simp  # Make sure we have the file which was copied into /tmp previously
61209513Simp  if [ ! -e "${INSTALL_CFG}" ]
62209513Simp  then
63209513Simp    echo "Error: ${INSTALL_CFG} is missing! Exiting in 10 seconds..."
64209513Simp    sleep 10
65209513Simp    exit 150
66209513Simp  fi
67209513Simpelse
68209513Simp  # We need to fetch a remote file, check and set any nic options before doing so
69209513Simp  NICCFG=`grep "nic_config:" ${CONF} | grep -v "^#" | sed "s|nic_config: ||g" | sed "s|nic_config:||g"`
70209513Simp  if [ "${NICCFG}" = "dhcp-all" -o "${NICCFG}" = "DHCP-ALL" ]
71209513Simp  then
72209513Simp    # Try to auto-enable dhcp on any nics we find
73209513Simp    enable_auto_dhcp
74209513Simp  else
75209513Simp    echo "Running command \"ifconfig ${NICCFG}\""
76209513Simp    ifconfig ${NICCFG}
77209513Simp    WRKNIC="`echo ${NICCFG} | cut -d ' ' -f 1`"
78209513Simp    NICDNS=`grep "nic_dns:" ${CONF} | grep -v "^#" | sed "s|nic_dns: ||g" | sed "s|nic_dns:||g"`
79209513Simp    NICGATE=`grep "nic_gateway:" ${CONF} | grep -v "^#" | sed "s|nic_gateway: ||g" | sed "s|nic_gateway:||g"`
80209513Simp
81209513Simp    echo "nameserver ${NICDNS}" >/etc/resolv.conf
82209513Simp
83209513Simp    echo "Running command \"route add default ${NICGATE}\""
84209513Simp    route add default ${NICGATE}
85209513Simp  fi
86209513Simp
87209513Simp  get_nic_mac "$WRKNIC"
88209513Simp  nic_mac="${FOUNDMAC}"
89209513Simp
90209513Simp  PCCFG=`echo ${PCCFG} | sed "s|%%NIC_MAC%%|${nic_mac}|g"`
91209513Simp
92209513Simp  # Now try to fetch the remove file
93209513Simp  echo "Fetching cfg with: \"fetch -o ${INSTALL_CFG} ${PCCFG}\""
94209513Simp  fetch -o "${INSTALL_CFG}" "${PCCFG}"
95220059Sjpaetzel  if [ $? -ne 0 ]
96209513Simp  then
97209513Simp    echo "ERROR: Failed to fetch ${PCCFG}, install aborted"
98209513Simp    exit 150
99209513Simp  fi
100209513Simp
101209513Simpfi
102209513Simp
103209513Simp# If we end up with a valid config, lets proccede
104209513Simpif [ -e "${INSTALL_CFG}" ]
105209513Simpthen
106209513Simp  
107209513Simp  if [ "${CONFIRM_INS}" != "no" -a "${CONFIRM_INS}" != "NO" ]
108209513Simp  then
109209513Simp    echo "Type in 'install' to begin automated installation. Warning: Data on target disks may be destroyed!"
110209513Simp    read tmp
111209513Simp    case $tmp in
112209513Simp       install|INSTALL) ;;
113209513Simp       *) echo "Install canceled!" ; exit 150 ;;
114209513Simp    esac
115209513Simp  fi
116209513Simp
117232880Sjpaetzel  pc-sysinstall -c ${INSTALL_CFG}
118220059Sjpaetzel  if [ $? -eq 0 ]
119209513Simp  then
120220059Sjpaetzel    if [ -n "$SHUTDOWN_CMD" ]
121209513Simp    then
122209513Simp      ${SHUTDOWN_CMD}
123209513Simp    else 
124209513Simp      echo "SUCCESS: Installation finished! Press ENTER to reboot." 
125209513Simp      read tmp
126209513Simp      shutdown -r now
127209513Simp    fi
128209513Simp  else 
129209513Simp    echo "ERROR: Installation failed, press ENTER to drop to shell."
130209513Simp    read tmp
131209513Simp    /bin/csh
132209513Simp  fi
133209513Simpelse
134209513Simp  echo "ERROR: Failed to get /tmp/pc-sysinstall.cfg for automated install..."
135209513Simp  exit 150
136209513Simpfi
137