]> git.plutz.net Git - blast/blob - Makefile
make sure umount does not run too early
[blast] / Makefile
1 DEFAULT: _target
2
3 nul_:=
4 comma_:= ,
5 space_:= ${nul_} ${nul_}
6
7 .PHONY: clean .FORCE _target
8 .PHONY: _qemu _bootstrap _install _copy _config 
9 .PHONY: _partition_gpt _partition_raspi _sparse _partcopy _mbr
10 .PHONY: _format _format_root _format_boot
11 .PHONY: _bootloader_raspi _bootloader_pc _bootloader_iso
12 .PHONY: _mount _mount_root _mount_boot _efi_mount
13 .PHONY: _umount _umount_root _umount_boot _efi_umount
14 .PHONY: _squash_modules _squash _isosquash _iso_image
15
16 ARCH := amd64
17 BOOTSTRAP := http://cdn.debian.net/debian
18 PACKAGES := ${PACKAGES} apt busybox-static debian-archive-keyring init systemd
19
20 # Partition Table (2M) + Boot Partition (126M)+ Root Partition + Slack Space (1M) = 2048M
21 BOOT_MB := 62
22 ROOT_MB := 959
23
24 config.mk:
25         cp "config.example" "$@"
26
27 ifndef config
28   include config.mk
29 else
30   include ${config}
31 endif
32
33 ifdef release
34   KEYRING := keys/debian-archive-${release}-stable.gpg
35 else
36   release := stretch
37   KEYRING := keys/debian-archive-stretch-stable.gpg
38 endif
39
40 ifndef SOURCES
41 define SOURCES := 
42 deb     http://cdn.debian.net/debian ${release} main
43 endef
44 endif
45
46 fstype := ext4
47
48 ifdef disk
49   VOLUME := ${disk}
50   PBOOT := ${disk}1
51   PROOT := ${disk}2
52   SYSROOT := hdd_mount/
53   CFGROOT := hdd_mount/
54   
55   _target: _mbr _umount _config _install _partition_gpt
56   
57   _partition_gpt:
58   _format: _partition_gpt
59   _mount: _format
60   _bootstrap: _mount
61   _install: _bootstrap _mount
62   _config: _install _mount
63   _bootloader_pc: _mount
64   _umount: _mount _config _bootloader_pc
65   _mbr: _umount _bootloader_pc
66
67 else ifdef image
68   VOLUME := ${image}
69   PBOOT := part_boot.img
70   PROOT := part_root.img
71   SYSROOT := sys_root_${release}_${ARCH}/
72   CFGROOT := img_mount/
73   
74   _target: _mbr _umount _config _install
75   
76   _bootstrap:
77   _install: _bootstrap
78   _sparse:
79   _format: _sparse
80   _partition_gpt: _sparse
81   _partcopy: _partition_gpt _format
82   _img_mount: _partcopy
83   _copy: _install _img_mount
84   _config: _copy _img_mount
85   _bootloader_pc: _img_mount
86   _umount: _img_mount _config _bootloader_pc
87   _mbr: _partcopy _bootloader_pc
88
89 else ifdef subvol
90   SYSROOT := sys_root_${release}_${ARCH}/
91   CFGROOT := cfg_root/
92
93   _target: _install _config ${subvol}
94   
95   _bootstrap:
96   _install: _bootstrap
97   _copy: _install
98   _config: _copy
99
100   ${subvol}: _config
101         btrfs sub snap "${CFG_ROOT}" "$@"
102
103 else ifdef squash
104   SQUASH := ${squash}
105   SYSROOT := sys_root_${release}_${ARCH}/
106   CFGROOT := cfg_root/
107
108   PACKAGES += squashfs-tools nbd-client
109   
110   _target: _squash _install _config
111   
112   _bootstrap:
113   _install: _bootstrap
114   _copy: _install
115   _config: _copy _squash_modules
116   _squash: _config
117
118 else ifdef iso
119   ISO := ${iso}
120   ISOROOT := iso_root/
121   SYSROOT := sys_root_${release}_${ARCH}/
122   CFGROOT := cfg_root/
123   BOOT_MB := 2
124   PBOOT := ${ISOROOT}/efi.img
125
126   PACKAGES += squashfs-tools
127   
128   _target: _iso_image _install _config
129   
130   _bootstrap:
131   _install: _bootstrap
132   _copy: _install
133   _config: _copy _squash_modules
134   _isosquash: _config
135   _bootloader_iso: _config
136   _iso_image: _bootloader_iso _isosquash # _efi_umount
137
138 else ifdef raspi
139   ARCH := armhf
140
141   VOLUME := ${raspi}
142   PBOOT := rpi_boot.img
143   PROOT := rpi_root.img
144   SYSROOT := sys_root_${release}_${ARCH}/
145   CFGROOT := img_mount/
146   
147   KEYRING := keys/raspbian-archive-keyring.gpg
148   BOOTSTRAP := http://raspbian.raspberrypi.org/raspbian
149
150   define SOURCES :=
151   deb   http://raspbian.raspberrypi.org/raspbian ${release} main non-free firmware rpi
152   deb   http://archive.raspberrypi.org/debian ${release} main
153   endef
154
155   PACKAGES += firmware-brcm80211 libraspberrypi-bin libraspberrypi0 pi-bluetooth raspberrypi-bootloader raspberrypi-kernel initramfs-tools nilfs-tools
156   fstype := nilfs2
157
158   _target: _umount _config
159   
160   _qemu:
161   _bootstrap: _qemu
162   _install: _bootstrap
163   _sparse:
164   _format: _sparse
165   _partition_raspi: _sparse
166   _partcopy: _partition_raspi _format
167   _img_mount: _partcopy
168   _copy: _install _img_mount
169   _config: _copy _img_mount
170   _bootloader_raspi: _img_mount
171   _umount: _copy _config _bootloader_raspi
172
173 else
174   _target:
175         false
176
177 endif
178
179 include modules/default.mk
180 ifdef modules
181   include $(addsuffix .mk, $(addprefix modules/, $(subst ${comma_},${space_},${modules})))
182 endif
183
184 ifeq "${ARCH}" "amd64"
185   PACKAGES += linux-image-amd64
186 else ifeq "${ARCH}" "i386"
187   PACKAGES += linux-image-686
188 endif
189
190 export SOURCES SYSROOT CFGROOT VOLUME PBOOT PROOT SQUASH ARCH KEYRING BOOTSTRAP ISOROOT ISO
191
192 sys_root_${release}_${ARCH}/:
193         btrfs subvol create "$@" || mkdir "$@"
194         chmod 755 "$@"
195
196 cfg_root/: sys_root_${release}_${ARCH}/ _install
197         -btrfs sub del "$@" || rm -r "$@"
198         btrfs sub snap "$<" "$@" || mkdir "$@"
199         chmod 755 "$@"
200
201 ${ISOROOT}:
202         -btrfs sub del "$@" || rm -r "$@"
203         btrfs subvol create "$@" || mkdir "$@"
204         mkdir "$@/live"
205         chmod 755 "$@" "$@/live"
206
207 img_mount/:
208         mkdir -p -m 755 "$@"
209         chmod 755 "$@"
210
211 hdd_mount/:
212         mkdir -p -m 755 "$@"
213
214 _qemu: ${SYSROOT}/usr/bin/qemu-arm-static
215 ${SYSROOT}/usr/bin/qemu-arm-static: /usr/bin/qemu-arm-static ${SYSROOT}
216         mkdir -p -m 755 "$${SYSROOT}"/usr/bin
217         chmod 755 "$${SYSROOT}"/usr
218         cp -p "$<" "$@"
219
220 _bootstrap: ${SYSROOT}/bin/sh
221 ${SYSROOT}/bin/sh: | ${SYSROOT}
222         debootstrap --variant=minbase --arch="$${ARCH}" \
223                 --keyring="$${KEYRING}" \
224                 "${release}" "$${SYSROOT}" "$${BOOTSTRAP}"
225         printf 'APT::Default-Release "%s";' "${release}" >"$${SYSROOT}/etc/apt/apt.conf.d/10release"
226
227
228 .PHONY: _aptkeys _norecommends
229 _aptkeys: ${SYSROOT}/etc/apt/trusted.gpg.d/
230 _norecommends: ${SYSROOT}/etc/apt/apt.conf.d/10norecommends
231
232 ${SYSROOT}/etc/apt/apt.conf.d/10norecommends: ${SYSROOT}
233         mkdir -p -m 755 "$(dir $@)"
234         printf 'APT::%s "false";\n' "Install-Recommends" "Keep-Recommends" "Install-Suggests" "Keep-Suggests" >"$@"
235         chmod 644 "$@"
236
237 ${SYSROOT}/etc/apt/trusted.gpg.d/: ${SYSROOT} _bootstrap
238         mkdir -p -m 755 "$@"
239         cp keys/*.gpg "$@"
240
241 _install: ${SYSROOT} _aptkeys _norecommends
242         printf '%s\n' "$${SOURCES}" |sort -u >"$${SYSROOT}/etc/apt/sources.list"
243         -cp /etc/resolv.conf "$${SYSROOT}/etc/"
244         -chroot "$${SYSROOT}" sh -c 'apt-mark showmanual |xargs apt-mark auto'
245         -chroot "$${SYSROOT}" apt-get update
246         chroot "$${SYSROOT}" ln -sf /bin/true /usr/local/sbin/invoke-rc.d
247         chroot "$${SYSROOT}" ln -sf /bin/true /usr/sbin/udevadm
248         chroot "$${SYSROOT}" apt-get --yes install ${PACKAGES}
249         mkdir -p -m 755 "$${SYSROOT}/var/lib/deborphan/"
250         chroot "$${SYSROOT}" sh -c 'printf "%s\\n" ${PACKAGES} >/var/lib/deborphan/keep'
251         -chroot "$${SYSROOT}" sh -c 'apt-get --yes purge $$(deborphan -an |sed "s;^.* ;;g")'
252         chroot "$${SYSROOT}" apt-get --yes --auto-remove purge
253         -chroot "$${SYSROOT}" apt-get --yes --auto-remove upgrade
254         chroot "$${SYSROOT}" apt-get clean
255         chroot "$${SYSROOT}" rm /usr/local/sbin/invoke-rc.d
256         chroot "$${SYSROOT}" rm /usr/sbin/udevadm
257
258 _copy: ${SYSROOT} ${CFGROOT}
259         sync
260         find "$${SYSROOT}" -lname '' -exec ln -sf /dev/null '{}' \;
261         cp -au "$${SYSROOT}/." "$${CFGROOT}/"
262
263 _config: ${CFGROOT}
264         ln -sf /dev/null "$${CFGROOT}/etc/systemd/network/99-default.link"
265         printf '\nsession       optional        pam_mkhomedir.so umask=0027\n' >>"$${CFGROOT}/etc/pam.d/common-session"
266         printf 'LABEL=FSROOT / ${fstype} auto,nofail,rw 0 0\nLABEL=BOOT /boot vfat auto,nofail,ro 0 0\n' \
267                 >"$${CFGROOT}/etc/fstab"
268         chroot "$${CFGROOT}" update-initramfs -u
269
270 ${SQUASH}: ${CFGROOT}
271         -rm "$${SQUASH}"
272         mksquashfs "$${CFGROOT}" "$${SQUASH}" -comp xz
273
274 _squash_modules: ${CFGROOT}
275         printf '%s\n' squashfs nbd >>"${CFGROOT}/etc/initramfs-tools/modules"
276
277 _squash: ${SQUASH} _squash_modules
278
279 _isosquash: ${ISOROOT}/live/live.squashfs
280 ${ISOROOT}/live/live.squashfs: ${CFGROOT} ${ISOROOT} .FORCE
281         -rm "$@"
282         { cd "$${CFGROOT}"; printf '%s\n' boot/*; } \
283         | mksquashfs "$${CFGROOT}" "$@" -comp xz -ef /dev/stdin
284
285 ${VOLUME}: .FORCE
286         ! printf '%s\n' "$${VOLUME}" |grep -q '^/dev/'  # abort if volume should be real device
287         dd count=0 of="$${VOLUME}"  # truncate
288         dd bs=1M count=0 seek=$$((3 + ${BOOT_MB} + ${ROOT_MB})) of="$${VOLUME}"
289         chmod g+w "$${VOLUME}"
290
291 ${PBOOT}: .FORCE
292         ! printf '%s\n' "$${PBOOT}" |grep -q '^/dev/'  # abort if volume should be real device
293         dd count=0 of="$${PBOOT}"  # truncate
294         dd bs=1M count=0 seek=${BOOT_MB} of="$${PBOOT}"
295
296 ${PROOT}: .FORCE
297         ! printf '%s\n' "$${PROOT}" |grep -q '^/dev/'  # abort if volume should be real device
298         dd count=0 of="$${PROOT}"  # truncate
299         dd bs=1M count=0 seek=${ROOT_MB} of="$${PROOT}"
300
301 _partition_raspi: ${VOLUME}
302         printf 'label: dos\n\n: start=2MiB, size=%iMiB, type=c\n: start=%iMiB, size=%iMiB, type=83\n' \
303                 ${BOOT_MB} "$$((${BOOT_MB} + 2))" ${ROOT_MB} | sfdisk "$${VOLUME}"
304
305 _partition_gpt: ${VOLUME}
306         printf 'label: gpt\n\n: start=2MiB, size=%iMiB, type=1, attrs="LegacyBIOSBootable"\n: size=%iMiB, type=20\n' \
307                 ${BOOT_MB} ${ROOT_MB} | sfdisk "$${VOLUME}"
308
309 _sparse: ${PBOOT} ${PROOT}
310
311 ifdef raspi
312 _format_root: ${PROOT}
313         -umount "$${CFGROOT}/boot" "$${CFGROOT}" "${PROOT}"
314         mkfs.nilfs2 -f -L FSROOT "$${PROOT}"
315 else
316 _format_root: ${PROOT}
317         -umount "$${CFGROOT}/boot" "$${CFGROOT}" "${PROOT}"
318         mkfs.ext4 -F -L FSROOT "$${PROOT}"
319 endif
320
321 _format_boot: ${PBOOT}
322         -umount "$${CFGROOT}/boot" "${PBOOT}"
323         mkfs.fat -F 32 -n BOOT "$${PBOOT}"
324
325 _format: _format_root _format_boot
326
327 _iso_image: ${ISOROOT}
328         grub-mkrescue -o "$${ISO}" "$${ISOROOT}"
329
330 .PHONY: _syslinux_efi32 _syslinux_efi64 _syslinux_bios
331 .PHONY: _grub_efi32 _grub_efi64
332
333 _syslinux_efi32: ${CFGROOT}
334         mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
335         cp /usr/lib/SYSLINUX.EFI/efi32/syslinux.efi "$${CFGROOT}/boot/EFI/BOOT/BOOTIA32.EFI"
336         mkdir -p "$${CFGROOT}/boot/efi32"
337         cp $(addprefix /usr/lib/syslinux/modules/efi32/, ldlinux.e32 menu.c32 libutil.c32) "$${CFGROOT}"/boot/efi32/
338         cp /usr/lib/syslinux/modules/efi32/ldlinux.e32 "$${CFGROOT}/boot/EFI/BOOT/"  # workaround
339         printf 'PATH /efi32\nINCLUDE /bootmenu.cfg\nMENU TITLE Syslinux EFI-32\n' >"$${CFGROOT}/boot/syslia32.cfg"
340
341 _syslinux_efi64: ${CFGROOT}
342         mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
343         cp /usr/lib/SYSLINUX.EFI/efi64/syslinux.efi "$${CFGROOT}/boot/EFI/BOOT/BOOTX64.EFI"
344         mkdir -p "$${CFGROOT}/boot/efi64"
345         cp $(addprefix /usr/lib/syslinux/modules/efi64/, ldlinux.e64 menu.c32 libutil.c32) "$${CFGROOT}"/boot/efi64/
346         cp /usr/lib/syslinux/modules/efi64/ldlinux.e64 "$${CFGROOT}/boot/EFI/BOOT/"  # workaround
347         printf 'PATH /efi64\nINCLUDE /bootmenu.cfg\nMENU TITLE Syslinux EFI-64\n' >"$${CFGROOT}/boot/syslx64.cfg"
348
349 _syslinux_bios: ${CFGROOT}
350         mkdir -p "$${CFGROOT}/boot/bios"
351         cp $(addprefix /usr/lib/syslinux/modules/bios/,  menu.c32 libutil.c32) "$${CFGROOT}"/boot/bios/
352         printf 'PATH bios\nINCLUDE bootmenu.cfg\nMENU TITLE Syslinux BIOS\n' >"$${CFGROOT}/boot/syslinux.cfg"
353
354 _grub_efi32: ${CFGROOT}
355         mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
356         grub-mkimage -p / -O i386-efi   fat part_gpt normal linux all_video >"$${CFGROOT}/boot/EFI/BOOT/BOOTIA32.EFI"
357
358 _grub_efi64: ${CFGROOT}
359         mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
360         grub-mkimage -p / -O x86_64-efi fat part_gpt normal linux all_video >"$${CFGROOT}/boot/EFI/BOOT/BOOTX64.EFI"
361
362 ${CFGROOT}/boot/cmdline.txt: ${CFGROOT}
363         printf 'root=/dev/mmcblk0p2 rootfstype=${fstype} fsck.repair=yes rootwait' >"$@"
364
365 _bootloader_raspi: ${CFGROOT} ${CFGROOT}/boot/cmdline.txt
366
367 _bootloader_pc: ${CFGROOT} _grub_efi32 _grub_efi64 _syslinux_bios #_syslinux_efi32 _syslinux_efi64
368         printf 'UI menu.c32\nDEFAULT stretch\nTIMEOUT 30\n' >"$${CFGROOT}/boot/bootmenu.cfg"
369         printf 'set timeout=3\ndefault=stretch\n\n' >"$${CFGROOT}/boot/grub.cfg"
370         k="$$(printf ' %s' "$${CFGROOT}/boot/"vmlinuz-*)"; k="$${k##*/}"; \
371         i="$$(printf ' %s' "$${CFGROOT}/boot/"initrd.img-*)";  i="$${i##*/}"; \
372         printf '\nLABEL stretch\n MENU LABEL Debian Stretch\n KERNEL /%s\n INITRD /%s\n APPEND root=LABEL=FSROOT\n' \
373                 "$$k" "$$i" >>"$${CFGROOT}/boot/bootmenu.cfg"; \
374         printf '\nLABEL rescue\n MENU LABEL Rescue\n KERNEL /%s\n INITRD /%s\n APPEND root=LABEL=FSROOT init=/bin/sh\n' \
375                 "$$k" "$$i" >>"$${CFGROOT}/boot/bootmenu.cfg"; \
376         printf "menuentry 'Debian Stretch' --id 'stretch' {\n linux '/%s' root=LABEL=FSROOT\n initrd '/%s'\n}\n" \
377                 "$$k" "$$i" >>"$${CFGROOT}/boot/grub.cfg"; \
378         printf "menuentry 'Rescue' --id 'rescue' {\n linux '/%s' root=LABEL=FSROOT init=/bin/sh\n initrd '/%s'\n}\n" \
379                 "$$k" "$$i" >>"$${CFGROOT}/boot/grub.cfg"
380
381 _bootloader_iso: ${CFGROOT} ${ISOROOT}
382         mkdir -p "$${ISOROOT}/boot/grub/"
383         cp -L "$${CFGROOT}/vmlinuz" "$${CFGROOT}/initrd.img" "$${ISOROOT}"
384         printf '%s\n' "set timeout=10" "default=buster" \
385                 "menuentry 'Debian Buster' --id 'buster' {" \
386                 "linux '/vmlinuz' boot=live" "initrd '/initrd.img'" "}" \
387                 >"$${ISOROOT}/boot/grub/grub.cfg"
388         -[ -f "$${ISOROOT}/install/initrd.gz" -a -f "$${ISOROOT}/install/linux" ] && \
389         printf '%s\n' "menuentry 'Install Debian' --id 'install' {" \
390                 "linux '/install/linux'" "initrd '/install/initrd.gz'" "}" \
391                 >>"$${ISOROOT}/boot/grub/grub.cfg"
392
393 _efi_mount: ${PBOOT}
394         -umount "efi/"
395         mkdir -p -m 755 "efi/"
396         mount -t vfat -o loop "$${PBOOT}" "efi/"
397
398 _efi_umount: _efi_mount
399         umount efi/ && rmdir efi/
400
401 .PHONY: _syslinux_mbr _grub_mbr
402
403 _syslinux_mbr: ${VOLUME}
404         syslinux -t 2097152 -i "$${VOLUME}"
405         dd conv=notrunc if=/usr/lib/syslinux/mbr/gptmbr.bin of="$${VOLUME}"
406
407 _grub_mbr: ${VOLUME}
408         # grub-mkimage -p / -O i386-pc fat part_gpt normal linux all_video |dd conv=notrunc of="$${VOLUME}"
409
410 _mbr: _syslinux_mbr # _grub_mbr
411
412 _partcopy: ${VOLUME} ${PROOT} ${PBOOT}
413         dd conv=notrunc,sparse bs=1M seek=2 if="$${PBOOT}" of="$${VOLUME}"
414         dd conv=notrunc,sparse bs=1M seek=$$((2 + ${BOOT_MB})) if="$${PROOT}" of="$${VOLUME}"
415
416 _mount_root: ${PROOT} ${CFGROOT}
417         mountpoint "$${CFGROOT}" \
418         || mount -t ${fstype} "$${PROOT}" "$${CFGROOT}"
419         chmod 755 "$${CFGROOT}"
420
421 _mount_boot: ${PBOOT}
422         mkdir -p -m 755 "$${CFGROOT}/boot"
423         mountpoint "$${CFGROOT}/boot" \
424         || mount -t vfat "$${PBOOT}" "$${CFGROOT}/boot"
425
426 ifdef disk
427   _mount_boot: _mount_root
428 endif
429
430 _mount: _mount_root _mount_boot
431
432 _img_mount: ${CFGROOT}
433         mountpoint "$${CFGROOT}" \
434         || mount -o loop,offset="$$((${BOOT_MB} + 2))"M,sizelimit=${ROOT_MB}M \
435                  -t ${fstype} "$${VOLUME}" "$${CFGROOT}"
436         sync
437         chmod 755 "$${CFGROOT}"
438         mkdir -p -m 755 "$${CFGROOT}/boot"
439         mountpoint "$${CFGROOT}/boot" \
440         || mount -o loop,offset=2M,sizelimit=${BOOT_MB}M \
441                  -t vfat "$${VOLUME}" "$${CFGROOT}/boot"
442
443 _umount_boot: ${CFGROOT} _config
444         -umount "$${CFGROOT}/boot"
445
446 _umount_root: _umount_boot _config
447         umount "$${CFGROOT}" && rmdir "$${CFGROOT}"
448
449 _umount: _umount_boot _umount_root
450
451 clean:
452         [ "$${SYSROOT#/}" ] && [ "$${CFGROOT#/}" ] || false  # (abort receipe)
453         -umount img_mount/boot
454         -umount img_mount
455         -rmdir img_mount
456         -umount hdd_mount/boot
457         -umount hdd_mount
458         -rmdir hdd_mount
459         if [ -d "cfg_root/" ]; then btrfs sub del "cfg_root/" || rm -r "cfg_root/"; fi
460         if [ -d "iso_root/" ]; then btrfs sub del "iso_root/" || rm -r "iso_root/"; fi
461         if [ -d "sys_root_${release}_${ARCH}/" ]; then btrfs sub del "sys_root_${release}_${ARCH}/" || rm -r "sys_root_${release}_${ARCH}/"; fi
462         -rm part_boot.img
463         -rm part_root.img
464         -rm rpi_boot.img
465         -rm rpi_root.img
466         -[ -f "$${SQUASH}" ] && rm "$${SQUASH}"
467         -[ -f "$${VOLUME}" ] && rm "$${VOLUME}"
468         -[ -f "$${ISO}" ] && rm "$${ISO}"