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