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# Script which reads the pc-autoinstall.conf directive, and begins the install
29#
30# $FreeBSD$
31
32# Source our functions scripts
33. ${BACKEND}/functions.sh
34. ${BACKEND}/functions-networking.sh
35. ${BACKEND}/functions-parse.sh
36
37# Check that the config file exists
38if [ ! -e "${1}" ]
39then
40  echo "ERROR: Install configuration $1 does not exist!"
41  exit 1
42fi
43
44# Set our config file variable
45CONF=${1}
46INSTALL_CFG="/tmp/pc-sysinstall.cfg"
47
48# Check if the config file is on disk as well
49PCCFG=`grep "pc_config:" ${CONF} | grep -v "^#" | sed "s|pc_config: ||g" | sed "s|pc_config:||g"`
50SHUTDOWN_CMD=`grep "shutdown_cmd:" ${CONF} | grep -v "^#" | sed "s|shutdown_cmd: ||g" | sed "s|shutdown_cmd:||g"`
51CONFIRM_INS=`grep "confirm_install:" ${CONF} | grep -v "^#" | sed "s|confirm_install: ||g" | sed "s|confirm_install:||g"`
52
53# Check that this isn't a http / ftp file we need to fetch later
54echo "${PCCFG}" | grep -q -e "^http" -e "^ftp" 2>/dev/null
55if [ $? -ne 0 ]
56then
57  # Copy over the install cfg file, if not done already
58  if [ ! -e "${INSTALL_CFG}" ]
59  then
60    cp ${PCCFG} ${INSTALL_CFG}
61  fi
62  # Make sure we have the file which was copied into /tmp previously
63  if [ ! -e "${INSTALL_CFG}" ]
64  then
65    echo "Error: ${INSTALL_CFG} is missing! Exiting in 10 seconds..."
66    sleep 10
67    exit 150
68  fi
69else
70  # We need to fetch a remote file, check and set any nic options before doing so
71  NICCFG=`grep "nic_config:" ${CONF} | grep -v "^#" | sed "s|nic_config: ||g" | sed "s|nic_config:||g"`
72  if [ "${NICCFG}" = "dhcp-all" -o "${NICCFG}" = "DHCP-ALL" ]
73  then
74    # Try to auto-enable dhcp on any nics we find
75    enable_auto_dhcp
76  else
77    echo "Running command \"ifconfig ${NICCFG}\""
78    ifconfig ${NICCFG}
79    WRKNIC="`echo ${NICCFG} | cut -d ' ' -f 1`"
80    NICDNS=`grep "nic_dns:" ${CONF} | grep -v "^#" | sed "s|nic_dns: ||g" | sed "s|nic_dns:||g"`
81    NICGATE=`grep "nic_gateway:" ${CONF} | grep -v "^#" | sed "s|nic_gateway: ||g" | sed "s|nic_gateway:||g"`
82
83    echo "nameserver ${NICDNS}" >/etc/resolv.conf
84
85    echo "Running command \"route add default ${NICGATE}\""
86    route add default ${NICGATE}
87  fi
88
89  get_nic_mac "$WRKNIC"
90  nic_mac="${FOUNDMAC}"
91
92  PCCFG=`echo ${PCCFG} | sed "s|%%NIC_MAC%%|${nic_mac}|g"`
93
94  # Now try to fetch the remove file
95  echo "Fetching cfg with: \"fetch -o ${INSTALL_CFG} ${PCCFG}\""
96  fetch -o "${INSTALL_CFG}" "${PCCFG}"
97  if [ $? -ne 0 ]
98  then
99    echo "ERROR: Failed to fetch ${PCCFG}, install aborted"
100    exit 150
101  fi
102
103fi
104
105# If we end up with a valid config, lets proccede
106if [ -e "${INSTALL_CFG}" ]
107then
108  
109  if [ "${CONFIRM_INS}" != "no" -a "${CONFIRM_INS}" != "NO" ]
110  then
111    echo "Type in 'install' to begin automated installation. Warning: Data on target disks may be destroyed!"
112    read tmp
113    case $tmp in
114       install|INSTALL) ;;
115       *) echo "Install canceled!" ; exit 150 ;;
116    esac
117  fi
118
119  pc-sysinstall -c ${INSTALL_CFG}
120  if [ $? -eq 0 ]
121  then
122    if [ -n "$SHUTDOWN_CMD" ]
123    then
124      ${SHUTDOWN_CMD}
125    else 
126      echo "SUCCESS: Installation finished! Press ENTER to reboot." 
127      read tmp
128      shutdown -r now
129    fi
130  else 
131    echo "ERROR: Installation failed, press ENTER to drop to shell."
132    read tmp
133    /bin/csh
134  fi
135else
136  echo "ERROR: Failed to get /tmp/pc-sysinstall.cfg for automated install..."
137  exit 150
138fi
139