-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-system
333 lines (303 loc) · 10.7 KB
/
update-system
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/bin/sh
####################################################################################################
# User-serviceable definitions
####################################################################################################
GIT_CLIENT_PACKAGE='git-tiny'
GIT_FETCH_ARGS='--depth=1'
GIT_RESET_ARGS='--keep'
MAKE_ARGS="-j$(sysctl -n hw.ncpu)"
MERGEMASTER_ARGS='-FiU'
PREFER_ETCUPDATE=1
####################################################################################################
# Internal definitions
####################################################################################################
KERNCONF_PATH="/usr/src/sys/$(uname -m)/conf"
MY_PATH="$(dirname "$(readlink -f "$0")")"
SRC_PATH='/usr/src'
####################################################################################################
# Utility functions
####################################################################################################
. "$MY_PATH/common"
# backs up GENERIC and UPDATING so that they can be compared post-upgrade
back_up_generic_and_updating() {
if [ -f "$KERNCONF_PATH/GENERIC" ] ; then
cp -fv "$KERNCONF_PATH/GENERIC" "$KERNCONF_PATH/GENERIC.last"
fi
if [ -f "$SRC_PATH/UPDATING" ]; then
cp -fv "$SRC_PATH/UPDATING" "$SRC_PATH/UPDATING.last"
fi
}
# determines whether etcupdate(8) needs bootstrapping
check_etcupdate() {
if [ "$PREFER_ETCUPDATE" -a ! "$(is_etcupdate_functional)" ]; then
line1="etcupdate(8) is set as the preferred configuration merging tool, but it needs"
line2="to be bootstrapped before its first use. Bootstrapping consists of making a copy"
line3="of the stock configuration in $SRC_PATH (which should match the currently running"
line4="world) and establishing it as the baseline for detecting local changes."
echo -e "$line1\n$line2\n$line3\n$line4"
prompt '[B]ootstrap and use etcupdate(8), or [f]all back to mergemaster(8)?'
selection="$(input '[bBfF]')"
case "$selection" in
b|B) echo -n ' Bootstrapping etcupdate(8); please wait... '
etcupdate extract || exit 1
echo 'done.'
;;
f|F) echo ' Falling back to mergemaster(8).'
;;
esac
fi
}
# determines whether etcupdate(8) has been bootstrapped and is functional
is_etcupdate_functional() {
etcupdate diff > /dev/null 2>&1 && echo 1
}
# determines whether the given string is a valid FreeBSD source repository URL
# $1: source repository URL
is_repository_url() {
branch_regexp='head|stable/[[:digit:]]+|releng/[[:digit:]]+\.[[:digit:]]+'
is_match "$1" "^https?://[-.[:alnum:]]+/base/($branch_regexp)/?$" -i
}
# shows help
print_usage() {
echo "Usage: $0 --stage1 [kernconf]"
indent=$(("$(echo "$0" | wc -m)" + 7))
repeat ' ' $indent; echo '--stage2'
repeat ' ' $indent; echo '--stage3'
echo 'Stages:'
echo ' --stage1: Updates the source tree, shows changes in UPDATING, merges'
echo ' inbound changes to GENERIC into `kernconf` (if one is given),'
echo ' builds the userland, and builds and installs the kernel.'
echo ' [kernconf]: File name (not path) of the kernel configuration to build.'
echo ' Defaults to GENERIC if unspecified.'
echo ' --stage2: Installs the userland and merges changes in configuration files.'
echo ' --stage3: Upgrades pkg(8), cleans build output, and removes obsolete files.'
echo
}
# builds world and kernel, and installs kernel
# $1: kernconf (optional; defaults to GENERIC)
stage1() {
# determine kernel to build
if [ -n "$1" ]; then
if [ -f "$KERNCONF_PATH/$1" ]; then
KERNCONF="$1"
else
echo "Kernel configuration file not found: $KERNCONF_PATH/$1"
exit 1
fi
else
KERNCONF='GENERIC'
fi
echo -e "\nKernel configuration: $KERNCONF"
# determine the type of source repository and whether the appropriate VCS client is installed
is_git_repository=
if [ -d "$SRC_PATH/.git" ]; then
is_git_repository=1
MERGEMASTER_ARGS="$MERGEMASTER_ARGS -s" # don't rely on VCS IDs for comparison
check_git_client "$SRC_PATH" "$GIT_CLIENT_PACKAGE" || exit 1
else
check_subversion_client "$SRC_PATH" || exit 1
fi
# confirm and sync source branch to be built
branch=
if [ "$is_git_repository" ]; then
branch="$(git rev-parse --abbrev-ref HEAD)"
else
branch="$(svnlite info "$SRC_PATH" | grep '^URL' | awk '{print $2}')"
fi
if [ -z "$branch" ]; then
echo
echo -n "Unable to determine current the source tree branch. Please verify that $SRC_PATH "
echo "is a valid repository."
exit 1
fi
prompt_Yn "Keep the same branch, $branch?"
# if staying on the same branch
if [ $(confirm) ]; then
prompt_Yn 'Sync source tree?'
if [ $(confirm) ]; then
# back up GENERIC and UPDATING so that they can be compared post-sync
back_up_generic_and_updating
# sync source tree
if [ "$is_git_repository" ]; then
# TODO: switch to `make update` once Makefile migrates to Git
git fetch $GIT_FETCH_ARGS && git reset $GIT_RESET_ARGS origin/$branch || exit 1
else
NO_PORTSUPDATE=yes make update || exit 1
fi
fi
# if switching branches
else
branch_regexp='head|stable/[[:digit:]]+|releng/[[:digit:]]+\.[[:digit:]]+'
if [ "$is_git_repository" ]; then
prompt 'New branch (e.g. releng/xx.y):'
branch="$(input "$branch_regexp")"
else
prompt 'New repository URL (e.g. https://svn.freebsd.org/base/releng/xx.y):'
repository_url="$(input "https?://[-.[:alnum:]]+/base/($branch_regexp)/?")"
fi
# back up GENERIC and UPDATING so that they can be compared post-sync
back_up_generic_and_updating
# switch branches and sync source tree
if [ "$is_git_repository" ]; then
git fetch $GIT_FETCH_ARGS && git checkout "$branch" && \
git reset $GIT_RESET_ARGS origin/$branch || exit 1
else
svnlite switch "$repository_url" || exit 1
fi
fi
# if we have a backup of UPDATING and it differs from the latest, display the differences
if [ -f "$SRC_PATH/UPDATING.last" -a \
$(are_files_different "$SRC_PATH/UPDATING" "$SRC_PATH/UPDATING.last") ]; then
prompt_Yn 'See changes in UPDATING?'
if [ $(confirm) ]; then
diff -ruN "$SRC_PATH/UPDATING.last" "$SRC_PATH/UPDATING" | less --QUIT-AT-EOF
fi
# otherwise, offer to display the whole file
else
prompt_Yn 'Read UPDATING?'
if [ $(confirm) ]; then
less --QUIT-AT-EOF "$SRC_PATH/UPDATING"
fi
fi
# clean and build world
prompt_Yn "Build the userland (\`make $MAKE_ARGS buildworld\`)?"
if [ $(confirm) ]; then
if [ ! "$PREFER_ETCUPDATE" -o ! "$(is_etcupdate_functional)" ]; then
prompt_Yn "Merge pre-buildworld configuration with \`mergemaster $MERGEMASTER_ARGS -p\`?"
if [ $(confirm) ]; then
mergemaster $MERGEMASTER_ARGS -p || exit 1
fi
fi
no_clean_arg=
prompt_Yn 'Clean the build directory before building?'
if [ ! $(confirm) ]; then
no_clean_arg='-DNO_CLEAN'
fi
make $MAKE_ARGS $no_clean_arg buildworld || exit 1
fi
# build kernel
prompt_Yn "Build the $KERNCONF kernel (\`make $MAKE_ARGS buildkernel KERNCONF=$KERNCONF\`)?"
if [ $(confirm) ]; then
# if building a custom kernel configuration, merge changes from GENERIC into it
custom_kernconf="$KERNCONF_PATH/$KERNCONF"
generic_kernconf="$KERNCONF_PATH/GENERIC"
if [ "$KERNCONF" != 'GENERIC' -a \
$(are_files_different "$generic_kernconf" "$custom_kernconf") ]; then
three_way_merge_files "$generic_kernconf.last" "$generic_kernconf" "$custom_kernconf"
fi
# build
make $MAKE_ARGS buildkernel KERNCONF="$KERNCONF" || exit 1
fi
# install kernel
prompt_Yn "Install the $KERNCONF kernel (\`make installkernel KERNCONF=$KERNCONF\`)?"
if [ $(confirm) ]; then
make $MAKE_ARGS installkernel KERNCONF="$KERNCONF" || exit 1
prompt_Yn "Reboot and activate the new $KERNCONF kernel?"
if [ $(confirm) ]; then
echo; header "After the reboot, run \`$0 --stage2\` to continue."; echo
sleep 3
shutdown -r now
fi
fi
}
# installs world and merges configuration
stage2() {
if [ "$PREFER_ETCUPDATE" -a "$(is_etcupdate_functional)" ]; then
prompt_Yn 'Merge pre-installworld configuration with `etcupdate -p`?'
if [ $(confirm) ]; then
etcupdate -p || exit 1
fi
fi
prompt_Yn 'Install the userland (`make installworld`)?'
world_installed=
if [ $(confirm) ]; then
make installworld || exit 1
world_installed=1
fi
if [ "$PREFER_ETCUPDATE" -a "$(is_etcupdate_functional)" ]; then
prompt_Yn 'Merge configuration files with `etcupdate -B`?'
if [ $(confirm) ]; then
etcupdate -B || (
prompt_Yn 'Resolve conflicts with `etcupdate resolve`?'
if [ $(confirm) ]; then
etcupdate resolve || exit 1
else
exit 1
fi
)
fi
else
prompt_Yn "Merge configuration files with \`mergemaster $MERGEMASTER_ARGS\`?"
if [ $(confirm) ]; then
mergemaster $MERGEMASTER_ARGS || exit 1
fi
fi
if [ "$world_installed" ]; then
prompt_Yn 'Reboot into the new userland?'
if [ $(confirm) ]; then
echo; header "After the reboot, run \`$0 --stage3\` to continue."; echo
sleep 3
shutdown -r now
fi
fi
}
# upgrades pkg(8) and cleans up after the build
stage3() {
if [ "$(which pkg-static)" ]; then
prompt_Yn 'Upgrade pkg(8)?'
if [ $(confirm) ]; then
pkg-static install pkg || ASSUME_ALWAYS_YES=yes pkg-static bootstrap -f
fi
fi
prompt_Yn 'Clean up obsolete files and directories (`make delete-old`)?'
if [ $(confirm) ]; then
yes | make delete-old
fi
prompt 'Checking for obsolete shared libraries...'
redundant_shlibs="$(make check-old-libs)"
if [ ! "$redundant_shlibs" = ">>> Checking for old libraries" ]; then
echo 'none found.'
else
echo -e "$redundant_shlibs" | less --QUIT-AT-EOF
message='Delete ALL redundant shared libraries listed above?\n'
message="$message WARNING: There may be ports still using some of these libraries."
prompt_yN "$message"
if [ $(confirm 'n') ]; then
yes | make delete-old-libs > /dev/null
else
prompt_Yn 'Interactively delete redundant shared libraries one at a time?'
if [ $(confirm) ]; then
make delete-old-libs
fi
fi
fi
message='Clean the build directory (`make cleanworld`)? Answer affirmatively if\n'
message="$message you are satisfied with the upgrade and have no further use for the build\n"
message="$message output (e.g. upgrading other hosts from the built binaries)."
prompt_yN "$message"
if [ $(confirm 'n') ]; then
make cleanworld
fi
}
# changes into the source directory and executes the requested stage
stage_switchboard() {
cd "$SRC_PATH" || exit 1
# execute requested stage
case "$1" in
1) check_etcupdate
stage1 "$2";;
2) check_etcupdate
stage2;;
3) stage3;;
esac
}
####################################################################################################
# Entry point
####################################################################################################
case "$1" in
--stage1) stage_switchboard 1 "$2";;
--stage2) stage_switchboard 2;;
--stage3) stage_switchboard 3;;
*) print_usage
exit 1;;
esac