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# $FreeBSD$
27209513Simp
28209513Simp# Functions which perform mounting / unmounting and switching of 
29209513Simp# optical / usb media
30209513Simp
31209513Simp. ${BACKEND}/functions.sh
32209513Simp. ${BACKEND}/functions-parse.sh
33209513Simp
34209513Simp# Displays an optical failure message
35209513Simpopt_fail()
36209513Simp{
37211730Simp  # If we got here, we must not have a DVD/USB we can find :(
38211730Simp  get_value_from_cfg installInteractive
39211730Simp  if [ "${VAL}" = "yes" ]
40211730Simp  then
41211730Simp    # We are running interactive, and didn't find a DVD, prompt user again
42211730Simp    echo_log "DISK ERROR: Unable to find installation disk!"
43211730Simp    echo_log "Please insert the installation disk and press enter."
44211730Simp    read tmp
45211730Simp  else
46211730Simp   exit_err "ERROR: Unable to locate installation DVD/USB"
47211730Simp  fi
48209513Simp};
49209513Simp
50209513Simp# Performs the extraction of data to disk
51209513Simpopt_mount()
52209513Simp{
53211730Simp  FOUND="0"
54209513Simp
55211730Simp  # Ensure we have a directory where its supposed to be
56211730Simp  if [ ! -d "${CDMNT}" ]
57211730Simp  then
58211730Simp    mkdir -p ${CDMNT}
59211730Simp  fi
60209513Simp
61209513Simp
62211730Simp  # Start by checking if we already have a cd mounted at CDMNT
63220059Sjpaetzel  mount | grep -q "${CDMNT} " 2>/dev/null
64220059Sjpaetzel  if [ $? -eq 0 ]
65211730Simp  then
66211730Simp    if [ -e "${CDMNT}/${INSFILE}" ]
67211730Simp    then
68211730Simp      echo "MOUNTED" >${TMPDIR}/cdmnt
69211730Simp      echo_log "FOUND DVD: MOUNTED"
70211730Simp      FOUND="1"
71211730Simp      return
72211730Simp    fi
73209513Simp
74211730Simp    # failed to find optical disk
75211730Simp    opt_fail
76211730Simp    return
77211730Simp  fi
78209513Simp
79211730Simp  # Setup our loop to search for installation media
80211730Simp  while
81211730Simp  z=1
82211730Simp  do
83209513Simp
84211730Simp    # Loop though and look for an installation disk
85211730Simp    for i in `ls -1 /dev/acd* /dev/cd* /dev/scd* /dev/rscd* 2>/dev/null`
86211730Simp    do
87211730Simp      # Find the CD Device
88211730Simp      /sbin/mount_cd9660 $i ${CDMNT}
89209513Simp
90211730Simp      # Check the package type to see if we have our install data
91211730Simp      if [ -e "${CDMNT}/${INSFILE}" ]
92211730Simp      then
93211730Simp        echo "${i}" >${TMPDIR}/cdmnt
94211730Simp        echo_log "FOUND DVD: ${i}"
95211730Simp        FOUND="1"
96211730Simp        break
97211730Simp      fi
98211730Simp      /sbin/umount ${CDMNT} >/dev/null 2>/dev/null
99211730Simp    done
100209513Simp
101211730Simp    # If no DVD found, try USB
102211730Simp    if [ "$FOUND" != "1" ]
103211730Simp    then
104211730Simp      # Loop though and look for an installation disk
105211730Simp      for i in `ls -1 /dev/da* 2>/dev/null`
106211730Simp      do
107211730Simp        # Check if we can mount this device UFS
108211730Simp        /sbin/mount -r $i ${CDMNT}
109209513Simp
110211730Simp        # Check the package type to see if we have our install data
111211730Simp        if [ -e "${CDMNT}/${INSFILE}" ]
112211730Simp        then
113211730Simp          echo "${i}" >${TMPDIR}/cdmnt
114211730Simp          echo_log "FOUND USB: ${i}"
115211730Simp          FOUND="1"
116211730Simp          break
117211730Simp        fi
118211730Simp        /sbin/umount ${CDMNT} >/dev/null 2>/dev/null
119209513Simp
120211730Simp        # Also check if it is a FAT mount
121211730Simp        /sbin/mount -r -t msdosfs $i ${CDMNT}
122209513Simp
123211730Simp        # Check the package type to see if we have our install data
124211730Simp        if [ -e "${CDMNT}/${INSFILE}" ]
125211730Simp        then
126211730Simp          echo "${i}" >${TMPDIR}/cdmnt
127211730Simp          echo_log "FOUND USB: ${i}"
128211730Simp          FOUND="1"
129211730Simp          break
130211730Simp        fi
131211730Simp        /sbin/umount ${CDMNT} >/dev/null 2>/dev/null
132211730Simp      done
133211730Simp    fi # End of USB Check
134209513Simp
135209513Simp
136211730Simp    if [ "$FOUND" = "1" ]
137211730Simp    then
138211730Simp      break
139211730Simp    fi
140209513Simp   
141211730Simp    # Failed to find a disk, take action now
142211730Simp    opt_fail
143209513Simp
144211730Simp  done
145209513Simp
146209513Simp};
147209513Simp
148209513Simp# Function to unmount optical media
149209513Simpopt_umount()
150209513Simp{
151209513Simp  /sbin/umount ${CDMNT} >/dev/null 2>/dev/null
152209513Simp};
153