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-cleanup.sh 210700 2010-07-31 19:25:51Z imp $
|
26# $FreeBSD: head/usr.sbin/pc-sysinstall/backend/functions-cleanup.sh 211730 2010-08-24 06:11:46Z imp $ |
27 28# Functions which perform the final cleanup after an install 29 30# Finishes up with ZFS setup before unmounting 31zfs_cleanup_unmount() 32{ 33 # Loop through our FS and see if we have any ZFS partitions to cleanup 34 for PART in `ls ${PARTDIR}` 35 do 36 PARTFS="`cat ${PARTDIR}/${PART} | cut -d ':' -f 1`" 37 PARTMNT="`cat ${PARTDIR}/${PART} | cut -d ':' -f 2`" 38 ZPOOLNAME=$(get_zpool_name "${PART}") 39 40 if [ "$PARTFS" = "ZFS" ] 41 then 42 # Check if we have multiple zfs mounts specified 43 for ZMNT in `echo ${PARTMNT} | sed 's|,| |g'` 44 do 45 if [ "${ZMNT}" = "/" ] 46 then 47 # Make sure we haven't already added the zfs boot line when 48 # Creating a dedicated "/boot" partition 49 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep "vfs.root.mountfrom=" >/dev/null 2>/dev/null 50 if [ "$?" != "0" ] ; then 51 echo "vfs.root.mountfrom=\"zfs:${ZPOOLNAME}\"" >> ${FSMNT}/boot/loader.conf 52 fi 53 FOUNDZFSROOT="${ZPOOLNAME}" ; export FOUNDZFSROOT 54 fi 55 done 56 FOUNDZFS="1" 57 fi 58 done 59 60 if [ ! -z "${FOUNDZFS}" ] 61 then 62 # Check if we need to add our ZFS flags to rc.conf, src.conf and loader.conf 63 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep 'zfs_load="YES"' >/dev/null 2>/dev/null 64 if [ "$?" != "0" ] 65 then 66 echo 'zfs_load="YES"' >>${FSMNT}/boot/loader.conf 67 fi 68 cat ${FSMNT}/etc/rc.conf 2>/dev/null | grep 'zfs_enable="YES"' >/dev/null 2>/dev/null 69 if [ "$?" != "0" ] 70 then 71 echo 'zfs_enable="YES"' >>${FSMNT}/etc/rc.conf 72 fi 73 74 sleep 2 75 # Copy over any ZFS cache data 76 cp /boot/zfs/* ${FSMNT}/boot/zfs/ 77 78 # Copy the hostid so that our zfs cache works 79 cp /etc/hostid ${FSMNT}/etc/hostid 80 fi 81 82 # Loop through our FS and see if we have any ZFS partitions to cleanup 83 for PART in `ls ${PARTDIR}` 84 do 85 PARTFS="`cat ${PARTDIR}/${PART} | cut -d ':' -f 1`" 86 PARTMNT="`cat ${PARTDIR}/${PART} | cut -d ':' -f 2`" 87 PARTENC="`cat ${PARTDIR}/${PART} | cut -d ':' -f 3`" 88 ZPOOLNAME=$(get_zpool_name "${PART}") 89 90 if [ "$PARTFS" = "ZFS" ] 91 then 92 # Check if we have multiple zfs mounts specified 93 for ZMNT in `echo ${PARTMNT} | sed 's|,| |g'` 94 do 95 PARTMNTREV="${ZMNT} ${PARTMNTREV}" 96 done 97 98 for ZMNT in ${PARTMNTREV} 99 do 100 if [ "${ZMNT}" != "/" ] 101 then 102 rc_halt "zfs set mountpoint=${ZMNT} ${ZPOOLNAME}${ZMNT}" 103 rc_halt "zfs unmount ${ZPOOLNAME}${ZMNT}" 104 sleep 2 105 fi 106 done 107 fi 108 done 109 110}; 111 112# Function which performs the specific setup for using a /boot partition 113setup_dedicated_boot_part() 114{ 115 ROOTFS="${1}" 116 ROOTFSTYPE="${2}" 117 BOOTFS="${3}" 118 BOOTMNT="${4}" 119 120 # Set the root mount in loader.conf 121 echo "vfs.root.mountfrom=\"${ROOTFSTYPE}:${ROOTFS}\"" >> ${FSMNT}/boot/loader.conf 122 rc_halt "mkdir -p ${FSMNT}/${BOOTMNT}/boot" 123 rc_halt "mv ${FSMNT}/boot/* ${FSMNT}${BOOTMNT}/boot/" 124 rc_halt "mv ${FSMNT}${BOOTMNT}/boot ${FSMNT}/boot/" 125 rc_halt "umount /dev/${BOOTFS}" 126 rc_halt "mount /dev/${BOOTFS} ${FSMNT}${BOOTMNT}" 127 rc_halt "rmdir ${FSMNT}/boot" 128 129 # Strip the '/' from BOOTMNT before making symlink 130 BOOTMNTNS="`echo ${BOOTMNT} | sed 's|/||g'`" 131 rc_halt "chroot ${FSMNT} ln -s ${BOOTMNTNS}/boot /boot" 132 133}; 134 135# Function which creates the /etc/fstab for the installed system 136setup_fstab() 137{ 138 FSTAB="${FSMNT}/etc/fstab" 139 rm ${FSTAB} >/dev/null 2>/dev/null 140 141 # Create the header 142 echo "# Device Mountpoint FStype Options Dump Pass" >> ${FSTAB} 143 144 # Loop through the partitions, and start creating /etc/fstab 145 for PART in `ls ${PARTDIR}` 146 do 147 PARTFS="`cat ${PARTDIR}/${PART} | cut -d ':' -f 1`" 148 PARTMNT="`cat ${PARTDIR}/${PART} | cut -d ':' -f 2`" 149 PARTENC="`cat ${PARTDIR}/${PART} | cut -d ':' -f 3`" 150 PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d ':' -f 4`" 151 152 DRIVE="`echo ${PART} | rev | cut -b 4- | rev`" 153 # Check if this device is being mirrored 154 if [ -e "${MIRRORCFGDIR}/${DRIVE}" ] 155 then 156 # This device is apart of a gmirror, lets reset PART to correct value 157 MDRIVE="mirror/`cat ${MIRRORCFGDIR}/${DRIVE} | cut -d ':' -f 3`" 158 TMP="`echo ${PART} | rev | cut -b -3 | rev`" 159 PART="${MDRIVE}${TMP}" 160 PARTLABEL="" 161 fi 162 163 # Unset EXT 164 EXT="" 165 166 # Set mount options for file-systems 167 case $PARTFS in 168 UFS+J) MNTOPTS="rw,noatime,async" ;;
|
169 SWAP) MNTOPTS="sw" ;;
170 *) MNTOPTS="rw,noatime" ;;
|
169 SWAP) MNTOPTS="sw" ;; 170 *) MNTOPTS="rw,noatime" ;; |
171 esac 172 173 174 # Figure out if we are using a glabel, or the raw name for this entry 175 if [ ! -z "${PARTLABEL}" ] 176 then 177 DEVICE="label/${PARTLABEL}" 178 else 179 # Check if using encryption 180 if [ "${PARTENC}" = "ON" ] ; then 181 EXT=".eli" 182 fi 183 184 if [ "${PARTFS}" = "UFS+J" ] ; then 185 EXT="${EXT}.journal" 186 fi 187 DEVICE="${PART}${EXT}" 188 fi 189 190 191 # Set our ROOTFSTYPE for loader.conf if necessary 192 check_for_mount "${PARTMNT}" "/" 193 if [ "$?" = "0" ] ; then 194 if [ "${PARTFS}" = "ZFS" ] ; then 195 ROOTFSTYPE="zfs" 196 XPOOLNAME=$(get_zpool_name "${PART}") 197 ROOTFS="${ZPOOLNAME}" 198 else 199 ROOTFS="${DEVICE}" 200 ROOTFSTYPE="ufs" 201 fi 202 fi 203 204 # Only create non-zfs partitions 205 if [ "${PARTFS}" != "ZFS" ] 206 then 207 208 # Make sure geom_journal is loaded 209 if [ "${PARTFS}" = "UFS+J" ] ; then 210 setup_gjournal 211 fi 212 213 # Save the BOOTFS for call at the end 214 if [ "${PARTMNT}" = "/boot" ] ; then 215 BOOTFS="${PART}${EXT}" 216 BOOTMNT="${BOOT_PART_MOUNT}" 217 PARTMNT="${BOOTMNT}" 218 fi 219 220 # Echo out the fstab entry now 221 if [ "${PARTFS}" = "SWAP" ] 222 then 223 echo "/dev/${DEVICE} none swap ${MNTOPTS} 0 0" >> ${FSTAB} 224 else 225 echo "/dev/${DEVICE} ${PARTMNT} ufs ${MNTOPTS} 1 1" >> ${FSTAB} 226 fi 227 228 fi # End of ZFS Check 229 done 230 231 # Setup some specific PC-BSD fstab options 232 if [ "$INSTALLTYPE" != "FreeBSD" ] 233 then 234 echo "procfs /proc procfs rw 0 0" >> ${FSTAB} 235 echo "linprocfs /compat/linux/proc linprocfs rw 0 0" >> ${FSTAB} 236 echo "tmpfs /tmp tmpfs rw,mode=1777 0 0" >> ${FSTAB} 237 fi 238 239 # If we have a dedicated /boot, run the post-install setup of it now 240 if [ ! -z "${BOOTMNT}" ] ; then 241 setup_dedicated_boot_part "${ROOTFS}" "${ROOTFSTYPE}" "${BOOTFS}" "${BOOTMNT}" 242 fi 243 244}; 245 246# Setup our disk mirroring with gmirror 247setup_gmirror() 248{ 249 NUM="0" 250 251 cd ${MIRRORCFGDIR} 252 for DISK in `ls *` 253 do 254 MIRRORDISK="`cat ${DISK} | cut -d ':' -f 1`" 255 MIRRORBAL="`cat ${DISK} | cut -d ':' -f 2`" 256 257 # Create this mirror device 258 gmirror label -vb $MIRRORBAL gm${NUM} /dev/${DISK} 259 260 sleep 3 261 262 # Save the gm<num> device in our config 263 echo "${MIRRORDISK}:${MIRRORBAL}:gm${NUM}" > ${DISK} 264 265 sleep 3 266 267 NUM="`expr ${NUM} + 1`" 268 done 269 270 271 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep 'geom_mirror_load="YES"' >/dev/null 2>/dev/null 272 if [ "$?" != "0" ] 273 then 274 echo 'geom_mirror_load="YES"' >>${FSMNT}/boot/loader.conf 275 fi 276 277}; 278 279# Function which saves geli keys and sets up loading of them at boot 280setup_geli_loading() 281{ 282 283 # Make our keys dir 284 mkdir -p ${FSMNT}/boot/keys >/dev/null 2>/dev/null 285 286 cd ${GELIKEYDIR} 287 for KEYFILE in `ls *` 288 do 289 # Figure out the partition name based on keyfile name removing .key 290 PART="`echo ${KEYFILE} | cut -d '.' -f 1`" 291 292 # Add the entries to loader.conf to start this geli provider at boot 293 echo "geli_${PART}_keyfile0_load=\"YES\"" >> ${FSMNT}/boot/loader.conf 294 echo "geli_${PART}_keyfile0_type=\"${PART}:geli_keyfile0\"" >> ${FSMNT}/boot/loader.conf 295 echo "geli_${PART}_keyfile0_name=\"/boot/keys/${KEYFILE}\"" >> ${FSMNT}/boot/loader.conf 296 297 # If we have a passphrase, set it up now 298 if [ -e "${PARTDIR}-enc/${PART}-encpass" ] ; then 299 cat ${PARTDIR}-enc/${PART}-encpass | geli setkey -S -n 0 -p -k ${KEYFILE} -K ${KEYFILE} ${PART} 300 geli configure -b ${PART} 301 fi 302 303 # Copy the key to the disk 304 cp ${KEYFILE} ${FSMNT}/boot/keys/${KEYFILE} 305 done 306 307 # Make sure we have geom_eli set to load at boot 308 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep 'geom_eli_load="YES"' >/dev/null 2>/dev/null 309 if [ "$?" != "0" ] 310 then 311 echo 'geom_eli_load="YES"' >>${FSMNT}/boot/loader.conf 312 fi 313 314}; 315 316 317# Function to generate a random hostname if none was specified 318gen_hostname() 319{ 320 RAND="`jot -r 1 1 9000`" 321 322 if [ "$INSTALLTYPE" = "FreeBSD" ] 323 then 324 VAL="freebsd-${RAND}" 325 else 326 VAL="pcbsd-${RAND}" 327 fi 328 329 export VAL 330 331}; 332 333# Function which sets up the hostname for the system 334setup_hostname() 335{ 336 337 get_value_from_cfg hostname 338 HOSTNAME="${VAL}" 339 340 # If we don't have a hostname, make one up 341 if [ -z "${HOSTNAME}" ] 342 then 343 gen_hostname 344 HOSTNAME="${VAL}" 345 fi 346 347 # Clean up any saved hostname 348 cat ${FSMNT}/etc/rc.conf | grep -v "hostname=" >${FSMNT}/etc/rc.conf.new 349 mv ${FSMNT}/etc/rc.conf.new ${FSMNT}/etc/rc.conf 350 351 # Set the hostname now 352 echo_log "Setting hostname: ${HOSTNAME}" 353 echo "hostname=\"${HOSTNAME}\"" >> ${FSMNT}/etc/rc.conf 354 sed -i -e "s|my.domain|${HOSTNAME} ${HOSTNAME}|g" ${FSMNT}/etc/hosts 355 356}; 357 358 359# Check and make sure geom_journal is enabled on the system 360setup_gjournal() 361{ 362 363 # Make sure we have geom_journal set to load at boot 364 cat ${FSMNT}/boot/loader.conf 2>/dev/null | grep 'geom_journal_load="YES"' >/dev/null 2>/dev/null 365 if [ "$?" != "0" ] 366 then 367 echo 'geom_journal_load="YES"' >>${FSMNT}/boot/loader.conf 368 fi 369 370}; 371 372# Function which sets the root password from the install config 373set_root_pw() 374{ 375 get_value_from_cfg_with_spaces rootPass 376 PW="${VAL}" 377 378 # If we don't have a root pass, return 379 if [ -z "${PW}" ] 380 then 381 return 0 382 fi 383 384 echo_log "Setting root password" 385 echo "${PW}" > ${FSMNT}/.rootpw 386 run_chroot_cmd "cat /.rootpw | pw usermod root -h 0" 387 rc_halt "rm ${FSMNT}/.rootpw" 388 389}; 390 391 392run_final_cleanup() 393{
|
394 # Check if we need to run any gmirror setup 395 ls ${MIRRORCFGDIR}/* >/dev/null 2>/dev/null 396 if [ "$?" = "0" ] 397 then 398 # Lets setup gmirror now 399 setup_gmirror 400 fi |
401
|
395 # Check if we need to run any gmirror setup
396 ls ${MIRRORCFGDIR}/* >/dev/null 2>/dev/null
397 if [ "$?" = "0" ]
398 then
399 # Lets setup gmirror now
400 setup_gmirror
401 fi
|
402 # Check if we need to save any geli keys 403 ls ${GELIKEYDIR}/* >/dev/null 2>/dev/null 404 if [ "$?" = "0" ] 405 then 406 # Lets setup geli loading 407 setup_geli_loading 408 fi |
409
|
403 # Check if we need to save any geli keys
404 ls ${GELIKEYDIR}/* >/dev/null 2>/dev/null
405 if [ "$?" = "0" ]
406 then
407 # Lets setup geli loading
408 setup_geli_loading
409 fi
|
410 # Set a hostname on the install system 411 setup_hostname |
412
|
411 # Set a hostname on the install system
412 setup_hostname
|
413 # Set the root_pw if it is specified 414 set_root_pw |
415
|
414 # Set the root_pw if it is specified
415 set_root_pw
416
417 # Generate the fstab for the installed system
418 setup_fstab
419
|
416 # Generate the fstab for the installed system 417 setup_fstab |
418};
|