functions-newfs.sh revision 233224
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: stable/9/usr.sbin/pc-sysinstall/backend/functions-newfs.sh 233224 2012-03-19 23:20:52Z jpaetzel $
27
28# Functions related to disk operations using newfs
29
30
31# Function which performs the ZFS magic
32setup_zfs_filesystem()
33{
34  PART="$1"
35  PARTFS="$2"
36  PARTMNT="$3"
37  EXT="$4"
38  PARTGEOM="$5"
39  ZPOOLOPTS="$6"
40  ROOTSLICE="`echo ${PART} | rev | cut -b 2- | rev`"
41  ZPOOLNAME=$(get_zpool_name "${PART}")
42
43  # Sleep a few moments, let the disk catch its breath
44  sleep 5
45  sync
46
47  # Check if we have some custom zpool arguments and use them if so
48  if [ ! -z "${ZPOOLOPTS}" ] ; then
49    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${ZPOOLOPTS}"
50  else
51    # No zpool options, create pool on single device
52    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${PART}${EXT}"
53  fi
54
55  # Disable atime for this zfs partition, speed increase
56  rc_nohalt "zfs set atime=off ${ZPOOLNAME}"
57
58  # Check if we have multiple zfs mounts specified
59  for i in `echo ${PARTMNT} | sed 's|,| |g'`
60  do
61    # Check if we ended up with needing a zfs bootable partition
62    if [ "${i}" = "/" -o "${i}" = "/boot" ]
63    then
64      if [ "$HAVEBOOT" = "YES" ] ; then continue ; fi
65      if [ "${PARTGEOM}" = "MBR" ]
66      then
67        # Lets stamp the proper ZFS boot loader
68        echo_log "Setting up ZFS boot loader support" 
69        rc_halt "zpool set bootfs=${ZPOOLNAME} ${ZPOOLNAME}"
70        rc_halt "zpool export ${ZPOOLNAME}"
71        rc_halt "dd if=/boot/zfsboot of=${ROOTSLICE} count=1"
72        rc_halt "dd if=/boot/zfsboot of=${PART}${EXT} skip=1 seek=1024"
73        rc_halt "zpool import ${ZPOOLNAME}"
74      fi
75    fi
76  done 
77
78};
79
80# Runs newfs on all the partiions which we've setup with bsdlabel
81setup_filesystems()
82{
83
84  # Create the keydir
85  rm -rf ${GELIKEYDIR} >/dev/null 2>/dev/null
86  mkdir ${GELIKEYDIR}
87
88  # Lets go ahead and read through the saved partitions we created, and determine if we need to run
89  # newfs on any of them
90  for PART in `ls ${PARTDIR}`
91  do
92    PARTDEV="`echo $PART | sed 's|-|/|g'`"
93    if [ ! -e "${PARTDEV}" ]
94    then
95      exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
96    fi 
97     
98    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
99    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
100    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
101    PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d '#' -f 4`"
102    PARTGEOM="`cat ${PARTDIR}/${PART} | cut -d '#' -f 5`"
103    PARTXTRAOPTS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 6`"
104    PARTIMAGE="`cat ${PARTDIR}/${PART} | cut -d '#' -f 7`"
105
106    # Make sure journaling isn't enabled on this device
107    if [ -e "${PARTDEV}.journal" ]
108    then
109      rc_nohalt "gjournal stop -f ${PARTDEV}.journal"
110      rc_nohalt "gjournal clear ${PARTDEV}"
111    fi
112
113    # Setup encryption if necessary
114    if [ "${PARTENC}" = "ON" -a "${PARTFS}" != "SWAP" ]
115    then
116      echo_log "Creating geli provider for ${PARTDEV}"
117
118      if [ -e "${PARTDIR}-enc/${PART}-encpass" ] ; then
119	# Using a passphrase
120        rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
121        rc_halt "geli init -J ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
122        rc_halt "geli attach -j ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
123      else
124	# No Encryption password, use key file
125        rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
126        rc_halt "geli init -b -s 4096 -P -K ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
127        rc_halt "geli attach -p -k ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
128
129      fi
130
131      EXT=".eli"
132    else
133      # No Encryption
134      EXT=""
135    fi
136
137    case ${PARTFS} in
138      UFS)
139        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
140        sleep 2
141        rc_halt "newfs ${PARTXTRAOPTS} ${PARTDEV}${EXT}"
142        sleep 2
143        rc_halt "sync"
144        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
145        rc_halt "sync"
146
147        # Set flag that we've found a boot partition
148        if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
149		  HAVEBOOT="YES"
150        fi
151        sleep 2
152        ;;
153
154      UFS+S)
155        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
156        sleep 2
157        rc_halt "newfs ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
158        sleep 2
159        rc_halt "sync"
160        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
161        rc_halt "sync"
162	    # Set flag that we've found a boot partition
163	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
164          HAVEBOOT="YES"
165        fi
166        sleep 2
167        ;;
168
169      UFS+SUJ)
170        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
171        sleep 2
172        rc_halt "newfs ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
173        sleep 2
174        rc_halt "sync"
175        rc_halt "tunefs -j enable ${PARTDEV}${EXT}"
176        sleep 2
177        rc_halt "sync"
178        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
179        rc_halt "sync"
180	    # Set flag that we've found a boot partition
181	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
182          HAVEBOOT="YES"
183        fi
184        sleep 2
185        ;;
186
187
188      UFS+J)
189        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
190        sleep 2
191        rc_halt "newfs ${PARTDEV}${EXT}"
192        sleep 2
193        rc_halt "gjournal label -f ${PARTDEV}${EXT}"
194        sleep 2
195        rc_halt "newfs ${PARTXTRAOPTS} -O 2 -J ${PARTDEV}${EXT}.journal"
196        sleep 2
197        rc_halt "sync"
198        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}.journal"
199        rc_halt "sync"
200	    # Set flag that we've found a boot partition
201	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
202          HAVEBOOT="YES"
203  	    fi
204        sleep 2
205        ;;
206
207      ZFS)
208        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}" 
209        setup_zfs_filesystem "${PARTDEV}" "${PARTFS}" "${PARTMNT}" "${EXT}" "${PARTGEOM}" "${PARTXTRAOPTS}"
210        ;;
211
212      SWAP)
213        rc_halt "sync"
214        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}" 
215        rc_halt "sync"
216        sleep 2
217        ;;
218
219      IMAGE)
220        write_image "${PARTIMAGE}" "${PARTDEV}"
221        sleep 2
222        ;; 
223
224      *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
225    esac
226
227  done
228};
229