source: hybrid/branches/releng-11/nanobsd/tools/image

Last change on this file was 14128, checked in by rick, 7 years ago

Fix pkg creation symlinks from image scipt

Using the old 'hackish' method, call package-build.sh instead.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
File size: 7.7 KB
Line 
1#!/bin/sh
2#
3BASEDIR=`dirname $0`
4. ${BASEDIR}/common.inc.sh
5
6NANOBSD="$NANO_SRC/tools/tools/nanobsd/nanobsd.sh"
7
8usage() {
9cat <<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
28EOF
29}
30
31
32deploy_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#
60mount -wo noatime /cfg || exit 1
61find /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/ssh/authorized_keys$' \
65 | xargs rm -v || exit 1
66umount /cfg || exit 1
67
68#
69# Release specific update
70#
71EOF
72 if echo $release | grep -q -e '^9\.[0-9]-RELEASE\(-p[0-9]\{1,2\}\)\?$'; then
73 cat >> $updatecmd <<'EOF'
74. /etc/nanobsd.conf
75NANO_LABEL="WLIMG"
76
77tunefs -L ${NANO_LABEL}s3 /dev/${NANO_DRIVE}s3 || exit 1
78/tools/update || exit 1
79
80# Quirck to fix partition table & label if partion is copied to second image.
81if mount | grep -q ${NANO_DRIVE}s1; then
82 tunefs -L ${NANO_LABEL}s2a /dev/${NANO_DRIVE}s2a || exit 1
83 trap "umount /mnt" 1 2 15 EXIT
84 mount /dev/${NANO_DRIVE}s2a /mnt
85 sed -i "" "s/${NANO_LABEL}s1/${NANO_LABEL}s2/" /mnt/conf/base/etc/fstab
86 sed -i "" "s/${NANO_LABEL}s1/${NANO_LABEL}s2/" /mnt/etc/fstab
87 umount /mnt
88 trap 1 2 15 EXIT
89fi
90EOF
91 elif echo $release | grep -q -e '^11\.[0-9]-RELEASE\(-p[0-9]\{1,2\}\)\?$'; then
92 cat >> $updatecmd <<'EOF'
93/tools/update || exit 1
94EOF
95 else
96 p_err "$release not supported for upgrade"
97 exit 1
98 fi
99
100 cat $updatecmd | ssh $host "cat > $updatecmd" || exit 1
101 cat $img | ssh -C $host "sh -x $updatecmd" || exit 1
102
103 if $do_reboot; then
104 echo "# Reboot requested, press CTRL+C in $prompt_timeout seconds to cancel"
105 sleep $prompt_timeout
106 ssh $host reboot || exit 1
107 fi
108}
109
110config_image() {
111 node_name=${1:+"-b -c $1"}
112
113 img=$OBJDIR/_.disk.full
114
115 mnt=`mktemp -d -t $(basename $0)`
116 md=`mdconfig -a -t vnode -f $img`
117
118 # Clean up when done
119 trap "umount $mnt/dev; umount $mnt/cfg; umount $mnt; mdconfig -d -u $md; rm -d $mnt" 0
120 trap "exit 1" 1 2 3 15
121
122 # Root filesystem
123 mount /dev/${md}s1a $mnt || exit 1
124
125 # /dev/null in chroot
126 mount -t devfs devfs ${mnt}/dev || exit 1
127
128 # Config files lives at /cfg location
129 mount /dev/${md}s3 $mnt/cfg || exit 1
130
131 # Try to fetch and store config
132 chroot $mnt /tools/wl-config -d -n -m startup $node_name || exit 1
133}
134
135edit_image() {
136 img=$OBJDIR/_.disk.full
137
138 mnt=`mktemp -d -t $(basename $0)`
139 md=`mdconfig -a -t vnode -f $img`
140
141 # Clean up when done
142 trap "umount $mnt/dev; umount $mnt/cfg; umount $mnt; mdconfig -d -u $md; rm -d $mnt" 0
143 trap "exit 1" 1 2 3 15
144
145 # Root filesystem
146 mount /dev/${md}s1a $mnt || exit 1
147
148 # /dev/null in chroot
149 mount -t devfs devfs ${mnt}/dev || exit 1
150
151 # Config files lives at /cfg location
152 mount /dev/${md}s3 $mnt/cfg || exit 1
153
154 # Nasty hack to set custom prompt
155 prompt='set prompt = "image# "'
156 echo $prompt >> $mnt/root/.cshrc
157
158 p_info "Type exit when done"
159 chroot $mnt /bin/csh
160 p_info "Any changes are made permanent on image $img"
161
162 # Unset prompt again
163 sed -I '' "/^$prompt$/d" $mnt/root/.cshrc
164}
165
166
167build_image() {
168 p_info Forcefully building kernel: $FORCE_KERNEL
169 p_info Forcefully building world : $FORCE_WORLD
170
171 NANOBSD_EXTRA=${NANOBSD_EXTRA:-''}
172
173 if [ ! -r "${NANOBSD}" ]; then
174 p_err ${NANOBSD} does not exists
175 exit 1
176 fi
177
178 if [ ! -x "${NANOBSD}" ]; then
179 NANOBSD="sh ${NANOBSD}"
180 fi
181
182 # Find object directory
183 OBJDIR="/usr/obj/nanobsd.${NANO_NAME}"
184
185 if [ -d "${OBJDIR}" ]; then
186 # No clean during run which was interrupted
187 if [ "$FORCE_WORLD" = "no" -a "$FORCE_KERNEL" = "no" ]; then
188 NANOBSD_FLAGS="-n"
189 else
190 NANOBSD_FLAGS=""
191 fi
192
193 # Detect succesfull buildworld
194 tail -10 ${OBJDIR}/_.bw | grep 'World build completed'
195 if [ $? -eq 0 -a ${FORCE_WORLD} = "no" ]; then
196 p_info NO building of world
197 NANOBSD_FLAGS="${NANOBSD_FLAGS} -w"
198 fi
199
200 # Detect succesfull buildkernel
201 tail -10 ${OBJDIR}/_.bk | grep 'Kernel build for .* completed'
202 if [ $? -eq 0 -a ${FORCE_KERNEL} = "no" ]; then
203 p_info NO building of kernel
204 NANOBSD_FLAGS="${NANOBSD_FLAGS} -k"
205 fi
206
207 else
208 p_warn Nothing yet, starting fresh
209 NANOBSD_FLAGS=""
210 fi
211
212 # Provide verbose output by default
213 COMMAND="${NANOBSD} ${NANOBSD_FLAGS} -c ${NANO_CFG_FILE} -v ${NANOBSD_EXTRA}"
214 f_time ${COMMAND}
215 RETVAL=$?
216
217 # Verify on build failures
218 tail -10 ${OBJDIR}/_.bw | grep 'World build completed'
219 if [ $? -eq 1 ]; then
220 p_err Building world FAILED, check ${OBJDIR}/_.bw
221 fi
222 tail -10 ${OBJDIR}/_.bk | grep 'Kernel build for .* completed'
223 if [ $? -eq 1 ]; then
224 p_err Building kernel FAILED, check ${OBJDIR}/_.bk
225 fi
226 if [ $RETVAL -ne 0 ]; then
227 p_err "Errors in building NanoBSD Image ($RETVAL)"
228 fi
229 p_info End time: `date`
230 exit ${RETVAL}
231}
232
233#
234# Argument parsing
235#
236FORCE_KERNEL=${FORCE_KERNEL:-"no"}
237FORCE_WORLD=${FORCE_WORLD:-"no"}
238if [ -z "$1" ]; then
239 usage; exit 1
240elif [ "$1" = "build" ]; then
241 if [ -z "$2" ]; then
242 elif [ "$2" = "force" ]; then
243 if [ "$3" = "kernel" ]; then
244 FORCE_KERNEL="yes"
245 elif [ "$3" = "world" ]; then
246 FORCE_WORLD="yes"
247 else
248 echo "Argument Error - '$3'"; exit 128
249 fi
250 else
251 echo "Argument Error - '$2'"; exit 128
252 fi
253 build_image
254elif [ "$1" = "rebuild" ]; then
255 FORCE_KERNEL="yes"
256 FORCE_WORLD="yes"
257 build_image
258elif [ "$1" = "deploy" -a "$2" = "on" ]; then
259 if [ -z "$3" ]; then
260 echo "Argument Error - '$3'"; exit 128
261 fi
262 host=$3
263 do_reboot=false
264 if [ -n "$4" -o -n "$5" ]; then
265 if [ "$4" = "and" -a "$5" = "reboot" ]; then
266 do_reboot=true
267 else
268 echo "Argument Error - '$4 $5'"; exit 128
269 fi
270 fi
271 deploy_image
272elif [ "$1" = "ports" ]; then
273 if [ "$2" = "update" ]; then
274 ${BASEDIR}/package-build.sh update
275 elif [ "$2" = "force" -a "$3" = "rebuild" ]; then
276 ${BASEDIR}/package-build.sh rebuild
277 else
278 shift 1
279 echo "Arguments Error - '$*'"; exit 128
280 fi
281elif [ "$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
292elif [ "$1" = "edit" ]; then
293 edit_image
294else
295 echo "Argument Error - '$1'"; exit 128
296fi
297
Note: See TracBrowser for help on using the repository browser.