Makefile.bootimage revision 1.2 1 # $NetBSD: Makefile.bootimage,v 1.2 2012/01/22 16:50:00 tsutsui Exp $
2 #
3 # Copyright (c) 2009, 2010, 2011 Izumi Tsutsui. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 #
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25 #
26 # Makefile to create a bootable FS image for USB flash or emulators
27 #
28
29 #
30 # Required variables:
31 # RELEASEDIR
32 # Should be defined in nbmake-${MACHINE}
33 # IMGBASE
34 # Basename of the image
35 #
36 # Optional variables:
37 # BOOTDISK
38 # device name of target bootable disk specified in /etc/fstab
39 # (default: sd0)
40 # USE_MBR
41 # set yes if target disk image requires MBR partition
42 # (defautl: no)
43 # USE_SUNLABEL
44 # set yes if target disk image requires Sun's label
45 # (default: no)
46 # INSTALLBOOT_AFTER_DISKLABEL (untested)
47 # set yes if the target ${MACHINE} requires disklabel
48 # to run installboot(8), like hp300
49 # (default: empty)
50 # IMAGEMB
51 # target image size in MB
52 # (default: 2048)
53 # SWAPMB
54 # swap size in target image in MB
55 # (default: 128)
56 # KERN_SET
57 # kernel set name which should be extracted into image
58 # (default: kern-GENERIC)
59 # SETS
60 # binary sets that should be extracted into image
61 # (default: modules base etc comp games man misc tests text
62 # xbase xcomp xetc xfont xserver)
63 # SETS_DIR
64 # directory path where binary sets are stored
65 # (default: ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets)
66 # IMGFILE_EXTRA
67 # list of additional files to be copied into images,
68 # containing one or more tuples of the form:
69 # FILE TARGETPATH
70 # for installation image etc.
71 # (default: empty)
72 # IMGDIR_EXTRA
73 # list of additional directories to be copied into images,
74 # containing one or more tuples of the form:
75 # DIR TARGETPATH
76 # for installation image etc.
77 # (default: empty)
78 # XXX: currently permittions in IMGDIR_EXTRA are not handled
79 # IMGDIR_EXCLUDE
80 # pax(1) options to exclude files which should not copied
81 # into TARGETPATH in IMGDIR_EXTRA
82 # (default: empty)
83 # FSTAB_IN
84 # template file of /etc/fstab
85 # (default: ${DISTRIBDIR}/common/bootimage/fstab.in)
86 # SPEC_IN
87 # default files of spec file for makefs(8)
88 # (default: ${DISTRIBDIR}/common/bootimage/spec.in)
89 # SPEC_EXTRA
90 # additional files of spec file for makefs(8)
91 # (default: empty)
92 # IMGMAKEFSOPTIONS
93 # options passed to makefs(8) to create root file system
94 # (default: -o bsize=16384,fsize=2048,density=8192)
95 # PRIMARY_BOOT
96 # primary boot loader that should be installed into
97 # the target image via installboot(8)
98 # (default: empty)
99 # SECONDARY_BOOT
100 # secondary bootloader that should be put into the target image
101 # (default: empty)
102 # SECONDARY_BOOT_ARG
103 # extra arguments that should be passed to installboot(8)
104 # to specify the secondary bootloader
105 # (default: empty)
106 # DISKPROTO_IN
107 # template file of disklabel -R
108 # (default: ${DISTRIBDIR}/common/bootimage/diskproto.in
109 # or ${DISTRIBDIR}/common/bootimage/diskproto.mbr.in)
110 # OMIT_SWAPIMG
111 # no need to put swap partition into image (for USB stick)
112 # (default: no)
113 #
114
115 .include <bsd.sys.mk> # for HOST_SH
116 .include <bsd.own.mk> #
117 .include <bsd.endian.mk> # for TARGET_ENDIANNESS
118
119 .include "${NETBSDSRCDIR}/distrib/common/Makefile.distrib"
120
121 .if empty(IMGBASE)
122 .BEGIN:
123 @echo "Error: IMGBASE is not set"
124 @false
125 .endif
126
127 # should be defined elsewhere?
128 CAT?= cat
129 CHMOD?= chmod
130 CP?= cp
131 DD?= dd
132 MKDIR?= mkdir -p
133 RM?= rm
134
135 #
136 # common definitions for image
137 #
138 BOOTDISK?= sd0
139 USE_MBR?= no
140 USE_SUNLABEL?= no
141 INSTALLBOOT_AFTER_DISKLABEL?= no
142
143 #
144 # size parameters for image
145 #
146 IMAGEMB?= 2048 # 2048MB
147 SWAPMB?= 128 # 128MB
148
149 IMAGESECTORS!= expr ${IMAGEMB} \* 1024 \* 1024 / 512
150 SWAPSECTORS!= expr ${SWAPMB} \* 1024 \* 1024 / 512
151
152 .if ${USE_MBR} == "no"
153 LABELSECTORS?= 0
154 .else
155 #LABELSECTORS?= 63 # historical
156 #LABELSECTORS?= 32 # 16KB aligned
157 LABELSECTORS?= 2048 # 1MB aligned for modern flash devices
158 .endif
159
160 FSSECTORS!= expr ${IMAGESECTORS} - ${SWAPSECTORS} - ${LABELSECTORS}
161 FSSIZE!= expr ${FSSECTORS} \* 512
162
163 # parameters for disklabel and MBR
164 HEADS= 64
165 SECTORS= 32
166 CYLINDERS!= expr ${IMAGESECTORS} / \( ${HEADS} \* ${SECTORS} \)
167 SECPERCYLINDERS!= expr ${HEADS} \* ${SECTORS}
168 MBRHEADS= 255
169 MBRSECTORS= 63
170 MBRCYLINDERS!= expr ${IMAGESECTORS} / \( ${MBRHEADS} \* ${MBRSECTORS} \)
171 MBRNETBSD= 169
172
173 BSDPARTSECTORS!= expr ${IMAGESECTORS} - ${LABELSECTORS}
174 FSOFFSET= ${LABELSECTORS}
175 SWAPOFFSET!= expr ${LABELSECTORS} + ${FSSECTORS}
176
177 # parameters for sunlabel
178 FSCYLINDERS!= expr ${FSSECTORS} / \( ${HEADS} \* ${SECTORS} \)
179 SWAPCYLINDERS!= expr ${SWAPSECTORS} / \( ${HEADS} \* ${SECTORS} \)
180
181
182 #
183 # definitions to create root fs
184 #
185 SETS_DEFAULT= modules base etc comp games man misc tests text
186 .if ${MKX11} != "no"
187 SETS_DEFAULT+= xbase xcomp xetc xfont xserver
188 .endif
189
190 KERN_SET?= kern-GENERIC
191 SETS?= ${SETS_DEFAULT}
192 IMG_SETS= ${KERN_SET} ${SETS}
193 SETS_DIR?= ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
194
195 FSTAB_IN?= ${DISTRIBDIR}/common/bootimage/fstab.in
196 SPEC_IN?= ${DISTRIBDIR}/common/bootimage/spec.in
197
198 IMGMAKEFSOPTIONS?= -o bsize=16384,fsize=2048,density=8192
199
200 WORKDIR?= work
201 WORKSPEC?= work.spec
202 WORKFSTAB?= work.fstab
203 WORKRCCONF?= work.rc.conf
204 WORKFS?= work.rootfs
205 TARGETFS?= imgroot.fs
206
207 CLEANFILES+= ${WORKSPEC} ${WORKFSTAB} ${WORKRCCONF} ${WORKFS}
208 CLEANFILES+= ${TARGETFS}
209
210 #
211 # create root file system for the image
212 #
213 ${TARGETFS}: prepare_md_post
214 @if [ ! -d ${RELEASEDIR}/${RELEASEMACHINEDIR} ]; then \
215 echo "Missing ${RELEASEDIR}/${RELEASEMACHINEDIR}, aborting"; \
216 false; \
217 fi;
218 @${MKDIR} ${WORKDIR}
219 .for set in ${IMG_SETS}
220 @if [ ! -f ${SETS_DIR}/${set}.tgz ]; then \
221 echo "Missing ${SETS_DIR}/${set}.tgz, aborting"; \
222 false; \
223 fi
224 @echo Extracting ${set}.tgz ...
225 @(cd ${WORKDIR}; ${TOOL_PAX} -rnz -f ${SETS_DIR}/${set}.tgz .)
226 .endfor
227 .if defined(SECONDARY_BOOT)
228 @echo Copying secondary boot...
229 ${CP} -f ${WORKDIR}/usr/mdec/${SECONDARY_BOOT} ${WORKDIR}
230 .endif
231 @echo Preparing /etc/fstab ...
232 ${TOOL_SED} "s/@@BOOTDISK@@/${BOOTDISK}/" < ${FSTAB_IN} > ${WORKFSTAB}
233 ${CP} ${WORKFSTAB} ${WORKDIR}/etc/fstab
234 @echo Setting rc_configured=YES in /etc/rc.conf ...
235 ${TOOL_SED} "s/rc_configured=NO/rc_configured=YES/" \
236 < ${WORKDIR}/etc/rc.conf > ${WORKRCCONF}
237 ${CP} ${WORKRCCONF} ${WORKDIR}/etc/rc.conf
238 .if defined(IMGDIR_EXTRA)
239 @echo Copying extra dirs...
240 .for _SRCDIR _TARGET in ${IMGDIR_EXTRA}
241 @if [ ! -d ${_SRCDIR} ]; then \
242 echo "${_SRCDIR} is not directory, aborting"; \
243 false; \
244 fi
245 ${MKDIR} ${WORKDIR}/${_TARGET}
246 (cd ${_SRCDIR} ; \
247 ${TOOL_PAX} -rw -pe -v \
248 ${IMGDIR_EXCLUDE} \
249 . ${.OBJDIR}/${WORKDIR}/${_TARGET} )
250 .endfor
251 .endif
252 .if defined(IMGFILE_EXTRA)
253 @echo Copying extra files...
254 .for _SRC _TARGET in ${IMGFILE_EXTRA}
255 @if [ ! -f ${_SRC} ]; then \
256 echo "${_SRC} in IMGFILE_EXTRA not found, aborting"; \
257 false; \
258 fi
259 @if [ -f ${_SRC} ]; then \
260 echo ${CP} ${_SRC} ${WORKDIR}/${_TARGET}; \
261 ${CP} ${_SRC} ${WORKDIR}/${_TARGET}; \
262 fi
263 .endfor
264 .endif
265 @echo Preparing spec files for makefs...
266 ${RM} -f ${WORKSPEC}
267 cat ${WORKDIR}/etc/mtree/* | \
268 ${TOOL_SED} -e 's/ size=[0-9]*//' > ${WORKSPEC}
269 ${HOST_SH} ${WORKDIR}/dev/MAKEDEV -s all | \
270 ${TOOL_SED} -e '/^\. type=dir/d' -e 's,^\.,./dev,' >> ${WORKSPEC}
271 cat ${SPEC_IN} >> ${WORKSPEC}
272 .if defined(SECONDARY_BOOT)
273 echo "./${SECONDARY_BOOT} type=file uname=root gname=wheel mode=0444" \
274 >> ${WORKSPEC}
275 .endif
276 .if defined(SPEC_EXTRA)
277 cat ${SPEC_EXTRA} >> ${WORKSPEC}
278 .endif
279 @echo Createing rootfs...
280 # XXX /var/spool/ftp/hidden is unreadable
281 ${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden
282 ${TOOL_MAKEFS} -M ${FSSIZE} -m ${FSSIZE} \
283 -B ${TARGET_ENDIANNESS} \
284 -F ${WORKSPEC} -N ${WORKDIR}/etc \
285 ${IMGMAKEFSOPTIONS} \
286 ${WORKFS} ${WORKDIR}
287 .if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} == "no"
288 ${TOOL_INSTALLBOOT} -vm ${MACHINE} ${WORKFS} \
289 ${WORKDIR}/usr/mdec/${PRIMARY_BOOT} ${SECONDARY_BOOT_ARG}
290 .endif
291 @echo done.
292 mv ${WORKFS} ${.TARGET}
293
294 #
295 # definitions to create image
296 #
297 .if ${USE_MBR} != "no"
298 DISKPROTO_IN?= ${DISTRIBDIR}/common/bootimage/diskproto.mbr.in
299 .else
300 DISKPROTO_IN?= ${DISTRIBDIR}/common/bootimage/diskproto.in
301 .endif
302
303 OMIT_SWAPIMG?= no
304
305 WORKMBR?= work.mbr
306 WORKSWAP?= work.swap
307 WORKLABEL?= work.diskproto
308 WORKIMG?= work.img
309
310 CLEANFILES+= ${WORKMBR} ${WORKSWAP}
311 CLEANFILES+= ${WORKLABEL}.tmp ${WORKLABEL}
312 CLEANFILES+= ${WORKIMG} ${IMGBASE}.img
313
314 ${WORKLABEL}:
315 ${TOOL_SED} \
316 -e "s/@@SECTORS@@/${SECTORS}/" \
317 -e "s/@@HEADS@@/${HEADS}/" \
318 -e "s/@@SECPERCYLINDERS@@/${SECPERCYLINDERS}/" \
319 -e "s/@@CYLINDERS@@/${CYLINDERS}/" \
320 -e "s/@@IMAGESECTORS@@/${IMAGESECTORS}/" \
321 -e "s/@@FSSECTORS@@/${FSSECTORS}/" \
322 -e "s/@@FSOFFSET@@/${FSOFFSET}/" \
323 -e "s/@@SWAPSECTORS@@/${SWAPSECTORS}/" \
324 -e "s/@@SWAPOFFSET@@/${SWAPOFFSET}/" \
325 -e "s/@@BSDPARTSECTORS@@/${BSDPARTSECTORS}/" \
326 < ${DISKPROTO_IN} > ${WORKLABEL}.tmp
327 mv ${WORKLABEL}.tmp ${WORKLABEL}
328
329 ${IMGBASE}.img: ${TARGETFS} ${WORKLABEL}
330 .if ${LABELSECTORS} != "0"
331 @echo creating MBR labels...
332 ${DD} if=/dev/zero of=${WORKMBR} seek=$$((${IMAGESECTORS} - 1)) count=1
333 ${TOOL_FDISK} -f -u \
334 -b ${MBRCYLINDERS}/${MBRHEADS}/${MBRSECTORS} \
335 -0 -a -s ${MBRNETBSD}/${FSOFFSET}/${BSDPARTSECTORS} \
336 -i -c ${WORKDIR}/usr/mdec/mbr \
337 -F ${WORKMBR}
338 ${DD} if=${WORKMBR} count=${LABELSECTORS} | \
339 ${CAT} - ${TARGETFS} > ${WORKIMG}
340 .else
341 ${CP} ${TARGETFS} ${WORKIMG}
342 .endif
343 .if ${OMIT_SWAPIMG} == "no"
344 ${DD} if=/dev/zero of=${WORKSWAP} seek=$$((${SWAPSECTORS} - 1)) count=1
345 ${CAT} ${WORKSWAP} >> ${WORKIMG}
346 .endif
347 .if ${USE_SUNLABEL} != "no"
348 @echo Creating sun disklabel...
349 printf 'V ncyl %d\nV nhead %d\nV nsect %d\na %d %d/0/0\nb %d %d/0/0\nW\n' \
350 ${CYLINDERS} ${HEADS} ${SECTORS} \
351 ${FSOFFSET} ${FSCYLINDERS} ${FSCYLINDERS} ${SWAPCYLINDERS} | \
352 ${TOOL_SUNLABEL} -nq ${WORKIMG}
353 .endif
354 ${TOOL_DISKLABEL} -R -F ${WORKIMG} ${WORKLABEL}
355 .if !empty(PRIMARY_BOOT) && ${INSTALLBOOT_AFTER_DISKLABEL} != "no"
356 ${TOOL_INSTALLBOOT} -vm ${MACHINE} ${WORKIMG} \
357 ${WORKDIR}/usr/mdec/${PRIMARY_BOOT}
358 .endif
359 mv ${WORKIMG} ${.TARGET}
360
361
362 CLEANFILES+= ${IMGBASE}.img.gz ${IMGBASE}.img.gz.tmp
363
364 ${IMGBASE}.img.gz: ${IMGBASE}.img
365 ${TOOL_GZIP} -9c ${IMGBASE}.img > ${.TARGET}.tmp
366 mv ${.TARGET}.tmp ${.TARGET}
367
368 clean:
369 @if [ -d ${WORKDIR}/var/spool/ftp/hidden ]; then \
370 ${CHMOD} +r ${WORKDIR}/var/spool/ftp/hidden; \
371 fi # XXX
372 ${RM} -fr ${WORKDIR}
373
374 prepare_md_post: .PHONY
375 image_md_post: .PHONY
376 image_md_pre: .PHONY
377
378 .include <bsd.prog.mk>
379