]> git.plutz.net Git - blast/blob - Makefile
partition alignment, efi boot, improved clean target
[blast] / Makefile
1
2 DEFAULT: _target
3 .PHONY: clean .FORCE
4 .PHONY: _qemu _bootstrap _install _copy _config _syslinux _squash
5 .PHONY: _target _partition _format _mount _sparse _umount _partcopy _mbr
6
7 ARCH := amd64
8 KEYRING := keys/debian-archive-stretch-stable.gpg
9 BOOTSTRAP := http://cdn.debian.net/debian
10
11 # Partition Table (2M) + Boot Partition (126M)+ Root Partition + Slack Space (1M) = 2048M
12 BOOT_MB := 126
13 ROOT_MB := 1919
14
15 define SOURCES = 
16 deb     http://cdn.debian.net/debian stretch main
17 endef
18
19 PACKAGES := ${PACKAGES} apt btrfs-tools busybox-static bzip2 ca-certificates cron debian-archive-keyring deborphan firmware-linux-free gzip htop ifupdown init iputils-ping irqbalance isc-dhcp-client less make net-tools nmap ntpdate openssh-client psmisc rsync sshfs sudo systemd traceroute unzip vim wget wireless-tools wpasupplicant xz-utils zip
20
21 ifdef disk
22 VOLUME := ${disk}
23 PBOOT := ${disk}1
24 PROOT := ${disk}2
25 SYSROOT := hdd_mount/
26 CFGROOT := hdd_mount/
27
28 _target: _mbr _umount _config _install _partition
29
30 _partition:
31 _format: _partition
32 _mount: _format
33 _bootstrap: _mount
34 _install: _bootstrap _mount
35 _config: _install _mount
36 _syslinux: _mount
37 _umount: _mount _config _syslinux
38 _mbr: _umount _syslinux
39
40 else ifdef image
41 VOLUME := ${image}
42 PBOOT := part_boot.img
43 PROOT := part_root.img
44 SYSROOT := sys_root/
45 CFGROOT := img_mount/
46
47 _target: _mbr _umount _partcopy _config _install
48
49 _bootstrap:
50 _install: _bootstrap
51 _sparse:
52 _format: _sparse
53 _mount: _format
54 _copy: _install _mount
55 _config: _copy _mount
56 _syslinux: _mount
57 _umount: _mount _config _syslinux
58 _partition:
59 _partcopy: _partition _umount
60 _mbr: _partcopy _syslinux
61
62 else ifdef squash
63 SQUASH := ${squash}
64 SYSROOT := sys_root/
65 CFGROOT := cfg_root/
66
67 _target: _squash _install _config
68
69 _bootstrap:
70 _install: _bootstrap
71 _copy: _install
72 _config: _copy
73 _squash: _config
74
75 else ifdef raspi
76 VOLUME := ${raspi}
77 PBOOT := rpi_boot.img
78 PROOT := rpi_root.img
79 SYSROOT := rpi_root/
80 CFGROOT := img_mount/
81
82 ARCH := armhf
83 KEYRING := keys/raspbian-archive-keyring.gpg
84 BOOTSTRAP := http://raspbian.raspberrypi.org/raspbian
85
86 define SOURCES =
87 deb     http://raspbian.raspberrypi.org/raspbian stretch main non-free firmware rpi
88 deb     http://archive.raspberrypi.org/debian stretch main
89 endef
90 PACKAGES := ${PACKAGES} firmware-brcm8011 libraspberrypi-bin libraspberrypi0 pi-bluetooth raspberrypi-bootloader raspberrypi-kernel
91
92 _target: _umount _partcopy _config _install
93
94 _qemu:
95 _bootstrap: _qemu
96 _install: _bootstrap
97 _sparse:
98 _format: _sparse
99 _mount: _format
100 _copy: _install _mount
101 _config: _copy _mount
102 _umount: _mount _config
103 _partition:
104 _partcopy: _partition _umount
105
106 else
107 _target:
108         false
109
110 endif
111
112 ifeq "${ARCH}" "amd64"
113 PACKAGES := ${PACKAGES} linux-image-amd64
114 else ifeq "${ARCH}" "i386"
115 PACKAGES := ${PACKAGES} linux-image-686
116 endif
117
118 export SOURCES SYSROOT CFGROOT VOLUME PBOOT PROOT SQUASH ARCH KEYRING BOOTSTRAP
119
120 sys_root/:
121         btrfs subvol create "$@" || mkdir -p "$@"
122         chmod 755 "$@"
123
124 cfg_root/: sys_root/ _install
125         btrfs sub snap "$<" "$@" || mkdir -p "$@"
126         chmod 755 "$@"
127
128 img_mount/:
129         mkdir -p "$@"
130         chmod 755 "$@"
131
132 hdd_mount/:
133         mkdir -p "$@"
134         chmod 755 "$@"
135
136 _qemu: ${SYSROOT}/usr/bin/qemu-arm-static
137 ${SYSROOT}/usr/bin/qemu-arm-static: /usr/bin/qemu-arm-static ${SYSROOT}
138         mkdir -p "$${SYSROOT}"/usr/bin
139         chmod 755 "$${SYSROOT}/usr" "$${SYSROOT}/usr/bin"
140         cp -p "$<" "$@"
141
142 _bootstrap: ${SYSROOT}/bin/sh
143 ${SYSROOT}/bin/sh: | ${SYSROOT}
144         debootstrap --variant=minbase --arch="$${ARCH}" \
145                 --keyring="$${KEYRING}" \
146                 stretch "$${SYSROOT}" "$${BOOTSTRAP}"
147
148 .PHONY: _aptkeys _norecommends
149 _aptkeys: ${SYSROOT}/etc/apt/trusted.gpg.d/
150 _norecommends: ${SYSROOT}/etc/apt/apt.conf.d/10norecommends
151
152 ${SYSROOT}/etc/apt/apt.conf.d/10norecommends: ${SYSROOT}
153         mkdir -p "$(dir $@)"
154         printf 'APT::Install-Recommends "false";\n' >"$@"
155         chmod 644 "$@"
156
157 ${SYSROOT}/etc/apt/trusted.gpg.d/: ${SYSROOT}
158         mkdir -p "$@"
159         cp keys/*.gpg "$@"
160
161 _install: ${SYSROOT} _aptkeys _norecommends
162         printf %s "$${SOURCES}" >"$${SYSROOT}/etc/apt/sources.list"
163         -cp /etc/resolv.conf "$${SYSROOT}/etc/"
164         -chroot "$${SYSROOT}" sh -c 'apt-mark showmanual |xargs apt-mark auto'
165         -chroot "$${SYSROOT}" apt-get update
166         chroot "$${SYSROOT}" ln -sf /bin/true /usr/local/sbin/invoke-rc.d
167         chroot "$${SYSROOT}" apt-get --yes install ${PACKAGES}
168         chroot "$${SYSROOT}" apt-get --yes --auto-remove purge
169         -chroot "$${SYSROOT}" apt-get --yes --auto-remove upgrade
170         chroot "$${SYSROOT}" apt-get clean
171         chroot "$${SYSROOT}" rm /usr/local/sbin/invoke-rc.d
172
173 _copy: ${SYSROOT} ${CFGROOT}
174         cp -au "$${SYSROOT}/." "$${CFGROOT}/"
175
176 .PHONY: _main_config
177 _config: _main_config
178 _main_config: ${CFGROOT} cfg_seed/
179         cp -au cfg_seed/. "$${CFGROOT}"
180
181 ${SQUASH}: ${CFGROOT}
182         -rm "$${SQUASH}"
183         mksquashfs "$${CFGROOT}" "$${SQUASH}" -comp xz
184
185 _squash: ${SQUASH}
186
187 ${VOLUME}: .FORCE
188         ! printf '%s\n' "$${VOLUME}" |grep -q '^/dev/'  # abort if volume should be real device
189         dd bs=1M count=0 seek=$$((3 + ${BOOT_MB} + ${ROOT_MB})) of="$${VOLUME}"
190
191 ${PBOOT}: .FORCE
192         ! printf '%s\n' "$${PBOOT}" |grep -q '^/dev/'  # abort if volume should be real device
193         dd bs=1M count=0 seek=${BOOT_MB} of="$${PBOOT}"
194
195 ${PROOT}: .FORCE
196         ! printf '%s\n' "$${PROOT}" |grep -q '^/dev/'  # abort if volume should be real device
197         dd bs=1M count=0 seek=${ROOT_MB} of="$${PROOT}"
198
199 _partition: ${VOLUME}
200         printf 'label: gpt\n\n: start=2MiB, size=%iMiB, type=1, attrs="LegacyBIOSBootable"\n: size=%iMiB, type=20\n' \
201                 ${BOOT_MB} ${ROOT_MB} | sfdisk "$${VOLUME}"
202
203 _sparse: ${PBOOT} ${PROOT}
204
205 _format: ${PBOOT} ${PROOT}
206         -umount "$${CFGROOT}/boot" "$${CFGROOT}"
207         mkfs.fat -F 32 -n BOOT "$${PBOOT}"
208         mkfs.ext4 -F -L FSROOT "$${PROOT}"
209
210 .PHONY: _syslinux_efi32 _syslinux_efi64 _syslinux_bios
211 _syslinux_efi32: ${CFGROOT}
212         mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
213         cp /usr/lib/SYSLINUX.EFI/efi32/syslinux.efi "$${CFGROOT}/boot/EFI/BOOT/BOOTIA32.EFI"
214         mkdir -p "$${CFGROOT}/boot/efi32"
215         cp $(addprefix /usr/lib/syslinux/modules/efi32/, ldlinux.e32 menu.c32 libutil.c32) "$${CFGROOT}"/boot/efi32/
216         cp /usr/lib/syslinux/modules/efi32/ldlinux.e32 "$${CFGROOT}/boot/EFI/BOOT/"  # workaround
217
218 _syslinux_efi64: ${CFGROOT}
219         mkdir -p "$${CFGROOT}/boot/EFI/BOOT"
220         cp /usr/lib/SYSLINUX.EFI/efi64/syslinux.efi "$${CFGROOT}/boot/EFI/BOOT/BOOTX64.EFI"
221         mkdir -p "$${CFGROOT}/boot/efi64"
222         cp $(addprefix /usr/lib/syslinux/modules/efi64/, ldlinux.e64 menu.c32 libutil.c32) "$${CFGROOT}"/boot/efi64/
223         cp /usr/lib/syslinux/modules/efi64/ldlinux.e64 "$${CFGROOT}/boot/EFI/BOOT/"  # workaround
224
225
226 _syslinux_bios: ${CFGROOT}
227         mkdir -p "$${CFGROOT}/boot/bios"
228         cp $(addprefix /usr/lib/syslinux/modules/bios/,  menu.c32 libutil.c32) "$${CFGROOT}"/boot/bios/
229
230 _syslinux: ${CFGROOT} _syslinux_efi32 _syslinux_efi64 _syslinux_bios
231         cp -L "$${CFGROOT}/vmlinuz" "$${CFGROOT}/initrd.img" "$${CFGROOT}/boot/"
232         cp -ru cfg_syslinux/. "$${CFGROOT}/"
233         # cp "$${CFGROOT}/boot/syslx64.cfg" "$${CFGROOT}/boot/EFI/BOOT/syslinux.cfg"  # workaround
234
235 _mbr: ${VOLUME}
236         syslinux -t 2097152 -i "$${VOLUME}"
237         dd conv=notrunc if=/usr/lib/syslinux/mbr/gptmbr.bin of="$${VOLUME}"
238
239 _partcopy: ${VOLUME} ${PROOT} ${PBOOT}
240         dd conv=notrunc,sparse bs=1M seek=2 if="$${PBOOT}" of="$${VOLUME}"
241         dd conv=notrunc,sparse bs=1M seek=$$((2 + ${BOOT_MB})) if="$${PROOT}" of="$${VOLUME}"
242
243 _mount: ${PBOOT} ${PROOT} ${CFG_ROOT}
244         mount -t ext4 "$${PROOT}" "$${CFGROOT}"
245         chmod 755 "$${CFGROOT}"
246         mkdir -p "$${CFGROOT}/boot"
247         chmod 755 "$${CFGROOT}/boot"
248         mount -t vfat "$${PBOOT}" "$${CFGROOT}/boot"
249         chmod 755 "$${CFGROOT}/boot"
250
251 _umount: ${CFGROOT}
252         -umount "$${CFGROOT}/boot"
253         umount "$${CFGROOT}" && rmdir "$${CFGROOT}"
254
255 clean:
256         [ "$${SYSROOT#/}" ] && [ "$${CFGROOT#/}" ] || false  # (abort receipe)
257         -umount img_mount/boot
258         -umount img_mount
259         -rmdir img_mount
260         -umount hdd_mount/boot
261         -umount hdd_mount
262         -rmdir hdd_mount
263         if [ -d "sys_root/" ]; then btrfs sub del "sys_root/" || echo rm -r "sys_root/"; fi
264         if [ -d "cfg_root/" ]; then btrfs sub del "cfg_root/" || echo rm -r "cfg_root/"; fi
265         if [ -d "rpi_root/" ]; then btrfs sub del "rpi_root/" || echo rm -r "rpi_root/"; fi
266         -rm part_boot.img
267         -rm part_root.img
268         -rm rpi_boot.img
269         -rm rpi_root.img
270         -[ -f "$${SQUASH}" ] && rm "$${SQUASH}"
271         -[ -f "$${VOLUME}" ] && rm "$${VOLUME}"