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 perform the mounting / unmount for upgrades
29209513Simp
30209513Simp. ${PROGDIR}/backend/functions-unmount.sh
31209513Simp
32209513Simpmount_target_slice()
33209513Simp{
34209513Simp  MPART="${1}"
35209513Simp
36209513Simp  # Import any zpools
37209513Simp  zpool import -o altroot=${FSMNT} -a
38209513Simp  umount_all_dir "${FSMNT}"
39209513Simp  
40209513Simp  # Set a variable of files we want to make backups of before doing upgrade
41209513Simp  BKFILES="/etc/rc.conf /boot/loader.conf"
42209513Simp
43209513Simp  if [ -e "/dev/${MPART}" ] ; then
44209513Simp    rc_nohalt "mount /dev/${MPART} ${FSMNT}"
45220059Sjpaetzel    if [ $? -ne 0 ] ; then
46211730Simp      # Check if we have ZFS tank name
47211730Simp      rc_halt "mount -t zfs ${MPART} ${FSMNT}"
48209513Simp    fi
49209513Simp  else
50209513Simp    # Check if we have ZFS tank name
51209513Simp    rc_halt "mount -t zfs ${MPART} ${FSMNT}"
52209513Simp  fi
53209513Simp
54209513Simp  # Mount devfs in chroot
55209513Simp  mount -t devfs devfs ${FSMNT}/dev
56209513Simp
57209513Simp  # Check if we have any ZFS partitions to mount
58209513Simp  zfs mount -a
59209513Simp
60209513Simp  # Mount all the fstab goodies on disk
61220059Sjpaetzel  chroot ${FSMNT} /sbin/mount -a -t nolinprocfs >>${LOGOUT} 2>>${LOGOUT}
62209513Simp  chroot ${FSMNT} umount /proc >/dev/null 2>/dev/null 
63209513Simp  chroot ${FSMNT} umount /compat/linux/proc  >/dev/null 2>/dev/null
64209513Simp
65209513Simp  # Now before we start the upgrade, make sure we set our noschg flags
66209513Simp  echo_log "Cleaning up old filesystem... Please wait..."
67209513Simp  rc_halt "chflags -R noschg ${FSMNT}"
68209513Simp
69209513Simp  # Make backup copies of some files
70209513Simp  for i in ${BKFILES}
71209513Simp  do
72209513Simp    cp ${FSMNT}${i} ${FSMNT}${i}.preUpgrade >/dev/null 2>/dev/null
73209513Simp  done
74209513Simp
75209513Simp  # Remove some old dirs
76209513Simp  rm -rf ${FSMNT}/etc/rc.d >/dev/null 2>/dev/null
77209513Simp
78209513Simp  # If we are doing PC-BSD install, lets cleanup old pkgs on disk
79209513Simp  if [ "$INSTALLTYPE" != "FreeBSD" ]
80209513Simp  then
81209513Simp    echo_log "Removing old packages, this may take a while... Please wait..."
82218960Sjpaetzel    echo '#!/bin/sh
83220059Sjpaetzelfor i in `pkg_info -aE`
84209513Simpdo
85209513Simp  echo "Uninstalling package: ${i}"
86209513Simp  pkg_delete -f ${i} >/dev/null 2>/dev/null
87209513Simpdone
88209513Simp' >${FSMNT}/.cleanPkgs.sh
89209513Simp    chmod 755 ${FSMNT}/.cleanPkgs.sh
90209513Simp    chroot ${FSMNT} /.cleanPkgs.sh
91209513Simp    rm ${FSMNT}/.cleanPkgs.sh
92209513Simp    run_chroot_cmd "pkg_delete -f \*" >/dev/null 2>/dev/null
93209513Simp    run_chroot_cmd "rm -rf /usr/PCBSD" >/dev/null 2>/dev/null
94209513Simp    run_chroot_cmd "rm -rf /PCBSD" >/dev/null 2>/dev/null
95209513Simp    run_chroot_cmd "rm -rf /var/db/pkgs" >/dev/null 2>/dev/null
96209513Simp    run_chroot_cmd "rm -rf /usr/local32" >/dev/null 2>/dev/null
97209513Simp    run_chroot_cmd "rm -rf /usr/sbin" >/dev/null 2>/dev/null
98209513Simp    run_chroot_cmd "rm -rf /usr/lib" >/dev/null 2>/dev/null
99209513Simp    run_chroot_cmd "rm -rf /usr/bin" >/dev/null 2>/dev/null
100209513Simp    run_chroot_cmd "rm -rf /boot/kernel" >/dev/null 2>/dev/null
101209513Simp    run_chroot_cmd "rm -rf /sbin" >/dev/null 2>/dev/null
102209513Simp    run_chroot_cmd "rm -rf /bin" >/dev/null 2>/dev/null
103209513Simp    run_chroot_cmd "rm -rf /lib" >/dev/null 2>/dev/null
104209513Simp    run_chroot_cmd "rm -rf /libexec" >/dev/null 2>/dev/null
105209513Simp  fi
106209513Simp
107211730Simp};
108209513Simp
109209513Simp# Mount the target upgrade partitions
110209513Simpmount_upgrade()
111209513Simp{
112209513Simp
113209513Simp  # Make sure we remove the old upgrade-mount script
114209513Simp  rm -rf ${TMPDIR}/.upgrade-unmount >/dev/null 2>/dev/null
115209513Simp
116209513Simp  # We are ready to start mounting, lets read the config and do it
117209513Simp  while read line
118209513Simp  do
119220059Sjpaetzel    echo $line | grep -q "^disk0=" 2>/dev/null
120220059Sjpaetzel    if [ $? -eq 0 ]
121211730Simp    then
122209513Simp
123211730Simp      # Found a disk= entry, lets get the disk we are working on
124211730Simp      get_value_from_string "${line}"
125211730Simp      strip_white_space "$VAL"
126211730Simp      DISK="$VAL"
127211730Simp    fi
128209513Simp
129220059Sjpaetzel    echo $line | grep -q "^commitDiskPart" 2>/dev/null
130220059Sjpaetzel    if [ $? -eq 0 ]
131211730Simp    then
132211730Simp      # Found our flag to commit this disk setup / lets do sanity check and do it
133220059Sjpaetzel      if [ -n "${DISK}" ]
134211730Simp      then
135209513Simp
136211730Simp        # Start mounting this slice
137211730Simp        mount_target_slice "${DISK}" 
138209513Simp
139211730Simp        # Increment our disk counter to look for next disk and unset
140211730Simp        unset DISK
141211730Simp	    break
142211730Simp      else
143211730Simp        exit_err "ERROR: commitDiskPart was called without procceding disk<num>= and partition= entries!!!" 
144211730Simp      fi
145211730Simp    fi
146209513Simp
147209513Simp  done <${CFGF}
148209513Simp
149209513Simp};
150209513Simp
151209513Simpcopy_skel_files_upgrade()
152209513Simp{
153209513Simp
154211730Simp  # Now make sure we fix any user profile scripts, which cause problems from 7.x->8.x
155211730Simp  echo '#!/bin/sh
156209513Simp
157209513Simpcd /home
158209513Simpfor i in `ls`
159209513Simpdo
160209513Simp
161209513Simp  # Backup the old profile dirs
162209513Simp  if [ -d "${i}" ]
163209513Simp  then
164209513Simp    mv /home/${i}/.kde4 /home/${i}/.kde4.preUpgrade >/dev/null 2>/dev/null
165209513Simp    mv /home/${i}/.kde /home/${i}/.kde.preUpgrade >/dev/null 2>/dev/null
166209513Simp    mv /home/${i}/.fluxbox /home/${i}/.fluxbox.preUpgrade >/dev/null 2>/dev/null
167209513Simp
168209513Simp    # Copy over the skel directories
169209513Simp    tar cv --exclude "./dot.*" -f - -C /usr/share/skel . 2>/dev/null | tar xvf - -C /home/${i} 2>/dev/null
170209513Simp
171209513Simp    for j in `ls /usr/share/skel/dot*`
172209513Simp    do
173209513Simp      dname=`echo ${j} | sed s/dot//`
174209513Simp      cp /usr/share/skel/${j} /home/${i}/${dname}
175209513Simp    done
176209513Simp
177209513Simp    chown -R ${i}:${i} /home/${i}
178209513Simp  fi
179209513Simp
180209513Simpdone
181209513Simp' >${FSMNT}/.fixUserProfile.sh  
182211730Simp  chmod 755 ${FSMNT}/.fixUserProfile.sh
183211730Simp  chroot ${FSMNT} /.fixUserProfile.sh >/dev/null 2>/dev/null
184211730Simp  rm ${FSMNT}/.fixUserProfile.sh
185209513Simp
186209513Simp
187209513Simp
188211730Simp  # if the user wants to keep their original .kde4 profile
189211730Simp  ###########################################################################
190211730Simp  get_value_from_cfg "upgradeKeepDesktopProfile"
191211730Simp  if [ "$VAL" = "YES" -o "$VAL" = "yes" ] ; then
192211730Simp    echo '#!/bin/sh
193209513Simp      cd /home
194209513Simpfor i in `ls`
195209513Simpdo
196209513Simp  # Import the old config again
197209513Simp  if [ -d "${i}/.kde4.preUpgrade" ]
198209513Simp  then
199209513Simp    # Copy over the skel directories
200209513Simp    tar cv -f - -C /home/${i}/.kde4.preUpgrade . 2>/dev/null | tar xvf - -C /home/${i}/.kde4 2>/dev/null
201209513Simp    chown -R ${i}:${i} /home/${i}/.kde4
202209513Simp  fi
203209513Simpdone
204209513Simp' >${FSMNT}/.fixUserProfile.sh
205211730Simp    chmod 755 ${FSMNT}/.fixUserProfile.sh
206211730Simp    chroot ${FSMNT} /.fixUserProfile.sh >/dev/null 2>/dev/null
207211730Simp    rm ${FSMNT}/.fixUserProfile.sh
208209513Simp
209211730Simp  fi
210209513Simp
211209513Simp};
212209513Simp
213209513Simp# Function which merges some configuration files with the new defaults
214209513Simpmerge_old_configs()
215209513Simp{
216209513Simp
217209513Simp  # Merge the loader.conf with old
218209513Simp  cp ${FSMNT}/boot/loader.conf ${FSMNT}/boot/loader.conf.new
219209513Simp  merge_config "${FSMNT}/boot/loader.conf.preUpgrade" "${FSMNT}/boot/loader.conf.new" "${FSMNT}/boot/loader.conf"
220209513Simp  rm ${FSMNT}/boot/loader.conf.new
221209513Simp
222209513Simp  # Merge the rc.conf with old
223209513Simp  cp ${FSMNT}/etc/rc.conf ${FSMNT}/etc/rc.conf.new
224209513Simp  merge_config "${FSMNT}/etc/rc.conf.preUpgrade" "${FSMNT}/etc/rc.conf.new" "${FSMNT}/etc/rc.conf"
225209513Simp  rm ${FSMNT}/etc/rc.conf.new
226209513Simp
227209513Simp};
228209513Simp
229209513Simp# Function which unmounts all the mounted file-systems
230209513Simpunmount_upgrade()
231209513Simp{
232209513Simp
233211730Simp  # If on PC-BSD, make sure we copy any fixed skel files
234211730Simp  if [ "$INSTALLTYPE" != "FreeBSD" ] ; then
235211730Simp    copy_skel_files_upgrade
236211730Simp  fi
237209513Simp
238211730Simp  cd /
239209513Simp
240211730Simp  # Unmount FS
241211730Simp  umount_all_dir "${FSMNT}"
242209513Simp
243211730Simp  # Run our saved unmount script for these file-systems
244211730Simp  rc_nohalt "umount -f ${FSMNT}"
245209513Simp 
246211730Simp  umount ${CDMNT} 
247209513Simp};
248