beinstall.sh (fc3bfe916e603950f509aeec656fa7112646971d) beinstall.sh (16702050ac953d957a42fbc498e22c95078ad689)
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:

--- 64 unchanged lines hidden (view full) ---

73 exit 1
74}
75
76rmdir_be() {
77 chflags -R noschg ${BE_MNTPT}
78 rm -rf ${BE_MNTPT}
79}
80
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:

--- 64 unchanged lines hidden (view full) ---

73 exit 1
74}
75
76rmdir_be() {
77 chflags -R noschg ${BE_MNTPT}
78 rm -rf ${BE_MNTPT}
79}
80
81unmount_be() {
82 mount | grep " on ${BE_MNTPT}" | awk '{print $3}' | sort -r | xargs -t umount -f
83}
84
81cleanup_be() {
85cleanup_be() {
86 # Before destroying, unmount any child filesystems that may have
87 # been mounted under the boot environment. Sort them in reverse
88 # order so children are unmounted first.
89 unmount_be
90 # Finally, clean up any directories that were created by the
91 # operation, via cleanup_be_dirs().
92 if [ -n "${created_be_dirs}" ]; then
93 chroot ${BE_MNTPT} /bin/rm -rf ${created_be_dirs}
94 fi
82 beadm destroy -F ${BENAME}
83}
84
95 beadm destroy -F ${BENAME}
96}
97
98create_be_dirs() {
99 echo "${BE_MNTPT}: Inspecting dirs $*"
100 for dir in $*; do
101 curdir="$dir"
102 topdir="$dir"
103 while :; do
104 [ -e "${BE_MNTPT}${curdir}" ] && break
105 topdir=$curdir
106 curdir=$(dirname ${curdir})
107 done
108 [ "$curdir" = "$dir" ] && continue
109
110 # Add the top-most nonexistent directory to the list, then
111 # mkdir -p the innermost directory specified by the argument.
112 # This way the least number of directories are rm'd directly.
113 created_be_dirs="${topdir} ${created_be_dirs}"
114 echo "${BE_MNTPT}: Created ${dir}"
115 mkdir -p ${BE_MNTPT}${dir} || return $?
116 done
117 return 0
118}
119
120update_mergemaster_pre() {
121 mergemaster -p -m ${srcdir} -D ${BE_MNTPT} -t ${BE_MM_ROOT} ${MERGEMASTER_FLAGS}
122}
123
85update_mergemaster() {
124update_mergemaster() {
86 mergemaster -m $(pwd) -D ${BE_MNTPT} -t ${BE_MM_ROOT} ${MERGEMASTER_FLAGS}
125 chroot ${BE_MNTPT} \
126 mergemaster -m ${srcdir} -t ${BE_MM_ROOT} ${MERGEMASTER_FLAGS}
87}
88
127}
128
129update_etcupdate_pre() {
130 etcupdate -p -s ${srcdir} -D ${BE_MNTPT} ${ETCUPDATE_FLAGS} || return $?
131 etcupdate resolve -D ${BE_MNTPT} || return $?
132}
133
89update_etcupdate() {
134update_etcupdate() {
90 etcupdate -s $(pwd) -D ${BE_MNTPT} ${ETCUPDATE_FLAGS} || return $?
91 etcupdate resolve -D ${BE_MNTPT}
135 chroot ${BE_MNTPT} \
136 etcupdate -s ${srcdir} ${ETCUPDATE_FLAGS} || return $?
137 chroot ${BE_MNTPT} etcupdate resolve
92}
93
94
138}
139
140
141# Special command-line subcommand that can be used to do a full cleanup
142# after a manual post-mortem has been completed.
143postmortem() {
144 [ -n "${BENAME}" ] || errx "Must specify BENAME"
145 [ -n "${BE_MNTPT}" ] || errx "Must specify BE_MNTPT"
146 echo "Performing post-mortem on BE ${BENAME} at ${BE_MNTPT} ..."
147 unmount_be
148 rmdir_be
149 echo "Post-mortem cleanup complete."
150 echo "To destroy the BE (recommended), run: beadm destroy ${BENAME}"
151 echo "To instead continue with the BE, run: beadm activate ${BENAME}"
152}
153
154if [ -n "$BEINSTALL_CMD" ]; then
155 ${BEINSTALL_CMD} $*
156 exit $?
157fi
158
159
95cleanup_commands=""
96trap 'errx "Interrupt caught"' HUP INT TERM
97
98[ "$(whoami)" != "root" ] && errx "Must be run as root"
99
100[ ! -f "Makefile.inc1" ] && errx "Must be in FreeBSD source tree"
160cleanup_commands=""
161trap 'errx "Interrupt caught"' HUP INT TERM
162
163[ "$(whoami)" != "root" ] && errx "Must be run as root"
164
165[ ! -f "Makefile.inc1" ] && errx "Must be in FreeBSD source tree"
166srcdir=$(pwd)
101objdir=$(make -V .OBJDIR 2>/dev/null)
102[ ! -d "${objdir}" ] && errx "Must have built FreeBSD from source tree"
103
104# May be a worktree, in which case .git is a file, not a directory.
105if [ -e .git ] ; then
106 commit_time=$(git show --format='%ct' 2>/dev/null | head -1)
107 [ $? -ne 0 ] && errx "Can't lookup git commit timestamp"
108 commit_ts=$(date -r ${commit_time} '+%Y%m%d.%H%M%S')

