functions-newfs.sh revision 209513
1#!/bin/sh
2#-
3# Copyright (c) 2010 iX Systems, 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 209513 2010-06-24 22:21:47Z imp $
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 some custom zpool arguments and use them if so
48  if [ ! -z "${ZPOOLOPTS}" ] ; then
49    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${ZPOOLOPTS}"
50  else
51    # No zpool options, create pool on single device
52    rc_halt "zpool create -m none -f ${ZPOOLNAME} ${PART}${EXT}"
53  fi
54
55  # Disable atime for this zfs partition, speed increase
56  rc_nohalt "zfs set atime=off ${ZPOOLNAME}"
57
58  # Check if we have multiple zfs mounts specified
59  for i in `echo ${PARTMNT} | sed 's|,| |g'`
60  do
61    # Check if we ended up with needing a zfs bootable partition
62    if [ "${i}" = "/" -o "${i}" = "/boot" ]
63    then
64      if [ "${PARTGEOM}" = "MBR" ]
65      then
66        # Lets stamp the proper ZFS boot loader
67        echo_log "Setting up ZFS boot loader support" 
68        rc_halt "zpool set bootfs=${ZPOOLNAME} ${ZPOOLNAME}"
69        rc_halt "zpool export ${ZPOOLNAME}"
70        rc_halt "dd if=/boot/zfsboot of=/dev/${ROOTSLICE} count=1"
71        rc_halt "dd if=/boot/zfsboot of=/dev/${PART}${EXT} skip=1 seek=1024"
72        rc_halt "zpool import ${ZPOOLNAME}"
73      fi
74    fi
75  done 
76
77};
78
79# Runs newfs on all the partiions which we've setup with bsdlabel
80setup_filesystems()
81{
82
83   # Create the keydir
84   rm -rf ${GELIKEYDIR} >/dev/null 2>/dev/null
85   mkdir ${GELIKEYDIR}
86
87   # Lets go ahead and read through the saved partitions we created, and determine if we need to run
88   # newfs on any of them
89   for PART in `ls ${PARTDIR}`
90   do
91     if [ ! -e "/dev/${PART}" ]
92     then
93       exit_err "ERROR: The partition ${PART} does not exist. Failure in bsdlabel?"
94     fi 
95     
96     PARTFS="`cat ${PARTDIR}/${PART} | cut -d ':' -f 1`"
97     PARTMNT="`cat ${PARTDIR}/${PART} | cut -d ':' -f 2`"
98     PARTENC="`cat ${PARTDIR}/${PART} | cut -d ':' -f 3`"
99     PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d ':' -f 4`"
100     PARTGEOM="`cat ${PARTDIR}/${PART} | cut -d ':' -f 5`"
101     PARTXTRAOPTS="`cat ${PARTDIR}/${PART} | cut -d ':' -f 6`"
102
103     # Make sure journaling isn't enabled on this device
104     if [ -e "/dev/${PART}.journal" ]
105     then
106       rc_nohalt "gjournal stop -f ${PART}.journal"
107       rc_nohalt "gjournal clear ${PART}"
108     fi
109
110     # Setup encryption if necessary
111     if [ "${PARTENC}" = "ON" -a "${PARTFS}" != "SWAP" ]
112     then
113       echo_log "Creating geli provider for ${PART}"
114       rc_halt "dd if=/dev/random of=${GELIKEYDIR}/${PART}.key bs=64 count=1"
115       rc_halt "geli init -b -s 4096 -P -K ${GELIKEYDIR}/${PART}.key /dev/${PART}"
116       rc_halt "geli attach -p -k ${GELIKEYDIR}/${PART}.key /dev/${PART}"
117
118       EXT=".eli"
119     else
120       # No Encryption
121       EXT=""
122     fi
123
124     case ${PARTFS} in
125         UFS) echo_log "NEWFS: /dev/${PART} - ${PARTFS}"
126              sleep 2
127              rc_halt "newfs /dev/${PART}${EXT}"
128              sleep 2
129              rc_halt "sync"
130              rc_halt "glabel label ${PARTLABEL} /dev/${PART}${EXT}"
131              rc_halt "sync"
132              sleep 2
133              ;;
134       UFS+S) echo_log "NEWFS: /dev/${PART} - ${PARTFS}"
135              sleep 2
136              rc_halt "newfs -U /dev/${PART}${EXT}"
137              sleep 2
138              rc_halt "sync"
139              rc_halt "glabel label ${PARTLABEL} /dev/${PART}${EXT}"
140              rc_halt "sync"
141              sleep 2
142              ;;
143       UFS+J) echo_log "NEWFS: /dev/${PART} - ${PARTFS}"
144              sleep 2
145              rc_halt "newfs /dev/${PART}${EXT}"
146              sleep 2
147              rc_halt "gjournal label -f /dev/${PART}${EXT}"
148              sleep 2
149              rc_halt "newfs -O 2 -J /dev/${PART}${EXT}.journal"
150              sleep 2
151              rc_halt "sync"
152              rc_halt "glabel label ${PARTLABEL} /dev/${PART}${EXT}.journal"
153              rc_halt "sync"
154              sleep 2
155              ;;
156         ZFS) echo_log "NEWFS: /dev/${PART} - ${PARTFS}" 
157              setup_zfs_filesystem "${PART}" "${PARTFS}" "${PARTMNT}" "${EXT}" "${PARTGEOM}" "${PARTXTRAOPTS}"
158              ;;
159        SWAP) rc_halt "sync"
160              rc_halt "glabel label ${PARTLABEL} /dev/${PART}${EXT}" 
161              rc_halt "sync"
162              sleep 2
163              ;;
164           *) exit_err "ERROR: Got unknown file-system type $PARTFS" ;;
165     esac
166
167   done
168};
169