/
create_qemu_image.sh
executable file
·75 lines (62 loc) · 2.06 KB
/
create_qemu_image.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
#!/bin/bash -eux
#
# Creates and configures a minimal Debian image for running HWASan tests in
# QEMU with the run_in_qemu_with_lam.sh script in this directory.
#
# Usage: sudo ./create_qemu_image.sh
#
# Outputs: the QEMU image and SSH keys in the current directory.
if [[ -f /etc/lsb-release ]] ; then
. /etc/lsb-release
RELEASE=${DISTRIB_CODENAME}
fi
if [[ -f /etc/os-release ]] ; then
. /etc/os-release
RELEASE=${VERSION_CODENAME}
fi
# Comma-separated list of packages.
: ${PREINSTALL_PKGS:="openssh-server,nfs-common"}
readonly DIR="$(mktemp -d)"
readonly IMAGE_DIR="${DIR}/${RELEASE}"
# Generate base system.
mkdir "${IMAGE_DIR}"
debootstrap --include="${PREINSTALL_PKGS}" "${RELEASE}" "${IMAGE_DIR}"
# Configure system to boot properly.
sed -i "/^root/ { s/:x:/::/ }" "${IMAGE_DIR}/etc/passwd"
echo "T0:23:respawn:/sbin/getty -L ttyS0 115200 vt100" \
>> "${IMAGE_DIR}/etc/inittab"
# debian
[[ -d ${IMAGE_DIR}/etc/network ]] && cat <<EOF >${IMAGE_DIR}/etc/network/interfaces
auto eth0
iface eth0 inet dhcp
EOF
# ubuntu
[[ -d ${IMAGE_DIR}/etc/netplan ]] && cat <<EOF >${IMAGE_DIR}/etc/netplan/config.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: true
EOF
echo "/dev/root / ext4 defaults 0 0" >> "${IMAGE_DIR}/etc/fstab"
echo -en "127.0.0.1\tlocalhost\n" > "${IMAGE_DIR}/etc/hosts"
echo "nameserver 8.8.8.8" >> "${IMAGE_DIR}/etc/resolve.conf"
echo "debian" > "${IMAGE_DIR}/etc/hostname"
# Set up SSH.
ssh-keygen -q -f "debian.id_rsa" -t rsa -N ""
mkdir -p "${IMAGE_DIR}/root/.ssh/"
cat "debian.id_rsa.pub" > "${IMAGE_DIR}/root/.ssh/authorized_keys"
echo "MaxSessions 1000" >>${IMAGE_DIR}/etc/ssh/sshd_config
# Configure for HWASan tests.
mkdir -p "${IMAGE_DIR}/workspace"
# Build disk image.
dd if=/dev/zero of="debian.img" bs=1M seek=2047 count=1
mkfs.ext4 -F "debian.img"
mkdir -p "/mnt/${RELEASE}"
mount -o loop "debian.img" "/mnt/${RELEASE}"
cp -a "${IMAGE_DIR}/." "/mnt/${RELEASE}/."
umount "/mnt/${RELEASE}"
while ! rm -rf "/mnt/${RELEASE}" ; do sleep 5; done;
# Allow non-root user to access image.
chmod 666 "debian.img"