functions-mountdisk.sh revision 232899
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: head/usr.sbin/pc-sysinstall/backend/functions-mountdisk.sh 232899 2012-03-12 21:32:43Z 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        ZNAME=""
85      else
86        ZNAME="${ZMNT}"
87        echo_log "zfs create $zcopt -p ${ZPOOLNAME}${ZNAME}"
88        rc_halt "zfs create $zcopt -p ${ZPOOLNAME}${ZNAME}"
89      fi
90      sleep 2
91      if [ -z "$zcopt" ] ; then
92        rc_halt "zfs set mountpoint=${FSMNT}${ZNAME} ${ZPOOLNAME}${ZNAME}"
93      fi
94
95      # If no ZFS options, we can skip
96      if [ -z "$ZMNTOPTS" ] ; then continue ; fi
97
98      # Parse any ZFS options now
99      for ZOPT in `echo $ZMNTOPTS | sed 's/|/ /g'`
100      do
101        echo "$ZOPT" | grep -q volsize
102        if [ $? -eq 0 ] ; then continue ; fi
103        rc_halt "zfs set $ZOPT ${ZPOOLNAME}${ZNAME}"
104      done
105    done # End of adding ZFS mounts
106
107  else
108    # If we are not on ZFS, lets do the mount now
109    # First make sure we create the mount point
110    if [ ! -d "${FSMNT}${MNTPOINT}" ]
111    then
112      mkdir -p ${FSMNT}${MNTPOINT} >>${LOGOUT} 2>>${LOGOUT}
113    fi
114
115    echo_log "mount ${MNTFLAGS} ${PART} -> ${FSMNT}${MNTPOINT}"
116    sleep 2
117    rc_halt "mount ${MNTFLAGS} ${PART} ${FSMNT}${MNTPOINT}"
118  fi
119
120};
121
122# Mounts all the new file systems to prepare for installation
123mount_all_filesystems()
124{
125  # Make sure our mount point exists
126  mkdir -p ${FSMNT} >/dev/null 2>/dev/null
127
128  # First lets find and mount the / partition
129  #########################################################
130  for PART in `ls ${PARTDIR}`
131  do
132    PARTDEV=`echo $PART | sed 's|-|/|g'` 
133    if [ ! -e "${PARTDEV}" ]
134    then
135      exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
136    fi 
137
138    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
139    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
140    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
141
142    if [ "${PARTENC}" = "ON" ]
143    then
144      EXT=".eli"
145    else
146      EXT=""
147    fi
148
149    # Check for root partition for mounting, including ZFS "/,/usr" type 
150    echo "$PARTMNT" | grep "/," >/dev/null
151    if [ "$?" = "0" -o "$PARTMNT" = "/" ]
152    then
153      case ${PARTFS} in
154        UFS) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
155        UFS+S) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
156        UFS+SUJ) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
157        UFS+J) mount_partition ${PARTDEV}${EXT}.journal ${PARTFS} ${PARTMNT} "async,noatime" ;;
158        ZFS) mount_partition ${PARTDEV} ${PARTFS} ${PARTMNT} ;;
159        IMAGE) mount_partition ${PARTDEV} ${PARTFS} ${PARTMNT} ;;
160        *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
161      esac
162    fi
163  done
164
165  # Now that we've mounted "/" lets do any other remaining mount-points
166  ##################################################################
167  for PART in `ls ${PARTDIR}`
168  do
169    PARTDEV=`echo $PART | sed 's|-|/|g'`
170    if [ ! -e "${PARTDEV}" ]
171    then
172      exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
173    fi 
174     
175    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
176    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
177    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
178
179    if [ "${PARTENC}" = "ON" ]
180    then
181      EXT=".eli"
182    else
183      EXT=""
184    fi
185
186    # Check if we've found "/" again, don't need to mount it twice
187    echo "$PARTMNT" | grep "/," >/dev/null
188    if [ "$?" != "0" -a "$PARTMNT" != "/" ]
189    then
190       case ${PARTFS} in
191         UFS) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
192         UFS+S) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
193         UFS+SUJ) mount_partition ${PARTDEV}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
194         UFS+J) mount_partition ${PARTDEV}${EXT}.journal ${PARTFS} ${PARTMNT} "async,noatime" ;;
195         ZFS) mount_partition ${PARTDEV} ${PARTFS} ${PARTMNT} ;;
196         SWAP)
197		   # Lets enable this swap now
198           if [ "$PARTENC" = "ON" ]
199           then
200             echo_log "Enabling encrypted swap on ${PARTDEV}"
201             rc_halt "geli onetime -d -e 3des ${PARTDEV}"
202             sleep 5
203             rc_halt "swapon ${PARTDEV}.eli"
204           else
205             echo_log "swapon ${PARTDEV}"
206             sleep 5
207             rc_halt "swapon ${PARTDEV}"
208            fi
209            ;;
210         IMAGE)
211           if [ ! -d "${PARTMNT}" ]
212           then
213             mkdir -p "${PARTMNT}" 
214           fi 
215           mount_partition ${PARTDEV} ${PARTFS} ${PARTMNT}
216           ;;
217         *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
218      esac
219    fi
220  done
221};
222