]> git.plutz.net Git - rpi_small/blob - Makefile
allow hooking of additional receipes
[rpi_small] / Makefile
1 #  Generator for Oktopus boot media
2 #  Copyright (C) 2018 Hochschule für Technik und Wirtschaft Berlin
3 #                written by Paul Hänsch <oktopus@plutz.net>
4 #
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU Affero General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU Affero General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Affero General Public License
16 #  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18 .FORCE:
19
20 BOOTSTRAP = http://raspbian.raspberrypi.org/raspbian
21
22 define SOURCES = 
23 deb     http://raspbian.raspberrypi.org/raspbian stretch main non-free firmware rpi\n\
24 deb     http://archive.raspberrypi.org/debian stretch main\n
25 endef
26
27 PACKAGES := apt bluez bluez-firmware bluez-tools btrfs-tools busybox-static bzip2 ca-certificates cron debian-archive-keyring deborphan firmware-brcm80211 firmware-linux-free firmware-misc-nonfree gzip htop ifupdown init iputils-ping irqbalance isc-dhcp-client less libraspberrypi-bin libraspberrypi0 make net-tools nmap ntpdate openssh-client openssh-server pi-bluetooth raspberrypi-bootloader raspberrypi-kernel rsync ssh sshfs sudo systemd traceroute unzip vim wget wireless-tools wpasupplicant xz-utils zip
28
29 # Do not change, only override in config.mk
30 WIFI-SSID = 
31 WIFI-PASS = 
32
33 config.mk: config.example
34         cp -n "$<" "$@"
35
36 .PHONY: imgfile imgmount root_copy norecommends apt_keys
37
38 IMGFILE = raspi.img
39
40 include config.mk
41 include *.mk
42
43 raspi_root:
44         btrfs subvolume create "$@" || mkdir "$@"
45         mkdir -p "$@/usr/bin"
46         chmod 755 -R "$@/"
47         cp -p "/usr/bin/qemu-arm-static" "$@/usr/bin/"
48         debootstrap --keyring=./raspbian-archive-keyring.gpg \
49                 --arch=armhf --variant=minbase \
50                 stretch "$@/" "${BOOTSTRAP}"
51
52 norecommends: raspi_root/etc/apt/apt.conf.d/10norecommends
53 raspi_root/etc/apt/apt.conf.d/10norecommends: raspi_root
54         mkdir -p "$</etc/apt/apt.conf.d/"
55         printf 'APT::Install-Recommends "false";\n' >"$@"
56         chmod 644 "$@"
57
58 apt_keys:
59         -chroot "$@" apt-key add - <./raspbian-archive-keyring.gpg
60         -chroot "$@" apt-key add - <./raspberrypi-archive-keyring.gpg
61         -chroot "$@" apt-key add - <./debian-archive-stretch-stable.gpg
62
63 raspi_root/: raspi_root norecommends apt_keys .FORCE
64         printf '${SOURCES}' >$@/etc/apt/sources.list
65         -cp /etc/resolv.conf "$@etc/"
66         -chroot "$@" sh -c 'apt-mark showmanual |xargs apt-mark auto'
67         -chroot "$@" apt-get update
68         chroot "$@" ln -sf /bin/true /usr/local/sbin/invoke-rc.d
69         chroot "$@" apt-get --yes install ${PACKAGES}
70         chroot "$@" apt-get --yes --auto-remove purge
71         chroot "$@" apt-get --yes --auto-remove upgrade
72         chroot "$@" apt-get clean
73         chroot "$@" rm /usr/local/sbin/invoke-rc.d
74         touch "$@"
75
76 id_rsa.pub:
77         ssh-keygen -b 2048 -t rsa -N '' -f id_rsa
78
79 files/etc/network/interfaces.d/wifi: wifi.tmpl
80         sed 's;#WIFI-SSID#;${WIFI-SSID};; s;#WIFI-PASS#;${WIFI-PASS};;' <'$<' >'$@'
81         chmod 644 '$@'
82
83 files/root/.ssh/authorized_keys: id_rsa.pub
84         mkdir -p files/root/.ssh/
85         cat '$<' >>'$@'
86         chmod 700 files/root/ files/root/.ssh/
87         chmod 600 '$@'
88
89 imgfile: raspi_root/  # do not set up image file before chroot
90 imgfile: partitions
91         dd bs=1M count=0 seek=1280 of="${IMGFILE}"  # set up sparse file
92         sfdisk "${IMGFILE}" <partitions
93
94 imgmount: imgfile
95         -rmdir "$@"
96         mkdir "$@"  # fail receipe if dir is nonempty
97         lo=$$(losetup -f); image='${IMGFILE}'; \
98         start=$$(sfdisk --dump "$$image" |sed -rn 's;^.*start= *([0-9]+),.*type=83;\1;p'); \
99         size=$$(sfdisk --dump "$$image" |sed -rn 's;^.*size= *([0-9]+),.*type=83;\1;p'); \
100         losetup -o $$((start * 512)) --sizelimit $$((size * 512)) "$${lo}" "$$image" && \
101         mkfs.ext4 -F "$$lo" && mount -t ext4 "$$lo" "$@/";
102         mkdir "$@/boot"
103         lo=$$(losetup -f); image='${IMGFILE}'; \
104         start=$$(sfdisk --dump "$$image" |sed -rn 's;^.*start= *([0-9]+),.*type=c;\1;p'); \
105         size=$$(sfdisk --dump "$$image" |sed -rn 's;^.*size= *([0-9]+),.*type=c;\1;p'); \
106         losetup -o $$((start * 512)) --sizelimit $$((size * 512)) "$${lo}" "$$image" && \
107         mkfs.fat -F 32 -n boot "$$lo" && mount -t vfat "$$lo" "$@/boot";
108
109 root_copy: files/root/.ssh/authorized_keys files/etc/network/interfaces.d/wifi
110 root_copy: imgmount raspi_root/ files/
111         cp -a "raspi_root/." "files/." "$</"
112
113 ${IMGFILE}: imgmount root_copy
114         umount "$</boot/" "$</"
115         losetup -a |sed -En '/${IMGFILE}/{s;^([^:]+):.*$$;\1;p;q}' |xargs losetup -d
116         losetup -a |sed -En '/${IMGFILE}/{s;^([^:]+):.*$$;\1;p;q}' |xargs losetup -d
117         rmdir "$</"