Last active
February 23, 2016 03:49
-
-
Save tjs-w/c5680c53b4785de259dc to your computer and use it in GitHub Desktop.
Debugging Linux Kernel using QEMU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright (C) Tejas Wanjari (twanjari@andrew.cmu.edu) | |
# GNU/GPL | |
# ------- | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
# Fill in the macros | |
# ------------------------------------------------------------------------ | |
DEBIAN_DISTRO="wheezy" | |
FS="ext4" | |
ARCH="i386" | |
MOUNT_POINT="/mnt/wheezy/" | |
IMG="$HOME/$DEBIAN_DISTRO-$ARCH.img" | |
# ------------------------------------------------------------------------ | |
# Make sure only root can run our script | |
if [ "$(id -u)" != "0" ] | |
then | |
echo "This script must be run as root" 1>&2 | |
exit 1 | |
fi | |
# Following binaries are mandetory | |
BINS="dd mount umount mkfs.$FS debootstrap" | |
for i in $BINS | |
do | |
which $i > /dev/null && continue || \ | |
{ echo "$i command not found."; exit 1; } | |
done | |
# Check if the disk image is already mounted | |
mountpoint -q $MOUNT_POINT && umount -f $MOUNT_POINT | |
# Create raw disk image [size = bs * count] | |
echo -e "\nCreating disk image $IMG" | |
dd if=/dev/zero of=$IMG bs=1K count=1M || exit 1 | |
du -h $IMG | |
# Format the hda (ext3 is perferred -- better for large no. of small files | |
# than ext2, and smaller footprint than ext4 since there is no journaling | |
# overhead) | |
echo -e "\nFormatting $IMG" | |
mkfs.$FS -F $IMG || exit 1 | |
file $IMG | |
# Mount the disk image | |
echo -e "\nCreating mount-point $MOUNT_POINT" | |
mkdir -p $MOUNT_POINT | |
echo "Mounting $IMG, mount-point: $MOUNT_POINT" | |
mount -o loop $IMG $MOUNT_POINT || exit 1 | |
# Bootstrapping the rootfs | |
echo -e "\nCreating the root file system" | |
echo "This may take a while..." | |
debootstrap --arch=$ARCH $DEBIAN_DISTRO $MOUNT_POINT \ | |
"http://ftp.us.debian.org/debian" || exit 1 | |
echo "======== rootfs created ========" | |
#Create serial port and sda in /dev of the image | |
mknod $MOUNT_POINT/dev/ttyS0 c 80 10 | |
mknod $MOUNT_POINT/dev/sda1 c 70 10 | |
# Unmount | |
echo -e "\nCleaning up" | |
umount $MOUNT_POINT | |
rm -rf $MOUNT_POINT | |
# hda ready | |
echo -e "\nhda: $IMG ready." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# -S Do not start CPU at startup (you must type 'c' in the monitor). | |
# -s Shorthand for -gdb tcp::1234, i.e. open a gdbserver on TCP port 1234. | |
# No-GUI debugging | |
qemu -kernel bzImage -append "root=/dev/sda console=ttyS0" -m 2G -hda wheezy.img -serial stdio -nographic -nodefaults -s -S | |
# QEMU opens GUI term to debug | |
qemu -kernel bzImage -append "root=/dev/sda console=tty0" -m 2G -hda wheezy.img -s -S | |
# Inside gdb in other term | |
gdb bzImage | |
(gdb) target remote localhost:1234 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment