1 | #!/bin/sh
|
---|
2 | #
|
---|
3 | BASEDIR=`dirname $0`
|
---|
4 | . ${BASEDIR}/common.inc.sh
|
---|
5 |
|
---|
6 | NANOBSD="$NANO_SRC/tools/tools/nanobsd/nanobsd.sh"
|
---|
7 |
|
---|
8 | usage() {
|
---|
9 | cat <<EOF
|
---|
10 | # Usage $0 <arguments>
|
---|
11 | #
|
---|
12 | # Wrapper around nanobsd.sh with autodetection of already processed steps to
|
---|
13 | # provide some failsafe net, which avoids building world and/or kernel by
|
---|
14 | # default.
|
---|
15 | #
|
---|
16 | # Rick van der Zwet <rick@wirelessleiden.nl>
|
---|
17 | #
|
---|
18 | # Arguments:
|
---|
19 | # build - Build NanoBSD parts which are not build yet
|
---|
20 | # build force kernel - Build NanoBSD and force rebuilding the kernel
|
---|
21 | # build force world - Build NanoBSD and force rebuilding world
|
---|
22 | # edit - Manually edit the image
|
---|
23 | # config [for <node>] - Configure image to be used for <node>
|
---|
24 | # rebuild - Rebuild NanoBSD (aka force rebuilding all)
|
---|
25 | # deploy on <node> [and reboot] - Deploy the image on node and reboot if needed
|
---|
26 | # ports update - Update the packages from ports
|
---|
27 | # ports force rebuild - Forcefully rebuilding all required packages
|
---|
28 | EOF
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 | deploy_image() {
|
---|
33 | # Find object directory
|
---|
34 | img=${OBJDIR}/_.disk.image
|
---|
35 |
|
---|
36 | if [ ! -r "$img" ]; then
|
---|
37 | p_err Source $img does not exists
|
---|
38 | exit 1
|
---|
39 | fi
|
---|
40 |
|
---|
41 | prompt_timeout=5
|
---|
42 | p_warn "Going to DEPLOY $img to $host:"
|
---|
43 | p_warn "You will need to type the root password at least twice, consider using a key"
|
---|
44 | $do_reboot && p_warn "AND will REBOOT the $host"
|
---|
45 |
|
---|
46 |
|
---|
47 | p_warn "Press CTRL+C in $prompt_timeout seconds to CANCEL"
|
---|
48 | # sleep $prompt_timeout
|
---|
49 |
|
---|
50 | echo "# Trying to connect to $host"
|
---|
51 | release=`ssh $host 'uname -r'` || exit 1
|
---|
52 |
|
---|
53 | # Release update cycle depends on the release we are coming from
|
---|
54 | echo "# Host has FreeBSD Release: $release"
|
---|
55 | updatecmd="/tmp/update$$"
|
---|
56 | cat >> $updatecmd <<'EOF'
|
---|
57 | #
|
---|
58 | # Pre-cleaning of /cfg
|
---|
59 | #
|
---|
60 | mount -wo noatime /cfg || exit 1
|
---|
61 | find /cfg -type f | grep -v -e '^/cfg/\(pf.hybrid.conf.local\|resolv.conf\|motd\|rc.conf.local\)$' \
|
---|
62 | -e '^/cfg/local/\(ntp.drift\|dhcpd.conf\|dnsmasq.conf\|unbound.wleiden.conf\|wleiden.yaml\)$' \
|
---|
63 | -e '^/cfg/ssh/ssh_host_*' \
|
---|
64 | -e '^/cfg/dot_ssh/authorized_keys$' \
|
---|
65 | | xargs rm -v || exit 1
|
---|
66 | umount /cfg || exit 1
|
---|
67 |
|
---|
68 | #
|
---|
69 | # Release specific update
|
---|
70 | #
|
---|
71 | EOF
|
---|
72 | if echo $release | grep -q -e '^9\.[0-9]-RELEASE\(-p[0-9]\{1,2\}\)\?$'; then
|
---|
73 | cat >> $updatecmd <<'EOF'
|
---|
74 | tunefs -L WLIMGs3 /dev/ada0s3 || exit 1
|
---|
75 | echo /tools/update || exit 1
|
---|
76 | EOF
|
---|
77 | elif echo $release | grep -q -e '^11\.[0-9]-RELEASE\(-p[0-9]\{1,2\}\)\?$'; then
|
---|
78 | cat >> $updatecmd <<'EOF'
|
---|
79 | echo /tools/update || exit 1
|
---|
80 | EOF
|
---|
81 | else
|
---|
82 | p_err "$release not supported for upgrade"
|
---|
83 | exit 1
|
---|
84 | fi
|
---|
85 |
|
---|
86 | cat $updatecmd | ssh $host "cat > $updatecmd" || exit 1
|
---|
87 | cat $img | ssh $host "sh -x $updatecmd" || exit 1
|
---|
88 |
|
---|
89 | if $do_reboot; then
|
---|
90 | echo "# Reboot requested, press CTRL+C in $prompt_timeout seconds to cancel"
|
---|
91 | sleep $prompt_timeout
|
---|
92 | ssh $host reboot || exit 1
|
---|
93 | fi
|
---|
94 | }
|
---|
95 |
|
---|
96 | config_image() {
|
---|
97 | node_name=${1:+"-b -c $1"}
|
---|
98 |
|
---|
99 | img=$OBJDIR/_.disk.full
|
---|
100 |
|
---|
101 | mnt=`mktemp -d -t $(basename $0)`
|
---|
102 | md=`mdconfig -a -t vnode -f $img`
|
---|
103 |
|
---|
104 | # Clean up when done
|
---|
105 | trap "umount $mnt/dev; umount $mnt/cfg; umount $mnt; mdconfig -d -u $md; rm -d $mnt" 0
|
---|
106 | trap "exit 1" 1 2 3 15
|
---|
107 |
|
---|
108 | # Root filesystem
|
---|
109 | mount /dev/${md}s1a $mnt || exit 1
|
---|
110 |
|
---|
111 | # /dev/null in chroot
|
---|
112 | mount -t devfs devfs ${mnt}/dev || exit 1
|
---|
113 |
|
---|
114 | # Config files lives at /cfg location
|
---|
115 | mount /dev/${md}s3 $mnt/cfg || exit 1
|
---|
116 |
|
---|
117 | # Try to fetch and store config
|
---|
118 | chroot $mnt /tools/wl-config -d -n -m startup $node_name || exit 1
|
---|
119 | }
|
---|
120 |
|
---|
121 | edit_image() {
|
---|
122 | img=$OBJDIR/_.disk.full
|
---|
123 |
|
---|
124 | mnt=`mktemp -d -t $(basename $0)`
|
---|
125 | md=`mdconfig -a -t vnode -f $img`
|
---|
126 |
|
---|
127 | # Clean up when done
|
---|
128 | trap "umount $mnt/dev; umount $mnt/cfg; umount $mnt; mdconfig -d -u $md; rm -d $mnt" 0
|
---|
129 | trap "exit 1" 1 2 3 15
|
---|
130 |
|
---|
131 | # Root filesystem
|
---|
132 | mount /dev/${md}s1a $mnt || exit 1
|
---|
133 |
|
---|
134 | # /dev/null in chroot
|
---|
135 | mount -t devfs devfs ${mnt}/dev || exit 1
|
---|
136 |
|
---|
137 | # Config files lives at /cfg location
|
---|
138 | mount /dev/${md}s3 $mnt/cfg || exit 1
|
---|
139 |
|
---|
140 | # Nasty hack to set custom prompt
|
---|
141 | prompt='set prompt = "image# "'
|
---|
142 | echo $prompt >> $mnt/root/.cshrc
|
---|
143 |
|
---|
144 | p_info "Type exit when done"
|
---|
145 | chroot $mnt /bin/csh
|
---|
146 | p_info "Any changes are made permanent on image $img"
|
---|
147 |
|
---|
148 | # Unset prompt again
|
---|
149 | sed -I '' "/^$prompt$/d" $mnt/root/.cshrc
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | build_image() {
|
---|
154 | p_info Forcefully building kernel: $FORCE_KERNEL
|
---|
155 | p_info Forcefully building world : $FORCE_WORLD
|
---|
156 |
|
---|
157 | NANOBSD_EXTRA=${NANOBSD_EXTRA:-''}
|
---|
158 |
|
---|
159 | if [ ! -r "${NANOBSD}" ]; then
|
---|
160 | p_err ${NANOBSD} does not exists
|
---|
161 | exit 1
|
---|
162 | fi
|
---|
163 |
|
---|
164 | if [ ! -x "${NANOBSD}" ]; then
|
---|
165 | NANOBSD="sh ${NANOBSD}"
|
---|
166 | fi
|
---|
167 |
|
---|
168 | # Find object directory
|
---|
169 | OBJDIR="/usr/obj/nanobsd.${NANO_NAME}"
|
---|
170 |
|
---|
171 | if [ -d "${OBJDIR}" ]; then
|
---|
172 | # No clean during run which was interrupted
|
---|
173 | if [ "$FORCE_WORLD" = "no" -a "$FORCE_KERNEL" = "no" ]; then
|
---|
174 | NANOBSD_FLAGS="-n"
|
---|
175 | else
|
---|
176 | NANOBSD_FLAGS=""
|
---|
177 | fi
|
---|
178 |
|
---|
179 | # Detect succesfull buildworld
|
---|
180 | tail -10 ${OBJDIR}/_.bw | grep 'World build completed'
|
---|
181 | if [ $? -eq 0 -a ${FORCE_WORLD} = "no" ]; then
|
---|
182 | p_info NO building of world
|
---|
183 | NANOBSD_FLAGS="${NANOBSD_FLAGS} -w"
|
---|
184 | fi
|
---|
185 |
|
---|
186 | # Detect succesfull buildkernel
|
---|
187 | tail -10 ${OBJDIR}/_.bk | grep 'Kernel build for .* completed'
|
---|
188 | if [ $? -eq 0 -a ${FORCE_KERNEL} = "no" ]; then
|
---|
189 | p_info NO building of kernel
|
---|
190 | NANOBSD_FLAGS="${NANOBSD_FLAGS} -k"
|
---|
191 | fi
|
---|
192 |
|
---|
193 | else
|
---|
194 | p_warn Nothing yet, starting fresh
|
---|
195 | NANOBSD_FLAGS=""
|
---|
196 | fi
|
---|
197 |
|
---|
198 | # Provide verbose output by default
|
---|
199 | COMMAND="${NANOBSD} ${NANOBSD_FLAGS} -c ${NANO_CFG_FILE} -v ${NANOBSD_EXTRA}"
|
---|
200 | f_time ${COMMAND}
|
---|
201 | RETVAL=$?
|
---|
202 |
|
---|
203 | # Verify on build failures
|
---|
204 | tail -10 ${OBJDIR}/_.bw | grep 'World build completed'
|
---|
205 | if [ $? -eq 1 ]; then
|
---|
206 | p_err Building world FAILED, check ${OBJDIR}/_.bw
|
---|
207 | fi
|
---|
208 | tail -10 ${OBJDIR}/_.bk | grep 'Kernel build for .* completed'
|
---|
209 | if [ $? -eq 1 ]; then
|
---|
210 | p_err Building kernel FAILED, check ${OBJDIR}/_.bk
|
---|
211 | fi
|
---|
212 | if [ $RETVAL -ne 0 ]; then
|
---|
213 | p_err "Errors in building NanoBSD Image ($RETVAL)"
|
---|
214 | fi
|
---|
215 | p_info End time: `date`
|
---|
216 | exit ${RETVAL}
|
---|
217 | }
|
---|
218 |
|
---|
219 | #
|
---|
220 | # Argument parsing
|
---|
221 | #
|
---|
222 | FORCE_KERNEL=${FORCE_KERNEL:-"no"}
|
---|
223 | FORCE_WORLD=${FORCE_WORLD:-"no"}
|
---|
224 | if [ -z "$1" ]; then
|
---|
225 | usage; exit 1
|
---|
226 | elif [ "$1" = "build" ]; then
|
---|
227 | if [ -z "$2" ]; then
|
---|
228 | elif [ "$2" = "force" ]; then
|
---|
229 | if [ "$3" = "kernel" ]; then
|
---|
230 | FORCE_KERNEL="yes"
|
---|
231 | elif [ "$3" = "world" ]; then
|
---|
232 | FORCE_WORLD="yes"
|
---|
233 | else
|
---|
234 | echo "Argument Error - '$3'"; exit 128
|
---|
235 | fi
|
---|
236 | else
|
---|
237 | echo "Argument Error - '$2'"; exit 128
|
---|
238 | fi
|
---|
239 | build_image
|
---|
240 | elif [ "$1" = "rebuild" ]; then
|
---|
241 | FORCE_KERNEL="yes"
|
---|
242 | FORCE_WORLD="yes"
|
---|
243 | build_image
|
---|
244 | elif [ "$1" = "deploy" -a "$2" = "on" ]; then
|
---|
245 | if [ -z "$3" ]; then
|
---|
246 | echo "Argument Error - '$3'"; exit 128
|
---|
247 | fi
|
---|
248 | host=$3
|
---|
249 | do_reboot=false
|
---|
250 | if [ -n "$4" -o -n "$5" ]; then
|
---|
251 | if [ "$4" = "and" -a "$5" = "reboot" ]; then
|
---|
252 | do_reboot=true
|
---|
253 | else
|
---|
254 | echo "Argument Error - '$4 $5'"; exit 128
|
---|
255 | fi
|
---|
256 | fi
|
---|
257 | deploy_image
|
---|
258 | elif [ "$1" = "ports" ]; then
|
---|
259 | if [ "$2" = "update" ]; then
|
---|
260 | # Fetch the latest details and provide listing of packages to be updated
|
---|
261 | portsnap fetch update || exit 1
|
---|
262 |
|
---|
263 | # HACK: install our own ports _inside_ the normal ports dir
|
---|
264 | cp -fR $WL_PORTSDIR/* $PORTSDIR || exit 1
|
---|
265 |
|
---|
266 | # Make sure portmaster is present to update all ports
|
---|
267 | portmaster --version 1>/dev/null 2>/dev/null || make -C /usr/ports/ports-mgmt/portmaster BATCH=yes install clean || exit 1
|
---|
268 |
|
---|
269 | # Update via portmaster
|
---|
270 | CMD="env `echo $PKG_MAKE_ARGS` portmaster --no-confirm --update-if-newer -t -y -d -G `echo $PACKAGE_LIST`"
|
---|
271 | echo "# Going to run port upgrade cycle: $CMD"; $CMD || exit 1
|
---|
272 |
|
---|
273 | . ${BASEDIR}/package-build.sh
|
---|
274 | elif [ "$2" = "force" -a "$3" = "rebuild" ]; then
|
---|
275 | export FORCE_REBUILD=1
|
---|
276 | . ${BASEDIR}/package-build.sh
|
---|
277 | else
|
---|
278 | shift 1
|
---|
279 | echo "Arguments Error - '$*'"; exit 128
|
---|
280 | fi
|
---|
281 | elif [ "$1" = "config" ]; then
|
---|
282 | if [ "$2" = "for" ]; then
|
---|
283 | if [ -n "$3" ]; then
|
---|
284 | node_name=$3
|
---|
285 | else
|
---|
286 | echo "Arguments Error - '$*'"; exit 128
|
---|
287 | fi
|
---|
288 | else
|
---|
289 | echo "Arguments Error - '$*'"; exit 128
|
---|
290 | fi
|
---|
291 | config_image $node_name
|
---|
292 | elif [ "$1" = "edit" ]; then
|
---|
293 | edit_image
|
---|
294 | else
|
---|
295 | echo "Argument Error - '$1'"; exit 128
|
---|
296 | fi
|
---|
297 |
|
---|