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