-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMakefile.linux
250 lines (193 loc) · 9.83 KB
/
Makefile.linux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Copyright (c) 2017, Cyberhaven
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
### Check that required variables are setup properly
LINUX_VERSION:=6.8.2
ifeq ("$(wildcard $(S2E_LINUX_KERNELS_ROOT)/linux-$(LINUX_VERSION)/config-i386)","")
$(error $(S2E_LINUX_KERNELS_ROOT)/linux-$(LINUX_VERSION)/config-i386 does not exist. Make sure S2E_LINUX_KERNELS_ROOT is set properly)
endif
### Provisioning the initial image ###
define COMMON_LINUX_IMAGE_TARGETS
$(OUTDIR)/$1: | $(OUTDIR)
$(TMPDIR)/$1: | $(TMPDIR)
mkdir -p "$$@"
$(TMPDIR)/$1/$1.iso: | $(TMPDIR)/$1
$(call INFO_MSG,[$$@] Downloading disk image...)
cd $(TMPDIR)/$1 && $(GET_ISO) $(call ISO_URL,$(1)) $$@
endef
# Debian-specific unattended installation
define TEMPLATE_BASE_DEBIAN_LINUX_IMAGE
$(TMPDIR)/$1/install_files.iso: $(TMPDIR)/$1/$1.iso $(SRC)/Linux/debian-12-bootcd/preseed.cfg $(SRC)/Linux/debian-12-bootcd/isolinux.cfg
$(call INFO_MSG,[$$@] Creating installation disk...)
rm -Rf $(TMPDIR)/$1/install_files && mkdir -p $(TMPDIR)/$1/install_files
7z x $(TMPDIR)/$1/$1.iso -o$(TMPDIR)/$1/install_files
cd $(TMPDIR)/$1 && chmod -R u+rwx install_files
cp "$(SRC)/Linux/debian-12-bootcd/preseed.cfg" "$(TMPDIR)/$1/install_files"
cp "$(SRC)/Linux/debian-12-bootcd/isolinux.cfg" "$(TMPDIR)/$1/install_files/isolinux/"
cp "$(SRC)/Linux/debian-12-bootcd/txt-$(call ARCH,$1).cfg" "$(TMPDIR)/$1/install_files/isolinux/txt.cfg"
cd $(TMPDIR)/$1/install_files && \
md5sum `find -follow -type f` > md5sum.txt
genisoimage -o "$$@" -r -J -no-emul-boot -boot-load-size 4 \
-boot-info-table -b isolinux/isolinux.bin -c isolinux/boot.cat "$(TMPDIR)/$1/install_files"
$(TMPDIR)/$1/image.raw: $(TMPDIR)/$1/install_files.iso
$(call INFO_MSG,[$$@] Creating disk image...)
$(QEMU_IMG) create -f raw $$@ $(call DISK_SIZE,$1)
$(call INFO_MSG,[$$@] Running initial setup...)
# Note: install the image using default KVM CPU because the stock
# kernel may not support the required CPU flags (e.g., pentium,-nommx).
# The custom S2E kernel will work though.
$(QEMU64) -m 4G -no-reboot $(GRAPHICS) $(QEMU_KVM) \
$(call QEMU_HD,0,$$@) \
-cdrom $(TMPDIR)/$1/install_files.iso \
-serial file:$(TMPDIR)/$1/serial.txt
endef
# Ubuntu-specific unattended installation
define TEMPLATE_BASE_UBUNTU_LINUX_IMAGE
$(TMPDIR)/$1/seed.iso: $(SRC)/Linux/ubuntu-preseed/user-data $(SRC)/Linux/ubuntu-preseed/meta-data
$(call INFO_MSG,[$$@] Creating installation disk...)
rm -Rf $(TMPDIR)/$1/install_files && mkdir -p $(TMPDIR)/$1/install_files
cloud-localds "$$@" $(SRC)/Linux/ubuntu-preseed/user-data $(SRC)/Linux/ubuntu-preseed/meta-data
$(TMPDIR)/$1/image.raw: $(TMPDIR)/$1/$1.iso $(TMPDIR)/$1/seed.iso
$(call INFO_MSG,[$$@] Creating disk image...)
$(QEMU_IMG) create -f raw $$@ $(call DISK_SIZE,$1)
rm -rf "$(TMPDIR)/$1/vmlinuz" "$(TMPDIR)/$1/initrd"
7z e "$(TMPDIR)/$1/$1.iso" "-o$(TMPDIR)/$1" vmlinuz initrd -r
$(call INFO_MSG,[$$@] Running initial setup...)
# Note: install the image using default KVM CPU because the stock
# kernel may not support the required CPU flags (e.g., pentium,-nommx).
# The custom S2E kernel will work though.
$(QEMU64) -m 4G -no-reboot $(GRAPHICS) $(QEMU_KVM) \
$(call QEMU_HD,0,$$@) \
$(call QEMU_CD,1,$(TMPDIR)/$1/$1.iso) \
$(call QEMU_CD,2,$(TMPDIR)/$1/seed.iso) \
-serial file:$(TMPDIR)/$1/serial.txt \
-kernel $(TMPDIR)/$1/vmlinuz \
-initrd $(TMPDIR)/$1/initrd \
-append autoinstall
endef
### Installing the kernel and S2E payload ###
define BUILD_S2E_IMAGE
$(OUTDIR)/$2$1/image.raw.s2e: $(TMPDIR)/$1/image.raw $(STAMPS)/$3 \
$(SRC)/Linux/s2e_home/launch.sh $(SRC)/Linux/s2e_home/.bash_login \
$(SRC)/Linux/override.conf
mkdir -p $$(shell dirname $$@)
cp --reflink=auto "$$<" "$$@"
$(call INFO_MSG,[$$@] Installing kernels...)
virt-copy-in -a "$$@" $(TMPDIR)/$3/*.deb /home/s2e
$(call INFO_MSG,[$$@] Installing payload...)
virt-copy-in -a "$$@" $(SRC)/Linux/s2e_home/launch.sh $(SRC)/Linux/s2e_home/.bash_login /home/s2e/
guestfish --rw -a "$$@" -i mkdir /etc/systemd/system/getty@tty1.service.d/
virt-copy-in -a "$$@" $(SRC)/Linux/override.conf /etc/systemd/system/getty@tty1.service.d/
$(call INFO_MSG,[$$@] Booting disk image...)
$(QEMU64) -m 8G -no-reboot $(GRAPHICS) $(QEMU_KVM) \
$(call QEMU_HD,0,$$@) -smp 4 \
-serial file:$(OUTDIR)/$2$1/serial.txt
virt-copy-in -a "$$@" $(S2E_INSTALL_ROOT)/bin/guest-tools32/* /home/s2e/
# This rule extracts all executable files from the guest image into the guestfs folder.
# This is useful for S2E plugins that need access to the binaries in order to extract
# information from them (e.g., import or export tables).
$(OUTDIR)/$2$1/guestfs: $(OUTDIR)/$2$1/image.raw.s2e
rm -Rf $$@ && mkdir -p $$@
$(call INFO_MSG,[$1] Extracting guestfs...)
@# First copy out all the files from the image
@# Unmount any previous folders (e.g., if interrupted)
-fusermount -u $(OUTDIR)/$2$1/imagefs
mkdir -p $(OUTDIR)/$2$1/imagefs
# TODO: rsync only executable files
cd $(OUTDIR)/$2$1 && trap 'fusermount -u imagefs && rmdir imagefs' EXIT && \
guestmount --ro -i --format=raw -a image.raw.s2e imagefs && \
rsync --links -rt --exclude '*.deb' imagefs/* guestfs
# Some files might not have read privileges, add them here
chmod -R u+r $(OUTDIR)/$2$1/guestfs
$(call OK_MSG,[$1] Guestfs extraction completed.)
# Copy the kernel that has debug info into the guestfs
$(OUTDIR)/$2$1/guestfs/vmlinux: $(OUTDIR)/$2$1/guestfs
cp "$(TMPDIR)/$3/"*/vmlinux "$$@"
endef
### Snapshot creation ###
define TAKE_SNAPSHOT
$(OUTDIR)/$2$1/image.raw.s2e.ready: $(OUTDIR)/$2$1/image.raw.s2e
$(call INFO_MSG,[$$@] Creating snapshot...)
LD_PRELOAD=$(call GETLIBS2E,$1) $(call GETQEMU,$1) -enable-kvm \
$(call QEMU_HD_S2E,0,$$<) \
-serial file:$(OUTDIR)/$2$1/serial_ready.txt -enable-serial-commands \
$(call QEMU_NIC,$(call NIC,$2$1)) $(call QEMU_CPU,$(call CPU,$2$1)) \
-m $(call SNAPSHOT_SIZE,$2$1) \
$(GRAPHICS)
$(OUTDIR)/$2$1/image.json: $(OUTDIR)/$2$1/image.raw.s2e.ready $(OUTDIR)/$2$1/guestfs $(OUTDIR)/$2$1/guestfs/vmlinux \
$(SRC)/images.json $(SRC)/scripts/generate_image_descriptor.py
$(call INFO_MSG,[$$@] Creating image descriptor...)
$(SRC)/scripts/generate_image_descriptor.py -i $(SRC)/images.json -o $$@ -n "$2$1" \
snapshot="ready" qemu_build="$(call ARCH,$2$1)" \
memory="$(call SNAPSHOT_SIZE,$2$1)" \
qemu_extra_flags="$(call QEMU_NIC,$(call NIC,$2$1)) $(call QEMU_CPU,$(call CPU,$2$1))"
endef
### Building Linux kernel ###
# This creates the base docker image inside which the kernel will be built
$(STAMPS)/linux-build-%: $(SRC)/Linux/docker/Dockerfile.i386 $(SRC)/Linux/docker/Dockerfile.x86_64
$(call INFO_MSG,[$@] Building docker image...)
mkdir -p $(STAMPS)
cd $(SRC)/Linux/docker && docker build -t linux-build-$(call ARCH,$@) -f Dockerfile.$(call ARCH,$@) .
touch $@
# Build the kernel
define TEMPLATE_LINUX_KERNEL
$(STAMPS)/$1-$2: $(STAMPS)/linux-build-$2 | $(TMPDIR)
$(call INFO_MSG,[$$@] Building kernel...)
rsync -a $(S2E_LINUX_KERNELS_ROOT)/$1 $(TMPDIR)/$1-$2
cd $(TMPDIR)/$1-$2/$1 && mv config-$2 .config
# Remove old builds
rm -rf $(TMPDIR)/$1-$2/*.deb
$(SRC)/Linux/docker/run-docker.sh $(SRC) $(S2E_LINUX_KERNELS_ROOT) linux-build-$2 \
$(TMPDIR)/$1-$2/$1 \
$(SRC)/Linux/docker/make-kernel.sh \
$(S2E_LINUX_KERNELS_ROOT)/include $(shell id -u) $(shell id -g)
touch $$@
endef
### Instantiating all build configurations ###
DEBIAN_IMAGES = debian-12.5-i386 debian-12.5-x86_64
UBUNTU_IMAGES = ubuntu-22.04-x86_64 ubuntu-24.04-x86_64
LINUX_IMAGES = $(DEBIAN_IMAGES) $(UBUNTU_IMAGES)
LINUX_ARCHIVES = $(addsuffix .tar.xz,$(addprefix $(OUTDIR)/,$(LINUX_IMAGES)))
$(foreach os,$(LINUX_IMAGES), \
$(eval $(call COMMON_LINUX_IMAGE_TARGETS,$(os))) \
)
$(foreach os,$(DEBIAN_IMAGES), \
$(eval $(call TEMPLATE_BASE_DEBIAN_LINUX_IMAGE,$(os))) \
$(eval $(call IMAGE_RULE,$(os))) \
)
$(foreach os,$(UBUNTU_IMAGES), \
$(eval $(call TEMPLATE_BASE_UBUNTU_LINUX_IMAGE,$(os))) \
$(eval $(call IMAGE_RULE,$(os))) \
)
# Rules for building the kernels
$(eval $(call TEMPLATE_LINUX_KERNEL,linux-$(LINUX_VERSION),i386))
$(eval $(call TEMPLATE_LINUX_KERNEL,linux-$(LINUX_VERSION),x86_64))
# Rules for building different images
$(eval $(call BUILD_S2E_IMAGE,debian-12.5-i386,,linux-$(LINUX_VERSION)-i386))
$(eval $(call TAKE_SNAPSHOT,debian-12.5-i386,))
$(eval $(call BUILD_S2E_IMAGE,debian-12.5-x86_64,,linux-$(LINUX_VERSION)-x86_64))
$(eval $(call TAKE_SNAPSHOT,debian-12.5-x86_64,))
$(eval $(call BUILD_S2E_IMAGE,ubuntu-22.04-x86_64,,linux-$(LINUX_VERSION)-x86_64))
$(eval $(call TAKE_SNAPSHOT,ubuntu-22.04-x86_64,))
$(eval $(call BUILD_S2E_IMAGE,ubuntu-24.04-x86_64,,linux-$(LINUX_VERSION)-x86_64))
$(eval $(call TAKE_SNAPSHOT,ubuntu-24.04-x86_64,))
### Top level rules ###
linux: $(LINUX_IMAGES)
linux_archives: $(LINUX_ARCHIVES)
all-kernels: $(STAMPS)/linux-$(LINUX_VERSION)-i386 $(STAMPS)/linux-$(LINUX_VERSION)-x86_64