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# $FreeBSD$
29
30# Functions related mounting the newly formatted disk partitions
31
32# Mounts all the specified partition to the mount-point
33mount_partition()
34{
35  if [ -z "${1}" -o -z "${2}" -o -z "${3}" ]
36  then
37    exit_err "ERROR: Missing arguments for mount_partition"
38  fi
39
40  PART="${1}"
41  PARTFS="${2}"
42  MNTPOINT="${3}"
43  MNTFLAGS="${4}"
44
45  # Setup the MNTOPTS
46  if [ -z "${MNTOPTS}" ]
47  then
48    MNTFLAGS="-o rw"
49  else
50    MNTFLAGS="-o rw,${MNTFLAGS}"
51  fi
52
53
54  #We are on ZFS, lets setup this mount-point
55  if [ "${PARTFS}" = "ZFS" ]
56  then
57    ZPOOLNAME=$(get_zpool_name "${PART}")
58
59    # Check if we have multiple zfs mounts specified
60    for ZMNT in `echo ${MNTPOINT} | sed 's|,| |g'`
61    do
62      # Check for any ZFS specific mount options
63      ZMNTOPTS="`echo $ZMNT | cut -d '(' -f 2 | cut -d ')' -f 1`"
64      if [ "$ZMNTOPTS" = "$ZMNT" ] ; then ZMNTOPTS="" ; fi
65
66      # Reset ZMNT with options removed
67      ZMNT="`echo $ZMNT | cut -d '(' -f 1`"
68
69      # First make sure we create the mount point
70      if [ ! -d "${FSMNT}${ZMNT}" ] ; then
71        mkdir -p ${FSMNT}${ZMNT} >>${LOGOUT} 2>>${LOGOUT}
72      fi
73
74      # Check for any volsize args
75      zcopt=""
76      for ZOPT in `echo $ZMNTOPTS | sed 's/|/ /g'`
77      do
78        echo "$ZOPT" | grep -q volsize
79        if [ $? -eq 0 ] ; then
80          volsize=`echo $ZOPT | cut -d '=' -f 2`
81          zcopt="-V $volsize"
82        fi
83      done
84
85      if [ "${ZMNT}" = "/" ] ; then
86	# If creating ZFS / dataset, give it name that beadm works with
87        ZNAME="/ROOT/default"
88        ZMKMNT=""
89        echo_log "zfs create $zcopt -p ${ZPOOLNAME}/ROOT"
90        rc_halt "zfs create $zcopt -p ${ZPOOLNAME}/ROOT"
91        echo_log "zfs create $zcopt -p ${ZPOOLNAME}${ZNAME}"
92        rc_halt "zfs create $zcopt -p ${ZPOOLNAME}${ZNAME}"
93      else
94        ZNAME="${ZMNT}"
95        ZMKMNT="${ZMNT}"
96        echo_log "zfs create $zcopt -p ${ZPOOLNAME}${ZNAME}"
97        rc_halt "zfs create $zcopt -p ${ZPOOLNAME}${ZNAME}"
98      fi
99      sleep 2
100      if [ -z "$zcopt" ] ; then
101        rc_halt "zfs set mountpoint=${FSMNT}${ZMKMNT} ${ZPOOLNAME}${ZNAME}"
102      fi
103
104      # Do we need to make this / zfs dataset bootable?
105      if [ "$ZMNT" = "/" ] ; then
106        echo_log "Stamping ${ZPOOLNAME}/ROOT/default as bootfs"
107        rc_halt "zpool set bootfs=${ZPOOLNAME}/ROOT/default ${ZPOOLNAME}"
108      fi
109
110      # Do we need to make this /boot zfs dataset bootable?
111      if [ "$ZMNT" = "/boot" ] ; then
112        echo_log "Stamping ${ZPOOLNAME}${ZMNT} as bootfs"
113        rc_halt "zpool set bootfs=${ZPOOLNAME}${ZMNT} ${ZPOOLNAME}"
114      fi
115
116      # If no ZFS options, we can skip
117      if [ -z "$ZMNTOPTS" ] ; then continue ; fi
118
119      # Parse any ZFS options now
120      for ZOPT in `echo $ZMNTOPTS | sed 's/|/ /g'`
121      do
122        echo "$ZOPT" | grep -q volsize
123        if [ $? -eq 0 ] ; then continue ; fi
124        rc_halt "zfs set $ZOPT ${ZPOOLNAME}${ZNAME}"
125      done
126    done # End of adding ZFS mounts
127
128  else
129    # If we are not on ZFS, lets do the mount now
130    # First make sure we create the mount point
131    if [ ! -d "${FSMNT}${MNTPOINT}" ]
132    then
133      mkdir -p ${FSMNT}${MNTPOINT} >>${LOGOUT} 2>>${LOGOUT}
134    fi
135
136    echo_log "mount ${MNTFLAGS} ${PART} -> ${FSMNT}${MNTPOINT}"
137    sleep 2
138    rc_halt "mount ${MNTFLAGS} ${PART} ${FSMNT}${MNTPOINT}"
139  fi
140
141};
142
143# Mounts all the new file systems to prepare for installation
144mount_all_filesystems()
145{
146  # Make sure our mount point exists
147  mkdir -p ${FSMNT} >/dev/null 2>/dev/null
148
149  # First lets find and mount the / partition
150  #########################################################
151  for PART in `ls ${PARTDIR}`
152  do
153    PARTDEV=`echo $PART | sed 's|-|/|g'`
154    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
155    if [ ! -e "${PARTDEV}" -a "${PARTFS}" != "ZFS" ]
156    then
157      exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
158    fi
159
160    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
161    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
162
163    if [ "${PARTENC}" = "ON" ]
164    then
165      EXT=".eli"
166    else
167      EXT=""
168    fi
169
170    # Check for root partition for mounting, including ZFS "/,/usr" type
171    echo "$PARTMNT" | grep "/," >/dev/null
172    if [ "$?" = "0" -o "$PARTMNT" = "/" ]
173    then
174      case ${PARTFS} in
175        UFS) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
176        UFS+S) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
177        UFS+SUJ) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
178        UFS+J) mount_partition ${PARTDEV}${EXT}.journal ${PARTFS} ${PARTMNT} "async,noatime" ;;
179        ZFS) mount_partition ${PARTDEV} ${PARTFS} ${PARTMNT} ;;
180        IMAGE) mount_partition ${PARTDEV} ${PARTFS} ${PARTMNT} ;;
181        *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
182      esac
183    fi
184  done
185
186  # Now that we've mounted "/" lets do any other remaining mount-points
187  ##################################################################
188  for PART in `ls ${PARTDIR}`
189  do
190    PARTDEV=`echo $PART | sed 's|-|/|g'`
191    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
192    if [ ! -e "${PARTDEV}" -a "${PARTFS}" != "ZFS" ]
193    then
194      exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
195    fi
196
197    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
198    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
199
200    if [ "${PARTENC}" = "ON" ]
201    then
202      EXT=".eli"
203    else
204      EXT=""
205    fi
206
207    # Check if we've found "/" again, don't need to mount it twice
208    echo "$PARTMNT" | grep "/," >/dev/null
209    if [ "$?" != "0" -a "$PARTMNT" != "/" ]
210    then
211       case ${PARTFS} in
212         UFS) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
213         UFS+S) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
214         UFS+SUJ) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
215         UFS+J) mount_partition ${PARTDEV}${EXT}.journal ${PARTFS} ${PARTMNT} "async,noatime" ;;
216         ZFS) mount_partition ${PARTDEV} ${PARTFS} ${PARTMNT} ;;
217         SWAP)
218		   # Lets enable this swap now
219           if [ "$PARTENC" = "ON" ]
220           then
221             echo_log "Enabling encrypted swap on ${PARTDEV}"
222             rc_halt "geli onetime -d -e 3des ${PARTDEV}"
223             sleep 5
224             rc_halt "swapon ${PARTDEV}.eli"
225           else
226             echo_log "swapon ${PARTDEV}"
227             sleep 5
228             rc_halt "swapon ${PARTDEV}"
229            fi
230            ;;
231         IMAGE)
232           if [ ! -d "${PARTMNT}" ]
233           then
234             mkdir -p "${PARTMNT}"
235           fi
236           mount_partition ${PARTDEV} ${PARTFS} ${PARTMNT}
237           ;;
238         *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
239      esac
240    fi
241  done
242};
243