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 related mounting the newly formatted disk partitions
29209513Simp
30209513Simp# Mounts all the specified partition to the mount-point
31209513Simpmount_partition()
32209513Simp{
33209513Simp  if [ -z "${1}" -o -z "${2}" -o -z "${3}" ]
34209513Simp  then
35209513Simp    exit_err "ERROR: Missing arguments for mount_partition"
36209513Simp  fi
37209513Simp
38209513Simp  PART="${1}"
39209513Simp  PARTFS="${2}"
40209513Simp  MNTPOINT="${3}"
41209513Simp  MNTFLAGS="${4}"
42209513Simp
43209513Simp  # Setup the MNTOPTS
44209513Simp  if [ -z "${MNTOPTS}" ]
45209513Simp  then
46209513Simp    MNTFLAGS="-o rw"
47209513Simp  else
48209513Simp    MNTFLAGS="-o rw,${MNTFLAGS}"
49209513Simp  fi
50209513Simp
51209513Simp
52209513Simp  #We are on ZFS, lets setup this mount-point
53209513Simp  if [ "${PARTFS}" = "ZFS" ]
54209513Simp  then
55211730Simp    ZPOOLNAME=$(get_zpool_name "${PART}")
56209513Simp
57211730Simp    # Check if we have multiple zfs mounts specified
58211730Simp    for ZMNT in `echo ${MNTPOINT} | sed 's|,| |g'`
59211730Simp    do
60232899Sjpaetzel      # Check for any ZFS specific mount options
61232899Sjpaetzel      ZMNTOPTS="`echo $ZMNT | cut -d '(' -f 2 | cut -d ')' -f 1`" 
62232899Sjpaetzel      if [ "$ZMNTOPTS" = "$ZMNT" ] ; then ZMNTOPTS="" ; fi
63232899Sjpaetzel
64232899Sjpaetzel      # Reset ZMNT with options removed
65232899Sjpaetzel      ZMNT="`echo $ZMNT | cut -d '(' -f 1`"
66232899Sjpaetzel
67211730Simp      # First make sure we create the mount point
68211730Simp      if [ ! -d "${FSMNT}${ZMNT}" ] ; then
69211730Simp        mkdir -p ${FSMNT}${ZMNT} >>${LOGOUT} 2>>${LOGOUT}
70211730Simp      fi
71209513Simp
72232899Sjpaetzel      # Check for any volsize args
73232899Sjpaetzel      zcopt=""
74232899Sjpaetzel      for ZOPT in `echo $ZMNTOPTS | sed 's/|/ /g'`
75232899Sjpaetzel      do
76232899Sjpaetzel        echo "$ZOPT" | grep -q volsize
77232899Sjpaetzel        if [ $? -eq 0 ] ; then
78232899Sjpaetzel          volsize=`echo $ZOPT | cut -d '=' -f 2`
79232899Sjpaetzel          zcopt="-V $volsize"
80232899Sjpaetzel        fi
81232899Sjpaetzel      done
82232899Sjpaetzel
83211730Simp      if [ "${ZMNT}" = "/" ] ; then
84240165Sjpaetzel	# If creating ZFS / dataset, give it name that beadm works with
85240165Sjpaetzel        ZNAME="/ROOT/default"
86240165Sjpaetzel        ZMKMNT=""
87240165Sjpaetzel        echo_log "zfs create $zcopt -p ${ZPOOLNAME}/ROOT"
88240165Sjpaetzel        rc_halt "zfs create $zcopt -p ${ZPOOLNAME}/ROOT"
89240165Sjpaetzel        echo_log "zfs create $zcopt -p ${ZPOOLNAME}${ZNAME}"
90240165Sjpaetzel        rc_halt "zfs create $zcopt -p ${ZPOOLNAME}${ZNAME}"
91211730Simp      else
92211730Simp        ZNAME="${ZMNT}"
93240165Sjpaetzel        ZMKMNT="${ZMNT}"
94232899Sjpaetzel        echo_log "zfs create $zcopt -p ${ZPOOLNAME}${ZNAME}"
95232899Sjpaetzel        rc_halt "zfs create $zcopt -p ${ZPOOLNAME}${ZNAME}"
96211730Simp      fi
97211730Simp      sleep 2
98232899Sjpaetzel      if [ -z "$zcopt" ] ; then
99240165Sjpaetzel        rc_halt "zfs set mountpoint=${FSMNT}${ZMKMNT} ${ZPOOLNAME}${ZNAME}"
100232899Sjpaetzel      fi
101209513Simp
102240165Sjpaetzel      # Do we need to make this / zfs dataset bootable?
103240165Sjpaetzel      if [ "$ZMNT" = "/" ] ; then
104240165Sjpaetzel        echo_log "Stamping ${ZPOOLNAME}/ROOT/default as bootfs"
105240165Sjpaetzel        rc_halt "zpool set bootfs=${ZPOOLNAME}/ROOT/default ${ZPOOLNAME}"
106240165Sjpaetzel      fi
107240165Sjpaetzel
108240165Sjpaetzel      # Do we need to make this /boot zfs dataset bootable?
109240165Sjpaetzel      if [ "$ZMNT" = "/boot" ] ; then
110240165Sjpaetzel        echo_log "Stamping ${ZPOOLNAME}${ZMNT} as bootfs"
111240165Sjpaetzel        rc_halt "zpool set bootfs=${ZPOOLNAME}${ZMNT} ${ZPOOLNAME}"
112240165Sjpaetzel      fi
113240165Sjpaetzel
114232899Sjpaetzel      # If no ZFS options, we can skip
115232899Sjpaetzel      if [ -z "$ZMNTOPTS" ] ; then continue ; fi
116209513Simp
117232899Sjpaetzel      # Parse any ZFS options now
118232899Sjpaetzel      for ZOPT in `echo $ZMNTOPTS | sed 's/|/ /g'`
119232899Sjpaetzel      do
120232899Sjpaetzel        echo "$ZOPT" | grep -q volsize
121232899Sjpaetzel        if [ $? -eq 0 ] ; then continue ; fi
122232899Sjpaetzel        rc_halt "zfs set $ZOPT ${ZPOOLNAME}${ZNAME}"
123232899Sjpaetzel      done
124232899Sjpaetzel    done # End of adding ZFS mounts
125232899Sjpaetzel
126209513Simp  else
127211730Simp    # If we are not on ZFS, lets do the mount now
128209513Simp    # First make sure we create the mount point
129209513Simp    if [ ! -d "${FSMNT}${MNTPOINT}" ]
130209513Simp    then
131209513Simp      mkdir -p ${FSMNT}${MNTPOINT} >>${LOGOUT} 2>>${LOGOUT}
132209513Simp    fi
133209513Simp
134220909Sjpaetzel    echo_log "mount ${MNTFLAGS} ${PART} -> ${FSMNT}${MNTPOINT}"
135209513Simp    sleep 2
136220909Sjpaetzel    rc_halt "mount ${MNTFLAGS} ${PART} ${FSMNT}${MNTPOINT}"
137209513Simp  fi
138209513Simp
139209513Simp};
140209513Simp
141209513Simp# Mounts all the new file systems to prepare for installation
142209513Simpmount_all_filesystems()
143209513Simp{
144211730Simp  # Make sure our mount point exists
145211730Simp  mkdir -p ${FSMNT} >/dev/null 2>/dev/null
146209513Simp
147211730Simp  # First lets find and mount the / partition
148211730Simp  #########################################################
149211730Simp  for PART in `ls ${PARTDIR}`
150211730Simp  do
151220909Sjpaetzel    PARTDEV=`echo $PART | sed 's|-|/|g'` 
152247735Sjpaetzel    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
153247735Sjpaetzel    if [ ! -e "${PARTDEV}" -a "${PARTFS}" != "ZFS" ]
154211730Simp    then
155220909Sjpaetzel      exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
156211730Simp    fi 
157209513Simp
158232899Sjpaetzel    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
159232899Sjpaetzel    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
160209513Simp
161209513Simp    if [ "${PARTENC}" = "ON" ]
162209513Simp    then
163209513Simp      EXT=".eli"
164209513Simp    else
165209513Simp      EXT=""
166209513Simp    fi
167209513Simp
168209513Simp    # Check for root partition for mounting, including ZFS "/,/usr" type 
169209513Simp    echo "$PARTMNT" | grep "/," >/dev/null
170209513Simp    if [ "$?" = "0" -o "$PARTMNT" = "/" ]
171209513Simp    then
172209513Simp      case ${PARTFS} in
173220909Sjpaetzel        UFS) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
174220909Sjpaetzel        UFS+S) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
175220909Sjpaetzel        UFS+SUJ) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
176220909Sjpaetzel        UFS+J) mount_partition ${PARTDEV}${EXT}.journal ${PARTFS} ${PARTMNT} "async,noatime" ;;
177220909Sjpaetzel        ZFS) mount_partition ${PARTDEV} ${PARTFS} ${PARTMNT} ;;
178220909Sjpaetzel        IMAGE) mount_partition ${PARTDEV} ${PARTFS} ${PARTMNT} ;;
179211730Simp        *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
180209513Simp      esac
181209513Simp    fi
182211730Simp  done
183209513Simp
184211730Simp  # Now that we've mounted "/" lets do any other remaining mount-points
185211730Simp  ##################################################################
186211730Simp  for PART in `ls ${PARTDIR}`
187211730Simp  do
188220909Sjpaetzel    PARTDEV=`echo $PART | sed 's|-|/|g'`
189247735Sjpaetzel    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
190247735Sjpaetzel    if [ ! -e "${PARTDEV}" -a "${PARTFS}" != "ZFS" ]
191211730Simp    then
192220909Sjpaetzel      exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
193211730Simp    fi 
194209513Simp     
195232899Sjpaetzel    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
196232899Sjpaetzel    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
197209513Simp
198211730Simp    if [ "${PARTENC}" = "ON" ]
199211730Simp    then
200211730Simp      EXT=".eli"
201211730Simp    else
202211730Simp      EXT=""
203211730Simp    fi
204209513Simp
205211730Simp    # Check if we've found "/" again, don't need to mount it twice
206211730Simp    echo "$PARTMNT" | grep "/," >/dev/null
207211730Simp    if [ "$?" != "0" -a "$PARTMNT" != "/" ]
208211730Simp    then
209209513Simp       case ${PARTFS} in
210220909Sjpaetzel         UFS) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
211220909Sjpaetzel         UFS+S) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
212220909Sjpaetzel         UFS+SUJ) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
213220909Sjpaetzel         UFS+J) mount_partition ${PARTDEV}${EXT}.journal ${PARTFS} ${PARTMNT} "async,noatime" ;;
214220909Sjpaetzel         ZFS) mount_partition ${PARTDEV} ${PARTFS} ${PARTMNT} ;;
215211730Simp         SWAP)
216211730Simp		   # Lets enable this swap now
217211730Simp           if [ "$PARTENC" = "ON" ]
218211730Simp           then
219220909Sjpaetzel             echo_log "Enabling encrypted swap on ${PARTDEV}"
220220909Sjpaetzel             rc_halt "geli onetime -d -e 3des ${PARTDEV}"
221211730Simp             sleep 5
222220909Sjpaetzel             rc_halt "swapon ${PARTDEV}.eli"
223211730Simp           else
224220909Sjpaetzel             echo_log "swapon ${PARTDEV}"
225211730Simp             sleep 5
226220909Sjpaetzel             rc_halt "swapon ${PARTDEV}"
227211730Simp            fi
228211730Simp            ;;
229213650Simp         IMAGE)
230213650Simp           if [ ! -d "${PARTMNT}" ]
231213650Simp           then
232213650Simp             mkdir -p "${PARTMNT}" 
233213650Simp           fi 
234220909Sjpaetzel           mount_partition ${PARTDEV} ${PARTFS} ${PARTMNT}
235213650Simp           ;;
236213650Simp         *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
237211730Simp      esac
238211730Simp    fi
239211730Simp  done
240209513Simp};
241