3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 # Copyright (c) 2010 iXsystems, Inc. All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 # Functions which perform the final cleanup after an install
32 # Finishes up with ZFS setup before unmounting
35 # Loop through our FS and see if we have any ZFS partitions to cleanup
36 for PART in `ls ${PARTDIR}`
38 PARTDEV=`echo $PART | sed 's|-|/|g'`
39 PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
40 PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
41 ZPOOLNAME=$(get_zpool_name "${PARTDEV}")
43 if [ "$PARTFS" = "ZFS" ]
45 # Check if we have multiple zfs mounts specified
46 for ZMNT in `echo ${PARTMNT} | sed 's|,| |g'`
48 if [ "${ZMNT}" = "/" ]
50 # Make sure we haven't already added the zfs boot line when
51 # Creating a dedicated "/boot" partition
52 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep -q "vfs.root.mountfrom=" 2>/dev/null
53 if [ $? -ne 0 ] ; then
54 echo "vfs.root.mountfrom=\"zfs:${ZPOOLNAME}/ROOT/default\"" >> ${FSMNT}/boot/loader.conf
56 export FOUNDZFSROOT="${ZPOOLNAME}"
63 if [ -n "${FOUNDZFS}" ]
65 # Check if we need to add our ZFS flags to rc.conf, src.conf and loader.conf
66 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep -q 'zfs_load="YES"' 2>/dev/null
69 echo 'zfs_load="YES"' >>${FSMNT}/boot/loader.conf
71 cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep -q 'zfs_enable="YES"' 2>/dev/null
74 echo 'zfs_enable="YES"' >>${FSMNT}/etc/rc.conf
78 # Copy over any ZFS cache data
79 cp /boot/zfs/* ${FSMNT}/boot/zfs/
81 # Copy the hostid so that our zfs cache works
82 cp /etc/hostid ${FSMNT}/etc/hostid
85 # Loop through our FS and see if we have any ZFS partitions to cleanup
86 for PART in `ls ${PARTDIR}`
88 PARTDEV=`echo $PART | sed 's|-|/|g'`
89 PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
90 PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
91 PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
92 ZPOOLNAME=$(get_zpool_name "${PARTDEV}")
94 if [ "$PARTFS" = "ZFS" ]
97 # Create a list of zpool names we can export
98 echo $ZPOOLEXPORTS | grep -q "$ZPOOLNAME "
99 if [ $? -ne 0 ] ; then
100 export ZPOOLEXPORTS="$ZPOOLNAME $ZPOOLEXPORTS"
103 # Check if we have multiple zfs mounts specified
104 for ZMNT in `echo ${PARTMNT} | sed 's|,| |g'`
106 ZMNT="`echo $ZMNT | cut -d '(' -f 1`"
107 PARTMNTREV="${ZMNT} ${PARTMNTREV}"
110 for ZMNT in ${PARTMNTREV}
112 if [ "${ZMNT}" = "/" ] ; then continue ; fi
113 # Some ZFS like /swap aren't mounted, and dont need unmounting
114 mount | grep -q "${FSMNT}${ZMNT}"
115 if [ $? -eq 0 ] ; then
116 rc_halt "zfs unmount ${ZPOOLNAME}${ZMNT}"
117 rc_halt "zfs set mountpoint=${ZMNT} ${ZPOOLNAME}${ZMNT}"
126 # Function which performs the specific setup for using a /boot partition
127 setup_dedicated_boot_part()
134 # Set the root mount in loader.conf
135 echo "vfs.root.mountfrom=\"${ROOTFSTYPE}:${ROOTFS}\"" >> ${FSMNT}/boot/loader.conf
136 rc_halt "mkdir -p ${FSMNT}/${BOOTMNT}/boot"
137 rc_halt "mv ${FSMNT}/boot/* ${FSMNT}${BOOTMNT}/boot/"
138 rc_halt "mv ${FSMNT}${BOOTMNT}/boot ${FSMNT}/boot/"
139 rc_halt "umount ${BOOTFS}"
140 rc_halt "mount ${BOOTFS} ${FSMNT}${BOOTMNT}"
141 rc_halt "rmdir ${FSMNT}/boot"
143 # Strip the '/' from BOOTMNT before making symlink
144 BOOTMNTNS="`echo ${BOOTMNT} | sed 's|/||g'`"
145 rc_halt "chroot ${FSMNT} ln -s ${BOOTMNTNS}/boot /boot"
149 # Function which creates the /etc/fstab for the installed system
152 FSTAB="${FSMNT}/etc/fstab"
153 rm ${FSTAB} >/dev/null 2>/dev/null
156 echo "# Device Mountpoint FStype Options Dump Pass" >> ${FSTAB}
158 # Loop through the partitions, and start creating /etc/fstab
159 for PART in `ls ${PARTDIR}`
161 PARTDEV=`echo $PART | sed 's|-|/|g'`
162 PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
163 PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
164 PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
165 PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d '#' -f 4`"
170 # Set mount options for file-systems
172 UFS+J) MNTOPTS="rw,noatime,async" ;;
173 SWAP) MNTOPTS="sw" ;;
174 *) MNTOPTS="rw,noatime" ;;
178 # Figure out if we are using a glabel, or the raw name for this entry
179 if [ -n "${PARTLABEL}" ]
181 DEVICE="label/${PARTLABEL}"
183 # Check if using encryption
184 if [ "${PARTENC}" = "ON" ] ; then
188 if [ "${PARTFS}" = "UFS+J" ] ; then
191 DEVICE="${PARTDEV}${EXT}"
195 # Set our ROOTFSTYPE for loader.conf if necessary
196 check_for_mount "${PARTMNT}" "/"
197 if [ $? -eq 0 ] ; then
198 if [ "${PARTFS}" = "ZFS" ] ; then
200 ZPOOLNAME=$(get_zpool_name "${PARTDEV}")
201 ROOTFS="${ZPOOLNAME}/ROOT/default"
208 # Only create non-zfs partitions
209 if [ "${PARTFS}" != "ZFS" ]
212 # Make sure geom_journal is loaded
213 if [ "${PARTFS}" = "UFS+J" ] ; then
217 # Save the BOOTFS for call at the end
218 if [ "${PARTMNT}" = "/boot" ] ; then
219 BOOTFS="${PARTDEV}${EXT}"
220 BOOTMNT="${BOOT_PART_MOUNT}"
224 # Echo out the fstab entry now
225 if [ "${PARTFS}" = "SWAP" ]
227 echo "/dev/${DEVICE} none swap ${MNTOPTS} 0 0" >> ${FSTAB}
229 echo "/dev/${DEVICE} ${PARTMNT} ufs ${MNTOPTS} 1 1" >> ${FSTAB}
232 fi # End of ZFS Check
235 # Setup some specific PC-BSD fstab options
236 if [ "$INSTALLTYPE" != "FreeBSD" ]
238 echo "procfs /proc procfs rw 0 0" >> ${FSTAB}
239 echo "linprocfs /compat/linux/proc linprocfs rw 0 0" >> ${FSTAB}
242 # If we have a dedicated /boot, run the post-install setup of it now
243 if [ ! -z "${BOOTMNT}" ] ; then
244 setup_dedicated_boot_part "${ROOTFS}" "${ROOTFSTYPE}" "${BOOTFS}" "${BOOTMNT}"
249 # Setup our disk mirroring with gmirror
252 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep -q 'geom_mirror_load="YES"' 2>/dev/null
255 echo 'geom_mirror_load="YES"' >>${FSMNT}/boot/loader.conf
260 # Function which saves geli keys and sets up loading of them at boot
265 mkdir -p ${FSMNT}/boot/keys >/dev/null 2>/dev/null
270 # Figure out the partition name based on keyfile name removing .key
271 PART="`echo ${KEYFILE} | cut -d '.' -f 1`"
272 PARTDEV="`echo ${PART} | sed 's|-|/|g'`"
273 PARTNAME="`echo ${PART} | sed 's|-dev-||g'`"
275 rc_halt "geli configure -b ${PARTDEV}"
277 # If no passphrase, setup key files
278 if [ ! -e "${PARTDIR}-enc/${PART}-encpass" ] ; then
279 echo "geli_${PARTNAME}_keyfile0_load=\"YES\"" >> ${FSMNT}/boot/loader.conf
280 echo "geli_${PARTNAME}_keyfile0_type=\"${PARTNAME}:geli_keyfile0\"" >> ${FSMNT}/boot/loader.conf
281 echo "geli_${PARTNAME}_keyfile0_name=\"/boot/keys/${PARTNAME}.key\"" >> ${FSMNT}/boot/loader.conf
283 # Copy the key to the disk
284 rc_halt "cp ${GELIKEYDIR}/${KEYFILE} ${FSMNT}/boot/keys/${PARTNAME}.key"
289 # Make sure we have geom_eli set to load at boot
290 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep -q 'geom_eli_load="YES"' 2>/dev/null
293 echo 'geom_eli_load="YES"' >>${FSMNT}/boot/loader.conf
299 # Function to generate a random hostname if none was specified
302 RAND="`jot -r 1 1 9000`"
304 if [ "$INSTALLTYPE" = "FreeBSD" ]
306 VAL="freebsd-${RAND}"
315 # Function which sets up the hostname for the system
319 get_value_from_cfg hostname
322 # If we don't have a hostname, make one up
323 if [ -z "${HOSTNAME}" ]
329 # Clean up any saved hostname
330 cat ${FSMNT}/etc/rc.conf | grep -v "hostname=" >${FSMNT}/etc/rc.conf.new
331 mv ${FSMNT}/etc/rc.conf.new ${FSMNT}/etc/rc.conf
333 # Set the hostname now
334 echo_log "Setting hostname: ${HOSTNAME}"
335 echo "hostname=\"${HOSTNAME}\"" >> ${FSMNT}/etc/rc.conf
336 sed -i -e "s|my.domain|${HOSTNAME} ${HOSTNAME}|g" ${FSMNT}/etc/hosts
341 # Check and make sure geom_journal is enabled on the system
345 # Make sure we have geom_journal set to load at boot
346 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep -q 'geom_journal_load="YES"' 2>/dev/null
349 echo 'geom_journal_load="YES"' >>${FSMNT}/boot/loader.conf
354 # Function which sets the root password from the install config
357 # Get the plaintext string
358 get_value_from_cfg_with_spaces rootPass
361 # Get the encrypted string
362 get_value_from_cfg_with_spaces rootEncPass
365 # If we don't have a root pass, return
366 if [ -z "${PW}" -a -z "${ENCPW}" ] ; then return 0 ; fi
368 echo_log "Setting root password"
370 # Check if setting plaintext password
371 if [ -n "${PW}" ] ; then
372 echo "${PW}" > ${FSMNT}/.rootpw
373 run_chroot_cmd "cat /.rootpw | pw usermod root -h 0"
374 rc_halt "rm ${FSMNT}/.rootpw"
377 # Check if setting encrypted password
378 if [ -n "${ENCPW}" ] ; then
379 echo "${ENCPW}" > ${FSMNT}/.rootpw
380 run_chroot_cmd "cat /.rootpw | pw usermod root -H 0"
381 rc_halt "rm ${FSMNT}/.rootpw"
389 # Check if we need to run any gmirror setup
390 ls ${MIRRORCFGDIR}/* >/dev/null 2>/dev/null
393 # Lets setup gmirror now
397 # Check if we need to save any geli keys
398 ls ${GELIKEYDIR}/* >/dev/null 2>/dev/null
401 # Lets setup geli loading
405 # Set a hostname on the install system
408 # Set the root_pw if it is specified
411 # Generate the fstab for the installed system