5 space_:=${nul_} ${nul_}
8 .PHONY: _config_raspi _partition_raspi
9 .PHONY: _qemu _bootstrap _install _copy _config_raspi _config _bootloader _squash
10 .PHONY: _target _partition _format _mount _sparse _umount _partcopy _mbr
13 KEYRING := keys/debian-archive-stretch-stable.gpg
14 BOOTSTRAP := http://cdn.debian.net/debian
16 # Partition Table (2M) + Boot Partition (126M)+ Root Partition + Slack Space (1M) = 2048M
21 deb http://cdn.debian.net/debian stretch main
24 PACKAGES := ${PACKAGES} apt btrfs-tools busybox-static bzip2 ca-certificates cron debian-archive-keyring deborphan firmware-linux-free gzip htop ifupdown init iputils-ping irqbalance isc-dhcp-client less make net-tools nmap ntpdate openssh-client psmisc rsync sshfs sudo systemd traceroute unzip vim wget wireless-tools wpasupplicant xz-utils zip
27 cp "config.example" "$@"
38 _target: _mbr _umount _config _install _partition
44 _install: _bootstrap _mount
45 _config: _install _mount
47 _umount: _mount _config _bootloader
48 _mbr: _umount _bootloader
52 PBOOT := part_boot.img
53 PROOT := part_root.img
57 _target: _mbr _umount _partcopy _config _install
64 _copy: _install _mount
67 _umount: _mount _config _bootloader
69 _partcopy: _partition _umount
70 _mbr: _partcopy _bootloader
77 _target: _squash _install _config
93 KEYRING := keys/raspbian-archive-keyring.gpg
94 BOOTSTRAP := http://raspbian.raspberrypi.org/raspbian
97 deb http://raspbian.raspberrypi.org/raspbian stretch main non-free firmware rpi
98 deb http://archive.raspberrypi.org/debian stretch main
100 PACKAGES := ${PACKAGES} firmware-brcm80211 libraspberrypi-bin libraspberrypi0 pi-bluetooth raspberrypi-bootloader raspberrypi-kernel
102 _target: _umount _partcopy _config _install
110 _copy: _install _mount
111 _config_raspi: _copy _mount
112 _config: _copy _mount _config_raspi
113 _umount: _mount _config
115 _partcopy: _partition_raspi _umount
123 include modules/default.mk
125 include $(addsuffix .mk, $(addprefix modules/, $(subst ${comma_},${space},${modules})))
128 ifeq "${ARCH}" "amd64"
129 PACKAGES := ${PACKAGES} linux-image-amd64
130 else ifeq "${ARCH}" "i386"
131 PACKAGES := ${PACKAGES} linux-image-686
134 export SOURCES SYSROOT CFGROOT VOLUME PBOOT PROOT SQUASH ARCH KEYRING BOOTSTRAP
137 btrfs subvol create "$@" || mkdir -p "$@"
140 cfg_root/: sys_root/ _install
141 btrfs sub snap "$<" "$@" || mkdir -p "$@"
145 btrfs subvol create "$@" || mkdir -p "$@"
155 _qemu: ${SYSROOT}/usr/bin/qemu-arm-static
156 ${SYSROOT}/usr/bin/qemu-arm-static: /usr/bin/qemu-arm-static ${SYSROOT}
157 mkdir -p -m 755 "$${SYSROOT}"/usr/bin
158 chmod 755 "$${SYSROOT}"/usr
161 _bootstrap: ${SYSROOT}/bin/sh
162 ${SYSROOT}/bin/sh: | ${SYSROOT}
163 debootstrap --variant=minbase --arch="$${ARCH}" \
164 --keyring="$${KEYRING}" \
165 stretch "$${SYSROOT}" "$${BOOTSTRAP}"
167 .PHONY: _aptkeys _norecommends
168 _aptkeys: ${SYSROOT}/etc/apt/trusted.gpg.d/
169 _norecommends: ${SYSROOT}/etc/apt/apt.conf.d/10norecommends
171 ${SYSROOT}/etc/apt/apt.conf.d/10norecommends: ${SYSROOT}
172 mkdir -p -m 755 "$(dir $@)"
173 printf 'APT::Install-Recommends "false";\n' >"$@"
176 ${SYSROOT}/etc/apt/trusted.gpg.d/: ${SYSROOT}
180 _install: ${SYSROOT} _aptkeys _norecommends
181 printf %s "$${SOURCES}" >"$${SYSROOT}/etc/apt/sources.list"
182 -cp /etc/resolv.conf "$${SYSROOT}/etc/"
183 -chroot "$${SYSROOT}" sh -c 'apt-mark showmanual |xargs apt-mark auto'
184 -chroot "$${SYSROOT}" apt-get update
185 chroot "$${SYSROOT}" ln -sf /bin/true /usr/local/sbin/invoke-rc.d
186 chroot "$${SYSROOT}" apt-get --yes install ${PACKAGES}
187 chroot "$${SYSROOT}" apt-get --yes --auto-remove purge
188 -chroot "$${SYSROOT}" apt-get --yes --auto-remove upgrade
189 chroot "$${SYSROOT}" apt-get clean
190 chroot "$${SYSROOT}" rm /usr/local/sbin/invoke-rc.d
192 _copy: ${SYSROOT} ${CFGROOT}
193 cp -au "$${SYSROOT}/." "$${CFGROOT}/"
195 _config_raspi: ${CFGROOT}/boot/cmdline.txt
196 ${CFGROOT}/boot/cmdline.txt: ${CFGROOT}
197 printf 'dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet' >"$@"
199 .PHONY: _common_config
200 _config: _common_config
201 _common_config: ${CFGROOT} cfg_seed/
202 ln -sf /dev/null "$${CFGROOT}/etc/systemd/network/99-default.link"
203 printf 'LABEL=FSROOT / ext4 auto,nofail,rw 0 0\nLABEL=BOOT /boot vfat auto,nofail,ro 0 0\n' \
204 >"$${CFGROOT}/etc/fstab"
206 ${SQUASH}: ${CFGROOT}
208 mksquashfs "$${CFGROOT}" "$${SQUASH}" -comp xz
213 ! printf '%s\n' "$${VOLUME}" |grep -q '^/dev/' # abort if volume should be real device
214 dd count=0 of="$${VOLUME}" # truncate
215 dd bs=1M count=0 seek=$$((3 + ${BOOT_MB} + ${ROOT_MB})) of="$${VOLUME}"
218 ! printf '%s\n' "$${PBOOT}" |grep -q '^/dev/' # abort if volume should be real device
219 dd count=0 of="$${PBOOT}" # truncate
220 dd bs=1M count=0 seek=${BOOT_MB} of="$${PBOOT}"
223 ! printf '%s\n' "$${PROOT}" |grep -q '^/dev/' # abort if volume should be real device
224 dd count=0 of="$${PROOT}" # truncate
225 dd bs=1M count=0 seek=${ROOT_MB} of="$${PROOT}"
227 _partition_raspi: ${VOLUME}
228 printf 'label: dos\n\n: start=2MiB, size=%iMiB, type=c\n: start=%iMiB, size=%iMiB, type=83\n' \
229 ${BOOT_MB} "$$((${BOOT_MB} + 2))" ${ROOT_MB} | sfdisk "$${VOLUME}"
231 _partition: ${VOLUME}
232 printf 'label: gpt\n\n: start=2MiB, size=%iMiB, type=1, attrs="LegacyBIOSBootable"\n: size=%iMiB, type=20\n' \
233 ${BOOT_MB} ${ROOT_MB} | sfdisk "$${VOLUME}"
235 _sparse: ${PBOOT} ${PROOT}
237 _format: ${PBOOT} ${PROOT}
238 -umount "$${CFGROOT}/boot" "$${CFGROOT}"
239 mkfs.fat -F 32 -n BOOT "$${PBOOT}"
240 mkfs.ext4 -F -L FSROOT "$${PROOT}"
242 .PHONY: _syslinux_efi32 _syslinux_efi64 _syslinux_bios
243 .PHONY: _grub_efi32 _grub_efi64
245 _syslinux_efi32: ${CFGROOT}
246 mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
247 cp /usr/lib/SYSLINUX.EFI/efi32/syslinux.efi "$${CFGROOT}/boot/EFI/BOOT/BOOTIA32.EFI"
248 mkdir -p "$${CFGROOT}/boot/efi32"
249 cp $(addprefix /usr/lib/syslinux/modules/efi32/, ldlinux.e32 menu.c32 libutil.c32) "$${CFGROOT}"/boot/efi32/
250 cp /usr/lib/syslinux/modules/efi32/ldlinux.e32 "$${CFGROOT}/boot/EFI/BOOT/" # workaround
251 printf 'PATH /efi32\nINCLUDE /bootmenu.cfg\nMENU TITLE Syslinux EFI-32\n' >"$${CFGROOT}/boot/syslia32.cfg"
253 _syslinux_efi64: ${CFGROOT}
254 mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
255 cp /usr/lib/SYSLINUX.EFI/efi64/syslinux.efi "$${CFGROOT}/boot/EFI/BOOT/BOOTX64.EFI"
256 mkdir -p "$${CFGROOT}/boot/efi64"
257 cp $(addprefix /usr/lib/syslinux/modules/efi64/, ldlinux.e64 menu.c32 libutil.c32) "$${CFGROOT}"/boot/efi64/
258 cp /usr/lib/syslinux/modules/efi64/ldlinux.e64 "$${CFGROOT}/boot/EFI/BOOT/" # workaround
259 printf 'PATH /efi64\nINCLUDE /bootmenu.cfg\nMENU TITLE Syslinux EFI-64\n' >"$${CFGROOT}/boot/syslx64.cfg"
261 _syslinux_bios: ${CFGROOT}
262 mkdir -p "$${CFGROOT}/boot/bios"
263 cp $(addprefix /usr/lib/syslinux/modules/bios/, menu.c32 libutil.c32) "$${CFGROOT}"/boot/bios/
264 printf 'PATH bios\nINCLUDE bootmenu.cfg\nMENU TITLE Syslinux BIOS\n' >"$${CFGROOT}/boot/syslinux.cfg"
266 _grub_efi32: ${CFGROOT}
267 mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
268 grub-mkimage -p / -O i386-efi fat part_gpt normal linux all_video >"$${CFGROOT}/boot/EFI/BOOT/BOOTIA32.EFI"
270 _grub_efi64: ${CFGROOT}
271 mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
272 grub-mkimage -p / -O x86_64-efi fat part_gpt normal linux all_video >"$${CFGROOT}/boot/EFI/BOOT/BOOTX64.EFI"
274 _bootloader: ${CFGROOT} _grub_efi32 _grub_efi64 _syslinux_bios #_syslinux_efi32 _syslinux_efi64
275 printf 'UI menu.c32\nDEFAULT stretch\nTIMEOUT 30\n' >"$${CFGROOT}/boot/bootmenu.cfg"
276 printf 'set timeout=3\ndefault=stretch\n\n' >"$${CFGROOT}/boot/grub.cfg"
277 k="$$(printf ' %s' "$${CFGROOT}/boot/"vmlinuz-*)"; k="$${k##*/}"; \
278 i="$$(printf ' %s' "$${CFGROOT}/boot/"initrd.img-*)"; i="$${i##*/}"; \
279 printf '\nLABEL stretch\n MENU LABEL Debian Stretch\n KERNEL /%s\n INITRD /%s\n APPEND root=LABEL=FSROOT\n' \
280 "$$k" "$$i" >>"$${CFGROOT}/boot/bootmenu.cfg"; \
281 printf '\nLABEL rescue\n MENU LABEL Rescue\n KERNEL /%s\n INITRD /%s\n APPEND root=LABEL=FSROOT init=/bin/sh\n' \
282 "$$k" "$$i" >>"$${CFGROOT}/boot/bootmenu.cfg"; \
283 printf "menuentry 'Debian Stretch' --id 'stretch' {\n linux '/%s' root=LABEL=FSROOT\n initrd '/%s'\n}\n" \
284 "$$k" "$$i" >>"$${CFGROOT}/boot/grub.cfg"; \
285 printf "menuentry 'Rescue' --id 'rescue' {\n linux '/%s' root=LABEL=FSROOT init=/bin/sh\n initrd '/%s'\n}\n" \
286 "$$k" "$$i" >>"$${CFGROOT}/boot/grub.cfg"
288 .PHONY: _syslinux_mbr _grub_mbr
290 _syslinux_mbr: ${VOLUME}
291 syslinux -t 2097152 -i "$${VOLUME}"
292 dd conv=notrunc if=/usr/lib/syslinux/mbr/gptmbr.bin of="$${VOLUME}"
295 # grub-mkimage -p / -O i386-pc fat part_gpt normal linux all_video |dd conv=notrunc of="$${VOLUME}"
297 _mbr: _syslinux_mbr # _grub_mbr
299 _partcopy: ${VOLUME} ${PROOT} ${PBOOT}
300 dd conv=notrunc,sparse bs=1M seek=2 if="$${PBOOT}" of="$${VOLUME}"
301 dd conv=notrunc,sparse bs=1M seek=$$((2 + ${BOOT_MB})) if="$${PROOT}" of="$${VOLUME}"
303 _mount: ${PBOOT} ${PROOT} ${CFG_ROOT}
304 mount -t ext4 "$${PROOT}" "$${CFGROOT}"
305 chmod 755 "$${CFGROOT}"
306 mkdir -p -m 755 "$${CFGROOT}/boot"
307 mount -t vfat "$${PBOOT}" "$${CFGROOT}/boot"
310 -umount "$${CFGROOT}/boot"
311 umount "$${CFGROOT}" && rmdir "$${CFGROOT}"
314 [ "$${SYSROOT#/}" ] && [ "$${CFGROOT#/}" ] || false # (abort receipe)
315 -umount img_mount/boot
318 -umount hdd_mount/boot
321 if [ -d "sys_root/" ]; then btrfs sub del "sys_root/" || echo rm -r "sys_root/"; fi
322 if [ -d "cfg_root/" ]; then btrfs sub del "cfg_root/" || echo rm -r "cfg_root/"; fi
323 if [ -d "rpi_root/" ]; then btrfs sub del "rpi_root/" || echo rm -r "rpi_root/"; fi
328 -[ -f "$${SQUASH}" ] && rm "$${SQUASH}"
329 -[ -f "$${VOLUME}" ] && rm "$${VOLUME}"