Skip to content

Commit 1feafb4

Browse files
authored
Create create-image.sh
1 parent 13f02fb commit 1feafb4

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed

kernel/create-image.sh

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2016 syzkaller project authors. All rights reserved.
3+
# Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
4+
5+
# create-image.sh creates a minimal Debian Linux image suitable for syzkaller.
6+
7+
set -eux
8+
9+
# Create a minimal Debian distribution in a directory.
10+
DIR=chroot
11+
PREINSTALL_PKGS=openssh-server,curl,tar,gcc,libc6-dev,time,strace,sudo,less,psmisc,selinux-utils,policycoreutils,checkpolicy,selinux-policy-default,firmware-atheros,debian-ports-archive-keyring
12+
13+
# If ADD_PACKAGE is not defined as an external environment variable, use our default packages
14+
if [ -z ${ADD_PACKAGE+x} ]; then
15+
ADD_PACKAGE="make,sysbench,git,vim,tmux,usbutils,tcpdump"
16+
fi
17+
18+
# Variables affected by options
19+
ARCH=$(uname -m)
20+
RELEASE=bullseye
21+
FEATURE=minimal
22+
SEEK=2047
23+
PERF=false
24+
IMG_HOSTNAME="localhost"
25+
26+
# Display help function
27+
display_help() {
28+
echo "Usage: $0 [option...] " >&2
29+
echo
30+
echo " -a, --arch Set architecture"
31+
echo " -d, --distribution Set on which debian distribution to create"
32+
echo " -f, --feature Check what packages to install in the image, options are minimal, full"
33+
echo " -n, --hostname Set hostname"
34+
echo " -s, --seek Image size (MB), default 2048 (2G)"
35+
echo " -h, --help Display help message"
36+
echo " -p, --add-perf Add perf support with this option enabled. Please set envrionment variable \$KERNEL at first"
37+
echo
38+
}
39+
40+
while true; do
41+
if [ $# -eq 0 ];then
42+
echo $#
43+
break
44+
fi
45+
case "$1" in
46+
-h | --help)
47+
display_help
48+
exit 0
49+
;;
50+
-a | --arch)
51+
ARCH=$2
52+
shift 2
53+
;;
54+
-d | --distribution)
55+
RELEASE=$2
56+
shift 2
57+
;;
58+
-f | --feature)
59+
FEATURE=$2
60+
shift 2
61+
;;
62+
-n | --hostname)
63+
IMG_HOSTNAME=$2
64+
shift 2
65+
;;
66+
-s | --seek)
67+
SEEK=$(($2 - 1))
68+
shift 2
69+
;;
70+
-p | --add-perf)
71+
PERF=true
72+
shift 1
73+
;;
74+
-*)
75+
echo "Error: Unknown option: $1" >&2
76+
exit 1
77+
;;
78+
*) # No more options
79+
break
80+
;;
81+
esac
82+
done
83+
84+
# Handle cases where qemu and Debian use different arch names
85+
case "$ARCH" in
86+
ppc64le)
87+
DEBARCH=ppc64el
88+
;;
89+
aarch64)
90+
DEBARCH=arm64
91+
;;
92+
arm)
93+
DEBARCH=armel
94+
;;
95+
x86_64)
96+
DEBARCH=amd64
97+
;;
98+
*)
99+
DEBARCH=$ARCH
100+
;;
101+
esac
102+
103+
# Foreign architecture
104+
105+
FOREIGN=false
106+
if [ $ARCH != $(uname -m) ]; then
107+
# i386 on an x86_64 host is exempted, as we can run i386 binaries natively
108+
if [ $ARCH != "i386" -o $(uname -m) != "x86_64" ]; then
109+
FOREIGN=true
110+
fi
111+
fi
112+
113+
if [ $FOREIGN = "true" ]; then
114+
# Check for according qemu static binary
115+
if ! which qemu-$ARCH-static; then
116+
echo "Please install qemu static binary for architecture $ARCH (package 'qemu-user-static' on Debian/Ubuntu/Fedora)"
117+
exit 1
118+
fi
119+
# Check for according binfmt entry
120+
if [ ! -r /proc/sys/fs/binfmt_misc/qemu-$ARCH ]; then
121+
echo "binfmt entry /proc/sys/fs/binfmt_misc/qemu-$ARCH does not exist"
122+
exit 1
123+
fi
124+
fi
125+
126+
# Double check KERNEL when PERF is enabled
127+
if [ $PERF = "true" ] && [ -z ${KERNEL+x} ]; then
128+
echo "Please set KERNEL environment variable when PERF is enabled"
129+
exit 1
130+
fi
131+
132+
# If full feature is chosen, install more packages
133+
if [ $FEATURE = "full" ]; then
134+
PREINSTALL_PKGS=$PREINSTALL_PKGS","$ADD_PACKAGE
135+
fi
136+
137+
sudo rm -rf $DIR
138+
sudo mkdir -p $DIR
139+
sudo chmod 0755 $DIR
140+
141+
# 1. debootstrap stage
142+
143+
DEBOOTSTRAP_PARAMS="--arch=$DEBARCH --include=$PREINSTALL_PKGS --components=main,contrib,non-free,non-free-firmware $RELEASE $DIR"
144+
if [ $FOREIGN = "true" ]; then
145+
DEBOOTSTRAP_PARAMS="--foreign $DEBOOTSTRAP_PARAMS"
146+
fi
147+
148+
# riscv64 is hosted in the debian-ports repository
149+
# debian-ports doesn't include non-free, so we exclude firmware-atheros
150+
if [ $DEBARCH == "riscv64" ]; then
151+
DEBOOTSTRAP_PARAMS="--keyring /usr/share/keyrings/debian-ports-archive-keyring.gpg --exclude firmware-atheros $DEBOOTSTRAP_PARAMS http://deb.debian.org/debian-ports"
152+
fi
153+
sudo --preserve-env=http_proxy,https_proxy,ftp_proxy,no_proxy debootstrap $DEBOOTSTRAP_PARAMS
154+
155+
# 2. debootstrap stage: only necessary if target != host architecture
156+
157+
if [ $FOREIGN = "true" ]; then
158+
sudo cp $(which qemu-$ARCH-static) $DIR/$(which qemu-$ARCH-static)
159+
sudo chroot $DIR /bin/bash -c "/debootstrap/debootstrap --second-stage"
160+
fi
161+
162+
# Set some defaults and enable promptless ssh to the machine for root.
163+
sudo sed -i '/^root/ { s/:x:/::/ }' $DIR/etc/passwd
164+
echo 'T0:23:respawn:/sbin/getty -L ttyS0 115200 vt100' | sudo tee -a $DIR/etc/inittab
165+
printf '\nauto eth0\niface eth0 inet dhcp\n' | sudo tee -a $DIR/etc/network/interfaces
166+
echo '/dev/root / ext4 defaults 0 0' | sudo tee -a $DIR/etc/fstab
167+
echo 'debugfs /sys/kernel/debug debugfs defaults 0 0' | sudo tee -a $DIR/etc/fstab
168+
echo 'securityfs /sys/kernel/security securityfs defaults 0 0' | sudo tee -a $DIR/etc/fstab
169+
echo 'configfs /sys/kernel/config/ configfs defaults 0 0' | sudo tee -a $DIR/etc/fstab
170+
echo 'binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0' | sudo tee -a $DIR/etc/fstab
171+
echo -en "127.0.0.1\tlocalhost\n" | sudo tee $DIR/etc/hosts
172+
echo "nameserver 8.8.8.8" | sudo tee -a $DIR/etc/resolve.conf
173+
echo $IMG_HOSTNAME | sudo tee $DIR/etc/hostname
174+
ssh-keygen -f $RELEASE.id_rsa -t rsa -N ''
175+
sudo mkdir -p $DIR/root/.ssh/
176+
cat $RELEASE.id_rsa.pub | sudo tee $DIR/root/.ssh/authorized_keys
177+
178+
# Add perf support
179+
if [ $PERF = "true" ]; then
180+
cp -r $KERNEL $DIR/tmp/
181+
BASENAME=$(basename $KERNEL)
182+
sudo chroot $DIR /bin/bash -c "apt-get update; apt-get install -y flex bison python-dev libelf-dev libunwind8-dev libaudit-dev libslang2-dev libperl-dev binutils-dev liblzma-dev libnuma-dev"
183+
sudo chroot $DIR /bin/bash -c "cd /tmp/$BASENAME/tools/perf/; make"
184+
sudo chroot $DIR /bin/bash -c "cp /tmp/$BASENAME/tools/perf/perf /usr/bin/"
185+
rm -r $DIR/tmp/$BASENAME
186+
fi
187+
188+
# Add udev rules for custom drivers.
189+
# Create a /dev/vim2m symlink for the device managed by the vim2m driver
190+
echo 'ATTR{name}=="vim2m", SYMLINK+="vim2m"' | sudo tee -a $DIR/etc/udev/rules.d/50-udev-default.rules
191+
192+
# Build a disk image
193+
dd if=/dev/zero of=$RELEASE.img bs=1M seek=$SEEK count=1
194+
sudo mkfs.ext4 -F $RELEASE.img
195+
sudo mkdir -p /mnt/$DIR
196+
sudo mount -o loop $RELEASE.img /mnt/$DIR
197+
sudo cp -a $DIR/. /mnt/$DIR/.
198+
sudo umount /mnt/$DIR

0 commit comments

Comments
 (0)