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