source: src-sh/pc-sysinstall/backend/functions-newfs.sh @ 3018894

9.2-releasereleng/10.0releng/10.0.1
Last change on this file since 3018894 was 3018894, checked in by Kris Moore <kris@…>, 8 months ago

Large update to pc-sysinstall:

  • Improve how we create disk layouts for ZFS mirror/raidz disks
  • Do not blindly create a single freebsd-zfs partition
  • Create partition / sizes to match the parent disk
  • When adding freebsd-swap to a ZFS mirror/raidz parent, we will now setup gmirror on the swap devices and label it as such
  • Property mode set to 100755
File size: 8.8 KB
Line 
1#!/bin/sh
2#-
3# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
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.
13#
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
24# SUCH DAMAGE.
25#
26# $FreeBSD: head/usr.sbin/pc-sysinstall/backend/functions-newfs.sh 247735 2013-03-03 23:07:27Z jpaetzel $
27
28# Functions related to disk operations using newfs
29
30
31# Function which performs the ZFS magic
32setup_zfs_filesystem()
33{
34  PART="$1"
35  PARTFS="$2"
36  PARTMNT="$3"
37  EXT="$4"
38  PARTGEOM="$5"
39  ZPOOLOPTS="$6"
40  ROOTSLICE="`echo ${PART} | rev | cut -b 2- | rev`"
41  ZPOOLNAME=$(get_zpool_name "${PART}")
42
43  # Sleep a few moments, let the disk catch its breath
44  sleep 5
45  sync
46
47  # Check if we have multiple zfs mounts specified
48  for i in `echo ${PARTMNT} | sed 's|,| |g'`
49  do
50    # Check if we ended up with needing a zfs bootable partition
51    if [ "${i}" = "/" -o "${i}" = "/boot" ]
52    then
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"
59      fi
60    fi
61  done 
62
63
64  # Are we going to skip the gnop trick?
65  if [ -z "$ZFSFORCE4K" ] ; then
66    if [ -n "${ZPOOLOPTS}" ] ; then
67      echo_log "Creating zpool ${ZPOOLNAME} with $ZPOOLOPTS"
68      rc_halt "zpool create -m none -f ${ZPOOLNAME} ${ZPOOLOPTS}"
69    else
70      # No zpool options, create pool on single device
71      echo_log "Creating zpool ${ZPOOLNAME} on ${PART}${EXT}"
72      rc_halt "zpool create -m none -f ${ZPOOLNAME} ${PART}${EXT}"
73    fi
74    return 0
75  fi
76
77  # We are doing the gnop trick! This breaks some cases with GRUB + GPT
78  if [ -n "${ZPOOLOPTS}" ] ; then
79    # Sort through devices and run gnop on them
80    local gnopDev=""
81    local newOpts=""
82    local hGnop=0
83    for i in $ZPOOLOPTS
84    do
85       echo "$i" | grep -q '/dev/'
86       if [ $? -eq 0 -a $hGnop -eq 0 ] ; then
87          rc_halt "gnop create -S 4096 ${i}"
88          gnopDev="$gnopDev $i"
89          newOpts="$newOpts ${i}.nop"
90          hGnop=1
91       else
92          newOpts="$newOpts $i"
93       fi
94    done
95   
96    echo_log "Creating zpool ${ZPOOLNAME} with $newOpts"
97    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${newOpts}"
98
99    # Export the pool
100    rc_halt "zpool export ${ZPOOLNAME}"
101
102    # Destroy the gnop devices
103    for i in $gnopDev
104    do
105       rc_halt "gnop destroy ${i}.nop"
106    done
107
108    # And lastly re-import the pool
109    rc_halt "zpool import ${ZPOOLNAME}"
110  else
111    # Lets do our pseudo-4k drive
112    rc_halt "gnop create -S 4096 ${PART}${EXT}"
113
114    # No zpool options, create pool on single device
115    echo_log "Creating zpool ${ZPOOLNAME} on ${PART}${EXT}"
116    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${PART}${EXT}.nop"
117
118    # Finish up the gnop 4k trickery
119    rc_halt "zpool export ${ZPOOLNAME}"
120    rc_halt "gnop destroy ${PART}${EXT}.nop"
121    rc_halt "zpool import ${ZPOOLNAME}"
122  fi
123
124};
125
126# Runs newfs on all the partiions which we've setup with bsdlabel
127setup_filesystems()
128{
129
130  # Create the keydir
131  rm -rf ${GELIKEYDIR} >/dev/null 2>/dev/null
132  mkdir ${GELIKEYDIR}
133
134  # Lets go ahead and read through the saved partitions we created, and determine if we need to run
135  # newfs on any of them
136  for PART in `ls ${PARTDIR}`
137  do
138    PARTDEV="`echo $PART | sed 's|-|/|g'`"
139    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
140    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
141    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
142    PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d '#' -f 4`"
143    PARTGEOM="`cat ${PARTDIR}/${PART} | cut -d '#' -f 5`"
144    PARTXTRAOPTS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 6`"
145    PARTIMAGE="`cat ${PARTDIR}/${PART} | cut -d '#' -f 7`"
146
147    if [ ! -e "${PARTDEV}" ] ; then
148      exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
149    fi 
150
151    # Make sure journaling isn't enabled on this device
152    if [ -e "${PARTDEV}.journal" ]
153    then
154      rc_nohalt "gjournal stop -f ${PARTDEV}.journal"
155      rc_nohalt "gjournal clear ${PARTDEV}"
156    fi
157
158    # Setup encryption if necessary
159    if [ "${PARTENC}" = "ON" -a "${PARTFS}" != "SWAP" ]
160    then
161      echo_log "Creating geli provider for ${PARTDEV}"
162
163      if [ -e "${PARTDIR}-enc/${PART}-encpass" ] ; then
164        # Using a passphrase
165        rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
166        rc_halt "geli init -J ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
167        rc_halt "geli attach -j ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
168      else
169        # No Encryption password, use key file
170        rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
171        rc_halt "geli init -b -s 4096 -P -K ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
172        rc_halt "geli attach -p -k ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
173
174      fi
175
176      EXT=".eli"
177    else
178      # No Encryption
179      EXT=""
180    fi
181
182    case ${PARTFS} in
183      UFS)
184        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
185        sleep 2
186        rc_halt "newfs -t ${PARTXTRAOPTS} ${PARTDEV}${EXT}"
187        sleep 2
188        rc_halt "sync"
189        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
190        rc_halt "sync"
191
192        # Set flag that we've found a boot partition
193        if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
194                  HAVEBOOT="YES"
195        fi
196        sleep 2
197        ;;
198
199      UFS+S)
200        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
201        sleep 2
202        rc_halt "newfs -t ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
203        sleep 2
204        rc_halt "sync"
205        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
206        rc_halt "sync"
207            # Set flag that we've found a boot partition
208            if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
209          HAVEBOOT="YES"
210        fi
211        sleep 2
212        ;;
213
214      UFS+SUJ)
215        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
216        sleep 2
217        rc_halt "newfs -t ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
218        sleep 2
219        rc_halt "sync"
220        rc_halt "tunefs -j enable ${PARTDEV}${EXT}"
221        sleep 2
222        rc_halt "sync"
223        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
224        rc_halt "sync"
225            # Set flag that we've found a boot partition
226            if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
227          HAVEBOOT="YES"
228        fi
229        sleep 2
230        ;;
231
232
233      UFS+J)
234        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
235        sleep 2
236        rc_halt "newfs ${PARTDEV}${EXT}"
237        sleep 2
238        rc_halt "gjournal label -f ${PARTDEV}${EXT}"
239        sleep 2
240        rc_halt "newfs ${PARTXTRAOPTS} -O 2 -J ${PARTDEV}${EXT}.journal"
241        sleep 2
242        rc_halt "sync"
243        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}.journal"
244        rc_halt "sync"
245            # Set flag that we've found a boot partition
246            if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
247          HAVEBOOT="YES"
248            fi
249        sleep 2
250        ;;
251
252      ZFS)
253        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}" 
254        setup_zfs_filesystem "${PARTDEV}" "${PARTFS}" "${PARTMNT}" "${EXT}" "${PARTGEOM}" "${PARTXTRAOPTS}"
255        ;;
256
257      SWAP)
258        rc_halt "sync"
259        if [ -n "$ZFS_SWAP_DEVS" ] ; then
260          setup_gmirror_swap "$ZFS_SWAP_DEVS"
261          sleep 5
262          rc_halt "glabel label ${PARTLABEL} /dev/mirror/swapmirror" 
263        else
264          rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}" 
265        fi
266        rc_halt "sync"
267        sleep 2
268        ;;
269
270      IMAGE)
271        write_image "${PARTIMAGE}" "${PARTDEV}"
272        sleep 2
273        ;;
274
275      *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
276    esac
277
278  done
279};
280
281
282# Takes a list of args to setup as a swapmirror
283setup_gmirror_swap()
284{
285  rc_halt "gmirror label swapmirror ${@}"
286}
Note: See TracBrowser for help on using the repository browser.