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