3 # Copyright (c) 2010 iXsystems, Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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
28 # Functions related to disk operations using newfs
31 # Function which performs the ZFS magic
32 setup_zfs_filesystem()
40 ROOTSLICE="`echo ${PART} | rev | cut -b 2- | rev`"
41 ZPOOLNAME=$(get_zpool_name "${PART}")
43 # Sleep a few moments, let the disk catch its breath
47 # Check if we have multiple zfs mounts specified
48 for i in `echo ${PARTMNT} | sed 's|,| |g'`
50 # Check if we ended up with needing a zfs bootable partition
51 if [ "${i}" = "/" -o "${i}" = "/boot" ]
53 if [ "$HAVEBOOT" = "YES" ] ; then continue ; fi
54 if [ "${PARTGEOM}" = "MBR" ] ; then
55 # Lets stamp the proper ZFS boot loader
56 echo_log "Setting up ZFS boot loader support"
57 rc_halt "dd if=/boot/zfsboot of=${ROOTSLICE} count=1"
58 rc_halt "dd if=/boot/zfsboot of=${PART}${EXT} skip=1 seek=1024"
64 # Check if we have some custom zpool arguments and use them if so
65 if [ ! -z "${ZPOOLOPTS}" ] ; then
66 rc_halt "zpool create -m none -f ${ZPOOLNAME} ${ZPOOLOPTS}"
68 # No zpool options, create pool on single device
69 rc_halt "zpool create -m none -f ${ZPOOLNAME} ${PART}${EXT}"
72 # Disable atime for this zfs partition, speed increase
73 rc_nohalt "zfs set atime=off ${ZPOOLNAME}"
75 # Check if we need to set a bootable zpool
76 for i in `echo ${PARTMNT} | sed 's|,| |g'`
78 if [ "${i}" = "/" -o "${i}" = "/boot" ] ; then
79 if [ "$HAVEBOOT" = "YES" ] ; then continue ; fi
80 echo_log "Stamping zpool as bootfs"
81 rc_halt "zpool set bootfs=${ZPOOLNAME} ${ZPOOLNAME}"
87 # Runs newfs on all the partiions which we've setup with bsdlabel
92 rm -rf ${GELIKEYDIR} >/dev/null 2>/dev/null
95 # Lets go ahead and read through the saved partitions we created, and determine if we need to run
96 # newfs on any of them
97 for PART in `ls ${PARTDIR}`
99 PARTDEV="`echo $PART | sed 's|-|/|g'`"
100 PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
101 PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
102 PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
103 PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d '#' -f 4`"
104 PARTGEOM="`cat ${PARTDIR}/${PART} | cut -d '#' -f 5`"
105 PARTXTRAOPTS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 6`"
106 PARTIMAGE="`cat ${PARTDIR}/${PART} | cut -d '#' -f 7`"
108 if [ ! -e "${PARTDEV}" ] ; then
109 exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
112 # Make sure journaling isn't enabled on this device
113 if [ -e "${PARTDEV}.journal" ]
115 rc_nohalt "gjournal stop -f ${PARTDEV}.journal"
116 rc_nohalt "gjournal clear ${PARTDEV}"
119 # Setup encryption if necessary
120 if [ "${PARTENC}" = "ON" -a "${PARTFS}" != "SWAP" ]
122 echo_log "Creating geli provider for ${PARTDEV}"
124 if [ -e "${PARTDIR}-enc/${PART}-encpass" ] ; then
126 rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
127 rc_halt "geli init -J ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
128 rc_halt "geli attach -j ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
130 # No Encryption password, use key file
131 rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
132 rc_halt "geli init -b -s 4096 -P -K ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
133 rc_halt "geli attach -p -k ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
145 echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
147 rc_halt "newfs ${PARTXTRAOPTS} ${PARTDEV}${EXT}"
150 rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
153 # Set flag that we've found a boot partition
154 if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
161 echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
163 rc_halt "newfs ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
166 rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
168 # Set flag that we've found a boot partition
169 if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
176 echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
178 rc_halt "newfs ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
181 rc_halt "tunefs -j enable ${PARTDEV}${EXT}"
184 rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
186 # Set flag that we've found a boot partition
187 if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
195 echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
197 rc_halt "newfs ${PARTDEV}${EXT}"
199 rc_halt "gjournal label -f ${PARTDEV}${EXT}"
201 rc_halt "newfs ${PARTXTRAOPTS} -O 2 -J ${PARTDEV}${EXT}.journal"
204 rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}.journal"
206 # Set flag that we've found a boot partition
207 if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
214 echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
215 setup_zfs_filesystem "${PARTDEV}" "${PARTFS}" "${PARTMNT}" "${EXT}" "${PARTGEOM}" "${PARTXTRAOPTS}"
220 rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
226 write_image "${PARTIMAGE}" "${PARTDEV}"
230 *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;