functions-mountdisk.sh revision 213647
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 213647 2010-10-09 07:45:24Z 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        *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
131      esac
132    fi
133  done
134
135  # Now that we've mounted "/" lets do any other remaining mount-points
136  ##################################################################
137  for PART in `ls ${PARTDIR}`
138  do
139    if [ ! -e "/dev/${PART}" ]
140    then
141      exit_err "ERROR: The partition ${PART} does not exist. Failure in bsdlabel?"
142    fi 
143     
144    PARTFS="`cat ${PARTDIR}/${PART} | cut -d ':' -f 1`"
145    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d ':' -f 2`"
146    PARTENC="`cat ${PARTDIR}/${PART} | cut -d ':' -f 3`"
147
148    if [ "${PARTENC}" = "ON" ]
149    then
150      EXT=".eli"
151    else
152      EXT=""
153    fi
154
155    # Check if we've found "/" again, don't need to mount it twice
156    echo "$PARTMNT" | grep "/," >/dev/null
157    if [ "$?" != "0" -a "$PARTMNT" != "/" ]
158    then
159       case ${PARTFS} in
160         UFS) mount_partition ${PART}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
161         UFS+S) mount_partition ${PART}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
162         UFS+SUJ) mount_partition ${PART}${EXT} ${PARTFS} ${PARTMNT} "noatime" ;;
163         UFS+J) mount_partition ${PART}${EXT}.journal ${PARTFS} ${PARTMNT} "async,noatime" ;;
164         ZFS) mount_partition ${PART} ${PARTFS} ${PARTMNT} ;;
165         SWAP)
166		   # Lets enable this swap now
167           if [ "$PARTENC" = "ON" ]
168           then
169             echo_log "Enabling encrypted swap on /dev/${PART}"
170             rc_halt "geli onetime -d -e 3des ${PART}"
171             sleep 5
172             rc_halt "swapon /dev/${PART}.eli"
173           else
174             echo_log "swapon ${PART}"
175             sleep 5
176             rc_halt "swapon /dev/${PART}"
177            fi
178            ;;
179          *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
180      esac
181    fi
182  done
183};
184