3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 # Copyright (c) 2010 iXsystems, Inc. All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
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
30 # Functions related to disk operations using newfs
33 # Function which performs the ZFS magic
34 setup_zfs_filesystem()
42 ROOTSLICE="`echo ${PART} | rev | cut -b 2- | rev`"
43 ZPOOLNAME=$(get_zpool_name "${PART}")
45 # Sleep a few moments, let the disk catch its breath
49 # Check if we have multiple zfs mounts specified
50 for i in `echo ${PARTMNT} | sed 's|,| |g'`
52 # Check if we ended up with needing a zfs bootable partition
53 if [ "${i}" = "/" -o "${i}" = "/boot" ]
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"
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
72 echo "$i" | grep -q '/dev/'
73 if [ $? -eq 0 ] ; then
74 rc_halt "gnop create -S 4096 ${i}"
76 newOpts="$newOpts ${i}.nop"
82 echo_log "Creating zpool ${ZPOOLNAME} with $newOpts"
83 rc_halt "zpool create -m none -f ${ZPOOLNAME} ${newOpts}"
86 rc_halt "zpool export ${ZPOOLNAME}"
88 # Destroy the gnop devices
91 rc_halt "gnop destroy ${i}.nop"
94 # And lastly re-import the pool
95 rc_halt "zpool import ${ZPOOLNAME}"
97 # Lets do our pseudo-4k drive
98 rc_halt "gnop create -S 4096 ${PART}${EXT}"
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"
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}"
110 # Disable atime for this zfs partition, speed increase
111 rc_nohalt "zfs set atime=off ${ZPOOLNAME}"
117 # Runs newfs on all the partiions which we've setup with bsdlabel
122 rm -rf ${GELIKEYDIR} >/dev/null 2>/dev/null
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}`
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`"
138 if [ ! -e "${PARTDEV}" ] ; then
139 exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
142 # Make sure journaling isn't enabled on this device
143 if [ -e "${PARTDEV}.journal" ]
145 rc_nohalt "gjournal stop -f ${PARTDEV}.journal"
146 rc_nohalt "gjournal clear ${PARTDEV}"
149 # Setup encryption if necessary
150 if [ "${PARTENC}" = "ON" -a "${PARTFS}" != "SWAP" ]
152 echo_log "Creating geli provider for ${PARTDEV}"
154 if [ -e "${PARTDIR}-enc/${PART}-encpass" ] ; then
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}"
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}"
175 echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
177 rc_halt "newfs -t ${PARTXTRAOPTS} ${PARTDEV}${EXT}"
180 rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
183 # Set flag that we've found a boot partition
184 if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
191 echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
193 rc_halt "newfs -t ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
196 rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
198 # Set flag that we've found a boot partition
199 if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
206 echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
208 rc_halt "newfs -t ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
211 rc_halt "tunefs -j enable ${PARTDEV}${EXT}"
214 rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
216 # Set flag that we've found a boot partition
217 if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
225 echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
227 rc_halt "newfs ${PARTDEV}${EXT}"
229 rc_halt "gjournal label -f ${PARTDEV}${EXT}"
231 rc_halt "newfs ${PARTXTRAOPTS} -O 2 -J ${PARTDEV}${EXT}.journal"
234 rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}.journal"
236 # Set flag that we've found a boot partition
237 if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
244 echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
245 setup_zfs_filesystem "${PARTDEV}" "${PARTFS}" "${PARTMNT}" "${EXT}" "${PARTGEOM}" "${PARTXTRAOPTS}"
250 rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
256 write_image "${PARTIMAGE}" "${PARTDEV}"
260 *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;