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