summaryrefslogtreecommitdiffstats
path: root/src/usr/local/share/pfSense/pre_upgrade_command
blob: ae1c41e6beab3a9c2c2dc4c3ff939fd675dab19f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/sh
#
# pre_upgrade_command
#
# part of pfSense (https://www.pfsense.org)
# Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

/etc/rc.conf_mount_rw

# Record the previous version
PRIOR_VERSION=`uname -r | cut -d'.' -f1`
echo $PRIOR_VERSION > /tmp/pre_upgrade_version

# De-activate sync on root filesystem. See ticket #4523
# Back up original fstab
/bin/cp /etc/fstab /etc/fstab.orig
# De-activate sync on the root slice only. This will not match NanoBSD since it already has sync,noatime
/usr/bin/sed -i '' 's/^\(\/.*[[:space:]]*\/[[:space:]]*ufs[[:space:]]*\)rw,sync\([[:space:]]*[[:digit:]][[:space:]]*[[:digit:]]\)$/\1rw\2/' /etc/fstab

# Uninstall all packages before upgrade to 2.3
PFSENSE_VERSION=$(cat /etc/version)

if echo "$PFSENSE_VERSION" | grep -q '^1.2'; then
	UNINSTALL="uninstall_package_from_name"
else
	UNINSTALL="uninstall_package"
fi

cat >/tmp/remove_all_packages.php <<EOD
#!/usr/local/bin/php
<?php

require_once("pkg-utils.inc");

global \$pkg_interface;
\$pkg_interface = "console";

if (!isset(\$config['installedpackages']['package'])) {
	exit(0);
}

if (!is_array(\$config['installedpackages']['package'])) {
	exit(0);
}

\$removed_packages = array();
foreach (\$config['installedpackages']['package'] as \$package) {
	if (isset(\$package['internal_name'])) {
		\$removed_packages[] = \$package['internal_name'];
	} else {
		\$rename_arr = array("Quagga OSPF" => "Quagga_OSPF", "System Patches" => "System_Patches");
		if (key_exists(\$package['name'], \$rename_arr)) {
			\$removed_packages[] = \$rename_arr[\$package['name']];
		} else {
			\$removed_packages[] = \$package['name'];
		}
	}
	${UNINSTALL}(\$package['name']);
}

@file_put_contents("{\$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt", implode("\\n",\$removed_packages));

?>
EOD

/usr/local/bin/php /tmp/remove_all_packages.php
rm -f /tmp/remove_all_packages.php

# Cleanup PBIs
if which pbi_info >/dev/null 2>&1; then
	for pbi in $(pbi_info); do
		echo ">>> Removing PBI ${pbi} and all symlinks"
		pbi_prefix=$(pbi_info ${pbi} | awk '$1 == "Prefix:" { print $2  }')

		for subdir in bin sbin; do
			if [ ! -d "${pbi_prefix}/${subdir}" ]; then
				continue
			fi

			for pbiopt in ${pbi_prefix}/${subdir}/*.pbiopt; do
				link_name=$(awk '$1 == "TARGET:" { print $2 }' ${pbiopt})

				if [ -z "${link_name}" ]; then
					continue
				fi

				if [ -L /usr/local/${link_name} ]; then
					rm -f /usr/local/${link_name}
				fi
			done
		done

		pbi_delete ${pbi}
	done

	# Cleanup remaining symbolic links
	find / -type l -lname '/usr/pbi/*' -delete
fi

# Hack to workaround ticket #3749
if [ ${PRIOR_VERSION} -le 8 ] && grep -q 'sh /etc/rc.reboot' /etc/rc.firmware; then
	PROC=$(ps axwww | grep '/etc/rc.firmware *pfSenseupgrade')
	PID=''
	IMG=''
	if [ -n "${PROC}" ]; then
		PID=$(echo "${PROC}" | awk '{print $1}')
		IMG=$(echo "${PROC}" | sed 's,^.*pfSenseupgrade *,,')
	fi

	if [ -n "${PID}" -a -n "${IMG}" -a -f "${IMG}" ]; then
		cp -fp /bin/sh /tmp/sh.old
		kill ${PID} >/dev/null 2>&1
		kill -9 ${PID} >/dev/null 2>&1
		sed -i '' -e 's,sh /,/tmp/sh.old /,' \
			  -e 's,/usr/sbin/mtree,/tmp/mtree.old,' /etc/rc.firmware
		# To avoid restarting rc.firmware twice because of #4328
		sed -i '' -e '/^restore_chflags()/, /^}/ s/mtree *-PU/mtree -P/' /etc/rc.firmware
		/etc/rc.firmware pfSenseupgrade "${IMG}"
		exit
	fi
fi

# One more hack to workaround #4328
if grep -q 'mtree *-PU' /etc/rc.firmware; then
	PROC=$(ps axwww | grep '/etc/rc.firmware *pfSenseupgrade')
	PID=''
	IMG=''
	if [ -n "${PROC}" ]; then
		PID=$(echo "${PROC}" | awk '{print $1}')
		IMG=$(echo "${PROC}" | sed 's,^.*pfSenseupgrade *,,')
	fi

	if [ -n "${PID}" -a -n "${IMG}" -a -f "${IMG}" ]; then
		kill ${PID} >/dev/null 2>&1
		kill -9 ${PID} >/dev/null 2>&1
		sed -i '' -e '/^restore_chflags()/, /^}/ s/mtree *-PU/mtree -P/' /etc/rc.firmware
		/etc/rc.firmware pfSenseupgrade "${IMG}"
		exit
	fi
fi

# Make sure /boot files are not protected with schg
chflags -R noschg /boot >/dev/null 2>&1

rm /boot/kernel/*

OpenPOWER on IntegriCloud