--- 25 unchanged lines hidden (view full) ---

134
135beadm create ${BENAME} >/dev/null || errx "Unable to create BE ${BENAME}"
136[ -z "$NO_CLEANUP_BE" ] && cleanup_commands="cleanup_be ${cleanup_commands}"
137
138beadm mount ${BENAME} ${BE_TMP}/mnt || errx "Unable to mount BE ${BENAME}."
139
140echo "Mounted ${BENAME} to ${BE_MNTPT}, performing install/update ..."
141make "$@" DESTDIR=${BE_MNTPT} installkernel || errx "Installkernel failed!"
167objdir=$(make -V .OBJDIR 2>/dev/null)
168[ ! -d "${objdir}" ] && errx "Must have built FreeBSD from source tree"
169
170# May be a worktree, in which case .git is a file, not a directory.
171if [ -e .git ] ; then
172 commit_time=$(git show --format='%ct' 2>/dev/null | head -1)
173 [ $? -ne 0 ] && errx "Can't lookup git commit timestamp"
174 commit_ts=$(date -r ${commit_time} '+%Y%m%d.%H%M%S')

--- 25 unchanged lines hidden (view full) ---

200
201beadm create ${BENAME} >/dev/null || errx "Unable to create BE ${BENAME}"
202[ -z "$NO_CLEANUP_BE" ] && cleanup_commands="cleanup_be ${cleanup_commands}"
203
204beadm mount ${BENAME} ${BE_TMP}/mnt || errx "Unable to mount BE ${BENAME}."
205
206echo "Mounted ${BENAME} to ${BE_MNTPT}, performing install/update ..."
207make "$@" DESTDIR=${BE_MNTPT} installkernel || errx "Installkernel failed!"
142make "$@" DESTDIR=${BE_MNTPT} installworld || errx "Installworld failed!"
208if [ -n "${CONFIG_UPDATER}" ]; then
209 "update_${CONFIG_UPDATER}_pre"
210 [ $? -ne 0 ] && errx "${CONFIG_UPDATER} (pre-world) failed!"
211fi
143
212
213# Mount the source and object tree within the BE in order to account for any
214# changes applied by the pre-installworld updater. Cleanup any directories
215# created if they didn't exist previously.
216create_be_dirs "${srcdir}" "${objdir}" || errx "Unable to create BE dirs"
217mount -t nullfs "${srcdir}" "${BE_MNTPT}${srcdir}" || errx "Unable to mount src"
218mount -t nullfs "${objdir}" "${BE_MNTPT}${objdir}" || errx "Unable to mount obj"
219
220chroot ${BE_MNTPT} make "$@" -C ${srcdir} installworld || \
221 errx "Installworld failed!"
222
144if [ -n "${CONFIG_UPDATER}" ]; then
145 "update_${CONFIG_UPDATER}"
223if [ -n "${CONFIG_UPDATER}" ]; then
224 "update_${CONFIG_UPDATER}"
146 [ $? -ne 0 ] && errx "${CONFIG_UPDATER} failed!"
225 [ $? -ne 0 ] && errx "${CONFIG_UPDATER} (post-world) failed!"
147fi
148
149BE_PKG="chroot ${BE_MNTPT} env ASSUME_ALWAYS_YES=true pkg"
150if [ -z "${NO_PKG_UPGRADE}" ]; then
151 ${BE_PKG} update || errx "Unable to update pkg"
152 ${BE_PKG} upgrade || errx "Unable to upgrade pkgs"
153fi
154
226fi
227
228BE_PKG="chroot ${BE_MNTPT} env ASSUME_ALWAYS_YES=true pkg"
229if [ -z "${NO_PKG_UPGRADE}" ]; then
230 ${BE_PKG} update || errx "Unable to update pkg"
231 ${BE_PKG} upgrade || errx "Unable to upgrade pkgs"
232fi
233
155beadm unmount ${BENAME} || errx "Unable to unmount BE"
156rmdir_be
234if [ -n "$NO_CLEANUP_BE" ]; then
235 echo "Boot Environment ${BENAME} may be examined in ${BE_MNTPT}."
236 echo "Afterwards, run this to cleanup:"
237 echo " env BENAME=${BENAME} BE_MNTPT=${BE_MNTPT} BEINSTALL_CMD=postmortem $0"
238 exit 0
239fi
240
241unmount_be || errx "Unable to unmount BE"
242rmdir_be || errx "Unable to cleanup BE"
157beadm activate ${BENAME} || errx "Unable to activate BE"
158echo
159beadm list
160echo
161echo "Boot environment ${BENAME} setup complete; reboot to use it."
243beadm activate ${BENAME} || errx "Unable to activate BE"
244echo
245beadm list
246echo
247echo "Boot environment ${BENAME} setup complete; reboot to use it."