]> 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://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 "${CFGROOT}" "$@"
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 btrfs-tools
156   fstype := btrfs
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         cp -au "$${SYSROOT}/." "$${CFGROOT}/"
261
262 _config: ${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
268
269 ${SQUASH}: ${CFGROOT}
270         -rm "$${SQUASH}"
271         mksquashfs "$${CFGROOT}" "$${SQUASH}" -comp xz
272
273 _squash_modules: ${CFGROOT}
274         printf '%s\n' squashfs nbd >>"${CFGROOT}/etc/initramfs-tools/modules"
275
276 _squash: ${SQUASH} _squash_modules
277
278 _isosquash: ${ISOROOT}/live/live.squashfs
279 ${ISOROOT}/live/live.squashfs: ${CFGROOT} ${ISOROOT} .FORCE
280         -rm "$@"
281         { cd "$${CFGROOT}"; printf '%s\n' boot/*; } \
282         | mksquashfs "$${CFGROOT}" "$@" -comp xz -ef /dev/stdin
283
284 ${VOLUME}: .FORCE
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}"
289
290 ${PBOOT}: .FORCE
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}"
294
295 ${PROOT}: .FORCE
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}"
299
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}"
303
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}"
307
308 _sparse: ${PBOOT} ${PROOT}
309
310 ifdef raspi
311 _format_root: ${PROOT}
312         -umount "$${CFGROOT}/boot" "$${CFGROOT}" "${PROOT}"
313         mkfs.${fstype} -f -L FSROOT "$${PROOT}"
314 else
315 _format_root: ${PROOT}
316         -umount "$${CFGROOT}/boot" "$${CFGROOT}" "${PROOT}"
317         mkfs.ext4 -F -L FSROOT "$${PROOT}"
318 endif
319
320 _format_boot: ${PBOOT}
321         -umount "$${CFGROOT}/boot" "${PBOOT}"
322         mkfs.fat -F 32 -n BOOT "$${PBOOT}"
323
324 _format: _format_root _format_boot
325
326 _iso_image: ${ISOROOT}
327         grub-mkrescue -o "$${ISO}" "$${ISOROOT}"
328
329 .PHONY: _syslinux_efi32 _syslinux_efi64 _syslinux_bios
330 .PHONY: _grub_efi32 _grub_efi64
331
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"
339
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"
347
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"
352
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"
356
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"
360
361 ${CFGROOT}/boot/cmdline.txt: ${CFGROOT}
362         printf 'root=/dev/mmcblk0p2 rootfstype=${fstype} fsck.repair=yes rootwait' >"$@"
363
364 _bootloader_raspi: ${CFGROOT} ${CFGROOT}/boot/cmdline.txt
365
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"
379
380 _bootloader_iso: ${CFGROOT} ${ISOROOT}
381         mkdir -p "$${ISOROOT}/boot/grub/"
382         cp -L "$${CFGROOT}/vmlinuz" "$${CFGROOT}/initrd.img" "$${ISOROOT}"
383         printf '%s\n' "set timeout=10" "default=buster" \
384                 "menuentry 'Debian Buster' --id 'buster' {" \
385                 "linux '/vmlinuz' boot=live" "initrd '/initrd.img'" "}" \
386                 >"$${ISOROOT}/boot/grub/grub.cfg"
387         -[ -f "$${ISOROOT}/install/initrd.gz" -a -f "$${ISOROOT}/install/linux" ] && \
388         printf '%s\n' "menuentry 'Install Debian' --id 'install' {" \
389                 "linux '/install/linux'" "initrd '/install/initrd.gz'" "}" \
390                 >>"$${ISOROOT}/boot/grub/grub.cfg"
391
392 _efi_mount: ${PBOOT}
393         -umount "efi/"
394         mkdir -p -m 755 "efi/"
395         mount -t vfat -o loop "$${PBOOT}" "efi/"
396
397 _efi_umount: _efi_mount
398         sync
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         chmod 755 "$${CFGROOT}"
437         mkdir -p -m 755 "$${CFGROOT}/boot"
438         mountpoint "$${CFGROOT}/boot" \
439         || mount -o loop,offset=2M,sizelimit=${BOOT_MB}M \
440                  -t vfat "$${VOLUME}" "$${CFGROOT}/boot"
441
442 _umount_boot: ${CFGROOT} _config
443         sync
444         -umount "$${CFGROOT}/boot"
445
446 _umount_root: _umount_boot _config
447         sync
448         umount "$${CFGROOT}" && rmdir "$${CFGROOT}"
449
450 _umount: _umount_boot _umount_root
451
452 clean:
453         [ "$${SYSROOT#/}" ] && [ "$${CFGROOT#/}" ] || false  # (abort receipe)
454         -umount img_mount/boot
455         -umount img_mount
456         -rmdir img_mount
457         -umount hdd_mount/boot
458         -umount hdd_mount
459         -rmdir hdd_mount
460         if [ -d "cfg_root/" ]; then btrfs sub del "cfg_root/" || rm -r "cfg_root/"; fi
461         if [ -d "iso_root/" ]; then btrfs sub del "iso_root/" || rm -r "iso_root/"; fi
462         if [ -d "sys_root_${release}_${ARCH}/" ]; then btrfs sub del "sys_root_${release}_${ARCH}/" || rm -r "sys_root_${release}_${ARCH}/"; fi
463         -rm part_boot.img
464         -rm part_root.img
465         -rm rpi_boot.img
466         -rm rpi_root.img
467         -[ -f "$${SQUASH}" ] && rm "$${SQUASH}"
468         -[ -f "$${VOLUME}" ] && rm "$${VOLUME}"
469         -[ -f "$${ISO}" ] && rm "$${ISO}"