1209513Simp#!/bin/sh
2209513Simp#-
3209552Simp# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4209513Simp#
5209513Simp# Redistribution and use in source and binary forms, with or without
6209513Simp# modification, are permitted provided that the following conditions
7209513Simp# are met:
8209513Simp# 1. Redistributions of source code must retain the above copyright
9209513Simp#    notice, this list of conditions and the following disclaimer.
10209513Simp# 2. Redistributions in binary form must reproduce the above copyright
11209513Simp#    notice, this list of conditions and the following disclaimer in the
12209513Simp#    documentation and/or other materials provided with the distribution.
13209513Simp#
14209513Simp# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15209513Simp# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16209513Simp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17209513Simp# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18209513Simp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19209513Simp# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20209513Simp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21209513Simp# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22209513Simp# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23209513Simp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24209513Simp# SUCH DAMAGE.
25209513Simp#
26209513Simp# $FreeBSD$
27209513Simp
28209513Simp# Functions which unmount all mounted disk filesystems
29209513Simp
30209513Simp# Unmount all mounted partitions under specified dir
31211730Simpumount_all_dir()
32211730Simp{
33211730Simp  _udir="$1"
34211730Simp  _umntdirs=`mount | sort -r | grep "on $_udir" | cut -d ' ' -f 3`
35211730Simp  for _ud in $_umntdirs
36211730Simp  do
37211730Simp    umount -f ${_ud} 
38211730Simp  done
39209513Simp}
40209513Simp
41209513Simp# Script that adds our gmirror devices for syncing
42209513Simpstart_gmirror_sync()
43209513Simp{
44209513Simp
45220909Sjpaetzel  cd ${MIRRORCFGDIR}
46227118Sjpaetzel  for DISK in `ls ${MIRRORCFGDIR}`
47209513Simp  do
48209513Simp    MIRRORDISK="`cat ${DISK} | cut -d ':' -f 1`"
49209513Simp    MIRRORBAL="`cat ${DISK} | cut -d ':' -f 2`"
50209513Simp    MIRRORNAME="`cat ${DISK} | cut -d ':' -f 3`"
51209513Simp   
52209513Simp    # Start the mirroring service
53220909Sjpaetzel    rc_nohalt "gmirror forget ${MIRRORNAME}"
54227118Sjpaetzel    rc_halt "gmirror insert ${MIRRORNAME} ${MIRRORDISK}"
55209513Simp
56209513Simp  done
57209513Simp
58209513Simp};
59209513Simp
60209513Simp# Unmounts all our mounted file-systems
61209513Simpunmount_all_filesystems()
62209513Simp{
63211730Simp  # Copy the logfile to disk before we unmount
64211730Simp  cp ${LOGOUT} ${FSMNT}/root/pc-sysinstall.log
65211730Simp  cd /
66209513Simp
67211730Simp  # Start by unmounting any ZFS partitions
68211730Simp  zfs_cleanup_unmount
69209513Simp
70211730Simp  # Lets read our partition list, and unmount each
71211730Simp  ##################################################################
72211730Simp  for PART in `ls ${PARTDIR}`
73211730Simp  do
74220909Sjpaetzel    PARTDEV=`echo $PART | sed 's|-|/|g'`    
75232899Sjpaetzel    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
76232899Sjpaetzel    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
77232899Sjpaetzel    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
78232899Sjpaetzel    PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d '#' -f 4`"
79209513Simp
80211730Simp    if [ "${PARTENC}" = "ON" ]
81211730Simp    then
82211730Simp      EXT=".eli"
83211730Simp    else
84211730Simp      EXT=""
85211730Simp    fi
86209513Simp
87220909Sjpaetzel    if [ "${PARTFS}" = "SWAP" ]
88220909Sjpaetzel    then
89220909Sjpaetzel      rc_nohalt "swapoff ${PARTDEV}${EXT}"
90220909Sjpaetzel    fi
91209513Simp
92211730Simp    # Check if we've found "/", and unmount that last
93211730Simp    if [ "$PARTMNT" != "/" -a "${PARTMNT}" != "none" -a "${PARTFS}" != "ZFS" ]
94211730Simp    then
95220909Sjpaetzel      rc_halt "umount -f ${PARTDEV}${EXT}"
96209513Simp
97211730Simp      # Re-check if we are missing a label for this device and create it again if so
98211730Simp      if [ ! -e "/dev/label/${PARTLABEL}" ]
99211730Simp      then
100211730Simp        case ${PARTFS} in
101220909Sjpaetzel          UFS) glabel label ${PARTLABEL} ${PARTDEV}${EXT} ;;
102220909Sjpaetzel          UFS+S) glabel label ${PARTLABEL} ${PARTDEV}${EXT} ;;
103220909Sjpaetzel          UFS+SUJ) glabel label ${PARTLABEL} ${PARTDEV}${EXT} ;;
104220909Sjpaetzel          UFS+J) glabel label ${PARTLABEL} ${PARTDEV}${EXT}.journal ;;
105211730Simp          *) ;;
106211730Simp        esac 
107211730Simp      fi
108211730Simp    fi
109209513Simp
110211730Simp    # Check if we've found "/" and make sure the label exists
111211730Simp    if [ "$PARTMNT" = "/" -a "${PARTFS}" != "ZFS" ]
112211730Simp    then
113211730Simp      if [ ! -e "/dev/label/${PARTLABEL}" ]
114211730Simp      then
115211730Simp        case ${PARTFS} in
116220909Sjpaetzel          UFS) ROOTRELABEL="glabel label ${PARTLABEL} ${PARTDEV}${EXT}" ;;
117220909Sjpaetzel          UFS+S) ROOTRELABEL="glabel label ${PARTLABEL} ${PARTDEV}${EXT}" ;;
118220909Sjpaetzel          UFS+SUJ) ROOTRELABEL="glabel label ${PARTLABEL} ${PARTDEV}${EXT}" ;;
119220909Sjpaetzel          UFS+J) ROOTRELABEL="glabel label ${PARTLABEL} ${PARTDEV}${EXT}.journal" ;;
120211730Simp          *) ;;
121211730Simp        esac 
122211730Simp      fi
123211730Simp    fi
124211730Simp  done
125209513Simp
126211730Simp  # Last lets the /mnt partition
127211730Simp  #########################################################
128211730Simp  rc_nohalt "umount -f ${FSMNT}"
129209513Simp
130211730Simp   # If are using a ZFS on "/" set it to legacy
131211730Simp  if [ ! -z "${FOUNDZFSROOT}" ]
132211730Simp  then
133211730Simp    rc_halt "zfs set mountpoint=legacy ${FOUNDZFSROOT}"
134211730Simp  fi
135209513Simp
136211730Simp  # If we need to relabel "/" do it now
137211730Simp  if [ ! -z "${ROOTRELABEL}" ]
138211730Simp  then
139211730Simp    ${ROOTRELABEL}
140211730Simp  fi
141209513Simp
142211730Simp  # Unmount our CDMNT
143211730Simp  rc_nohalt "umount -f ${CDMNT}" >/dev/null 2>/dev/null
144209513Simp
145211730Simp  # Check if we need to run any gmirror syncing
146211730Simp  ls ${MIRRORCFGDIR}/* >/dev/null 2>/dev/null
147220059Sjpaetzel  if [ $? -eq 0 ]
148211730Simp  then
149211730Simp    # Lets start syncing now
150211730Simp    start_gmirror_sync
151211730Simp  fi
152209513Simp
153209513Simp};
154209513Simp
155209513Simp# Unmounts any filesystems after a failure
156209513Simpunmount_all_filesystems_failure()
157209513Simp{
158209513Simp  cd /
159209513Simp
160209513Simp  # if we did a fresh install, start unmounting
161209513Simp  if [ "${INSTALLMODE}" = "fresh" ]
162209513Simp  then
163209513Simp
164209513Simp    # Lets read our partition list, and unmount each
165209513Simp    ##################################################################
166209513Simp    if [ -d "${PARTDIR}" ]
167209513Simp    then
168209513Simp    for PART in `ls ${PARTDIR}`
169209513Simp    do
170220909Sjpaetzel      PARTDEV=`echo $PART | sed 's|-|/|g'` 
171232899Sjpaetzel      PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
172232899Sjpaetzel      PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
173232899Sjpaetzel      PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
174209513Simp
175220909Sjpaetzel      if [ "${PARTFS}" = "SWAP" ]
176220909Sjpaetzel      then
177220909Sjpaetzel        if [ "${PARTENC}" = "ON" ]
178220909Sjpaetzel        then
179225657Sjpaetzel          swapoff ${PARTDEV}.eli >/dev/null 2>/dev/null
180220909Sjpaetzel        else
181225657Sjpaetzel          swapoff ${PARTDEV} >/dev/null 2>/dev/null
182220909Sjpaetzel        fi
183220909Sjpaetzel      fi
184209513Simp
185211730Simp      # Check if we've found "/" again, don't need to mount it twice
186211730Simp      if [ "$PARTMNT" != "/" -a "${PARTMNT}" != "none" -a "${PARTFS}" != "ZFS" ]
187211730Simp      then
188225657Sjpaetzel        umount -f ${PARTDEV} >/dev/null 2>/dev/null
189225657Sjpaetzel        umount -f ${FSMNT}${PARTMNT} >/dev/null 2>/dev/null
190211730Simp      fi
191211730Simp    done
192209513Simp
193211730Simp    # Last lets the /mnt partition
194211730Simp    #########################################################
195225657Sjpaetzel    umount -f ${FSMNT} >/dev/null 2>/dev/null
196209513Simp
197209513Simp   fi
198211730Simp  else
199211730Simp    # We are doing a upgrade, try unmounting any of these filesystems
200225657Sjpaetzel    chroot ${FSMNT} /sbin/umount -a >/dev/null 2>/dev/null
201225657Sjpaetzel    umount -f ${FSMNT}/usr >/dev/null 2>/dev/null
202225657Sjpaetzel    umount -f ${FSMNT}/dev >/dev/null 2>/dev/null
203225657Sjpaetzel    umount -f ${FSMNT} >/dev/null 2>/dev/null 
204225657Sjpaetzel    sh ${TMPDIR}/.upgrade-unmount >/dev/null 2>/dev/null
205211730Simp  fi
206209513Simp   
207211730Simp  # Unmount our CDMNT
208225657Sjpaetzel  umount ${CDMNT} >/dev/null 2>/dev/null
209209513Simp
210209513Simp};
211