120305Sjoerg#!/bin/sh
220305Sjoerg#-
379755Sdd# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
420269Sjoerg#
520269Sjoerg# Redistribution and use in source and binary forms, with or without
620269Sjoerg# modification, are permitted provided that the following conditions
720269Sjoerg# are met:
820269Sjoerg# 1. Redistributions of source code must retain the above copyright
920269Sjoerg#    notice, this list of conditions and the following disclaimer.
1020269Sjoerg# 2. Redistributions in binary form must reproduce the above copyright
1120269Sjoerg#    notice, this list of conditions and the following disclaimer in the
1279755Sdd#    documentation and/or other materials provided with the distribution.
1320305Sjoerg#
1420269Sjoerg# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1520269Sjoerg# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620305Sjoerg# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720269Sjoerg# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1820269Sjoerg# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920269Sjoerg# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020269Sjoerg# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120269Sjoerg# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220269Sjoerg# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320269Sjoerg# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420269Sjoerg# SUCH DAMAGE.
2550479Speter#
2620269Sjoerg# $FreeBSD: releng/10.3/usr.sbin/pc-sysinstall/backend/functions-newfs.sh 247735 2013-03-03 23:07:27Z jpaetzel $
27298525Swblock
2820253Sjoerg# Functions related to disk operations using newfs
2920253Sjoerg
3020253Sjoerg
3120253Sjoerg# Function which performs the ZFS magic
3220269Sjoergsetup_zfs_filesystem()
3320253Sjoerg{
3468965Sru  PART="$1"
35283962Sgjb  PARTFS="$2"
3644229Sdavidn  PARTMNT="$3"
3720253Sjoerg  EXT="$4"
38286258Sdteske  PARTGEOM="$5"
3920253Sjoerg  ZPOOLOPTS="$6"
4020253Sjoerg  ROOTSLICE="`echo ${PART} | rev | cut -b 2- | rev`"
4120253Sjoerg  ZPOOLNAME=$(get_zpool_name "${PART}")
4220253Sjoerg
4320253Sjoerg  # Sleep a few moments, let the disk catch its breath
4420253Sjoerg  sleep 5
4520253Sjoerg  sync
4620253Sjoerg
4720253Sjoerg  # Check if we have multiple zfs mounts specified
48167919Sle  for i in `echo ${PARTMNT} | sed 's|,| |g'`
4920253Sjoerg  do
5098854Ssada    # Check if we ended up with needing a zfs bootable partition
5120253Sjoerg    if [ "${i}" = "/" -o "${i}" = "/boot" ]
5220253Sjoerg    then
5320253Sjoerg      if [ "$HAVEBOOT" = "YES" ] ; then continue ; fi
54124382Siedowse      if [ "${PARTGEOM}" = "MBR" ] ; then
5520269Sjoerg        # Lets stamp the proper ZFS boot loader
5620269Sjoerg        echo_log "Setting up ZFS boot loader support" 
5721330Sdavidn        rc_halt "dd if=/boot/zfsboot of=${ROOTSLICE} count=1"
5868965Sru        rc_halt "dd if=/boot/zfsboot of=${PART}${EXT} skip=1 seek=1024"
59283962Sgjb      fi
6044229Sdavidn    fi
6120253Sjoerg  done 
6221330Sdavidn
6320253Sjoerg  # Check if we have some custom zpool arguments and use them if so
6420253Sjoerg  if [ ! -z "${ZPOOLOPTS}" ] ; then
6520253Sjoerg    # Sort through devices and run gnop on them
6620253Sjoerg    local gnopDev=""
6720253Sjoerg    local newOpts=""
6820253Sjoerg    for i in $ZPOOLOPTS
6920253Sjoerg    do
7020253Sjoerg       echo "$i" | grep -q '/dev/'
71168044Sle       if [ $? -eq 0 ] ; then
7271898Sru          rc_halt "gnop create -S 4096 ${i}"
7371898Sru          gnopDev="$gnopDev $i"
7420253Sjoerg          newOpts="$newOpts ${i}.nop"
7520253Sjoerg       else
7621330Sdavidn          newOpts="$newOpts $i"
7768965Sru       fi
78283962Sgjb    done
7944229Sdavidn    
8020253Sjoerg    echo_log "Creating zpool ${ZPOOLNAME} with $newOpts"
81286258Sdteske    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${newOpts}"
8220253Sjoerg
8321330Sdavidn    # Export the pool
8468965Sru    rc_halt "zpool export ${ZPOOLNAME}"
85283962Sgjb
8644229Sdavidn    # Destroy the gnop devices
8720253Sjoerg    for i in $gnopDev
88286258Sdteske    do
8920253Sjoerg       rc_halt "gnop destroy ${i}.nop"
9020253Sjoerg    done
9120253Sjoerg
9220253Sjoerg    # And lastly re-import the pool
9320253Sjoerg    rc_halt "zpool import ${ZPOOLNAME}"
9420253Sjoerg  else
9520253Sjoerg    # Lets do our pseudo-4k drive
9620253Sjoerg    rc_halt "gnop create -S 4096 ${PART}${EXT}"
97286258Sdteske
9820253Sjoerg    # No zpool options, create pool on single device
99167919Sle    echo_log "Creating zpool ${ZPOOLNAME} on ${PART}${EXT}"
10020253Sjoerg    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${PART}${EXT}.nop"
10120269Sjoerg
10220253Sjoerg    # Finish up the gnop 4k trickery
10320253Sjoerg    rc_halt "zpool export ${ZPOOLNAME}"
104124382Siedowse    rc_halt "gnop destroy ${PART}${EXT}.nop"
10520269Sjoerg    rc_halt "zpool import ${ZPOOLNAME}"
10620269Sjoerg  fi
10721330Sdavidn
10868965Sru  # Disable atime for this zfs partition, speed increase
109283962Sgjb  rc_nohalt "zfs set atime=off ${ZPOOLNAME}"
11044229Sdavidn
11120253Sjoerg
112286258Sdteske
11320253Sjoerg};
11420269Sjoerg
11593865Sdd# Runs newfs on all the partiions which we've setup with bsdlabel
11620253Sjoergsetup_filesystems()
11768965Sru{
118283962Sgjb
11944229Sdavidn  # Create the keydir
12020269Sjoerg  rm -rf ${GELIKEYDIR} >/dev/null 2>/dev/null
12120269Sjoerg  mkdir ${GELIKEYDIR}
122284149Sbapt
12368965Sru  # Lets go ahead and read through the saved partitions we created, and determine if we need to run
124283962Sgjb  # newfs on any of them
12544229Sdavidn  for PART in `ls ${PARTDIR}`
12620253Sjoerg  do
127286258Sdteske    PARTDEV="`echo $PART | sed 's|-|/|g'`"
12820253Sjoerg    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
12920253Sjoerg    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
13020269Sjoerg    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
13120253Sjoerg    PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d '#' -f 4`"
132124382Siedowse    PARTGEOM="`cat ${PARTDIR}/${PART} | cut -d '#' -f 5`"
13320269Sjoerg    PARTXTRAOPTS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 6`"
13420269Sjoerg    PARTIMAGE="`cat ${PARTDIR}/${PART} | cut -d '#' -f 7`"
13521330Sdavidn
13668965Sru    if [ ! -e "${PARTDEV}" ] ; then
137283962Sgjb      exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
13844229Sdavidn    fi 
13920253Sjoerg
140286258Sdteske    # Make sure journaling isn't enabled on this device
14121330Sdavidn    if [ -e "${PARTDEV}.journal" ]
14268965Sru    then
143283962Sgjb      rc_nohalt "gjournal stop -f ${PARTDEV}.journal"
14444229Sdavidn      rc_nohalt "gjournal clear ${PARTDEV}"
14520253Sjoerg    fi
146286258Sdteske
14720253Sjoerg    # Setup encryption if necessary
14820253Sjoerg    if [ "${PARTENC}" = "ON" -a "${PARTFS}" != "SWAP" ]
149286258Sdteske    then
15020269Sjoerg      echo_log "Creating geli provider for ${PARTDEV}"
15120269Sjoerg
152176474Sscf      if [ -e "${PARTDIR}-enc/${PART}-encpass" ] ; then
153124382Siedowse	# Using a passphrase
15420269Sjoerg        rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
15520269Sjoerg        rc_halt "geli init -J ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
15621330Sdavidn        rc_halt "geli attach -j ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
15768965Sru      else
158283962Sgjb	# No Encryption password, use key file
15944229Sdavidn        rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
16020253Sjoerg        rc_halt "geli init -b -s 4096 -P -K ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
161286258Sdteske        rc_halt "geli attach -p -k ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
16220253Sjoerg
16320269Sjoerg      fi
16420253Sjoerg
16568965Sru      EXT=".eli"
166283962Sgjb    else
16744229Sdavidn      # No Encryption
16820269Sjoerg      EXT=""
16920269Sjoerg    fi
170284149Sbapt
17174224Sdd    case ${PARTFS} in
172283962Sgjb      UFS)
17374224Sdd        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
17474224Sdd        sleep 2
175286258Sdteske        rc_halt "newfs -t ${PARTXTRAOPTS} ${PARTDEV}${EXT}"
17674224Sdd        sleep 2
17774224Sdd        rc_halt "sync"
17874224Sdd        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
179283962Sgjb        rc_halt "sync"
18074224Sdd
18174224Sdd        # Set flag that we've found a boot partition
182286258Sdteske        if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
18374224Sdd		  HAVEBOOT="YES"
18474224Sdd        fi
18520253Sjoerg        sleep 2
18699968Scharnier        ;;
18799968Scharnier
18899968Scharnier      UFS+S)
18930259Scharnier        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
19020253Sjoerg        sleep 2
19130259Scharnier        rc_halt "newfs -t ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
19223374Sdanny        sleep 2
19320253Sjoerg        rc_halt "sync"
19420253Sjoerg        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
19530259Scharnier        rc_halt "sync"
19699968Scharnier	    # Set flag that we've found a boot partition
19799968Scharnier	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
19899968Scharnier          HAVEBOOT="YES"
19999968Scharnier        fi
20099968Scharnier        sleep 2
20199968Scharnier        ;;
20299968Scharnier
20399968Scharnier      UFS+SUJ)
20499968Scharnier        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
20579755Sdd        sleep 2
20679755Sdd        rc_halt "newfs -t ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
20720265Smpp        sleep 2
20820265Smpp        rc_halt "sync"
20920253Sjoerg        rc_halt "tunefs -j enable ${PARTDEV}${EXT}"
21020253Sjoerg        sleep 2
21133354Ssteve        rc_halt "sync"
21279755Sdd        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
21333354Ssteve        rc_halt "sync"
21433354Ssteve	    # Set flag that we've found a boot partition
21520253Sjoerg	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
21620253Sjoerg          HAVEBOOT="YES"
21720253Sjoerg        fi
21833354Ssteve        sleep 2
21920253Sjoerg        ;;
22020253Sjoerg
22120269Sjoerg
22220269Sjoerg      UFS+J)
22320253Sjoerg        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
22433354Ssteve        sleep 2
22557695Ssheldonh        rc_halt "newfs ${PARTDEV}${EXT}"
22657695Ssheldonh        sleep 2
22779755Sdd        rc_halt "gjournal label -f ${PARTDEV}${EXT}"
22879755Sdd        sleep 2
22971898Sru        rc_halt "newfs ${PARTXTRAOPTS} -O 2 -J ${PARTDEV}${EXT}.journal"
23071898Sru        sleep 2
23133354Ssteve        rc_halt "sync"
23233354Ssteve        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}.journal"
23333354Ssteve        rc_halt "sync"
23430259Scharnier	    # Set flag that we've found a boot partition
23533354Ssteve	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
236286258Sdteske          HAVEBOOT="YES"
237286258Sdteske  	    fi
238286258Sdteske        sleep 2
23920253Sjoerg        ;;
24020253Sjoerg
24120253Sjoerg      ZFS)
24220269Sjoerg        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}" 
24320253Sjoerg        setup_zfs_filesystem "${PARTDEV}" "${PARTFS}" "${PARTMNT}" "${EXT}" "${PARTGEOM}" "${PARTXTRAOPTS}"
24499108Sdd        ;;
24520269Sjoerg
246283962Sgjb      SWAP)
247283962Sgjb        rc_halt "sync"
248283962Sgjb        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}" 
249283962Sgjb        rc_halt "sync"
250283962Sgjb        sleep 2
251283962Sgjb        ;;
25244229Sdavidn
253286258Sdteske      IMAGE)
254286258Sdteske        write_image "${PARTIMAGE}" "${PARTDEV}"
25544229Sdavidn        sleep 2
25644229Sdavidn        ;; 
257286258Sdteske
258286258Sdteske      *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
259286258Sdteske    esac
26044229Sdavidn
26144229Sdavidn  done
26244229Sdavidn};
26344229Sdavidn