]> git.plutz.net Git - rpi_small/blob - Makefile
5d4e7f08728faa4288156166ea9f1269a2adea05
[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
24 deb     http://archive.raspberrypi.org/debian stretch main
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 psmisc raspberrypi-bootloader raspberrypi-kernel rsync ssh sshfs sudo systemd traceroute unzip vim wget wireless-tools wpasupplicant xz-utils zip
28
29 IMGFILE = raspi.img
30
31 config.mk: config.example
32         cp -n "$<" "$@"
33 include config.mk
34
35 .PHONY: imgfile imgmount root_copy norecommends apt_keys wifi_cfg ssh_key
36
37 export SOURCES
38 export WIFI_CFG
39
40 raspi_root:
41         btrfs subvolume create "$@" || mkdir "$@"
42         mkdir -p "$@/usr/bin"
43         chmod 755 -R "$@/"
44         cp -p "/usr/bin/qemu-arm-static" "$@/usr/bin/"
45         debootstrap --keyring=./raspbian-archive-keyring.gpg \
46                 --arch=armhf --variant=minbase \
47                 stretch "$@/" "${BOOTSTRAP}"
48
49 norecommends: raspi_root/etc/apt/apt.conf.d/10norecommends
50 raspi_root/etc/apt/apt.conf.d/10norecommends: raspi_root
51         mkdir -p "$</etc/apt/apt.conf.d/"
52         printf 'APT::Install-Recommends "false";\n' >"$@"
53         chmod 644 "$@"
54
55 apt_keys: raspi_root
56         -chroot "$<" apt-key add - <./raspbian-archive-keyring.gpg
57         -chroot "$<" apt-key add - <./raspberrypi-archive-keyring.gpg
58         -chroot "$<" apt-key add - <./debian-archive-stretch-stable.gpg
59
60 raspi_root/: raspi_root norecommends apt_keys .FORCE
61         printf %s "$$SOURCES" >$@/etc/apt/sources.list
62         -cp /etc/resolv.conf "$@etc/"
63         -chroot "$@" sh -c 'apt-mark showmanual |xargs apt-mark auto'
64         -chroot "$@" apt-get update
65         chroot "$@" ln -sf /bin/true /usr/local/sbin/invoke-rc.d
66         chroot "$@" apt-get --yes install ${PACKAGES}
67         chroot "$@" apt-get --yes --auto-remove purge
68         -chroot "$@" apt-get --yes --auto-remove upgrade
69         chroot "$@" apt-get clean
70         chroot "$@" rm /usr/local/sbin/invoke-rc.d
71         touch "$@"
72
73 imgfile: raspi_root/  # do not set up image file before chroot
74 imgfile: partitions
75         dd bs=1M count=0 seek=1280 of="${IMGFILE}"  # set up sparse file
76         sfdisk "${IMGFILE}" <partitions
77
78 imgmount: imgfile
79         -rmdir "$@"
80         mkdir "$@"  # fail receipe if dir is nonempty
81         lo=$$(losetup -f); image='${IMGFILE}'; \
82         start=$$(sfdisk --dump "$$image" |sed -rn 's;^.*start= *([0-9]+),.*type=83;\1;p'); \
83         size=$$(sfdisk --dump "$$image" |sed -rn 's;^.*size= *([0-9]+),.*type=83;\1;p'); \
84         losetup -o $$((start * 512)) --sizelimit $$((size * 512)) "$${lo}" "$$image" && \
85         mkfs.ext4 -F "$$lo" && mount -t ext4 "$$lo" "$@/";
86         mkdir "$@/boot"
87         lo=$$(losetup -f); image='${IMGFILE}'; \
88         start=$$(sfdisk --dump "$$image" |sed -rn 's;^.*start= *([0-9]+),.*type=c;\1;p'); \
89         size=$$(sfdisk --dump "$$image" |sed -rn 's;^.*size= *([0-9]+),.*type=c;\1;p'); \
90         losetup -o $$((start * 512)) --sizelimit $$((size * 512)) "$${lo}" "$$image" && \
91         mkfs.fat -F 32 -n boot "$$lo" && mount -t vfat "$$lo" "$@/boot";
92
93 root_copy: imgmount raspi_root/ files/
94         cp -a "raspi_root/." "files/." "$</"
95
96 id_rsa.pub:
97         ssh-keygen -b 2048 -t rsa -N '' -f id_rsa
98
99 ssh_key: imgmount id_rsa.pub
100         mkdir -p "$</root/.ssh/"
101         cat id_rsa.pub >>"$</root/.ssh/authorized_keys"
102         chmod 700 "$</root" "$</root/.ssh"
103         chmod 600 "$</root/.ssh/authorized_keys"
104
105 wifi_cfg: imgmount
106         printf '%s\n' "$$WIFI_CFG" >"$</etc/network/interfaces.d/wifi"
107         chmod 644 "$</etc/network/interfaces.d/wifi"
108         chroot "$<" systemctl disable systemd-networkd-wait-online.service
109
110 ${IMGFILE}: imgmount root_copy wifi_cfg ssh_key
111         umount "$</boot/" "$</"
112         losetup -a |sed -En '/${IMGFILE}/{s;^([^:]+):.*$$;\1;p;q}' |xargs losetup -d
113         losetup -a |sed -En '/${IMGFILE}/{s;^([^:]+):.*$$;\1;p;q}' |xargs losetup -d
114         rmdir "$</"