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