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