1#!/bin/sh
2#-
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4#
5# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28# $FreeBSD: stable/12/usr.sbin/pc-sysinstall/backend/functions-newfs.sh 326275 2017-11-27 15:28:26Z pfg $
29
30# Functions related to disk operations using newfs
31
32
33# Function which performs the ZFS magic
34setup_zfs_filesystem()
35{
36  PART="$1"
37  PARTFS="$2"
38  PARTMNT="$3"
39  EXT="$4"
40  PARTGEOM="$5"
41  ZPOOLOPTS="$6"
42  ROOTSLICE="`echo ${PART} | rev | cut -b 2- | rev`"
43  ZPOOLNAME=$(get_zpool_name "${PART}")
44
45  # Sleep a few moments, let the disk catch its breath
46  sleep 5
47  sync
48
49  # Check if we have multiple zfs mounts specified
50  for i in `echo ${PARTMNT} | sed 's|,| |g'`
51  do
52    # Check if we ended up with needing a zfs bootable partition
53    if [ "${i}" = "/" -o "${i}" = "/boot" ]
54    then
55      if [ "$HAVEBOOT" = "YES" ] ; then continue ; fi
56      if [ "${PARTGEOM}" = "MBR" ] ; then
57        # Lets stamp the proper ZFS boot loader
58        echo_log "Setting up ZFS boot loader support"
59        rc_halt "dd if=/boot/zfsboot of=${ROOTSLICE} count=1"
60        rc_halt "dd if=/boot/zfsboot of=${PART}${EXT} skip=1 seek=1024"
61      fi
62    fi
63  done
64
65  # Check if we have some custom zpool arguments and use them if so
66  if [ ! -z "${ZPOOLOPTS}" ] ; then
67    # Sort through devices and run gnop on them
68    local gnopDev=""
69    local newOpts=""
70    for i in $ZPOOLOPTS
71    do
72       echo "$i" | grep -q '/dev/'
73       if [ $? -eq 0 ] ; then
74          rc_halt "gnop create -S 4096 ${i}"
75          gnopDev="$gnopDev $i"
76          newOpts="$newOpts ${i}.nop"
77       else
78          newOpts="$newOpts $i"
79       fi
80    done
81
82    echo_log "Creating zpool ${ZPOOLNAME} with $newOpts"
83    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${newOpts}"
84
85    # Export the pool
86    rc_halt "zpool export ${ZPOOLNAME}"
87
88    # Destroy the gnop devices
89    for i in $gnopDev
90    do
91       rc_halt "gnop destroy ${i}.nop"
92    done
93
94    # And lastly re-import the pool
95    rc_halt "zpool import ${ZPOOLNAME}"
96  else
97    # Lets do our pseudo-4k drive
98    rc_halt "gnop create -S 4096 ${PART}${EXT}"
99
100    # No zpool options, create pool on single device
101    echo_log "Creating zpool ${ZPOOLNAME} on ${PART}${EXT}"
102    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${PART}${EXT}.nop"
103
104    # Finish up the gnop 4k trickery
105    rc_halt "zpool export ${ZPOOLNAME}"
106    rc_halt "gnop destroy ${PART}${EXT}.nop"
107    rc_halt "zpool import ${ZPOOLNAME}"
108  fi
109
110  # Disable atime for this zfs partition, speed increase
111  rc_nohalt "zfs set atime=off ${ZPOOLNAME}"
112
113
114
115};
116
117# Runs newfs on all the partiions which we've setup with bsdlabel
118setup_filesystems()
119{
120
121  # Create the keydir
122  rm -rf ${GELIKEYDIR} >/dev/null 2>/dev/null
123  mkdir ${GELIKEYDIR}
124
125  # Lets go ahead and read through the saved partitions we created, and determine if we need to run
126  # newfs on any of them
127  for PART in `ls ${PARTDIR}`
128  do
129    PARTDEV="`echo $PART | sed 's|-|/|g'`"
130    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
131    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
132    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
133    PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d '#' -f 4`"
134    PARTGEOM="`cat ${PARTDIR}/${PART} | cut -d '#' -f 5`"
135    PARTXTRAOPTS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 6`"
136    PARTIMAGE="`cat ${PARTDIR}/${PART} | cut -d '#' -f 7`"
137
138    if [ ! -e "${PARTDEV}" ] ; then
139      exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
140    fi
141
142    # Make sure journaling isn't enabled on this device
143    if [ -e "${PARTDEV}.journal" ]
144    then
145      rc_nohalt "gjournal stop -f ${PARTDEV}.journal"
146      rc_nohalt "gjournal clear ${PARTDEV}"
147    fi
148
149    # Setup encryption if necessary
150    if [ "${PARTENC}" = "ON" -a "${PARTFS}" != "SWAP" ]
151    then
152      echo_log "Creating geli provider for ${PARTDEV}"
153
154      if [ -e "${PARTDIR}-enc/${PART}-encpass" ] ; then
155	# Using a passphrase
156        rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
157        rc_halt "geli init -J ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
158        rc_halt "geli attach -j ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
159      else
160	# No Encryption password, use key file
161        rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
162        rc_halt "geli init -b -s 4096 -P -K ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
163        rc_halt "geli attach -p -k ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
164
165      fi
166
167      EXT=".eli"
168    else
169      # No Encryption
170      EXT=""
171    fi
172
173    case ${PARTFS} in
174      UFS)
175        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
176        sleep 2
177        rc_halt "newfs -t ${PARTXTRAOPTS} ${PARTDEV}${EXT}"
178        sleep 2
179        rc_halt "sync"
180        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
181        rc_halt "sync"
182
183        # Set flag that we've found a boot partition
184        if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
185		  HAVEBOOT="YES"
186        fi
187        sleep 2
188        ;;
189
190      UFS+S)
191        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
192        sleep 2
193        rc_halt "newfs -t ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
194        sleep 2
195        rc_halt "sync"
196        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
197        rc_halt "sync"
198	    # Set flag that we've found a boot partition
199	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
200          HAVEBOOT="YES"
201        fi
202        sleep 2
203        ;;
204
205      UFS+SUJ)
206        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
207        sleep 2
208        rc_halt "newfs -t ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
209        sleep 2
210        rc_halt "sync"
211        rc_halt "tunefs -j enable ${PARTDEV}${EXT}"
212        sleep 2
213        rc_halt "sync"
214        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
215        rc_halt "sync"
216	    # Set flag that we've found a boot partition
217	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
218          HAVEBOOT="YES"
219        fi
220        sleep 2
221        ;;
222
223
224      UFS+J)
225        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
226        sleep 2
227        rc_halt "newfs ${PARTDEV}${EXT}"
228        sleep 2
229        rc_halt "gjournal label -f ${PARTDEV}${EXT}"
230        sleep 2
231        rc_halt "newfs ${PARTXTRAOPTS} -O 2 -J ${PARTDEV}${EXT}.journal"
232        sleep 2
233        rc_halt "sync"
234        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}.journal"
235        rc_halt "sync"
236	    # Set flag that we've found a boot partition
237	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
238          HAVEBOOT="YES"
239  	    fi
240        sleep 2
241        ;;
242
243      ZFS)
244        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
245        setup_zfs_filesystem "${PARTDEV}" "${PARTFS}" "${PARTMNT}" "${EXT}" "${PARTGEOM}" "${PARTXTRAOPTS}"
246        ;;
247
248      SWAP)
249        rc_halt "sync"
250        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
251        rc_halt "sync"
252        sleep 2
253        ;;
254
255      IMAGE)
256        write_image "${PARTIMAGE}" "${PARTDEV}"
257        sleep 2
258        ;;
259
260      *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
261    esac
262
263  done
264};
265