functions-mountdisk.sh revision 213650
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 213650 2010-10-09 08:52:09Z imp $
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      # First make sure we create the mount point
61      if [ ! -d "${FSMNT}${ZMNT}" ] ; then
62        mkdir -p ${FSMNT}${ZMNT} >>${LOGOUT} 2>>${LOGOUT}
63      fi
64
65      if [ "${ZMNT}" = "/" ] ; then
66        ZNAME=""
67      else
68        ZNAME="${ZMNT}"
69        echo_log "zfs create -p ${ZPOOLNAME}${ZNAME}"
70        rc_halt "zfs create -p ${ZPOOLNAME}${ZNAME}"
71      fi
72      sleep 2
73      rc_halt "zfs set mountpoint=${FSMNT}${ZNAME} ${ZPOOLNAME}${ZNAME}"
74
75      # Disable atime for this zfs partition, speed increase
76      rc_nohalt "zfs set atime=off ${ZPOOLNAME}${ZNAME}"
77    done 
78
79  else
80    # If we are not on ZFS, lets do the mount now
81    # First make sure we create the mount point
82    if [ ! -d "${FSMNT}${MNTPOINT}" ]
83    then
84      mkdir -p ${FSMNT}${MNTPOINT} >>${LOGOUT} 2>>${LOGOUT}
85    fi
86
87    echo_log "mount ${MNTFLAGS} /dev/${PART} -> ${FSMNT}${MNTPOINT}"
88    sleep 2
89    rc_halt "mount ${MNTFLAGS} /dev/${PART} ${FSMNT}${MNTPOINT}"
90  fi
91
92};
93
94# Mounts all the new file systems to prepare for installation
95mount_all_filesystems()
96{
97  # Make sure our mount point exists
98  mkdir -p ${FSMNT} >/dev/null 2>/dev/null
99
100  # First lets find and mount the / partition
101  #########################################################
102  for PART in `ls ${PARTDIR}`
103  do
104    if [ ! -e "/dev/${PART}" ]
105    then
106      exit_err "ERROR: The partition ${PART} does not exist. Failure in bsdlabel?"
107    fi 
108
109    PARTFS="`cat ${PARTDIR}/${PART} | cut -d ':' -f 1`"
110    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d ':' -f 2`"
111    PARTENC="`cat ${PARTDIR}/${PART} | cut -d ':' -f 3`"
112
113    if [ "${PARTENC}" = "ON" ]
114    then
115      EXT=".eli"
116    else
117      EXT=""
118    fi
119
120    # Check for root partition for mounting, including ZFS "/,/usr" type 
121    echo "$PARTMNT" | grep "/," >/dev/null
122    if [ "$?" = "0" -o "$PARTMNT" = "/" ]
123    then
124      case ${PARTFS} in
125        UFS) mount_partition ${PART}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
126        UFS+S) mount_partition ${PART}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
127        UFS+SUJ) mount_partition ${PART}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
128        UFS+J) mount_partition ${PART}${EXT}.journal ${PARTFS} ${PARTMNT} "async,noatime" ;;
129        ZFS) mount_partition ${PART} ${PARTFS} ${PARTMNT} ;;
130        IMAGE) mount_partition ${PART} ${PARTFS} ${PARTMNT} ;;
131        *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
132      esac
133    fi
134  done
135
136  # Now that we've mounted "/" lets do any other remaining mount-points
137  ##################################################################
138  for PART in `ls ${PARTDIR}`
139  do
140    if [ ! -e "/dev/${PART}" ]
141    then
142      exit_err "ERROR: The partition ${PART} does not exist. Failure in bsdlabel?"
143    fi 
144     
145    PARTFS="`cat ${PARTDIR}/${PART} | cut -d ':' -f 1`"
146    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d ':' -f 2`"
147    PARTENC="`cat ${PARTDIR}/${PART} | cut -d ':' -f 3`"
148
149    if [ "${PARTENC}" = "ON" ]
150    then
151      EXT=".eli"
152    else
153      EXT=""
154    fi
155
156    # Check if we've found "/" again, don't need to mount it twice
157    echo "$PARTMNT" | grep "/," >/dev/null
158    if [ "$?" != "0" -a "$PARTMNT" != "/" ]
159    then
160       case ${PARTFS} in
161         UFS) mount_partition ${PART}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
162         UFS+S) mount_partition ${PART}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
163         UFS+SUJ) mount_partition ${PART}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
164         UFS+J) mount_partition ${PART}${EXT}.journal ${PARTFS} ${PARTMNT} "async,noatime" ;;
165         ZFS) mount_partition ${PART} ${PARTFS} ${PARTMNT} ;;
166         SWAP)
167		   # Lets enable this swap now
168           if [ "$PARTENC" = "ON" ]
169           then
170             echo_log "Enabling encrypted swap on /dev/${PART}"
171             rc_halt "geli onetime -d -e 3des ${PART}"
172             sleep 5
173             rc_halt "swapon /dev/${PART}.eli"
174           else
175             echo_log "swapon ${PART}"
176             sleep 5
177             rc_halt "swapon /dev/${PART}"
178            fi
179            ;;
180         IMAGE)
181           if [ ! -d "${PARTMNT}" ]
182           then
183             mkdir -p "${PARTMNT}" 
184           fi 
185           mount_partition ${PART} ${PARTFS} ${PARTMNT}
186           ;;
187         *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
188      esac
189    fi
190  done
191};
192