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

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

Update pc-sysinstall to now allow doing restores from a replicated
ZFS dataset via life-preserver

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