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