functions-unmount.sh revision 296373
1246149Ssjg#!/bin/sh
2246149Ssjg#-
3246149Ssjg# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4246149Ssjg#
5246149Ssjg# Redistribution and use in source and binary forms, with or without
6246149Ssjg# modification, are permitted provided that the following conditions
7246149Ssjg# are met:
8246149Ssjg# 1. Redistributions of source code must retain the above copyright
9246149Ssjg#    notice, this list of conditions and the following disclaimer.
10246149Ssjg# 2. Redistributions in binary form must reproduce the above copyright
11246149Ssjg#    notice, this list of conditions and the following disclaimer in the
12246149Ssjg#    documentation and/or other materials provided with the distribution.
13246149Ssjg#
14246149Ssjg# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15246149Ssjg# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16246149Ssjg# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17246149Ssjg# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18246149Ssjg# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19246149Ssjg# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20246149Ssjg# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21246149Ssjg# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22246149Ssjg# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23246149Ssjg# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24246149Ssjg# SUCH DAMAGE.
25246149Ssjg#
26246149Ssjg# $FreeBSD: releng/10.3/usr.sbin/pc-sysinstall/backend/functions-unmount.sh 232899 2012-03-12 21:32:43Z jpaetzel $
27246149Ssjg
28246149Ssjg# Functions which unmount all mounted disk filesystems
29246149Ssjg
30246149Ssjg# Unmount all mounted partitions under specified dir
31246149Ssjgumount_all_dir()
32246149Ssjg{
33246149Ssjg  _udir="$1"
34246149Ssjg  _umntdirs=`mount | sort -r | grep "on $_udir" | cut -d ' ' -f 3`
35246149Ssjg  for _ud in $_umntdirs
36246149Ssjg  do
37246149Ssjg    umount -f ${_ud} 
38246149Ssjg  done
39246149Ssjg}
40246149Ssjg
41246149Ssjg# Script that adds our gmirror devices for syncing
42246149Ssjgstart_gmirror_sync()
43246149Ssjg{
44246149Ssjg
45246149Ssjg  cd ${MIRRORCFGDIR}
46246149Ssjg  for DISK in `ls ${MIRRORCFGDIR}`
47246149Ssjg  do
48246149Ssjg    MIRRORDISK="`cat ${DISK} | cut -d ':' -f 1`"
49246149Ssjg    MIRRORBAL="`cat ${DISK} | cut -d ':' -f 2`"
50246149Ssjg    MIRRORNAME="`cat ${DISK} | cut -d ':' -f 3`"
51246149Ssjg   
52246149Ssjg    # Start the mirroring service
53246149Ssjg    rc_nohalt "gmirror forget ${MIRRORNAME}"
54246149Ssjg    rc_halt "gmirror insert ${MIRRORNAME} ${MIRRORDISK}"
55246149Ssjg
56246149Ssjg  done
57246149Ssjg
58246149Ssjg};
59246149Ssjg
60246149Ssjg# Unmounts all our mounted file-systems
61246149Ssjgunmount_all_filesystems()
62246149Ssjg{
63246149Ssjg  # Copy the logfile to disk before we unmount
64246149Ssjg  cp ${LOGOUT} ${FSMNT}/root/pc-sysinstall.log
65246149Ssjg  cd /
66246149Ssjg
67246149Ssjg  # Start by unmounting any ZFS partitions
68246149Ssjg  zfs_cleanup_unmount
69246149Ssjg
70246149Ssjg  # Lets read our partition list, and unmount each
71246149Ssjg  ##################################################################
72246149Ssjg  for PART in `ls ${PARTDIR}`
73246149Ssjg  do
74246149Ssjg    PARTDEV=`echo $PART | sed 's|-|/|g'`    
75246149Ssjg    PARTFS="`cat ${PARTDIR}/${PART} | cut -d '#' -f 1`"
76246149Ssjg    PARTMNT="`cat ${PARTDIR}/${PART} | cut -d '#' -f 2`"
77246149Ssjg    PARTENC="`cat ${PARTDIR}/${PART} | cut -d '#' -f 3`"
78246149Ssjg    PARTLABEL="`cat ${PARTDIR}/${PART} | cut -d '#' -f 4`"
79246149Ssjg
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