-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkimagebuilder.sh
353 lines (314 loc) · 9.14 KB
/
mkimagebuilder.sh
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#! /bin/sh
#
# Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
# 2019 2020 Izumi Tsutsui.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# a dumb script to build a qemu image that configures liveimage automatically
#
# This script creates a NetBSD/i386 disk image for qemu that is intended
# to configure the target liveimage without prompts.
#
# It will mount the target liveimage on /targetroot,
# also mount setupliveimage on /targetroot/mnt,
# and then just invoke inst.sh in setupliveimage.
# The setup script checks "vioif0" and in that case
# it copies xorg.conf.vesa to /etc/X11.
#
# expected usage:
# % qemu-system-i386 -m 512 \
# -hda obj/work.i386.qemu/liveimage-i386-qemu-YYYYMMDD.img \
# -hdb obj/work.i386.usb/liveimage-i386-usb-YYYYMMDD.img \
# -hdc obj/work.setupliveimage/setupliveimage-YYYYMMDD.fs
#
# for VirtualBox image:
# % qemu-system-i386 -m 512 \
# -hda obj/work.i386.qemu/liveimage-i386-qemu-YYYYMMDD.img \
# -hdb obj/liveimage-i386-vbox-YYYYMMDD.img \
# -hdc obj/work.setupliveimage/setupliveimage-YYYYMMDD.fs
# -net nic,model=virtio
#
if [ -f REVISION ]; then
. ./REVISION
fi
if [ "${REVISION}"X = "X" ]; then
REVISION=`date +%C%y%m%d`
fi
DISKNAME=TeokureBuilder
IMAGEHOSTNAME=teokure
TIMEZONE=Japan
IMAGE_TYPE=qemu
usage()
{
echo "usage: $0 <machine>"
echo "supported machine: amd64, i386"
exit 1
}
err()
{
echo $1 failed!
exit 1
}
if [ $# != 1 ]; then
usage
fi
MACHINE=$1
#
# target dependent info
#
if [ "${MACHINE}" = "amd64" ]; then
MACHINE_ARCH=x86_64
MACHINE_GNU_PLATFORM=x86_64--netbsd # for fdisk(8)
TARGET_ENDIAN=le
KERN_SET=kern-GENERIC
SUFFIX_SETS=tar.xz
EXTRA_SETS= # nothing
PRIMARY_BOOT=bootxx_ffsv1
SECONDARY_BOOT=boot
SECONDARY_BOOT_ARG= # nothing
fi
if [ "${MACHINE}" = "i386" ]; then
MACHINE_ARCH=i386
MACHINE_GNU_PLATFORM=i486--netbsdelf # for fdisk(8)
TARGET_ENDIAN=le
KERN_SET=kern-GENERIC
SUFFIX_SETS=tgz
EXTRA_SETS= # nothing
PRIMARY_BOOT=bootxx_ffsv1
SECONDARY_BOOT=boot
SECONDARY_BOOT_ARG= # nothing
fi
if [ -z ${MACHINE_ARCH} ]; then
echo "Unsupported MACHINE (${MACHINE})"
exit 1
fi
#
# tooldir settings
#
#NETBSDSRCDIR=/usr/src
#TOOLDIR=/usr/tools/${MACHINE_ARCH}
if [ -z ${NETBSDSRCDIR} ]; then
NETBSDSRCDIR=/usr/src
fi
if [ -z ${TOOLDIR} ]; then
_HOST_OSNAME=`uname -s`
_HOST_OSREL=`uname -r`
_HOST_ARCH=`uname -p 2> /dev/null || uname -m`
TOOLDIRNAME=tooldir.${_HOST_OSNAME}-${_HOST_OSREL}-${_HOST_ARCH}
TOOLDIR=${NETBSDSRCDIR}/obj.${MACHINE}/${TOOLDIRNAME}
if [ ! -d ${TOOLDIR} ]; then
TOOLDIR=${NETBSDSRCDIR}/${TOOLDIRNAME}
fi
fi
if [ ! -d ${TOOLDIR} ]; then
echo 'set TOOLDIR first'; exit 1
fi
if [ ! -x ${TOOLDIR}/bin/nbmake-${MACHINE} ]; then
echo 'build tools in ${TOOLDIR} first'; exit 1
fi
#
# info about ftp to get binary sets
#
#FTPHOST=ftp.NetBSD.org
#FTPHOST=ftp.jp.NetBSD.org
#FTPHOST=ftp7.jp.NetBSD.org
FTPHOST=cdn.NetBSD.org
#FTPHOST=nycdn.NetBSD.org
RELEASE=10.0
RELEASEDIR=pub/NetBSD/NetBSD-${RELEASE}
#RELEASEDIR=pub/NetBSD-daily/netbsd-10/latest
#
# misc build settings
#
# tools binaries
TOOL_MAKEFS=${TOOLDIR}/bin/nbmakefs
TOOL_DISKLABEL=${TOOLDIR}/bin/nbdisklabel
TOOL_FDISK=${TOOLDIR}/bin/${MACHINE_GNU_PLATFORM}-fdisk
TOOL_INSTALLBOOT=${TOOLDIR}/bin/nbinstallboot
TOOL_SED=${TOOLDIR}/bin/nbsed
INSTALLBOOT_OPT="-v -o console=com0"
# host binaries
CAT=cat
CP=cp
DD=dd
FTP=ftp
#FTP=tnftp
FTP_OPTIONS=-V
MKDIR=mkdir
RM=rm
SH=sh
TAR=tar
# working directories
if [ "${OBJDIR}"X = "X" ]; then
OBJDIR=.
fi
TARGETROOTDIR=${OBJDIR}/targetroot.${MACHINE}.${IMAGE_TYPE}
DOWNLOADDIR=download.${RELEASE}.${MACHINE}
WORKDIR=${OBJDIR}/work.${MACHINE}.${IMAGE_TYPE}
IMAGE=${WORKDIR}/liveimage-${MACHINE}-${IMAGE_TYPE}-${REVISION}.img
#
# target image size settings
#
IMAGEMB=400 # minimum
IMAGESECTORS=$((${IMAGEMB} * 1024 * 1024 / 512))
# no swap
BSDPARTSECTORS=${IMAGESECTORS}
FSSECTORS=${IMAGESECTORS}
FSOFFSET=0
FSSIZE=$((${FSSECTORS} * 512))
HEADS=64
SECTORS=32
CYLINDERS=$((${IMAGESECTORS} / ( ${HEADS} * ${SECTORS} ) ))
FSCYLINDERS=$((${FSSECTORS} / ( ${HEADS} * ${SECTORS} ) ))
# makefs(8) parameters
BLOCKSIZE=16384
FRAGSIZE=4096
DENSITY=8192
echo creating ${IMAGE_TYPE} image for ${MACHINE}...
#
# get binary sets
#
URL_SETS=http://${FTPHOST}/${RELEASEDIR}/${MACHINE}/binary/sets
SETS="${KERN_SET} base etc"
${MKDIR} -p ${DOWNLOADDIR}
for set in ${SETS}; do
if [ ! -f ${DOWNLOADDIR}/${set}.${SUFFIX_SETS} ]; then
echo Fetching ${set}.${SUFFIX_SETS}...
${FTP} ${FTP_OPTIONS} \
-o ${DOWNLOADDIR}/${set}.${SUFFIX_SETS} \
${URL_SETS}/${set}.${SUFFIX_SETS} \
|| err ${FTP}
fi
done
#
# create targetroot
#
echo Removing ${TARGETROOTDIR}...
${RM} -rf ${TARGETROOTDIR}
${MKDIR} -p ${TARGETROOTDIR}
for set in ${SETS}; do
echo Extracting ${set}...
${TAR} -C ${TARGETROOTDIR} -zxf ${DOWNLOADDIR}/${set}.${SUFFIX_SETS} \
|| err ${TAR}
done
# XXX /var/spool/ftp/hidden is unreadable
chmod u+r ${TARGETROOTDIR}/var/spool/ftp/hidden
# copy secondary boot for bootstrap
# XXX probabry more machine dependent
if [ ! -z ${SECONDARY_BOOT} ]; then
echo Copying secondary boot...
${CP} ${TARGETROOTDIR}/usr/mdec/${SECONDARY_BOOT} ${TARGETROOTDIR}
fi
#
# create target fs
#
echo Removing ${WORKDIR}...
${RM} -rf ${WORKDIR}
${MKDIR} -p ${WORKDIR}
echo Preparing /etc/fstab...
${CAT} > ${WORKDIR}/fstab <<EOF
ROOT.a / ffs rw,log 1 1
ROOT.b none none sw 0 0
ptyfs /dev/pts ptyfs rw 0 0
kernfs /kern kernfs rw 0 0
procfs /proc procfs rw 0 0
tmpfs /tmp tmpfs rw 0 0
EOF
${CP} ${WORKDIR}/fstab ${TARGETROOTDIR}/etc
echo Preparing setup script...
${CAT} > ${WORKDIR}/etc.rc <<EOF
export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/pkg/sbin:/usr/pkg/bin
export PATH=\${PATH}:/usr/X11R7/bin:/usr/local/sbin:/usr/local/bin
set -o emacs
stty erase ^H
mount -o async /dev/dk1 /targetroot || mount -o async /dev/wd1a /targetroot
mount -r /dev/wd2a /targetroot/mnt
chroot /targetroot /bin/sh /mnt/inst.sh
IFNAME=\`ifconfig -l | awk '{print \$1}'\`
if [ "\${IFNAME}"x = "vioif0"x ]; then
cp /targetroot/mnt/etc/xorg.conf.vesa /targetroot/etc/X11/xorg.conf
fi
halt -p
EOF
${RM} -f ${TARGETROOTDIR}/etc/rc
${CP} ${WORKDIR}/etc.rc ${TARGETROOTDIR}/etc/rc
echo Setting localtime...
ln -sf /usr/share/zoneinfo/${TIMEZONE} ${TARGETROOTDIR}/etc/localtime
echo Preparing spec file for makefs...
${CAT} ${TARGETROOTDIR}/etc/mtree/* | \
${TOOL_SED} -e 's/ size=[0-9]*//' > ${WORKDIR}/spec
${SH} ${TARGETROOTDIR}/dev/MAKEDEV -s all | \
${TOOL_SED} -e '/^\. type=dir/d' -e 's,^\.,./dev,' >> ${WORKDIR}/spec
# spec for optional files/dirs
${CAT} >> ${WORKDIR}/spec <<EOF
./boot type=file mode=0444
./kern type=dir mode=0755
./netbsd type=file mode=0755
./proc type=dir mode=0755
./targetroot type=dir mode=0755
./tmp type=dir mode=1777
EOF
echo Creating rootfs...
${TOOL_MAKEFS} -M ${FSSIZE} -m ${FSSIZE} \
-B ${TARGET_ENDIAN} \
-F ${WORKDIR}/spec -N ${TARGETROOTDIR}/etc \
-o bsize=${BLOCKSIZE},fsize=${FRAGSIZE},density=${DENSITY} \
${WORKDIR}/rootfs ${TARGETROOTDIR} \
|| err ${TOOL_MAKEFS}
if [ ${PRIMARY_BOOT}x != "x" ]; then
echo Installing bootstrap...
${TOOL_INSTALLBOOT} ${INSTALLBOOT_OPT} -m ${MACHINE} ${WORKDIR}/rootfs \
${TARGETROOTDIR}/usr/mdec/${PRIMARY_BOOT} ${SECONDARY_BOOT_ARG} \
|| err ${TOOL_INSTALLBOOT}
fi
echo Copying target disk image...
${CP} ${WORKDIR}/rootfs ${IMAGE} \
|| err ${CP}
echo Creating disklabel...
${CAT} > ${WORKDIR}/labelproto <<EOF
type: ESDI
disk: ${DISKNAME}
label:
flags:
bytes/sector: 512
sectors/track: ${SECTORS}
tracks/cylinder: ${HEADS}
sectors/cylinder: $((${HEADS} * ${SECTORS}))
cylinders: ${CYLINDERS}
total sectors: ${IMAGESECTORS}
rpm: 3600
interleave: 1
trackskew: 0
cylinderskew: 0
headswitch: 0 # microseconds
track-to-track seek: 0 # microseconds
drivedata: 0
8 partitions:
# size offset fstype [fsize bsize cpg/sgs]
a: ${FSSECTORS} ${FSOFFSET} 4.2BSD ${FRAGSIZE} ${BLOCKSIZE} 128
c: ${BSDPARTSECTORS} ${FSOFFSET} unused 0 0
d: ${IMAGESECTORS} 0 unused 0 0
EOF
${TOOL_DISKLABEL} -R -F -M ${MACHINE} ${IMAGE} ${WORKDIR}/labelproto \
|| err ${TOOL_DISKLABEL}
echo Creating image \"${IMAGE}\" complete.