nrelease: Split "pkgs" target out from "customizeiso" target
[dragonfly.git] / nrelease / Makefile
1 #########################################################################
2 #                               ENHANCEMENTS                            #
3 #########################################################################
4
5 # Optional targets may be specified which set make variables which enhance
6 # the build in various ways.
7 #
8 gui nopkgs:
9
10 #########################################################################
11 #                               SETUP                                   #
12 #########################################################################
13
14 DPORTS_PATH?=   /usr/dports
15 ISODIR?=        /usr/obj/release
16 ISOROOT?=       ${ISODIR}/root
17 GITURL_SRC?=    git://git.dragonflybsd.org/dragonfly.git
18 GITURL_DPORTS?= git://mirror-master.dragonflybsd.org/dports.git
19 MAKE_JOBS?=     $$(sysctl -n hw.ncpu)
20 .if !defined(NOFSCHG)
21 MTREE_FSCHG=    -i
22 .endif
23
24 GITREV!=        sh ${.CURDIR}/../tools/gitrev.sh
25
26 # The label/name of the disklabel64(5) slice in the ".img" file.  This is
27 # used to identify the USB device with DragonFly installation image, avoiding
28 # hardcoding the USB device name (e.g., "da8").
29 #
30 # This label is also used as the volume ID of the ".iso" file.
31 #
32 LABEL?=         DragonFly_${GITREV}
33
34 CHROOT_CMD?=    /usr/sbin/chroot ${ISOROOT} sh -c
35
36 ISOFILE?=       ${ISODIR}/dfly.iso
37 IMGFILE?=       ${ISODIR}/dfly.img
38 IMGMNT?=        ${ISODIR}/mnt
39
40 .if !make(nopkgs)
41 # User may specify extra packages in addition to the defaults
42 #
43 DPORTS_EXTRA_PACKAGES?=
44
45 # dports packages to be built and installed on the release ISO
46 #
47 DPORTS_PACKAGES?=       security/ca_root_nss \
48                         devel/git-lite \
49                         dns/bind-tools \
50                         net/isc-dhcp44-server \
51                         sysutils/cdrtools \
52                         security/wpa_supplicant \
53                         ${DPORTS_EXTRA_PACKAGES}
54
55 # dports options to use when building packages
56 #
57 DPORTS_OPTIONS+=        -DBATCH
58 DPORTS_OPTIONS+=        dns_bind-tools_UNSET=PYTHON
59 .endif
60
61 # Specify which root skeletons are required, and let the user include
62 # their own.  They are copied into ISODIR during the `customizeiso'
63 # target; each overwrites the last.
64 #
65 REQ_ROOTSKELS=  ${.CURDIR}/root
66 ROOTSKELS?=     ${REQ_ROOTSKELS}
67
68 .if make(gui)
69 ISOFILE?=               ${ISODIR}/dfly-gui.iso
70 IMGFILE?=               ${ISODIR}/dfly-gui.img
71
72 .if !make(nopkgs)
73 # NOTE: order important, do not sort package list
74 #
75 DPORTS_PACKAGES+=       x11/xorg \
76                         x11-wm/fluxbox \
77                         x11-wm/fvwm \
78                         www/firefox \
79                         graphics/xpdf \
80                         shells/zsh \
81                         editors/emacs \
82                         editors/vim \
83                         irc/irssi \
84                         lang/perl5 \
85                         editors/nano \
86                         shells/bash \
87                         devel/ctags \
88                         archivers/zip \
89                         security/sudo \
90                         www/links \
91                         ftp/wget \
92                         x11-fonts/terminus-font \
93                         net/rsync \
94                         x11-clocks/asclock \
95                         sysutils/screen
96 .endif
97
98 ROOTSKELS+=             ${.CURDIR}/gui
99 .endif
100
101 #########################################################################
102 #                               BASE ISO TARGETS                        #
103 #########################################################################
104
105 release:        check clean buildworld1 buildkernel1 \
106                 buildiso customizeiso pkgs srcs mkiso mkimg
107
108 quickrel:       check clean buildworld2 buildkernel2 \
109                 buildiso customizeiso pkgs srcs mkiso mkimg
110
111 realquickrel:   check clean \
112                 buildiso customizeiso pkgs srcs mkiso mkimg
113
114 restartpkgs:    check pkgs srcs mkiso mkimg
115
116 quick:          quickrel
117
118 realquick:      realquickrel
119
120 #########################################################################
121 #                       CORE SUPPORT TARGETS                            #
122 #########################################################################
123
124 check:
125 .if !exists(/usr/local/bin/mkisofs) && !exists(/usr/pkg/bin/mkisofs)
126         @echo
127         @echo "Your machine does not have cdrtools installed.  You can install it with:"
128         @echo "    pkg install cdrtools"
129 .endif
130         @if [ ! -d ${DPORTS_PATH} ]; then \
131                 echo "${DPORTS_PATH} does not exist."; \
132                 echo ""; \
133                 echo "Please set DPORTS_PATH to the dports tree that shall be used for"; \
134                 echo "package building. The default is /usr/dports. See the Makefile in"; \
135                 echo "/usr if you are unfamiliar with dports."; \
136                 /usr/bin/false; \
137         fi
138 .if !exists(/usr/local/bin/mkisofs) && !exists(/usr/pkg/bin/mkisofs)
139         @/usr/bin/false
140 .endif
141         @echo "Using ${DPORTS_PATH} as the dports tree."
142
143 buildworld1 buildworld2:
144         cd ${.CURDIR}/..; \
145         ${WORLD_CCVER:C/^..*$/WORLD_CCVER=/}${WORLD_CCVER} \
146                 make -j ${MAKE_JOBS} -DWANT_INSTALLER ${.TARGET:C/build(.*)2/quick\1/:C/1//}
147
148 buildkernel1 buildkernel2:
149         cd ${.CURDIR}/..; \
150         ${WORLD_CCVER:C/^..*$/WORLD_CCVER=/}${WORLD_CCVER} \
151                 make -j ${MAKE_JOBS} ${.TARGET:C/build(.*)2/quick\1/:C/1//}
152
153 # Unconditionally clean out ${ISOROOT} so a previous img build
154 # does not blow up a future quick iso build
155 #
156 # We install with INSTALLSTRIPPEDMODULES to make things fit into the ISO.
157 # We leave the kernel's debug variables intact.
158 #
159 buildiso:
160         -chflags -R noschg ${ISOROOT}
161         rm -rf ${ISOROOT}
162         mkdir -p ${ISOROOT}
163         ( cd ${.CURDIR}/..; make -DWANT_INSTALLER DESTDIR=${ISOROOT} installworld )
164         # Do not mess with any /usr/obj directories not related to
165         # buildworld, buildkernel, or nrelease.
166         ( cd ${.CURDIR}/../etc && \
167                 tmpdir=`mktemp -d -t nrelease` && \
168                 MAKEOBJDIRPREFIX=$${tmpdir} \
169                         make -m ${.CURDIR}/../share/mk \
170                         DESTDIR=${ISOROOT} distribution && \
171                 rm -rf $${tmpdir} )
172         cpdup ${ISOROOT}/etc ${ISOROOT}/etc.hdd
173         ( cd ${.CURDIR}/..; make DESTDIR=${ISOROOT} reinstallkernel )
174         rm -rf ${ISOROOT}/boot/kernel.old
175         ln -sf kernel ${ISOROOT}/boot/kernel/kernel.BOOTP
176         mtree ${MTREE_FSCHG} -deU -f ${.CURDIR}/../etc/mtree/BSD.var.dist \
177                 -p ${ISOROOT}/var
178         dev_mkdb -f ${ISOROOT}/var/run/dev.db ${ISOROOT}/dev
179         ${CHROOT_CMD} "rcrestart ldconfig"
180
181 # The GUI build includes the full system source (~500 MB) and the full
182 # dports tree (~250 MB).  The nominal release build only includes the
183 # kernel source (~30 MB).
184 #
185 srcs:
186         rm -rf ${ISOROOT}/usr/dports
187         rm -f ${ISOROOT}/usr/src-sys.tar.bz2
188 .if !defined(WITHOUT_SRCS)
189 .if make(gui)
190         ( cd ${ISOROOT}/usr && \
191                 make dports-create-shallow GITURL_DPORTS=${GITURL_DPORTS} )
192         ( cd ${ISOROOT}/usr && \
193                 make src-create-shallow GITURL_SRC=${GITURL_SRC} )
194 .else
195         ( cd ${.CURDIR}/.. && \
196                 tar --exclude .git -s '/^\./src/' -cf - \
197                         ./Makefile ./Makefile.inc1 ./sys | \
198                         bzip2 -9 > ${ISOROOT}/usr/src-sys.tar.bz2 )
199 .endif
200 .endif
201
202 # Customize the ISO by copying rootskels in reverse priority order.
203 #
204 customizeiso:
205         # Copy the rootskels.  Allow sources to be owned by someone other
206         # than root (as is common when checked out via git).
207         #
208 .for ROOTSKEL in ${ROOTSKELS}
209         cpdup -X cpignore -o ${ROOTSKEL} ${ISOROOT}
210         @test -O ${.CURDIR} || echo "chowning copied files to root:wheel"
211         @test -O ${.CURDIR} || ((cd ${ROOTSKEL} && find .) | fgrep -v cpignore | (cd ${ISOROOT} && xargs chown root:wheel))
212 .endfor
213         pwd_mkdb -p -d ${ISOROOT}/etc ${ISOROOT}/etc/master.passwd
214 .for UPGRADE_ITEM in Makefile                   \
215                      etc.${MACHINE_ARCH}        \
216                      rc.d/Makefile              \
217                      periodic/Makefile          \
218                      periodic/daily/Makefile    \
219                      periodic/security/Makefile \
220                      periodic/weekly/Makefile   \
221                      periodic/monthly/Makefile
222         cp -R ${.CURDIR}/../etc/${UPGRADE_ITEM} ${ISOROOT}/etc/${UPGRADE_ITEM}
223 .endfor
224
225 # Install packages by building from dports.
226 #
227 pkgs:
228 .if !empty(DPORTS_PACKAGES)
229         cp /etc/resolv.conf ${ISOROOT}/etc
230         -@umount ${ISOROOT}/dev
231         mount_null /dev ${ISOROOT}/dev
232
233         -@umount ${ISOROOT}/usr/distfiles
234         -@umount ${ISOROOT}/usr/dports
235         rm -rf ${ISOROOT}/usr/obj/dports
236
237         mkdir -p ${ISOROOT}/usr/dports
238         mkdir -p ${ISOROOT}/usr/distfiles
239
240         # Mount /usr/dports read-only for safety, else a failed umount
241         # and our rm -rf will do bad things.
242         mount_null -o ro ${DPORTS_PATH} ${ISOROOT}/usr/dports
243
244         # Make sure /usr/distfiles is writable
245         cp /etc/shells ${ISOROOT}/usr/distfiles/.test > /dev/null 2>&1 \
246             || mount_null ${ISODIR}/distfiles ${ISOROOT}/usr/distfiles
247
248 .for PKG in ${DPORTS_PACKAGES}
249         ${CHROOT_CMD} "cd /usr/dports/${PKG} && make ${DPORTS_OPTIONS} install"
250 .endfor
251 .for PKG in ${DPORTS_PACKAGES}
252         ${CHROOT_CMD} "cd /usr/dports/${PKG} && make ${DPORTS_OPTIONS} clean"
253 .endfor
254
255         ${CHROOT_CMD} "yes | pkg autoremove"
256
257         -umount ${ISOROOT}/usr/distfiles
258         umount ${ISOROOT}/usr/dports
259
260         rm -rf ${ISOROOT}/usr/dports
261         rm -rf ${ISOROOT}/usr/distfiles
262         rm -rf ${ISOROOT}/usr/obj/dports
263
264         # Update the locate(8) and whatis(1) databases, allow ISODIR
265         # to be on tmpfs (fails to create locate database then)
266         #
267         -${CHROOT_CMD} /etc/periodic/weekly/310.locate
268         -${CHROOT_CMD} /etc/periodic/weekly/320.whatis
269
270         umount ${ISOROOT}/dev
271         rm -f ${ISOROOT}/etc/resolv.conf
272
273         # Recopy files that dports may have updated in /etc into /etc.hdd
274         #
275         cpdup ${ISOROOT}/etc/shells ${ISOROOT}/etc.hdd/shells
276         cpdup ${ISOROOT}/etc/group ${ISOROOT}/etc.hdd/group
277 .endif
278
279 mkiso:
280         if [ ! -d ${IMGMNT} ]; then mkdir -p ${IMGMNT}; fi
281         -if [ -f ${ISODIR}/vn.which ]; then umount ${IMGMNT};           \
282             vnconfig -u `cat ${ISODIR}/vn.which`; fi
283         newfs_msdos -C 400k -F 12 -L EFI -m 0xf8 ${ISOROOT}/boot/efiboot.img
284         vnconfig -l | grep "not in use" | head -n 1 |                   \
285             cut -f 1 -d: > ${ISODIR}/vn.which
286         vnconfig -e -s labels `cat ${ISODIR}/vn.which` ${ISOROOT}/boot/efiboot.img
287         mount_msdos /dev/`cat ${ISODIR}/vn.which` ${IMGMNT}
288         mkdir -p ${IMGMNT}/EFI/BOOT
289         cp ${ISOROOT}/boot/loader.efi ${IMGMNT}/EFI/BOOT/BOOTX64.EFI
290         umount ${IMGMNT}
291         ( cd ${ISOROOT}; mkisofs -R -J -o ${ISOFILE} \
292             -b boot/cdboot -no-emul-boot \
293             -eltorito-alt-boot -eltorito-platform efi \
294             -eltorito-boot boot/efiboot.img -no-emul-boot \
295             -V ${LABEL:C/(^.{32}).*/\1/} . )  # cut label to <= 32 characters
296         rm -f ${ISOROOT}/boot/efiboot.img
297
298 mkimg:
299         if [ ! -d ${IMGMNT} ]; then mkdir -p ${IMGMNT}; fi
300         -if [ -f ${ISODIR}/vn.which ]; then umount ${IMGMNT};           \
301             vnconfig -u `cat ${ISODIR}/vn.which`; fi
302 .ifdef IMGSIZE
303         @echo "STEP: use an image size of ${IMGSIZE} 512-byte sectors"
304         rm -f ${IMGFILE}
305         sz=`bc -e "((${IMGSIZE}) * 512)" -equit`; truncate -s $${sz} ${IMGFILE}
306 .else
307         @echo "STEP: Determine required image size in 1GB steps"
308         @echo "      Leave ~600MB of unused space"
309         sz=`du -ck ${ISOROOT} | tail -n 1 | cut -f 1`;                  \
310             sz=`bc -e "(($${sz}) * 1.15 + 999999 + 600000) / 1000000" -equit | \
311             cut -f1 -d.`;                                               \
312             sz=`bc -e "(($${sz}) * 953)" -equit | cut -f1 -d.`;         \
313             rm -f ${IMGFILE};                                           \
314             truncate -s $${sz}M ${IMGFILE};
315 .endif
316         @echo "STEP: determine free vn device"
317         vnconfig -l | grep "not in use" | head -n 1 |                   \
318             cut -f 1 -d: > ${ISODIR}/vn.which
319         vnconfig -e -s labels `cat ${ISODIR}/vn.which` ${IMGFILE}
320         @echo "STEP: set up legacy MBR"
321         fdisk -b ${ISOROOT}/boot/mbr -IB `cat ${ISODIR}/vn.which`
322         fdisk -s `cat ${ISODIR}/vn.which` >${ISODIR}/fdisk.dat
323         awk '(NR==1){printf("g c%s h%s s%s\n", $$2, $$4, $$6);}' \
324             ${ISODIR}/fdisk.dat >${ISODIR}/fdisk.conf
325         echo "p 1 239 63 257985" >>${ISODIR}/fdisk.conf
326         awk '($$1=="1:"){printf("p 2 165 258048 %lu\n", $$3 - 258528);}' \
327             ${ISODIR}/fdisk.dat >>${ISODIR}/fdisk.conf
328         echo "a 2" >>${ISODIR}/fdisk.conf
329         fdisk -iv -f ${ISODIR}/fdisk.conf `cat ${ISODIR}/vn.which`
330         rm ${ISODIR}/fdisk.conf ${ISODIR}/fdisk.dat
331         newfs_msdos -F 32 -c 2 -L EFI -m 0xf8 `cat ${ISODIR}/vn.which`s1
332         mount_msdos /dev/`cat ${ISODIR}/vn.which`s1 ${IMGMNT}
333         mkdir -p ${IMGMNT}/EFI/BOOT
334         cp ${ISOROOT}/boot/boot1.efi ${IMGMNT}/EFI/BOOT/BOOTX64.EFI
335         umount ${IMGMNT}
336         @echo "STEP: write standard disklabel"
337         disklabel -w -r `cat ${ISODIR}/vn.which`s2 auto
338         @echo "STEP: read disklabel back"
339         disklabel -r `cat ${ISODIR}/vn.which`s2 > ${IMGFILE}.label
340         @echo "STEP: set disklabel name"
341         echo "label: ${LABEL}" >> ${IMGFILE}.label
342         @echo "STEP: add slice parition"
343         echo "a: * * 4.2BSD" >> ${IMGFILE}.label;
344         @echo "STEP: write modified disklabel back"
345         disklabel -R -r `cat ${ISODIR}/vn.which`s2 ${IMGFILE}.label
346         rm ${IMGFILE}.label
347         disklabel -B -b ${ISOROOT}/boot/boot1_64 -s ${ISOROOT}/boot/boot2_64 \
348             `cat ${ISODIR}/vn.which`s2
349         newfs /dev/`cat ${ISODIR}/vn.which`s2a
350         mount /dev/`cat ${ISODIR}/vn.which`s2a ${IMGMNT}
351         cpdup ${ISOROOT} ${IMGMNT}
352         @echo "STEP: fixup ${IMGMNT}/etc/fstab"
353         echo "/dev/part-by-label/${LABEL}.a / ufs rw,noatime 0 1" > ${IMGMNT}/etc/fstab
354         echo "dummy /tmp tmpfs rw 0 0" >> ${IMGMNT}/etc/fstab
355         echo "dummy /var/tmp tmpfs rw 0 0" >> ${IMGMNT}/etc/fstab
356         echo "dummy /var/run tmpfs rw,-C 0 0" >> ${IMGMNT}/etc/fstab
357         echo "dummy /usr/obj tmpfs rw 0 0" >> ${IMGMNT}/etc/fstab
358         echo "proc /proc procfs rw 0 0" >> ${IMGMNT}/etc/fstab
359         @echo "STEP: fixup ${IMGMNT}/boot/loader.conf"
360         -fgrep -v kernel_options ${IMGMNT}/boot/loader.conf > ${IMGMNT}/boot/loader.conf.new
361         echo 'vfs.root.mountfrom="ufs:part-by-label/${LABEL}.a"' >> ${IMGMNT}/boot/loader.conf.new
362         mv ${IMGMNT}/boot/loader.conf.new ${IMGMNT}/boot/loader.conf
363         @echo "STEP: cleanup"
364         df ${IMGMNT}
365         sync
366         sleep 1
367         umount ${IMGMNT}
368         vnconfig -u `cat ${ISODIR}/vn.which`
369         rm -f ${ISODIR}/vn.which
370         rmdir ${IMGMNT}
371         @echo "STEP: done, image files are in ${ISODIR}"
372
373 clean:
374         sync
375         sleep 1
376         -umount ${ISOROOT}/usr/distfiles > /dev/null 2>&1
377         -umount ${ISOROOT}/usr/dports > /dev/null 2>&1
378         -umount ${ISOROOT}/dev > /dev/null 2>&1
379         -if [ -f ${ISODIR}/vn.which ]; then umount ${IMGMNT};           \
380             vnconfig -u `cat ${ISODIR}/vn.which`; fi
381         if [ -d ${ISOROOT} ]; then chflags -R noschg ${ISOROOT}; fi
382         rm -rf ${ISOROOT}
383         rm -f ${ISODIR}/.didbootstrap ${ISODIR}/vn.which
384
385 realclean:      clean
386         rm -rf ${ISODIR}/packages
387         rm -rf ${ISODIR}/distfiles
388
389 help all:
390         @echo "Targets:"
391         @echo "  release     - full build from scratch"
392         @echo "  quick       - attempt to do an incremental rebuild"
393         @echo "  realquick   - attempt to restart after world & kernel"
394         @echo "  restartpkgs - attempt to restart at the pkg building stage"
395         @echo ""
396         @echo "Optional targets:"
397         @echo "  nopkgs      - do not install any packages"
398         @echo "  gui         - do a GUI release"
399         @echo ""
400         @echo "Variables:"
401         @echo "  DPORTS_EXTRA_PACKAGES: add additional packages"
402         @echo "  GITURL_SRC: override the Git URL to source repository"
403         @echo "  GITURL_DPORTS: override the Git URL to dports repository"
404         @echo "  IMGSIZE: override the size of .img (in 512-byte sectors)"
405         @echo "  MAKE_JOBS: override the default value (sysctl hw.ncpu)"
406         @echo ""
407
408 .PHONY: release quickrel realquickrel
409 .PHONY: quick realquick
410 .PHONY: check buildworld1 buildworld2
411 .PHONY: buildkernel1 buildkernel2 buildiso customizeiso mkiso mkimg
412 .PHONY: clean realclean help all srcs pkgs