]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/build/beinstall.sh
Add missing break statements in r351004.
[FreeBSD/FreeBSD.git] / tools / build / beinstall.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2016 Will Andrews
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 #    notice, this list of conditions and the following disclaimer 
11 #    in this position and unchanged.
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 ``AS IS'' AND ANY EXPRESS OR
17 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #
27 # $FreeBSD$
28 #
29 ##
30 # Install a boot environment using the current FreeBSD source tree.
31 # Requires a fully built world & kernel.
32 #
33 # Non-base tools required: beadm, pkg
34 #
35 # In a sandbox for the new boot environment, this script also runs etcupdate
36 # and pkg upgrade automatically in the sandbox.  Upon successful completion,
37 # the system will be ready to boot into the new boot environment.  Upon
38 # failure, the target boot environment will be destroyed.  In all cases, the
39 # running system is left untouched.
40 #
41 ## Usage:
42 # beinstall [optional world/kernel flags e.g. KERNCONF]
43 #
44 ## User modifiable variables - set these in the environment if desired.
45 # If not empty, 'pkg upgrade' will be skipped.
46 NO_PKG_UPGRADE="${NO_PKG_UPGRADE:-""}"
47 # Config updater - 'etcupdate' and 'mergemaster' are supported.  Set to an
48 # empty string to skip.
49 CONFIG_UPDATER="${CONFIG_UPDATER:-"etcupdate"}"
50 # Flags for etcupdate if used.
51 ETCUPDATE_FLAGS="${ETCUPDATE_FLAGS:-"-F"}"
52 # Flags for mergemaster if used.
53 MERGEMASTER_FLAGS="${MERGEMASTER_FLAGS:-"-iFU"}"
54
55
56 ########################################################################
57 ## Functions
58 cleanup() {
59         [ -z "${cleanup_commands}" ] && return
60         echo "Cleaning up ..."
61         for command in ${cleanup_commands}; do
62                 ${command}
63         done
64 }
65
66 errx() {
67         cleanup
68         echo "error: $@"
69         exit 1
70 }
71
72 rmdir_be() {
73         chflags -R noschg ${BE_MNTPT}
74         rm -rf ${BE_MNTPT}
75 }
76
77 unmount_be() {
78         mount | grep " on ${BE_MNTPT}" | awk '{print $3}' | sort -r | xargs -t umount -f
79 }
80
81 copy_pkgs() {
82         # Before cleaning up, try to save progress in pkg(8) updates, to
83         # speed up future updates.  This is only called on the error path;
84         # no need to run on success.
85         echo "Rsyncing back newly saved packages..."
86         rsync -av --progress ${BE_MNTPT}/var/cache/pkg/. /var/cache/pkg/.
87 }
88
89 cleanup_be() {
90         # Before destroying, unmount any child filesystems that may have
91         # been mounted under the boot environment.  Sort them in reverse
92         # order so children are unmounted first.
93         unmount_be
94         # Finally, clean up any directories that were created by the
95         # operation, via cleanup_be_dirs().
96         if [ -n "${created_be_dirs}" ]; then
97                 chroot ${BE_MNTPT} /bin/rm -rf ${created_be_dirs}
98         fi
99         beadm destroy -F ${BENAME}
100 }
101
102 create_be_dirs() {
103         echo "${BE_MNTPT}: Inspecting dirs $*"
104         for dir in $*; do
105                 curdir="$dir"
106                 topdir="$dir"
107                 while :; do
108                         [ -e "${BE_MNTPT}${curdir}" ] && break
109                         topdir=$curdir
110                         curdir=$(dirname ${curdir})
111                 done
112                 [ "$curdir" = "$dir" ] && continue
113
114                 # Add the top-most nonexistent directory to the list, then
115                 # mkdir -p the innermost directory specified by the argument.
116                 # This way the least number of directories are rm'd directly.
117                 created_be_dirs="${topdir} ${created_be_dirs}"
118                 echo "${BE_MNTPT}: Created ${dir}"
119                 mkdir -p ${BE_MNTPT}${dir} || return $?
120         done
121         return 0
122 }
123
124 update_mergemaster_pre() {
125         ${MERGEMASTER_CMD} -p -m ${srcdir} -D ${BE_MNTPT} -t ${BE_MM_ROOT} ${MERGEMASTER_FLAGS}
126 }
127
128 update_mergemaster() {
129         ${MERGEMASTER_CMD} -m ${srcdir} -D ${BE_MNTPT} -t ${BE_MM_ROOT} ${MERGEMASTER_FLAGS}
130 }
131
132 update_etcupdate_pre() {
133         ${ETCUPDATE_CMD} -p -s ${srcdir} -D ${BE_MNTPT} ${ETCUPDATE_FLAGS} || return $?
134         ${ETCUPDATE_CMD} resolve -D ${BE_MNTPT} || return $?
135 }
136
137 update_etcupdate() {
138         chroot ${BE_MNTPT} \
139                 ${ETCUPDATE_CMD} -s ${srcdir} ${ETCUPDATE_FLAGS} || return $?
140         chroot ${BE_MNTPT} ${ETCUPDATE_CMD} resolve
141 }
142
143
144 # Special command-line subcommand that can be used to do a full cleanup
145 # after a manual post-mortem has been completed.
146 postmortem() {
147         [ -n "${BENAME}" ] || errx "Must specify BENAME"
148         [ -n "${BE_MNTPT}" ] || errx "Must specify BE_MNTPT"
149         echo "Performing post-mortem on BE ${BENAME} at ${BE_MNTPT} ..."
150         unmount_be
151         rmdir_be
152         echo "Post-mortem cleanup complete."
153         echo "To destroy the BE (recommended), run: beadm destroy ${BENAME}"
154         echo "To instead continue with the BE, run: beadm activate ${BENAME}"
155 }
156
157 if [ -n "$BEINSTALL_CMD" ]; then
158         ${BEINSTALL_CMD} $*
159         exit $?
160 fi
161
162
163 cleanup_commands=""
164 trap 'errx "Interrupt caught"' HUP INT TERM
165
166 [ "$(whoami)" != "root" ] && errx "Must be run as root"
167
168 [ ! -f "Makefile.inc1" ] && errx "Must be in FreeBSD source tree"
169 srcdir=$(pwd)
170 objdir=$(make -V .OBJDIR 2>/dev/null)
171 [ ! -d "${objdir}" ] && errx "Must have built FreeBSD from source tree"
172
173 ## Constants
174 ETCUPDATE_CMD="${srcdir}/usr.sbin/etcupdate/etcupdate.sh"
175 MERGEMASTER_CMD="${srcdir}/usr.sbin/mergemaster/mergemaster.sh"
176
177 # May be a worktree, in which case .git is a file, not a directory.
178 if [ -e .git ] ; then
179     commit_time=$(git show --format='%ct' 2>/dev/null | head -1)
180     [ $? -ne 0 ] && errx "Can't lookup git commit timestamp"
181     commit_ts=$(date -r ${commit_time} '+%Y%m%d.%H%M%S')
182 elif [ -d .svn ] ; then
183       if [ -e /usr/bin/svnlite ]; then
184         svn=/usr/bin/svnlite
185       elif [ -e /usr/local/bin/svn ]; then
186         svn=/usr/local/bin/svn
187       else
188         errx "Unable to find subversion"
189       fi
190       commit_ts="$( "$svn" info --show-item last-changed-date | sed -e 's/\..*//' -e 's/T/./' -e 's/-//g' -e s'/://g' )"
191     [ $? -ne 0 ] && errx "Can't lookup Subversion commit timestamp"
192 else
193     errx "Unable to determine source control type"
194 fi
195
196 commit_ver=$(${objdir}/bin/freebsd-version/freebsd-version -u 2>/dev/null)
197 [ -z "${commit_ver}" ] && errx "Unable to determine FreeBSD version"
198
199 BENAME="${commit_ver}-${commit_ts}"
200
201 BE_TMP=$(mktemp -d /tmp/beinstall.XXXXXX)
202 [ $? -ne 0 -o ! -d ${BE_TMP} ] && errx "Unable to create mountpoint"
203 [ -z "$NO_CLEANUP_BE" ] && cleanup_commands="rmdir_be ${cleanup_commands}"
204 BE_MNTPT=${BE_TMP}/mnt
205 BE_MM_ROOT=${BE_TMP}/mergemaster # mergemaster will create
206 mkdir -p ${BE_MNTPT}
207
208 beadm create ${BENAME} >/dev/null || errx "Unable to create BE ${BENAME}"
209 [ -z "$NO_CLEANUP_BE" ] && cleanup_commands="cleanup_be ${cleanup_commands}"
210
211 beadm mount ${BENAME} ${BE_TMP}/mnt || errx "Unable to mount BE ${BENAME}."
212
213 echo "Mounted ${BENAME} to ${BE_MNTPT}, performing install/update ..."
214 make "$@" DESTDIR=${BE_MNTPT} installkernel || errx "Installkernel failed!"
215 if [ -n "${CONFIG_UPDATER}" ]; then
216         "update_${CONFIG_UPDATER}_pre"
217         [ $? -ne 0 ] && errx "${CONFIG_UPDATER} (pre-world) failed!"
218 fi
219
220 # Mount the source and object tree within the BE in order to account for any
221 # changes applied by the pre-installworld updater.  Cleanup any directories
222 # created if they didn't exist previously.
223 create_be_dirs "${srcdir}" "${objdir}" || errx "Unable to create BE dirs"
224 mount -t nullfs "${srcdir}" "${BE_MNTPT}${srcdir}" || errx "Unable to mount src"
225 mount -t nullfs "${objdir}" "${BE_MNTPT}${objdir}" || errx "Unable to mount obj"
226
227 chroot ${BE_MNTPT} make "$@" -C ${srcdir} installworld || \
228         errx "Installworld failed!"
229
230 if [ -n "${CONFIG_UPDATER}" ]; then
231         "update_${CONFIG_UPDATER}"
232         [ $? -ne 0 ] && errx "${CONFIG_UPDATER} (post-world) failed!"
233 fi
234
235 if which rsync >/dev/null 2>&1; then
236         cleanup_commands="copy_pkgs ${cleanup_commands}"
237 fi
238
239 BE_PKG="chroot ${BE_MNTPT} env ASSUME_ALWAYS_YES=true pkg"
240 if [ -z "${NO_PKG_UPGRADE}" ]; then
241         ${BE_PKG} update || errx "Unable to update pkg"
242         ${BE_PKG} upgrade || errx "Unable to upgrade pkgs"
243 fi
244
245 if [ -n "$NO_CLEANUP_BE" ]; then
246         echo "Boot Environment ${BENAME} may be examined in ${BE_MNTPT}."
247         echo "Afterwards, run this to cleanup:"
248         echo "  env BENAME=${BENAME} BE_MNTPT=${BE_MNTPT} BEINSTALL_CMD=postmortem $0"
249         exit 0
250 fi
251
252 unmount_be || errx "Unable to unmount BE"
253 rmdir_be || errx "Unable to cleanup BE"
254 beadm activate ${BENAME} || errx "Unable to activate BE"
255 echo
256 beadm list
257 echo
258 echo "Boot environment ${BENAME} setup complete; reboot to use it."