5 space_:= ${nul_} ${nul_}
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
17 BOOTSTRAP := http://cdn.debian.net/debian
18 PACKAGES := ${PACKAGES} apt busybox-static debian-archive-keyring init systemd
20 # Partition Table (2M) + Boot Partition (126M)+ Root Partition + Slack Space (1M) = 2048M
27 cp "config.example" "$@"
36 KEYRING := keys/debian-archive-${release}-stable.gpg
39 KEYRING := keys/debian-archive-stretch-stable.gpg
44 deb http://cdn.debian.net/debian ${release} main
55 _target: _mbr _umount _config _install _partition_gpt
58 _format: _partition_gpt
61 _install: _bootstrap _mount
62 _config: _install _mount
63 _bootloader_pc: _mount
64 _umount: _mount _config _bootloader_pc
65 _mbr: _umount _bootloader_pc
69 PBOOT := part_boot.img
70 PROOT := part_root.img
71 SYSROOT := sys_root_${release}_${ARCH}/
74 _target: _mbr _umount _config _install
80 _partition_gpt: _sparse
81 _partcopy: _partition_gpt _format
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
90 SYSROOT := sys_root_${release}_${ARCH}/
93 _target: _install _config ${subvol}
101 btrfs sub snap "${CFGROOT}" "$@"
105 SYSROOT := sys_root_${release}_${ARCH}/
108 PACKAGES += squashfs-tools nbd-client
110 _target: _squash _install _config
115 _config: _copy _squash_modules
121 SYSROOT := sys_root_${release}_${ARCH}/
124 PBOOT := ${ISOROOT}/efi.img
126 PACKAGES += squashfs-tools
128 _target: _iso_image _install _config
133 _config: _copy _squash_modules
135 _bootloader_iso: _config
136 _iso_image: _bootloader_iso _isosquash # _efi_umount
142 PBOOT := rpi_boot.img
143 PROOT := rpi_root.img
144 SYSROOT := sys_root_${release}_${ARCH}/
145 CFGROOT := img_mount/
147 KEYRING := keys/raspbian-archive-keyring.gpg
148 BOOTSTRAP := http://raspbian.raspberrypi.org/raspbian
151 deb http://raspbian.raspberrypi.org/raspbian ${release} main non-free firmware rpi
152 deb http://archive.raspberrypi.org/debian ${release} main
155 PACKAGES += firmware-brcm80211 libraspberrypi-bin libraspberrypi0 pi-bluetooth raspberrypi-bootloader raspberrypi-kernel initramfs-tools
157 _target: _umount _config
164 _partition_raspi: _sparse
165 _partcopy: _partition_raspi _format
166 _img_mount: _partcopy
167 _copy: _install _img_mount
168 _config: _copy _img_mount
169 _bootloader_raspi: _img_mount
170 _umount: _copy _config _bootloader_raspi
178 include modules/default.mk
180 include $(addsuffix .mk, $(addprefix modules/, $(subst ${comma_},${space_},${modules})))
183 ifeq "${ARCH}" "amd64"
184 PACKAGES += linux-image-amd64
185 else ifeq "${ARCH}" "i386"
186 PACKAGES += linux-image-686
189 export SOURCES SYSROOT CFGROOT VOLUME PBOOT PROOT SQUASH ARCH KEYRING BOOTSTRAP ISOROOT ISO
191 sys_root_${release}_${ARCH}/:
192 btrfs subvol create "$@" || mkdir "$@"
195 cfg_root/: sys_root_${release}_${ARCH}/ _install
196 -btrfs sub del "$@" || rm -r "$@"
197 btrfs sub snap "$<" "$@" || mkdir "$@"
201 -btrfs sub del "$@" || rm -r "$@"
202 btrfs subvol create "$@" || mkdir "$@"
204 chmod 755 "$@" "$@/live"
213 _qemu: ${SYSROOT}/usr/bin/qemu-arm-static
214 ${SYSROOT}/usr/bin/qemu-arm-static: /usr/bin/qemu-arm-static ${SYSROOT}
215 mkdir -p -m 755 "$${SYSROOT}"/usr/bin
216 chmod 755 "$${SYSROOT}"/usr
219 _bootstrap: ${SYSROOT}/bin/sh
220 ${SYSROOT}/bin/sh: | ${SYSROOT}
221 debootstrap --variant=minbase --arch="$${ARCH}" \
222 --keyring="$${KEYRING}" \
223 "${release}" "$${SYSROOT}" "$${BOOTSTRAP}"
224 printf 'APT::Default-Release "%s";' "${release}" >"$${SYSROOT}/etc/apt/apt.conf.d/10release"
227 .PHONY: _aptkeys _norecommends
228 _aptkeys: ${SYSROOT}/etc/apt/trusted.gpg.d/
229 _norecommends: ${SYSROOT}/etc/apt/apt.conf.d/10norecommends
231 ${SYSROOT}/etc/apt/apt.conf.d/10norecommends: ${SYSROOT}
232 mkdir -p -m 755 "$(dir $@)"
233 printf 'APT::%s "false";\n' "Install-Recommends" "Keep-Recommends" "Install-Suggests" "Keep-Suggests" >"$@"
236 ${SYSROOT}/etc/apt/trusted.gpg.d/: ${SYSROOT} _bootstrap
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
258 _copy: ${SYSROOT} ${CFGROOT}
260 cp -au "$${SYSROOT}/." "$${CFGROOT}/"
263 ln -sf /dev/null "$${CFGROOT}/etc/systemd/network/99-default.link"
264 printf '\nsession optional pam_mkhomedir.so umask=0027\n' >>"$${CFGROOT}/etc/pam.d/common-session"
265 printf 'LABEL=FSROOT / ${fstype} auto,nofail,rw 0 0\nLABEL=BOOT /boot vfat auto,nofail,ro 0 0\n' \
266 >"$${CFGROOT}/etc/fstab"
267 chroot "$${CFGROOT}" update-initramfs -u
269 ${SQUASH}: ${CFGROOT}
271 mksquashfs "$${CFGROOT}" "$${SQUASH}" -comp xz
273 _squash_modules: ${CFGROOT}
274 printf '%s\n' squashfs nbd >>"${CFGROOT}/etc/initramfs-tools/modules"
276 _squash: ${SQUASH} _squash_modules
278 _isosquash: ${ISOROOT}/live/live.squashfs
279 ${ISOROOT}/live/live.squashfs: ${CFGROOT} ${ISOROOT} .FORCE
281 { cd "$${CFGROOT}"; printf '%s\n' boot/*; } \
282 | mksquashfs "$${CFGROOT}" "$@" -comp xz -ef /dev/stdin
285 ! printf '%s\n' "$${VOLUME}" |grep -q '^/dev/' # abort if volume should be real device
286 dd count=0 of="$${VOLUME}" # truncate
287 dd bs=1M count=0 seek=$$((3 + ${BOOT_MB} + ${ROOT_MB})) of="$${VOLUME}"
288 chmod g+w "$${VOLUME}"
291 ! printf '%s\n' "$${PBOOT}" |grep -q '^/dev/' # abort if volume should be real device
292 dd count=0 of="$${PBOOT}" # truncate
293 dd bs=1M count=0 seek=${BOOT_MB} of="$${PBOOT}"
296 ! printf '%s\n' "$${PROOT}" |grep -q '^/dev/' # abort if volume should be real device
297 dd count=0 of="$${PROOT}" # truncate
298 dd bs=1M count=0 seek=${ROOT_MB} of="$${PROOT}"
300 _partition_raspi: ${VOLUME}
301 printf 'label: dos\n\n: start=2MiB, size=%iMiB, type=c\n: start=%iMiB, size=%iMiB, type=83\n' \
302 ${BOOT_MB} "$$((${BOOT_MB} + 2))" ${ROOT_MB} | sfdisk "$${VOLUME}"
304 _partition_gpt: ${VOLUME}
305 printf 'label: gpt\n\n: start=2MiB, size=%iMiB, type=1, attrs="LegacyBIOSBootable"\n: size=%iMiB, type=20\n' \
306 ${BOOT_MB} ${ROOT_MB} | sfdisk "$${VOLUME}"
308 _sparse: ${PBOOT} ${PROOT}
310 ifeq "${fstype}" "ext4"
316 _format_root: ${PROOT}
317 -umount "$${CFGROOT}/boot" "$${CFGROOT}" "${PROOT}"
318 mkfs.${fstype} ${fsflag} -L FSROOT "$${PROOT}"
320 _format_boot: ${PBOOT}
321 -umount "$${CFGROOT}/boot" "${PBOOT}"
322 mkfs.fat -F 32 -n BOOT "$${PBOOT}"
324 _format: _format_root _format_boot
326 _iso_image: ${ISOROOT}
327 grub-mkrescue -o "$${ISO}" "$${ISOROOT}"
329 .PHONY: _syslinux_efi32 _syslinux_efi64 _syslinux_bios
330 .PHONY: _grub_efi32 _grub_efi64
332 _syslinux_efi32: ${CFGROOT}
333 mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
334 cp /usr/lib/SYSLINUX.EFI/efi32/syslinux.efi "$${CFGROOT}/boot/EFI/BOOT/BOOTIA32.EFI"
335 mkdir -p "$${CFGROOT}/boot/efi32"
336 cp $(addprefix /usr/lib/syslinux/modules/efi32/, ldlinux.e32 menu.c32 libutil.c32) "$${CFGROOT}"/boot/efi32/
337 cp /usr/lib/syslinux/modules/efi32/ldlinux.e32 "$${CFGROOT}/boot/EFI/BOOT/" # workaround
338 printf 'PATH /efi32\nINCLUDE /bootmenu.cfg\nMENU TITLE Syslinux EFI-32\n' >"$${CFGROOT}/boot/syslia32.cfg"
340 _syslinux_efi64: ${CFGROOT}
341 mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
342 cp /usr/lib/SYSLINUX.EFI/efi64/syslinux.efi "$${CFGROOT}/boot/EFI/BOOT/BOOTX64.EFI"
343 mkdir -p "$${CFGROOT}/boot/efi64"
344 cp $(addprefix /usr/lib/syslinux/modules/efi64/, ldlinux.e64 menu.c32 libutil.c32) "$${CFGROOT}"/boot/efi64/
345 cp /usr/lib/syslinux/modules/efi64/ldlinux.e64 "$${CFGROOT}/boot/EFI/BOOT/" # workaround
346 printf 'PATH /efi64\nINCLUDE /bootmenu.cfg\nMENU TITLE Syslinux EFI-64\n' >"$${CFGROOT}/boot/syslx64.cfg"
348 _syslinux_bios: ${CFGROOT}
349 mkdir -p "$${CFGROOT}/boot/bios"
350 cp $(addprefix /usr/lib/syslinux/modules/bios/, menu.c32 libutil.c32) "$${CFGROOT}"/boot/bios/
351 printf 'PATH bios\nINCLUDE bootmenu.cfg\nMENU TITLE Syslinux BIOS\n' >"$${CFGROOT}/boot/syslinux.cfg"
353 _grub_efi32: ${CFGROOT}
354 mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
355 grub-mkimage -p / -O i386-efi fat part_gpt normal linux all_video >"$${CFGROOT}/boot/EFI/BOOT/BOOTIA32.EFI"
357 _grub_efi64: ${CFGROOT}
358 mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
359 grub-mkimage -p / -O x86_64-efi fat part_gpt normal linux all_video >"$${CFGROOT}/boot/EFI/BOOT/BOOTX64.EFI"
361 ${CFGROOT}/boot/cmdline.txt: ${CFGROOT}
362 printf 'root=/dev/mmcblk0p2 rootfstype=${fstype} fsck.repair=yes rootwait' >"$@"
364 _bootloader_raspi: ${CFGROOT} ${CFGROOT}/boot/cmdline.txt
366 _bootloader_pc: ${CFGROOT} _grub_efi32 _grub_efi64 _syslinux_bios #_syslinux_efi32 _syslinux_efi64
367 printf 'UI menu.c32\nDEFAULT stretch\nTIMEOUT 30\n' >"$${CFGROOT}/boot/bootmenu.cfg"
368 printf 'set timeout=3\ndefault=stretch\n\n' >"$${CFGROOT}/boot/grub.cfg"
369 k="$$(printf ' %s' "$${CFGROOT}/boot/"vmlinuz-*)"; k="$${k##*/}"; \
370 i="$$(printf ' %s' "$${CFGROOT}/boot/"initrd.img-*)"; i="$${i##*/}"; \
371 printf '\nLABEL stretch\n MENU LABEL Debian Stretch\n KERNEL /%s\n INITRD /%s\n APPEND root=LABEL=FSROOT\n' \
372 "$$k" "$$i" >>"$${CFGROOT}/boot/bootmenu.cfg"; \
373 printf '\nLABEL rescue\n MENU LABEL Rescue\n KERNEL /%s\n INITRD /%s\n APPEND root=LABEL=FSROOT init=/bin/sh\n' \
374 "$$k" "$$i" >>"$${CFGROOT}/boot/bootmenu.cfg"; \
375 printf "menuentry 'Debian Stretch' --id 'stretch' {\n linux '/%s' root=LABEL=FSROOT\n initrd '/%s'\n}\n" \
376 "$$k" "$$i" >>"$${CFGROOT}/boot/grub.cfg"; \
377 printf "menuentry 'Rescue' --id 'rescue' {\n linux '/%s' root=LABEL=FSROOT init=/bin/sh\n initrd '/%s'\n}\n" \
378 "$$k" "$$i" >>"$${CFGROOT}/boot/grub.cfg"
380 _bootloader_iso: ${CFGROOT} ${ISOROOT}
381 mkdir -p "$${ISOROOT}/boot/grub/"
382 cp -L "$${CFGROOT}/vmlinuz" "$${CFGROOT}/initrd.img" "$${ISOROOT}"
383 printf '%s\n' "insmod all_video" "set gfxmode=auto" "load_video" "insmod gfxterm" \
384 >"$${ISOROOT}/boot/grub/grub.cfg"
385 printf '%s\n' "set timeout=10" "default=buster" \
386 "menuentry 'Debian Buster' --id 'buster' {" \
387 "linux '/vmlinuz' boot=live" "initrd '/initrd.img'" "}" \
388 >>"$${ISOROOT}/boot/grub/grub.cfg"
389 -[ -f "$${ISOROOT}/install/initrd.gz" -a -f "$${ISOROOT}/install/linux" ] && \
390 printf '%s\n' "menuentry 'Install Debian' --id 'install' {" \
391 "linux '/install/linux'" "initrd '/install/initrd.gz'" "}" \
392 >>"$${ISOROOT}/boot/grub/grub.cfg"
396 mkdir -p -m 755 "efi/"
397 mount -t vfat -o loop "$${PBOOT}" "efi/"
399 _efi_umount: _efi_mount
401 umount efi/ && rmdir efi/
403 .PHONY: _syslinux_mbr _grub_mbr
405 _syslinux_mbr: ${VOLUME}
406 syslinux -t 2097152 -i "$${VOLUME}"
407 dd conv=notrunc if=/usr/lib/syslinux/mbr/gptmbr.bin of="$${VOLUME}"
410 # grub-mkimage -p / -O i386-pc fat part_gpt normal linux all_video |dd conv=notrunc of="$${VOLUME}"
412 _mbr: _syslinux_mbr # _grub_mbr
414 _partcopy: ${VOLUME} ${PROOT} ${PBOOT}
415 dd conv=notrunc,sparse bs=1M seek=2 if="$${PBOOT}" of="$${VOLUME}"
416 dd conv=notrunc,sparse bs=1M seek=$$((2 + ${BOOT_MB})) if="$${PROOT}" of="$${VOLUME}"
418 _mount_root: ${PROOT} ${CFGROOT}
419 mountpoint "$${CFGROOT}" \
420 || mount -t ${fstype} "$${PROOT}" "$${CFGROOT}"
421 chmod 755 "$${CFGROOT}"
423 _mount_boot: ${PBOOT}
424 mkdir -p -m 755 "$${CFGROOT}/boot"
425 mountpoint "$${CFGROOT}/boot" \
426 || mount -t vfat "$${PBOOT}" "$${CFGROOT}/boot"
429 _mount_boot: _mount_root
432 _mount: _mount_root _mount_boot
434 _img_mount: ${CFGROOT}
435 mountpoint "$${CFGROOT}" \
436 || mount -o loop,offset="$$((${BOOT_MB} + 2))"M,sizelimit=${ROOT_MB}M \
437 -t ${fstype} "$${VOLUME}" "$${CFGROOT}"
438 chmod 755 "$${CFGROOT}"
439 mkdir -p -m 755 "$${CFGROOT}/boot"
440 mountpoint "$${CFGROOT}/boot" \
441 || mount -o loop,offset=2M,sizelimit=${BOOT_MB}M \
442 -t vfat "$${VOLUME}" "$${CFGROOT}/boot"
444 _umount_boot: ${CFGROOT} _config
446 -umount "$${CFGROOT}/boot"
448 _umount_root: _umount_boot _config
450 umount "$${CFGROOT}" && rmdir "$${CFGROOT}"
452 _umount: _umount_boot _umount_root
455 [ "$${SYSROOT#/}" ] && [ "$${CFGROOT#/}" ] || false # (abort receipe)
456 -umount img_mount/boot
459 -umount hdd_mount/boot
462 if [ -d "cfg_root/" ]; then btrfs sub del "cfg_root/" || rm -r "cfg_root/"; fi
463 if [ -d "iso_root/" ]; then btrfs sub del "iso_root/" || rm -r "iso_root/"; fi
464 if [ -d "sys_root_${release}_${ARCH}/" ]; then btrfs sub del "sys_root_${release}_${ARCH}/" || rm -r "sys_root_${release}_${ARCH}/"; fi
469 -[ -f "$${SQUASH}" ] && rm "$${SQUASH}"
470 -[ -f "$${VOLUME}" ] && rm "$${VOLUME}"
471 -[ -f "$${ISO}" ] && rm "$${ISO}"