-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·144 lines (129 loc) · 3.65 KB
/
build.sh
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
#!/bin/bash
set -euo pipefail
root_dir="$(readlink -e "$(dirname "$0")")"
# Don't close on error (useful if running as a shortcut)
# trap "read -p 'Press ENTER to continue...'" ERR
# Guard against sudo-happy users
if [ "${EUID}" -eq 0 ]; then
echo "Do not run this script as root!"
exit 1
fi
# Parse flag options
compat=0
debug=0
lto=0
optlevel=
netdebug=0
rebuild=0
ubsan=0
while getopts ":cglO:nru" opt; do
case "${opt}" in
c)
compat=1
;;
g)
debug=1
;;
l)
lto=1
;;
O)
optlevel="${OPTARG}"
;;
n)
netdebug=1
;;
r)
rebuild=1
;;
u)
ubsan=1
;;
*)
echo "Invalid option: -${OPTARG}"
exit 1
;;
esac
done
shift $((OPTIND-1))
# If command is "clean" or -r flag is passed, run make clean and git clean
if [ "${rebuild}" -eq 1 ] || { [ "$#" -gt 0 ] && [ "$1" = "clean" ]; }; then
command -v git >/dev/null && git clean -fx "${root_dir}/filesystem"
make -C "${root_dir}/userspace" clean
make -C "${root_dir}/kernel" clean
rm -f "${root_dir}/filesys_img.new"
rm -f "${root_dir}/disk.img"
if [ "$#" -gt 0 ] && [ "$1" = "clean" ]; then
exit 0
fi
fi
# If command is "debug", start GDB attached to QEMU
if [ "$#" -gt 0 ] && [ "$1" = "debug" ]; then
if [ "$#" -gt 1 ]; then
gdb \
-ex "target remote 127.0.0.1:1234" \
-ex "add-symbol-file '${root_dir}/userspace/build/$2'" \
"${root_dir}/kernel/bootimg"
else
gdb \
-ex "target remote 127.0.0.1:1234" \
"${root_dir}/kernel/bootimg"
fi
exit 0
fi
if [ -n "${optlevel}" ]; then
export CFLAGS="${CFLAGS-} -O${optlevel}"
fi
if [ "${debug}" -eq 1 ]; then
export CFLAGS="${CFLAGS-} -g"
export CPPFLAGS="${CPPFLAGS-} -DDEBUG_PRINT=1"
fi
if [ "${ubsan}" -eq 1 ]; then
export CFLAGS="${CFLAGS-} -fsanitize=undefined"
export CPPFLAGS="${CPPFLAGS-} -DUBSAN_ENABLED=1 -DMYA_POISON=1"
fi
if [ "${lto}" -eq 1 ]; then
export CFLAGS="${CFLAGS-} -flto=auto"
fi
# If compat mode is set, use the original filesystem image,
# otherwise build it from filesystem/ + userspace/
if [ "${compat}" -eq 1 ]; then
cp "${root_dir}/filesys_img" "${root_dir}/filesys_img.new"
else
# Compile userspace programs
make -j "$(nproc)" -C "${root_dir}/userspace"
cp "${root_dir}/userspace/build/"* "${root_dir}/filesystem/"
# Build filesystem image
rm -f "${root_dir}/filesys_img.new"
python3 "${root_dir}/createfs.py" -i "${root_dir}/filesystem" -o "${root_dir}/filesys_img.new"
fi
# Build kernel executable
make -j "$(nproc)" -C "${root_dir}/kernel"
# Generate disk image (mount needs to be run as root)
cp "${root_dir}/orig.img" "${root_dir}/disk.img"
temp_dir="$(mktemp -d)"
trap 'rmdir "${temp_dir}"' EXIT
sudo -s <<EOF
mount -o loop,offset=32256 "${root_dir}/disk.img" "${temp_dir}"
trap 'umount "${temp_dir}"' EXIT
cp -f "${root_dir}/kernel/bootimg" "${temp_dir}/bootimg"
cp -f "${root_dir}/filesys_img.new" "${temp_dir}/filesys_img"
EOF
# If netdebug is set, dump net traffic to /tmp/net.pcap
netfilter=()
if [ "${netdebug}" -eq 1 ]; then
netfilter=("-object" "filter-dump,id=ne2k_filter,netdev=ne2k,file=/tmp/net.pcap")
fi
# If command is "run", boot the VM
if [ "$#" -gt 0 ] && [ "$1" = "run" ]; then
qemu-system-i386 \
-m 256 \
-drive format=raw,file="${root_dir}/disk.img" \
-gdb tcp:127.0.0.1:1234 \
-serial stdio \
-device isa-vga \
-device sb16 \
-device ne2k_isa,netdev=ne2k \
-netdev user,id=ne2k,hostfwd=udp::4321-:4321,hostfwd=tcp::5432-:5432 \
"${netfilter[@]}"
fi