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 which perform the final cleanup after an install
30 # Finishes up with ZFS setup before unmounting
33 # Loop through our FS and see if we have any ZFS partitions to cleanup
34 for PART in `ls ${PARTDIR}`
36 PARTDEV=`echo $PART | sed 's|-|/|g'`
37 PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
38 PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
39 ZPOOLNAME=$(get_zpool_name "${PARTDEV}")
41 if [ "$PARTFS" = "ZFS" ]
43 # Check if we have multiple zfs mounts specified
44 for ZMNT in `echo ${PARTMNT} | sed 's|,| |g'`
46 if [ "${ZMNT}" = "/" ]
48 # Make sure we haven't already added the zfs boot line when
49 # Creating a dedicated "/boot" partition
50 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep -q "vfs.root.mountfrom=" 2>/dev/null
51 if [ $? -ne 0 ] ; then
52 echo "vfs.root.mountfrom=\"zfs:${ZPOOLNAME}/ROOT/default\"" >> ${FSMNT}/boot/loader.conf
54 export FOUNDZFSROOT="${ZPOOLNAME}"
61 if [ -n "${FOUNDZFS}" ]
63 # Check if we need to add our ZFS flags to rc.conf, src.conf and loader.conf
64 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep -q 'zfs_load="YES"' 2>/dev/null
67 echo 'zfs_load="YES"' >>${FSMNT}/boot/loader.conf
69 cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep -q 'zfs_enable="YES"' 2>/dev/null
72 echo 'zfs_enable="YES"' >>${FSMNT}/etc/rc.conf
76 # Copy over any ZFS cache data
77 cp /boot/zfs/* ${FSMNT}/boot/zfs/
79 # Copy the hostid so that our zfs cache works
80 cp /etc/hostid ${FSMNT}/etc/hostid
83 # Loop through our FS and see if we have any ZFS partitions to cleanup
84 for PART in `ls ${PARTDIR}`
86 PARTDEV=`echo $PART | sed 's|-|/|g'`
87 PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
88 PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
89 PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
90 ZPOOLNAME=$(get_zpool_name "${PARTDEV}")
92 if [ "$PARTFS" = "ZFS" ]
95 # Create a list of zpool names we can export
96 echo $ZPOOLEXPORTS | grep -q "$ZPOOLNAME "
97 if [ $? -ne 0 ] ; then
98 export ZPOOLEXPORTS="$ZPOOLNAME $ZPOOLEXPORTS"
101 # Check if we have multiple zfs mounts specified
102 for ZMNT in `echo ${PARTMNT} | sed 's|,| |g'`
104 ZMNT="`echo $ZMNT | cut -d '(' -f 1`"
105 PARTMNTREV="${ZMNT} ${PARTMNTREV}"
108 for ZMNT in ${PARTMNTREV}
110 if [ "${ZMNT}" = "/" ] ; then continue ; fi
111 # Some ZFS like /swap aren't mounted, and dont need unmounting
112 mount | grep -q "${FSMNT}${ZMNT}"
113 if [ $? -eq 0 ] ; then
114 rc_halt "zfs unmount ${ZPOOLNAME}${ZMNT}"
115 rc_halt "zfs set mountpoint=${ZMNT} ${ZPOOLNAME}${ZMNT}"
124 # Function which performs the specific setup for using a /boot partition
125 setup_dedicated_boot_part()
132 # Set the root mount in loader.conf
133 echo "vfs.root.mountfrom=\"${ROOTFSTYPE}:${ROOTFS}\"" >> ${FSMNT}/boot/loader.conf
134 rc_halt "mkdir -p ${FSMNT}/${BOOTMNT}/boot"
135 rc_halt "mv ${FSMNT}/boot/* ${FSMNT}${BOOTMNT}/boot/"
136 rc_halt "mv ${FSMNT}${BOOTMNT}/boot ${FSMNT}/boot/"
137 rc_halt "umount ${BOOTFS}"
138 rc_halt "mount ${BOOTFS} ${FSMNT}${BOOTMNT}"
139 rc_halt "rmdir ${FSMNT}/boot"
141 # Strip the '/' from BOOTMNT before making symlink
142 BOOTMNTNS="`echo ${BOOTMNT} | sed 's|/||g'`"
143 rc_halt "chroot ${FSMNT} ln -s ${BOOTMNTNS}/boot /boot"
147 # Function which creates the /etc/fstab for the installed system
150 FSTAB="${FSMNT}/etc/fstab"
151 rm ${FSTAB} >/dev/null 2>/dev/null
154 echo "# Device Mountpoint FStype Options Dump Pass" >> ${FSTAB}
156 # Loop through the partitions, and start creating /etc/fstab
157 for PART in `ls ${PARTDIR}`
159 PARTDEV=`echo $PART | sed 's|-|/|g'`
160 PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
161 PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
162 PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
163 PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d '#' -f 4`"
168 # Set mount options for file-systems
170 UFS+J) MNTOPTS="rw,noatime,async" ;;
171 SWAP) MNTOPTS="sw" ;;
172 *) MNTOPTS="rw,noatime" ;;
176 # Figure out if we are using a glabel, or the raw name for this entry
177 if [ -n "${PARTLABEL}" ]
179 DEVICE="label/${PARTLABEL}"
181 # Check if using encryption
182 if [ "${PARTENC}" = "ON" ] ; then
186 if [ "${PARTFS}" = "UFS+J" ] ; then
189 DEVICE="${PARTDEV}${EXT}"
193 # Set our ROOTFSTYPE for loader.conf if necessary
194 check_for_mount "${PARTMNT}" "/"
195 if [ $? -eq 0 ] ; then
196 if [ "${PARTFS}" = "ZFS" ] ; then
198 ZPOOLNAME=$(get_zpool_name "${PARTDEV}")
199 ROOTFS="${ZPOOLNAME}/ROOT/default"
206 # Only create non-zfs partitions
207 if [ "${PARTFS}" != "ZFS" ]
210 # Make sure geom_journal is loaded
211 if [ "${PARTFS}" = "UFS+J" ] ; then
215 # Save the BOOTFS for call at the end
216 if [ "${PARTMNT}" = "/boot" ] ; then
217 BOOTFS="${PARTDEV}${EXT}"
218 BOOTMNT="${BOOT_PART_MOUNT}"
222 # Echo out the fstab entry now
223 if [ "${PARTFS}" = "SWAP" ]
225 echo "/dev/${DEVICE} none swap ${MNTOPTS} 0 0" >> ${FSTAB}
227 echo "/dev/${DEVICE} ${PARTMNT} ufs ${MNTOPTS} 1 1" >> ${FSTAB}
230 fi # End of ZFS Check
233 # Setup some specific PC-BSD fstab options
234 if [ "$INSTALLTYPE" != "FreeBSD" ]
236 echo "procfs /proc procfs rw 0 0" >> ${FSTAB}
237 echo "linprocfs /compat/linux/proc linprocfs rw 0 0" >> ${FSTAB}
240 # If we have a dedicated /boot, run the post-install setup of it now
241 if [ ! -z "${BOOTMNT}" ] ; then
242 setup_dedicated_boot_part "${ROOTFS}" "${ROOTFSTYPE}" "${BOOTFS}" "${BOOTMNT}"
247 # Setup our disk mirroring with gmirror
250 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep -q 'geom_mirror_load="YES"' 2>/dev/null
253 echo 'geom_mirror_load="YES"' >>${FSMNT}/boot/loader.conf
258 # Function which saves geli keys and sets up loading of them at boot
263 mkdir -p ${FSMNT}/boot/keys >/dev/null 2>/dev/null
268 # Figure out the partition name based on keyfile name removing .key
269 PART="`echo ${KEYFILE} | cut -d '.' -f 1`"
270 PARTDEV="`echo ${PART} | sed 's|-|/|g'`"
271 PARTNAME="`echo ${PART} | sed 's|-dev-||g'`"
273 rc_halt "geli configure -b ${PARTDEV}"
275 # If no passphrase, setup key files
276 if [ ! -e "${PARTDIR}-enc/${PART}-encpass" ] ; then
277 echo "geli_${PARTNAME}_keyfile0_load=\"YES\"" >> ${FSMNT}/boot/loader.conf
278 echo "geli_${PARTNAME}_keyfile0_type=\"${PARTNAME}:geli_keyfile0\"" >> ${FSMNT}/boot/loader.conf
279 echo "geli_${PARTNAME}_keyfile0_name=\"/boot/keys/${PARTNAME}.key\"" >> ${FSMNT}/boot/loader.conf
281 # Copy the key to the disk
282 rc_halt "cp ${GELIKEYDIR}/${KEYFILE} ${FSMNT}/boot/keys/${PARTNAME}.key"
287 # Make sure we have geom_eli set to load at boot
288 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep -q 'geom_eli_load="YES"' 2>/dev/null
291 echo 'geom_eli_load="YES"' >>${FSMNT}/boot/loader.conf
297 # Function to generate a random hostname if none was specified
300 RAND="`jot -r 1 1 9000`"
302 if [ "$INSTALLTYPE" = "FreeBSD" ]
304 VAL="freebsd-${RAND}"
313 # Function which sets up the hostname for the system
317 get_value_from_cfg hostname
320 # If we don't have a hostname, make one up
321 if [ -z "${HOSTNAME}" ]
327 # Clean up any saved hostname
328 cat ${FSMNT}/etc/rc.conf | grep -v "hostname=" >${FSMNT}/etc/rc.conf.new
329 mv ${FSMNT}/etc/rc.conf.new ${FSMNT}/etc/rc.conf
331 # Set the hostname now
332 echo_log "Setting hostname: ${HOSTNAME}"
333 echo "hostname=\"${HOSTNAME}\"" >> ${FSMNT}/etc/rc.conf
334 sed -i -e "s|my.domain|${HOSTNAME} ${HOSTNAME}|g" ${FSMNT}/etc/hosts
339 # Check and make sure geom_journal is enabled on the system
343 # Make sure we have geom_journal set to load at boot
344 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep -q 'geom_journal_load="YES"' 2>/dev/null
347 echo 'geom_journal_load="YES"' >>${FSMNT}/boot/loader.conf
352 # Function which sets the root password from the install config
355 # Get the plaintext string
356 get_value_from_cfg_with_spaces rootPass
359 # Get the encrypted string
360 get_value_from_cfg_with_spaces rootEncPass
363 # If we don't have a root pass, return
364 if [ -z "${PW}" -a -z "${ENCPW}" ] ; then return 0 ; fi
366 echo_log "Setting root password"
368 # Check if setting plaintext password
369 if [ -n "${PW}" ] ; then
370 echo "${PW}" > ${FSMNT}/.rootpw
371 run_chroot_cmd "cat /.rootpw | pw usermod root -h 0"
372 rc_halt "rm ${FSMNT}/.rootpw"
375 # Check if setting encrypted password
376 if [ -n "${ENCPW}" ] ; then
377 echo "${ENCPW}" > ${FSMNT}/.rootpw
378 run_chroot_cmd "cat /.rootpw | pw usermod root -H 0"
379 rc_halt "rm ${FSMNT}/.rootpw"
387 # Check if we need to run any gmirror setup
388 ls ${MIRRORCFGDIR}/* >/dev/null 2>/dev/null
391 # Lets setup gmirror now
395 # Check if we need to save any geli keys
396 ls ${GELIKEYDIR}/* >/dev/null 2>/dev/null
399 # Lets setup geli loading
403 # Set a hostname on the install system
406 # Set the root_pw if it is specified
409 # Generate the fstab for the installed system