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