Skip to content

Instantly share code, notes, and snippets.

@suxue
Last active August 29, 2015 14:05
Show Gist options
  • Save suxue/dade5b978b276e290512 to your computer and use it in GitHub Desktop.
Save suxue/dade5b978b276e290512 to your computer and use it in GitHub Desktop.
qemu qcow2 format kernel testing image
linux.img
.lock
record
.gdbinit
#!/bin/bash
source `pwd`/config.sh
while [[ $# -gt 0 ]]; do
eval "$1"
shift
done
if [[ -f $LOCK_FILE ]]; then
echo "$LOCK_FILE existing, abort..."
exit 1
fi
touch $LOCK_FILE
pid_file=`env mktemp --suffix=.pid`
comm=`mktemp`
function append {
printf "%s" "$* " >>$comm
}
function boot {
append qemu-system-x86_64
append $ACCEL
append -drive file=$IMAGE_FILE,if=$HD_DRV
append -net nic,model=$NET_DRV
append -net user,hostfwd=::$REDIR_PORT-10.0.2.1:22
append -s
append -device virtio-serial-pci
append -m $MEM_SIZE -cpu $CPU_TYPE -smp $SMP
append -nodefaults -display none -vga qxl -pidfile $pid_file
append -usb
if [[ -v MONITOR ]]; then
append -monitor telnet:127.0.0.1:$MONITOR,server,nowait
fi
if [[ -v EXTRA ]]; then
append "$EXTRA"
fi
if [[ $SERIAL == pty ]]; then
append -device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0
append -chardev spicevmc,id=spicechannel0,name=vdagent
append -spice port=$SPICE_PORT,addr=127.0.0.1,disable-ticketing,ipv4
append -daemonize -serial pty
append '2>&1 | grep -o "/dev/pts/[0-9]\+"'
elif [[ $SERIAL == stdio ]]; then
append -serial stdio
else
echo 'abort...'
exit 1
fi
evil="$(cat $comm)"
rm $comm
eval "$evil"
}
if [[ $SERIAL == pty ]]; then
ptyfile=`boot $@`
if (( $? != 0 )); then
exit 1
fi
qemu_pid=`cat $pid_file`
trap "kill $qemu_pid; rm -f $PID_FILE $LOCK_FILE" EXIT
trap "kill $qemu_pid; rm -f $PID_FILE $LOCK_FILE" HUP
screen $ptyfile
read i
rm $pid_file
elif [[ $SERIAL == stdio ]]; then
trap "rm -f $PID_FILE $LOCK_FILE" EXIT
trap "rm -f $PID_FILE $LOCK_FILE" HUP
boot "$@"
rm $pid_file
fi
#!/bin/sh
NET_DRV=virtio # virtio|e1000
HD_DRV=virtio # ide|scsi|virtio
MEM_SIZE=128m
CPU_TYPE=host
SMP=2
ACCEL=--enable-kvm
SERIAL=pty
MONITOR=8849
#EXTRA="-device usb-host,vendorid=0xf055,productid=0xcec5"
#EXTRA="-device usb-avrk,id=usb-avrk0,filename=avrk0"
IMAGE_FILE=linux.img
if [[ ! -f $IMAGE_FILE ]]; then
tmp_file=`mktemp`
git show origin/image:linux.qcow2 > $tmp_file
qemu-img convert $tmp_file $IMAGE_FILE
rm $tmp_file
fi
REDIR_PORT=60022
WORK_DIR=fs_root
KERNEL_WORKDIR=../qemu/
KERNEL_SRCDIR=../../linux/3.16
MODULE_DIR_A=../avr
LOCK_FILE=.lock
SPICE_PORT=33777
MODULE_DIR=`[[ -d $MODULE_DIR_A ]] && cd $MODULE_DIR_A && pwd`
if [[ $# == 1 ]]; then
if [[ $1 == "MODULE_DIR" ]]; then
echo $MODULE_DIR
fi
fi
#!/bin/bash
source `pwd`/config.sh
mod_file=`find $MODULE_DIR/*.ko -print`
mod_name=`basename $mod_file | sed -e 's/\.ko$//'`
total="`./ssh.sh cat /sys/module/$mod_name/sections/.{text,data}`"
text=`echo $total | awk '{print $1}'`
#bss=`echo $total | awk '{print $2}'`
data=`echo $total | awk '{print $2}'`
cat > .gdbinit <<EOF
file $KERNEL_WORKDIR/vmlinux
add-symbol-file $mod_file $text -s .data $data
target remote :1234
EOF
cgdb
#expect <(echo "spawn gdb -q
#expect \"(gdb) \"
#send \"file $KERNEL_WORKDIR/vmlinux\r\"
#send \"add-symbol-file $mod_file $text -s .data $data \r\"
#send \"target remote :1234\r\"
#interact")
#echo "target remote :1234"
#!/bin/sh
source `pwd`/config.sh
setsid spicec -h localhost --port $SPICE_PORT &
#!/bin/sh
source `pwd`/config.sh
#rsync -ve "ssh -p $REDIR_PORT" $KERNEL_WORKDIR/arch/x86/boot/bzImage root@127.0.0.1:/boot/bzImage
if [[ -d $MODULE_DIR_A ]]; then
modfile_name=$(basename `find $MODULE_DIR/ -name '*.ko' -print` 2>/dev/null)
if [[ ! -z "$modfile_name" ]]; then
scp -P $REDIR_PORT $MODULE_DIR/$modfile_name root@127.0.0.1:
./ssh.sh 'touch /lib/modules/`uname -r`/modules.{order,builtin}'
./ssh.sh "install $modfile_name "'-D /lib/modules/`uname -r`/'"$modfile_name; depmod -A;"
./ssh.sh '$HOME/.local/bin/reload '"$modfile_name; rm $modfile_name"
fi
fi
#!/bin/sh
source `pwd`/config.sh
if [[ -d ../repo/linus/include/config ]]; then
rmdir ../repo/linus/include/config
fi
first=`echo "$KERNEL_WORKDIR" | sed -e 's/^\(.\).*$/\1/'`
if [[ $first == . ]]; then
KERNEL_WORKDIR="$PWD/$KERNEL_WORKDIR"
fi
make -C $KERNEL_SRCDIR O=$KERNEL_WORKDIR "$@"
mod_dir=`./config.sh MODULE_DIR`
all: mod
kernel:
./make.sh
kernel-install: kernel
./install.sh kernel
mod:
./make.sh M=$(mod_dir)
edit:
vim $(mod_dir)
install:
./ssh.sh "echo 'new module:$(mod_dir) from host...' >/dev/kmsg"
./install.sh
#!/bin/sh
source `pwd`/config.sh
telnet 127.0.0.1 "$MONITOR"
#!/bin/bash
# ./mount.sh m => mount image $WORK_DIR
# ./mount.sh um => umount $WORK_DIR and cleanup
# ./mount => mount, chroot and finally umount
source `pwd`/config.sh
if [[ -f $LOCK_FILE ]]; then
echo "$LOCK_FILE existing, abort..."
exit 1
fi
touch $LOCK_FILE
function l {
echo "map loop device ..."
sudo losetup /dev/loop1 $IMAGE_FILE
sudo kpartx -a /dev/loop1
}
function ul {
echo "unload loop device..."
sudo kpartx -d /dev/loop1
sudo losetup -d /dev/loop1
rm -f $LOCK_FILE
}
function m {
l
echo "mount ..."
mkdir -p $WORK_DIR
sudo mount -t ext4 /dev/mapper/loop1p2 $WORK_DIR
#sudo mount -t squashfs -o loop,ro $WORK_DIR/usr.squashfs $WORK_DIR/usr
sudo mount --bind /dev $WORK_DIR/dev
sudo mount -t devpts devpts $WORK_DIR/dev/pts
sudo mount -t proc proc $WORK_DIR/proc
sudo mount -t sysfs sysfs $WORK_DIR/sys
sudo mount -t tmpfs shm $WORK_DIR/dev/shm
}
function c {
echo "chroot ..."
sudo chroot $WORK_DIR /bin/sh -c 'cd ~; /bin/bash'
}
function um {
echo "umount ..."
sudo umount $WORK_DIR/sys
sudo umount $WORK_DIR/proc
sudo umount $WORK_DIR/dev/pts
sudo umount $WORK_DIR/dev/shm
sudo umount $WORK_DIR/dev
#sudo umount $WORK_DIR/usr
sudo umount $WORK_DIR
ul
rmdir $WORK_DIR
}
function dump_image {
l
sudo fsck /dev/mapper/loop1p2
sudo zerofree /dev/mapper/loop1p2
sudo fsck /dev/mapper/loop1p2
ul
qemu-img convert -c -p -o compat=1.1 -O qcow2 linux.img linux.qcow2
}
if [[ $# == 0 ]]; then
m && c; um
elif [[ "$1" == "l" ]]; then
l
elif [[ "$1" == "m" ]]; then
m
elif [[ "$1" == "um" ]]; then
um
elif [[ "$1" == "ul" ]]; then
ul
elif [[ "$1" == "d" ]]; then
dump_image
fi
#!/bin/sh
screen -d -m -S kernel ./boot.sh "$@"
screen -d -r -S kernel -X shell ./ssh.sh
(sleep 5; screen -d -r -S kernel -X screen ./monitor.sh; screen -r -d -S kernel -X select 1) &
screen -r kernel
#!/bin/sh
source `pwd`/config.sh
ssh root@127.0.0.1 -p $REDIR_PORT "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment