Skip to content

Commit 132b4fc

Browse files
committed
tools: labs: qemu: Add run-qemu.sh as alternative
1 parent 0252285 commit 132b4fc

File tree

4 files changed

+169
-1
lines changed

4 files changed

+169
-1
lines changed

tools/labs/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ rootfs.img
66
disk1.img
77
disk2.img
88
/*core-image-*.ext4
9+
/*core-image-*.bz2
910
.modinst
1011
/out/
12+
rootfs

tools/labs/qemu/Makefile

+38-1
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,41 @@ clean::
9595
-rm -f disk1.img disk2.img
9696
-rm -f pipe1.in pipe1.out pipe2.in pipe2.out
9797

98-
.PHONY: boot gdb clean tap0 tap1
98+
# Run with one of the following:
99+
# make -j$(nproc) console
100+
# make -j$(nproc) gui
101+
102+
# Attach debugger with
103+
# make gdb
104+
105+
# Stop with
106+
# sync # make sure all filesystem changes are propagated
107+
# CTRL-A X # kill qemu, faster and more reliable than poweroff etc.
108+
109+
# Compile in skel directories with something like:
110+
# alias kmake='make -C "$HOME/src/linux/" M="$(pwd)"'
111+
# kmake
112+
113+
YOCTO_ROOTFS = core-image-minimal-qemu$(ARCH).tar.bz2
114+
115+
console: $(ZIMAGE) rootfs
116+
MODE=console qemu/run-qemu.sh
117+
118+
gui:
119+
MODE=gui qemu/run-qemu.sh
120+
121+
rootfs: $(YOCTO_ROOTFS)
122+
mkdir -p rootfs
123+
tar -xf core-image-minimal-qemux86.tar.bz2 -C rootfs
124+
sed -i 's@/sbin/getty@& -n -l "/sbin/rootlogin"@' rootfs/bin/start_getty
125+
printf '%s\n' '#!/bin/sh' '/bin/login -f root' > rootfs/sbin/rootlogin
126+
chmod +x rootfs/sbin/rootlogin
127+
mkdir -p rootfs/home/root/skels
128+
echo "//10.0.2.2/skels /home/root/skels cifs port=4450,guest,user=dummy 0 0" >> rootfs/etc/fstab
129+
130+
$(YOCTO_ROOTFS):
131+
wget $(YOCTO_URL)/$(YOCTO_ROOTFS)
132+
133+
134+
135+
.PHONY: console gui boot gdb clean tap0 tap1

tools/labs/qemu/kernel_config.x86

+15
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,18 @@ CONFIG_UPROBE_EVENTS=y
9797
CONFIG_LOCKDEP=y
9898
# kernel lock tracing:
9999
CONFIG_LOCK_STAT=y
100+
101+
# for qemu serial console ttyS0:
102+
CONFIG_SERIAL_8250=y
103+
CONFIG_SERIAL_8250_CONSOLE=y
104+
105+
# for qemu default netdev:
106+
CONFIG_E1000=y
107+
108+
# for CIFS/SMB/samba rootfs and share
109+
CONFIG_IP_PNP=y
110+
CONFIG_IP_PNP_DHCP=y
111+
CONFIG_CIFS=y
112+
CONFIG_CIFS_XATTR=y
113+
CONFIG_CIFS_POSIX=y
114+
CONFIG_CIFS_ROOT=y

tools/labs/qemu/run-qemu.sh

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/bash
2+
3+
die() { echo "$0: error: $@" >&2; exit 1; }
4+
5+
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null && pwd)"
6+
7+
base_dir=${ROOT:-"$(readlink -f "$script_dir/..")"}
8+
arch=${ARCH:-"x86"}
9+
kernel=${ZIMAGE:-"$(readlink -f "$base_dir/../../arch/$arch/boot/bzImage")"}
10+
rootfs=${ROOTFS:-"$(readlink -f "$base_dir/rootfs")"}
11+
skels=${SKELS:-"$(readlink -f "$base_dir/skels")"}
12+
13+
mode="${MODE:-console}"
14+
case "$mode" in
15+
console)
16+
qemu_display="-nographic"
17+
linux_console="console=ttyS0"
18+
;;
19+
gui)
20+
# QEMU_DISPLAY = sdl, gtk, ...
21+
qemu_display="-display ${QEMU_DISPLAY:-"sdl"}"
22+
linux_console=""
23+
;;
24+
*) echo "unknown mode '$MODE'" >&2; exit 1 ;;
25+
esac
26+
27+
case "$arch" in
28+
x86) qemu_arch=i386 ;;
29+
arm) qemu_arch=arm ;;
30+
*) echo "unknown architecture '$arch'" >&2; exit 1 ;;
31+
esac
32+
33+
smbd=${SMBD:-"smbd"}
34+
35+
qemu=${QEMU:-"qemu-system-$qemu_arch"}
36+
qemu_kvm=${QEMU_KVM:-"-enable-kvm -cpu host"}
37+
qemu_cpus=${QEMU_CPUS:-"1"}
38+
qemu_mem=${QEMU_MEM:-"512"}
39+
qemu_display=${QEMU_DISPLAY:-"$qemu_display"}
40+
qemu_addopts=${QEMU_ADD_OPTS:-""}
41+
linux_console=${LINUX_CONSOLE:-"$linux_console"}
42+
linux_loglevel=${LINUX_LOGLEVEL:-"15"}
43+
linux_term=${LINUX_TERM:-"TERM=xterm"}
44+
linux_addcmdline=${LINUX_ADD_CMDLINE:-""}
45+
46+
linux_cmdline=${LINUX_CMDLINE:-"root=/dev/cifs rw ip=dhcp cifsroot=//10.0.2.2/rootfs,port=4450,guest,user=dummy $linux_console loglevel=$linux_loglevel $linux_term $linux_addcmdline"}
47+
48+
tmp_dir=$(mktemp -d)
49+
user=$(id -un)
50+
51+
cat << EOF > "$tmp_dir/smbd.conf"
52+
[global]
53+
interfaces = 127.0.0.1
54+
smb ports = 4450
55+
private dir = $tmp_dir
56+
bind interfaces only = yes
57+
pid directory = $tmp_dir
58+
lock directory = $tmp_dir
59+
state directory = $tmp_dir
60+
cache directory = $tmp_dir
61+
ncalrpc dir = $tmp_dir/ncalrpc
62+
log file = $tmp_dir/log.smbd
63+
smb passwd file = $tmp_dir/smbpasswd
64+
security = user
65+
map to guest = Bad User
66+
load printers = no
67+
printing = bsd
68+
disable spoolss = yes
69+
usershare max shares = 0
70+
71+
server min protocol = NT1
72+
unix extensions = yes
73+
74+
server role = standalone server
75+
public = yes
76+
writeable = yes
77+
#admin users = root
78+
#create mask = 0777
79+
#directory mask = 0777
80+
force user = $user
81+
force group = $user
82+
83+
84+
[rootfs]
85+
path = $rootfs
86+
[skels]
87+
path = $skels
88+
EOF
89+
90+
[ -x "$(command -v "$smbd")" ] || die "samba ('$smbd') not found"
91+
[ -x "$(command -v "$qemu")" ] || die "qemu ('$qemu') not found"
92+
93+
mkdir -p "$skels"
94+
95+
"$smbd" --no-process-group -s "$tmp_dir/smbd.conf" -l "$tmp_dir" >/dev/null 2>/dev/null &
96+
97+
"$qemu" \
98+
$qemu_kvm \
99+
-smp "$qemu_cpus" -m "$qemu_mem" \
100+
-no-reboot \
101+
-kernel "$kernel" \
102+
-append "$linux_cmdline" \
103+
-gdb tcp::1234 \
104+
$qemu_display \
105+
$qemu_addopts
106+
107+
# This seems to reset to the mode the terminal was prior to launching QEMU
108+
# Inspired by
109+
# https://github.com/landley/toybox/blob/990e0e7a40e4509c7987a190febe5d867f412af6/toys/other/reset.c#L26-L28
110+
# man 4 console_codes, ESC [ ? 7 h
111+
printf '\e[?7h'
112+
113+
pkill -F "$tmp_dir/smbd.pid"
114+
rm -rf "$tmp_dir"

0 commit comments

Comments
 (0)