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$
27
28# Functions which unmount all mounted disk filesystems
29
30# Unmount all mounted partitions under specified dir
31umount_all_dir()
32{
33  _udir="$1"
34  _umntdirs=`mount | sort -r | grep "on $_udir" | cut -d ' ' -f 3`
35  for _ud in $_umntdirs
36  do
37    umount -f ${_ud} 
38  done
39}
40
41# Script that adds our gmirror devices for syncing
42start_gmirror_sync()
43{
44
45  cd ${MIRRORCFGDIR}
46  for DISK in `ls ${MIRRORCFGDIR}`
47  do
48    MIRRORDISK="`cat ${DISK} | cut -d ':' -f 1`"
49    MIRRORBAL="`cat ${DISK} | cut -d ':' -f 2`"
50    MIRRORNAME="`cat ${DISK} | cut -d ':' -f 3`"
51   
52    # Start the mirroring service
53    rc_nohalt "gmirror forget ${MIRRORNAME}"
54    rc_halt "gmirror insert ${MIRRORNAME} ${MIRRORDISK}"
55
56  done
57
58};
59
60# Unmounts all our mounted file-systems
61unmount_all_filesystems()
62{
63  # Copy the logfile to disk before we unmount
64  cp ${LOGOUT} ${FSMNT}/root/pc-sysinstall.log
65  cd /
66
67  # Start by unmounting any ZFS partitions
68  zfs_cleanup_unmount
69
70  # Lets read our partition list, and unmount each
71  ##################################################################
72  for PART in `ls ${PARTDIR}`
73  do
74    PARTDEV=`echo $PART | sed 's|-|/|g'`    
75    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
76    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
77    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
78    PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d '#' -f 4`"
79
80    if [ "${PARTENC}" = "ON" ]
81    then
82      EXT=".eli"
83    else
84      EXT=""
85    fi
86
87    if [ "${PARTFS}" = "SWAP" ]
88    then
89      rc_nohalt "swapoff ${PARTDEV}${EXT}"
90    fi
91
92    # Check if we've found "/", and unmount that last
93    if [ "$PARTMNT" != "/" -a "${PARTMNT}" != "none" -a "${PARTFS}" != "ZFS" ]
94    then
95      rc_halt "umount -f ${PARTDEV}${EXT}"
96
97      # Re-check if we are missing a label for this device and create it again if so
98      if [ ! -e "/dev/label/${PARTLABEL}" ]
99      then
100        case ${PARTFS} in
101          UFS) glabel label ${PARTLABEL} ${PARTDEV}${EXT} ;;
102          UFS+S) glabel label ${PARTLABEL} ${PARTDEV}${EXT} ;;
103          UFS+SUJ) glabel label ${PARTLABEL} ${PARTDEV}${EXT} ;;
104          UFS+J) glabel label ${PARTLABEL} ${PARTDEV}${EXT}.journal ;;
105          *) ;;
106        esac 
107      fi
108    fi
109
110    # Check if we've found "/" and make sure the label exists
111    if [ "$PARTMNT" = "/" -a "${PARTFS}" != "ZFS" ]
112    then
113      if [ ! -e "/dev/label/${PARTLABEL}" ]
114      then
115        case ${PARTFS} in
116          UFS) ROOTRELABEL="glabel label ${PARTLABEL} ${PARTDEV}${EXT}" ;;
117          UFS+S) ROOTRELABEL="glabel label ${PARTLABEL} ${PARTDEV}${EXT}" ;;
118          UFS+SUJ) ROOTRELABEL="glabel label ${PARTLABEL} ${PARTDEV}${EXT}" ;;
119          UFS+J) ROOTRELABEL="glabel label ${PARTLABEL} ${PARTDEV}${EXT}.journal" ;;
120          *) ;;
121        esac 
122      fi
123    fi
124  done
125
126  # Last lets the /mnt partition
127  #########################################################
128  rc_nohalt "umount -f ${FSMNT}"
129
130   # If are using a ZFS on "/" set it to legacy
131  if [ ! -z "${FOUNDZFSROOT}" ]
132  then
133    rc_halt "zfs set mountpoint=legacy ${FOUNDZFSROOT}"
134  fi
135
136  # If we need to relabel "/" do it now
137  if [ ! -z "${ROOTRELABEL}" ]
138  then
139    ${ROOTRELABEL}
140  fi
141
142  # Unmount our CDMNT
143  rc_nohalt "umount -f ${CDMNT}" >/dev/null 2>/dev/null
144
145  # Check if we need to run any gmirror syncing
146  ls ${MIRRORCFGDIR}/* >/dev/null 2>/dev/null
147  if [ $? -eq 0 ]
148  then
149    # Lets start syncing now
150    start_gmirror_sync
151  fi
152
153};
154
155# Unmounts any filesystems after a failure
156unmount_all_filesystems_failure()
157{
158  cd /
159
160  # if we did a fresh install, start unmounting
161  if [ "${INSTALLMODE}" = "fresh" ]
162  then
163
164    # Lets read our partition list, and unmount each
165    ##################################################################
166    if [ -d "${PARTDIR}" ]
167    then
168    for PART in `ls ${PARTDIR}`
169    do
170      PARTDEV=`echo $PART | sed 's|-|/|g'` 
171      PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
172      PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
173      PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
174
175      if [ "${PARTFS}" = "SWAP" ]
176      then
177        if [ "${PARTENC}" = "ON" ]
178        then
179          swapoff ${PARTDEV}.eli >/dev/null 2>/dev/null
180        else
181          swapoff ${PARTDEV} >/dev/null 2>/dev/null
182        fi
183      fi
184
185      # Check if we've found "/" again, don't need to mount it twice
186      if [ "$PARTMNT" != "/" -a "${PARTMNT}" != "none" -a "${PARTFS}" != "ZFS" ]
187      then
188        umount -f ${PARTDEV} >/dev/null 2>/dev/null
189        umount -f ${FSMNT}${PARTMNT} >/dev/null 2>/dev/null
190      fi
191    done
192
193    # Last lets the /mnt partition
194    #########################################################
195    umount -f ${FSMNT} >/dev/null 2>/dev/null
196
197   fi
198  else
199    # We are doing a upgrade, try unmounting any of these filesystems
200    chroot ${FSMNT} /sbin/umount -a >/dev/null 2>/dev/null
201    umount -f ${FSMNT}/usr >/dev/null 2>/dev/null
202    umount -f ${FSMNT}/dev >/dev/null 2>/dev/null
203    umount -f ${FSMNT} >/dev/null 2>/dev/null 
204    sh ${TMPDIR}/.upgrade-unmount >/dev/null 2>/dev/null
205  fi
206   
207  # Unmount our CDMNT
208  umount ${CDMNT} >/dev/null 2>/dev/null
209
210};
211