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 perform the mounting / unmount for upgrades
29
30. ${PROGDIR}/backend/functions-unmount.sh
31
32mount_target_slice()
33{
34  MPART="${1}"
35
36  # Import any zpools
37  zpool import -o altroot=${FSMNT} -a
38  umount_all_dir "${FSMNT}"
39  
40  # Set a variable of files we want to make backups of before doing upgrade
41  BKFILES="/etc/rc.conf /boot/loader.conf"
42
43  if [ -e "/dev/${MPART}" ] ; then
44    rc_nohalt "mount /dev/${MPART} ${FSMNT}"
45    if [ $? -ne 0 ] ; then
46      # Check if we have ZFS tank name
47      rc_halt "mount -t zfs ${MPART} ${FSMNT}"
48    fi
49  else
50    # Check if we have ZFS tank name
51    rc_halt "mount -t zfs ${MPART} ${FSMNT}"
52  fi
53
54  # Mount devfs in chroot
55  mount -t devfs devfs ${FSMNT}/dev
56
57  # Check if we have any ZFS partitions to mount
58  zfs mount -a
59
60  # Mount all the fstab goodies on disk
61  chroot ${FSMNT} /sbin/mount -a -t nolinprocfs >>${LOGOUT} 2>>${LOGOUT}
62  chroot ${FSMNT} umount /proc >/dev/null 2>/dev/null 
63  chroot ${FSMNT} umount /compat/linux/proc  >/dev/null 2>/dev/null
64
65  # Now before we start the upgrade, make sure we set our noschg flags
66  echo_log "Cleaning up old filesystem... Please wait..."
67  rc_halt "chflags -R noschg ${FSMNT}"
68
69  # Make backup copies of some files
70  for i in ${BKFILES}
71  do
72    cp ${FSMNT}${i} ${FSMNT}${i}.preUpgrade >/dev/null 2>/dev/null
73  done
74
75  # Remove some old dirs
76  rm -rf ${FSMNT}/etc/rc.d >/dev/null 2>/dev/null
77
78  # If we are doing PC-BSD install, lets cleanup old pkgs on disk
79  if [ "$INSTALLTYPE" != "FreeBSD" ]
80  then
81    echo_log "Removing old packages, this may take a while... Please wait..."
82    echo '#!/bin/sh
83for i in `pkg_info -aE`
84do
85  echo "Uninstalling package: ${i}"
86  pkg_delete -f ${i} >/dev/null 2>/dev/null
87done
88' >${FSMNT}/.cleanPkgs.sh
89    chmod 755 ${FSMNT}/.cleanPkgs.sh
90    chroot ${FSMNT} /.cleanPkgs.sh
91    rm ${FSMNT}/.cleanPkgs.sh
92    run_chroot_cmd "pkg_delete -f \*" >/dev/null 2>/dev/null
93    run_chroot_cmd "rm -rf /usr/PCBSD" >/dev/null 2>/dev/null
94    run_chroot_cmd "rm -rf /PCBSD" >/dev/null 2>/dev/null
95    run_chroot_cmd "rm -rf /var/db/pkgs" >/dev/null 2>/dev/null
96    run_chroot_cmd "rm -rf /usr/local32" >/dev/null 2>/dev/null
97    run_chroot_cmd "rm -rf /usr/sbin" >/dev/null 2>/dev/null
98    run_chroot_cmd "rm -rf /usr/lib" >/dev/null 2>/dev/null
99    run_chroot_cmd "rm -rf /usr/bin" >/dev/null 2>/dev/null
100    run_chroot_cmd "rm -rf /boot/kernel" >/dev/null 2>/dev/null
101    run_chroot_cmd "rm -rf /sbin" >/dev/null 2>/dev/null
102    run_chroot_cmd "rm -rf /bin" >/dev/null 2>/dev/null
103    run_chroot_cmd "rm -rf /lib" >/dev/null 2>/dev/null
104    run_chroot_cmd "rm -rf /libexec" >/dev/null 2>/dev/null
105  fi
106
107};
108
109# Mount the target upgrade partitions
110mount_upgrade()
111{
112
113  # Make sure we remove the old upgrade-mount script
114  rm -rf ${TMPDIR}/.upgrade-unmount >/dev/null 2>/dev/null
115
116  # We are ready to start mounting, lets read the config and do it
117  while read line
118  do
119    echo $line | grep -q "^disk0=" 2>/dev/null
120    if [ $? -eq 0 ]
121    then
122
123      # Found a disk= entry, lets get the disk we are working on
124      get_value_from_string "${line}"
125      strip_white_space "$VAL"
126      DISK="$VAL"
127    fi
128
129    echo $line | grep -q "^commitDiskPart" 2>/dev/null
130    if [ $? -eq 0 ]
131    then
132      # Found our flag to commit this disk setup / lets do sanity check and do it
133      if [ -n "${DISK}" ]
134      then
135
136        # Start mounting this slice
137        mount_target_slice "${DISK}" 
138
139        # Increment our disk counter to look for next disk and unset
140        unset DISK
141	    break
142      else
143        exit_err "ERROR: commitDiskPart was called without procceding disk<num>= and partition= entries!!!" 
144      fi
145    fi
146
147  done <${CFGF}
148
149};
150
151copy_skel_files_upgrade()
152{
153
154  # Now make sure we fix any user profile scripts, which cause problems from 7.x->8.x
155  echo '#!/bin/sh
156
157cd /home
158for i in `ls`
159do
160
161  # Backup the old profile dirs
162  if [ -d "${i}" ]
163  then
164    mv /home/${i}/.kde4 /home/${i}/.kde4.preUpgrade >/dev/null 2>/dev/null
165    mv /home/${i}/.kde /home/${i}/.kde.preUpgrade >/dev/null 2>/dev/null
166    mv /home/${i}/.fluxbox /home/${i}/.fluxbox.preUpgrade >/dev/null 2>/dev/null
167
168    # Copy over the skel directories
169    tar cv --exclude "./dot.*" -f - -C /usr/share/skel . 2>/dev/null | tar xvf - -C /home/${i} 2>/dev/null
170
171    for j in `ls /usr/share/skel/dot*`
172    do
173      dname=`echo ${j} | sed s/dot//`
174      cp /usr/share/skel/${j} /home/${i}/${dname}
175    done
176
177    chown -R ${i}:${i} /home/${i}
178  fi
179
180done
181' >${FSMNT}/.fixUserProfile.sh  
182  chmod 755 ${FSMNT}/.fixUserProfile.sh
183  chroot ${FSMNT} /.fixUserProfile.sh >/dev/null 2>/dev/null
184  rm ${FSMNT}/.fixUserProfile.sh
185
186
187
188  # if the user wants to keep their original .kde4 profile
189  ###########################################################################
190  get_value_from_cfg "upgradeKeepDesktopProfile"
191  if [ "$VAL" = "YES" -o "$VAL" = "yes" ] ; then
192    echo '#!/bin/sh
193      cd /home
194for i in `ls`
195do
196  # Import the old config again
197  if [ -d "${i}/.kde4.preUpgrade" ]
198  then
199    # Copy over the skel directories
200    tar cv -f - -C /home/${i}/.kde4.preUpgrade . 2>/dev/null | tar xvf - -C /home/${i}/.kde4 2>/dev/null
201    chown -R ${i}:${i} /home/${i}/.kde4
202  fi
203done
204' >${FSMNT}/.fixUserProfile.sh
205    chmod 755 ${FSMNT}/.fixUserProfile.sh
206    chroot ${FSMNT} /.fixUserProfile.sh >/dev/null 2>/dev/null
207    rm ${FSMNT}/.fixUserProfile.sh
208
209  fi
210
211};
212
213# Function which merges some configuration files with the new defaults
214merge_old_configs()
215{
216
217  # Merge the loader.conf with old
218  cp ${FSMNT}/boot/loader.conf ${FSMNT}/boot/loader.conf.new
219  merge_config "${FSMNT}/boot/loader.conf.preUpgrade" "${FSMNT}/boot/loader.conf.new" "${FSMNT}/boot/loader.conf"
220  rm ${FSMNT}/boot/loader.conf.new
221
222  # Merge the rc.conf with old
223  cp ${FSMNT}/etc/rc.conf ${FSMNT}/etc/rc.conf.new
224  merge_config "${FSMNT}/etc/rc.conf.preUpgrade" "${FSMNT}/etc/rc.conf.new" "${FSMNT}/etc/rc.conf"
225  rm ${FSMNT}/etc/rc.conf.new
226
227};
228
229# Function which unmounts all the mounted file-systems
230unmount_upgrade()
231{
232
233  # If on PC-BSD, make sure we copy any fixed skel files
234  if [ "$INSTALLTYPE" != "FreeBSD" ] ; then
235    copy_skel_files_upgrade
236  fi
237
238  cd /
239
240  # Unmount FS
241  umount_all_dir "${FSMNT}"
242
243  # Run our saved unmount script for these file-systems
244  rc_nohalt "umount -f ${FSMNT}"
245 
246  umount ${CDMNT} 
247};
248