functions-newfs.sh revision 240315
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: stable/9/usr.sbin/pc-sysinstall/backend/functions-newfs.sh 240315 2012-09-10 14:10:10Z 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  # 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}"
67  else
68    # No zpool options, create pool on single device
69    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${PART}${EXT}"
70  fi
71
72  # Disable atime for this zfs partition, speed increase
73  rc_nohalt "zfs set atime=off ${ZPOOLNAME}"
74
75};
76
77# Runs newfs on all the partiions which we've setup with bsdlabel
78setup_filesystems()
79{
80
81  # Create the keydir
82  rm -rf ${GELIKEYDIR} >/dev/null 2>/dev/null
83  mkdir ${GELIKEYDIR}
84
85  # Lets go ahead and read through the saved partitions we created, and determine if we need to run
86  # newfs on any of them
87  for PART in `ls ${PARTDIR}`
88  do
89    PARTDEV="`echo $PART | sed 's|-|/|g'`"
90    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
91    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
92    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
93    PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d '#' -f 4`"
94    PARTGEOM="`cat ${PARTDIR}/${PART} | cut -d '#' -f 5`"
95    PARTXTRAOPTS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 6`"
96    PARTIMAGE="`cat ${PARTDIR}/${PART} | cut -d '#' -f 7`"
97
98    if [ ! -e "${PARTDEV}" ] ; then
99      exit_err "ERROR: The partition ${PARTDEV} does not exist. Failure in bsdlabel?"
100    fi 
101
102    # Make sure journaling isn't enabled on this device
103    if [ -e "${PARTDEV}.journal" ]
104    then
105      rc_nohalt "gjournal stop -f ${PARTDEV}.journal"
106      rc_nohalt "gjournal clear ${PARTDEV}"
107    fi
108
109    # Setup encryption if necessary
110    if [ "${PARTENC}" = "ON" -a "${PARTFS}" != "SWAP" ]
111    then
112      echo_log "Creating geli provider for ${PARTDEV}"
113
114      if [ -e "${PARTDIR}-enc/${PART}-encpass" ] ; then
115	# Using a passphrase
116        rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
117        rc_halt "geli init -J ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
118        rc_halt "geli attach -j ${PARTDIR}-enc/${PART}-encpass ${PARTDEV}"
119      else
120	# No Encryption password, use key file
121        rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
122        rc_halt "geli init -b -s 4096 -P -K ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
123        rc_halt "geli attach -p -k ${GELIKEYDIR}/${PART}.key ${PARTDEV}"
124
125      fi
126
127      EXT=".eli"
128    else
129      # No Encryption
130      EXT=""
131    fi
132
133    case ${PARTFS} in
134      UFS)
135        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
136        sleep 2
137        rc_halt "newfs -t ${PARTXTRAOPTS} ${PARTDEV}${EXT}"
138        sleep 2
139        rc_halt "sync"
140        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
141        rc_halt "sync"
142
143        # Set flag that we've found a boot partition
144        if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
145		  HAVEBOOT="YES"
146        fi
147        sleep 2
148        ;;
149
150      UFS+S)
151        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
152        sleep 2
153        rc_halt "newfs -t ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
154        sleep 2
155        rc_halt "sync"
156        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
157        rc_halt "sync"
158	    # Set flag that we've found a boot partition
159	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
160          HAVEBOOT="YES"
161        fi
162        sleep 2
163        ;;
164
165      UFS+SUJ)
166        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
167        sleep 2
168        rc_halt "newfs -t ${PARTXTRAOPTS} -U ${PARTDEV}${EXT}"
169        sleep 2
170        rc_halt "sync"
171        rc_halt "tunefs -j enable ${PARTDEV}${EXT}"
172        sleep 2
173        rc_halt "sync"
174        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}"
175        rc_halt "sync"
176	    # Set flag that we've found a boot partition
177	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
178          HAVEBOOT="YES"
179        fi
180        sleep 2
181        ;;
182
183
184      UFS+J)
185        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}"
186        sleep 2
187        rc_halt "newfs ${PARTDEV}${EXT}"
188        sleep 2
189        rc_halt "gjournal label -f ${PARTDEV}${EXT}"
190        sleep 2
191        rc_halt "newfs ${PARTXTRAOPTS} -O 2 -J ${PARTDEV}${EXT}.journal"
192        sleep 2
193        rc_halt "sync"
194        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}.journal"
195        rc_halt "sync"
196	    # Set flag that we've found a boot partition
197	    if [ "$PARTMNT" = "/boot" -o "${PARTMNT}" = "/" ] ; then
198          HAVEBOOT="YES"
199  	    fi
200        sleep 2
201        ;;
202
203      ZFS)
204        echo_log "NEWFS: ${PARTDEV} - ${PARTFS}" 
205        setup_zfs_filesystem "${PARTDEV}" "${PARTFS}" "${PARTMNT}" "${EXT}" "${PARTGEOM}" "${PARTXTRAOPTS}"
206        ;;
207
208      SWAP)
209        rc_halt "sync"
210        rc_halt "glabel label ${PARTLABEL} ${PARTDEV}${EXT}" 
211        rc_halt "sync"
212        sleep 2
213        ;;
214
215      IMAGE)
216        write_image "${PARTIMAGE}" "${PARTDEV}"
217        sleep 2
218        ;; 
219
220      *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
221    esac
222
223  done
224};
225