functions-upgrade.sh revision 218960
117680Spst#!/bin/sh
239300Sfenner#-
317680Spst# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
417680Spst#
517680Spst# Redistribution and use in source and binary forms, with or without
617680Spst# modification, are permitted provided that the following conditions
717680Spst# are met:
817680Spst# 1. Redistributions of source code must retain the above copyright
917680Spst#    notice, this list of conditions and the following disclaimer.
1017680Spst# 2. Redistributions in binary form must reproduce the above copyright
1117680Spst#    notice, this list of conditions and the following disclaimer in the
1217680Spst#    documentation and/or other materials provided with the distribution.
1317680Spst#
1417680Spst# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1517680Spst# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1617680Spst# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1717680Spst# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1817680Spst# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1917680Spst# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2056896Sfenner# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2156896Sfenner# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2217680Spst# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2317680Spst# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2417680Spst# SUCH DAMAGE.
2526183Sfenner#
2698527Sfenner# $FreeBSD: head/usr.sbin/pc-sysinstall/backend/functions-upgrade.sh 218960 2011-02-22 19:37:12Z jpaetzel $
2717680Spst
2817680Spst# Functions which perform the mounting / unmount for upgrades
2956896Sfenner
3056896Sfenner. ${PROGDIR}/backend/functions-unmount.sh
3156896Sfenner
3256896Sfennermount_target_slice()
3317680Spst{
3417680Spst  MPART="${1}"
3517680Spst
3617680Spst  # Import any zpools
3717680Spst  zpool import -o altroot=${FSMNT} -a
3817680Spst  umount_all_dir "${FSMNT}"
3917680Spst  
4017680Spst  # Set a variable of files we want to make backups of before doing upgrade
4117680Spst  BKFILES="/etc/rc.conf /boot/loader.conf"
4217680Spst
4375118Sfenner  if [ -e "/dev/${MPART}" ] ; then
4417680Spst    rc_nohalt "mount /dev/${MPART} ${FSMNT}"
4517680Spst    if [ "$?" != "0" ] ; then
4617680Spst      # Check if we have ZFS tank name
4775118Sfenner      rc_halt "mount -t zfs ${MPART} ${FSMNT}"
4875118Sfenner    fi
4975118Sfenner  else
5075118Sfenner    # Check if we have ZFS tank name
5175118Sfenner    rc_halt "mount -t zfs ${MPART} ${FSMNT}"
5275118Sfenner  fi
5375118Sfenner
5475118Sfenner  # Mount devfs in chroot
5575118Sfenner  mount -t devfs devfs ${FSMNT}/dev
5675118Sfenner
5775118Sfenner  # Check if we have any ZFS partitions to mount
5875118Sfenner  zfs mount -a
5975118Sfenner
6075118Sfenner  # Mount all the fstab goodies on disk
6198527Sfenner  chroot ${FSMNT} /sbin/mount -a -t nolinprocfs >>${LOGOUT} 2>>${LOGOUT
6275118Sfenner  chroot ${FSMNT} umount /proc >/dev/null 2>/dev/null 
6398527Sfenner  chroot ${FSMNT} umount /compat/linux/proc  >/dev/null 2>/dev/null
6498527Sfenner
6575118Sfenner  # Now before we start the upgrade, make sure we set our noschg flags
6675118Sfenner  echo_log "Cleaning up old filesystem... Please wait..."
6775118Sfenner  rc_halt "chflags -R noschg ${FSMNT}"
6875118Sfenner
6975118Sfenner  # Make backup copies of some files
7075118Sfenner  for i in ${BKFILES}
7175118Sfenner  do
7275118Sfenner    cp ${FSMNT}${i} ${FSMNT}${i}.preUpgrade >/dev/null 2>/dev/null
7375118Sfenner  done
7475118Sfenner
7575118Sfenner  # Remove some old dirs
7675118Sfenner  rm -rf ${FSMNT}/etc/rc.d >/dev/null 2>/dev/null
7775118Sfenner
7875118Sfenner  # If we are doing PC-BSD install, lets cleanup old pkgs on disk
7975118Sfenner  if [ "$INSTALLTYPE" != "FreeBSD" ]
8075118Sfenner  then
8175118Sfenner    echo_log "Removing old packages, this may take a while... Please wait..."
8275118Sfenner    echo '#!/bin/sh
8375118Sfennerfor i in `pkg_info -E \*`
8475118Sfennerdo
8598527Sfenner  echo "Uninstalling package: ${i}"
8698527Sfenner  pkg_delete -f ${i} >/dev/null 2>/dev/null
8798527Sfennerdone
8898527Sfenner' >${FSMNT}/.cleanPkgs.sh
8975118Sfenner    chmod 755 ${FSMNT}/.cleanPkgs.sh
9075118Sfenner    chroot ${FSMNT} /.cleanPkgs.sh
9175118Sfenner    rm ${FSMNT}/.cleanPkgs.sh
9275118Sfenner    run_chroot_cmd "pkg_delete -f \*" >/dev/null 2>/dev/null
9398527Sfenner    run_chroot_cmd "rm -rf /usr/PCBSD" >/dev/null 2>/dev/null
9498527Sfenner    run_chroot_cmd "rm -rf /PCBSD" >/dev/null 2>/dev/null
9598527Sfenner    run_chroot_cmd "rm -rf /var/db/pkgs" >/dev/null 2>/dev/null
9698527Sfenner    run_chroot_cmd "rm -rf /usr/local32" >/dev/null 2>/dev/null
9798527Sfenner    run_chroot_cmd "rm -rf /usr/sbin" >/dev/null 2>/dev/null
9898527Sfenner    run_chroot_cmd "rm -rf /usr/lib" >/dev/null 2>/dev/null
9998527Sfenner    run_chroot_cmd "rm -rf /usr/bin" >/dev/null 2>/dev/null
10098527Sfenner    run_chroot_cmd "rm -rf /boot/kernel" >/dev/null 2>/dev/null
10198527Sfenner    run_chroot_cmd "rm -rf /sbin" >/dev/null 2>/dev/null
10275118Sfenner    run_chroot_cmd "rm -rf /bin" >/dev/null 2>/dev/null
10317680Spst    run_chroot_cmd "rm -rf /lib" >/dev/null 2>/dev/null
10417680Spst    run_chroot_cmd "rm -rf /libexec" >/dev/null 2>/dev/null
10517680Spst  fi
10698527Sfenner
10717680Spst};
10898527Sfenner
10998527Sfenner# Mount the target upgrade partitions
11017680Spstmount_upgrade()
11198527Sfenner{
11298527Sfenner
11398527Sfenner  # Make sure we remove the old upgrade-mount script
11417680Spst  rm -rf ${TMPDIR}/.upgrade-unmount >/dev/null 2>/dev/null
11598527Sfenner
11617680Spst  # We are ready to start mounting, lets read the config and do it
11717680Spst  while read line
11817680Spst  do
11998527Sfenner    echo $line | grep "^disk0=" >/dev/null 2>/dev/null
12098527Sfenner    if [ "$?" = "0" ]
12198527Sfenner    then
12217680Spst
12398527Sfenner      # Found a disk= entry, lets get the disk we are working on
12417680Spst      get_value_from_string "${line}"
12598527Sfenner      strip_white_space "$VAL"
12617680Spst      DISK="$VAL"
12717680Spst    fi
12817680Spst
12917680Spst    echo $line | grep "^commitDiskPart" >/dev/null 2>/dev/null
13017680Spst    if [ "$?" = "0" ]
13117680Spst    then
13217680Spst      # Found our flag to commit this disk setup / lets do sanity check and do it
13317680Spst      if [ ! -z "${DISK}" ]
13498527Sfenner      then
13598527Sfenner
13698527Sfenner        # Start mounting this slice
13717680Spst        mount_target_slice "${DISK}" 
13817680Spst
13917680Spst        # Increment our disk counter to look for next disk and unset
14017680Spst        unset DISK
14117680Spst	    break
14298527Sfenner      else
14317680Spst        exit_err "ERROR: commitDiskPart was called without procceding disk<num>= and partition= entries!!!" 
14417680Spst      fi
14598527Sfenner    fi
14617680Spst
14798527Sfenner  done <${CFGF}
14898527Sfenner
14917680Spst};
15017680Spst
15198527Sfennercopy_skel_files_upgrade()
15217680Spst{
15398527Sfenner
15417680Spst  # Now make sure we fix any user profile scripts, which cause problems from 7.x->8.x
15517680Spst  echo '#!/bin/sh
15617680Spst
15717680Spstcd /home
15817680Spstfor i in `ls`
15998527Sfennerdo
16017680Spst
16117680Spst  # Backup the old profile dirs
16217680Spst  if [ -d "${i}" ]
16326183Sfenner  then
16498527Sfenner    mv /home/${i}/.kde4 /home/${i}/.kde4.preUpgrade >/dev/null 2>/dev/null
16598527Sfenner    mv /home/${i}/.kde /home/${i}/.kde.preUpgrade >/dev/null 2>/dev/null
16698527Sfenner    mv /home/${i}/.fluxbox /home/${i}/.fluxbox.preUpgrade >/dev/null 2>/dev/null
16717680Spst
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