1219888Sed#!/bin/sh
2219888Sed#-
3219888Sed# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4219888Sed#
5219888Sed# Redistribution and use in source and binary forms, with or without
6219888Sed# modification, are permitted provided that the following conditions
7219888Sed# are met:
8219888Sed# 1. Redistributions of source code must retain the above copyright
9219888Sed#    notice, this list of conditions and the following disclaimer.
10219888Sed# 2. Redistributions in binary form must reproduce the above copyright
11219888Sed#    notice, this list of conditions and the following disclaimer in the
12219888Sed#    documentation and/or other materials provided with the distribution.
13219888Sed#
14219888Sed# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15219888Sed# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16219888Sed# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17219888Sed# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18219888Sed# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19219888Sed# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20219888Sed# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21219888Sed# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22219888Sed# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23219888Sed# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24219888Sed# SUCH DAMAGE.
25219888Sed#
26219888Sed# $FreeBSD$
27219888Sed
28219888Sed# Functions related to disk operations using newfs
29219888Sed
30219888Sed
31219888Sed# Function which performs the ZFS magic
32219888Sedsetup_zfs_filesystem()
33219888Sed{
34219888Sed  PART="$1"
35219888Sed  PARTFS="$2"
36219888Sed  PARTMNT="$3"
37219888Sed  EXT="$4"
38219888Sed  PARTGEOM="$5"
39219888Sed  ZPOOLOPTS="$6"
40219888Sed  ROOTSLICE="`echo ${PART} | rev | cut -b 2- | rev`"
41219888Sed  ZPOOLNAME=$(get_zpool_name "${PART}")
42219888Sed
43219888Sed  # Sleep a few moments, let the disk catch its breath
44267978Smarius  sleep 5
45267978Smarius  sync
46267978Smarius
47219888Sed  # Check if we have multiple zfs mounts specified
48219888Sed  for i in `echo ${PARTMNT} | sed 's|,| |g'`
49219888Sed  do
50219888Sed    # Check if we ended up with needing a zfs bootable partition
51219888Sed    if [ "${i}" = "/" -o "${i}" = "/boot" ]
52219888Sed    then
53219888Sed      if [ "$HAVEBOOT" = "YES" ] ; then continue ; fi
54219888Sed      if [ "${PARTGEOM}" = "MBR" ] ; then
55219888Sed        # Lets stamp the proper ZFS boot loader
56219888Sed        echo_log "Setting up ZFS boot loader support" 
57219888Sed        rc_halt "dd if=/boot/zfsboot of=${ROOTSLICE} count=1"
58219888Sed        rc_halt "dd if=/boot/zfsboot of=${PART}${EXT} skip=1 seek=1024"
59219888Sed      fi
60219888Sed    fi
61219888Sed  done 
62219888Sed
63219888Sed  # Check if we have some custom zpool arguments and use them if so
64219888Sed  if [ ! -z "${ZPOOLOPTS}" ] ; then
65219888Sed    # Sort through devices and run gnop on them
66219888Sed    local gnopDev=""
67219888Sed    local newOpts=""
68259667Sed    for i in $ZPOOLOPTS
69219888Sed    do
70219888Sed       echo "$i" | grep -q '/dev/'
71219888Sed       if [ $? -eq 0 ] ; then
72219888Sed          rc_halt "gnop create -S 4096 ${i}"
73219888Sed          gnopDev="$gnopDev $i"
74219888Sed          newOpts="$newOpts ${i}.nop"
75259667Sed       else
76219888Sed          newOpts="$newOpts $i"
77267978Smarius       fi
78219888Sed    done
79267978Smarius    
80267978Smarius    echo_log "Creating zpool ${ZPOOLNAME} with $newOpts"
81219888Sed    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${newOpts}"
82267978Smarius
83267978Smarius    # Export the pool
84219888Sed    rc_halt "zpool export ${ZPOOLNAME}"
85219888Sed
86267978Smarius    # Destroy the gnop devices
87267978Smarius    for i in $gnopDev
88267978Smarius    do
89267978Smarius       rc_halt "gnop destroy ${i}.nop"
90267978Smarius    done
91267978Smarius
92267978Smarius    # And lastly re-import the pool
93267978Smarius    rc_halt "zpool import ${ZPOOLNAME}"
94267978Smarius  else
95267978Smarius    # Lets do our pseudo-4k drive
96267978Smarius    rc_halt "gnop create -S 4096 ${PART}${EXT}"
97267978Smarius
98267978Smarius    # No zpool options, create pool on single device
99267978Smarius    echo_log "Creating zpool ${ZPOOLNAME} on ${PART}${EXT}"
100267978Smarius    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${PART}${EXT}.nop"
101267978Smarius
102267978Smarius    # Finish up the gnop 4k trickery
103267978Smarius    rc_halt "zpool export ${ZPOOLNAME}"
104267978Smarius    rc_halt "gnop destroy ${PART}${EXT}.nop"
105267978Smarius    rc_halt "zpool import ${ZPOOLNAME}"
106267978Smarius  fi
107267978Smarius
108267978Smarius  # Disable atime for this zfs partition, speed increase
109267978Smarius  rc_nohalt "zfs set atime=off ${ZPOOLNAME}"
110267978Smarius
111267978Smarius
112267978Smarius
113267978Smarius};
114267978Smarius
115267978Smarius# Runs newfs on all the partiions which we've setup with bsdlabel
116267978Smariussetup_filesystems()
117267978Smarius{
118267978Smarius
119267978Smarius  # Create the keydir
120267978Smarius  rm -rf ${GELIKEYDIR} >/dev/null 2>/dev/null
121267978Smarius  mkdir ${GELIKEYDIR}
122267978Smarius
123267978Smarius  # Lets go ahead and read through the saved partitions we created, and determine if we need to run
124267978Smarius  # newfs on any of them
125267978Smarius  for PART in `ls ${PARTDIR}`
126267978Smarius  do
127267978Smarius    PARTDEV="`echo $PART | sed 's|-|/|g'`"
128267978Smarius    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
129267978Smarius    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
130267978Smarius    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
131267978Smarius    PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d '#' -f 4`"
132267978Smarius    PARTGEOM="`cat ${PARTDIR}/${PART} | cut -d '#' -f 5`"
133267978Smarius    PARTXTRAOPTS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 6`"
134267978Smarius    PARTIMAGE="`cat ${PARTDIR}/${PART} | cut -d '#' -f 7`"
135267978Smarius
136267978Smarius    if [ ! -e "${PARTDEV}" ] ; then
137267978Smarius      exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
138267978Smarius    fi 
139267978Smarius
140267978Smarius    # Make sure journaling isn't enabled on this device
141267978Smarius    if [ -e "${PARTDEV}.journal" ]
142219888Sed    then
143219888Sed      rc_nohalt "gjournal stop -f ${PARTDEV}.journal"
144219888Sed      rc_nohalt "gjournal clear ${PARTDEV}"
145219888Sed    fi
146219888Sed
147219888Sed    # Setup encryption if necessary
148219888Sed    if [ "${PARTENC}" = "ON" -a "${PARTFS}" != "SWAP" ]
149219888Sed    then
150219888Sed      echo_log "Creating geli provider for ${PARTDEV}"
151219888Sed
152219888Sed      if [ -e "${PARTDIR}-enc/${PART}-encpass" ] ; then
153219888Sed	# Using a passphrase
154219888Sed        rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
155219888Sed        rc_halt "geli init -J ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
156219888Sed        rc_halt "geli attach -j ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
157219888Sed      else
158270705Sdumbbell	# No Encryption password, use key file
159270705Sdumbbell        rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
160270705Sdumbbell        rc_halt "geli init -b -s 4096 -P -K ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
161219888Sed        rc_halt "geli attach -p -k ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
162219888Sed
163219888Sed      fi
164259777Sray
165259777Sray      EXT=".eli"
166219888Sed    else
167219888Sed      # No Encryption
168219888Sed      EXT=""
169219888Sed    fi
170219888Sed
171219888Sed    case ${PARTFS} in
172219888Sed      UFS)
173219888Sed        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
174219888Sed        sleep 2
175219888Sed        rc_halt "newfs -t ${PARTXTRAOPTS} ${PARTDEV}${EXT}"
176219888Sed        sleep 2
177219888Sed        rc_halt "sync"
178219888Sed        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
179219888Sed        rc_halt "sync"
180259777Sray
181270705Sdumbbell        # Set flag that we've found a boot partition
182270705Sdumbbell        if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
183270705Sdumbbell		  HAVEBOOT="YES"
184270705Sdumbbell        fi
185219888Sed        sleep 2
186219888Sed        ;;
187219888Sed
188259777Sray      UFS+S)
189219888Sed        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
190219888Sed        sleep 2
191219888Sed        rc_halt "newfs -t ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
192219888Sed        sleep 2
193219888Sed        rc_halt "sync"
194219888Sed        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
195219888Sed        rc_halt "sync"
196219888Sed	    # Set flag that we've found a boot partition
197219888Sed	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
198219888Sed          HAVEBOOT="YES"
199219888Sed        fi
200219888Sed        sleep 2
201219888Sed        ;;
202219888Sed
203256143Sray      UFS+SUJ)
204219888Sed        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
205219888Sed        sleep 2
206219888Sed        rc_halt "newfs -t ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
207219888Sed        sleep 2
208219888Sed        rc_halt "sync"
209219888Sed        rc_halt "tunefs -j enable ${PARTDEV}${EXT}"
210273932Sdumbbell        sleep 2
211256897Sray        rc_halt "sync"
212267978Smarius        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
213219888Sed        rc_halt "sync"
214219888Sed	    # Set flag that we've found a boot partition
215219888Sed	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
216219888Sed          HAVEBOOT="YES"
217219888Sed        fi
218219888Sed        sleep 2
219256143Sray        ;;
220256143Sray
221219888Sed
222256143Sray      UFS+J)
223219888Sed        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
224219888Sed        sleep 2
225219888Sed        rc_halt "newfs ${PARTDEV}${EXT}"
226219888Sed        sleep 2
227219888Sed        rc_halt "gjournal label -f ${PARTDEV}${EXT}"
228219888Sed        sleep 2
229219888Sed        rc_halt "newfs ${PARTXTRAOPTS} -O 2 -J ${PARTDEV}${EXT}.journal"
230256143Sray        sleep 2
231219888Sed        rc_halt "sync"
232219888Sed        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}.journal"
233219888Sed        rc_halt "sync"
234219888Sed	    # Set flag that we've found a boot partition
235	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
236          HAVEBOOT="YES"
237  	    fi
238        sleep 2
239        ;;
240
241      ZFS)
242        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}" 
243        setup_zfs_filesystem "${PARTDEV}" "${PARTFS}" "${PARTMNT}" "${EXT}" "${PARTGEOM}" "${PARTXTRAOPTS}"
244        ;;
245
246      SWAP)
247        rc_halt "sync"
248        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}" 
249        rc_halt "sync"
250        sleep 2
251        ;;
252
253      IMAGE)
254        write_image "${PARTIMAGE}" "${PARTDEV}"
255        sleep 2
256        ;; 
257
258      *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
259    esac
260
261  done
262};
263