Skip to content

Instantly share code, notes, and snippets.

@vianpl
Created June 5, 2017 12:40
Show Gist options
  • Save vianpl/23bb68198171267032479cd0714f9807 to your computer and use it in GitHub Desktop.
Save vianpl/23bb68198171267032479cd0714f9807 to your computer and use it in GitHub Desktop.
Semiautomated qemu kernel development
#!/bin/bash
#
# Create qemu image to test kernel builds
#
# defines
MNT_PATH=~/qemu/
MNT=rootfs
QEMU_PATH=~/qemu
QEMU_IMG=qemu.img
KERNEL_PATH=~/code/linux
KERNEL_BUILD_DIR=$KERNEL_PATH/build/arch/x86_64/boot
KERNEL_IMG=bzImage
UBUNTU_VERSION=zesty
function create_qemu_base_image {
# create qemu image
qemu-img create $QEMU_PATH/$QEMU_IMG 8G
mkfs.ext4 $QEMU_PATH/$QEMU_IMG
# mount qemu image
mkdir -p $MNT_PATH/$MNT
sudo mount -oloop $QEMU_PATH/$QEMU_IMG $MNT_PATH/$MNT
# install basic ubuntu image
sudo debootstrap --arch=amd64 --variant=buildd $UBUNTU_VERSION rootfs http://archive.ubuntu.com/ubuntu/
sudo cp /etc/resolv.conf "$MNT_PATH/$MNT/etc/resolv.conf"
cat <<REPO_LIST | sudo tee "$MNT_PATH/$MNT/etc/apt/sources.list"
# Repositories
deb http://archive.ubuntu.com/ubuntu $UBUNTU_VERSION main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu $UBUNTU_VERSION-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu $UBUNTU_VERSION-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu $UBUNTU_VERSION-backports main restricted universe multiverse
REPO_LIST
# chroot
sudo mount --bind /dev $MNT_PATH/$MNT/dev
sudo mount --bind /proc $MNT_PATH/$MNT/proc
sudo mount --bind /sys $MNT_PATH/$MNT/sys
sudo chroot "$MNT_PATH/$MNT" /usr/bin/env PATH=/usr/sbin:/usr/bin/:/bin:/sbin bash -i
# add root password
passwd
# install sw, create users or whatever
apt-get update
apt-get install ubuntu-gnome-desktop -y
apt-get install qv4l2 gstreamer1.0 git build-essential locales sudo -y
# unmount everything
exit
sudo umount $MNT_PATH/$MNT/dev
sudo umount $MNT_PATH/$MNT/proc
sudo umount $MNT_PATH/$MNT/sys
sudo umount $MNT_PATH/$MNT
}
function mount_and_chroot_qemu_img {
# mount qemu image
mkdir -p $MNT_PATH/$MNT
sudo mount -oloop $QEMU_PATH/$QEMU_IMG $MNT_PATH/$MNT
# chroot
sudo mount --bind /dev $MNT_PATH/$MNT/dev
sudo mount --bind /proc $MNT_PATH/$MNT/proc
sudo mount --bind /sys $MNT_PATH/$MNT/sys
sudo chroot "$MNT_PATH/$MNT" /usr/bin/env PATH=/usr/sbin:/usr/bin/:/bin:/sbin bash -i
# at this point you can safely install whatever packages needed to exit just write exit
}
function clean_chroot {
# unmount everything
sudo umount $MNT_PATH/$MNT/dev
sudo umount $MNT_PATH/$MNT/proc
sudo umount $MNT_PATH/$MNT/sys
sudo umount $MNT_PATH/$MNT
# remove mount dir
rm -r $MNT_PATH/$MNT
}
function launch_qemu {
sudo qemu-system-x86_64 -hda $QEMU_PATH/$QEMU_IMG -m 4G -smp 4 --enable-kvm --kernel $KERNEL_BUILD_DIR/$KERNEL_IMG --append "root=/dev/sda rw"
}
function linux_configure {
mkdir -p build
make O=build x86_64_defconfig
make O=build kvmconfig
}
function linux_build_and_install {
# move to linux dir
cd $KERNEL_PATH
# mount qemu image
mkdir -p $MNT_PATH/$MNT
sudo mount -oloop $QEMU_PATH/$QEMU_IMG $MNT_PATH/$MNT
# build and install
mkdir -p build
make O=build -j8
sudo make O=build modules_install INSTALL_MOD_PATH=$MNT_PATH/$MNT
sudo make O=build headers_install INSTALL_HDR_PATH=$MNT_PATH/$MNT
sudo make O=build install INSTALL_HDR_PATH=$MNT_PATH/$MNT
# move back to previous dir
cd -
# build fbsrc driver and copy it into the image
cd ~/code/fbsrc
make KDIR=$KERNEL_PATH/build/
cp fbsrc.ko $MNT_PATH/$MNT/home/nico
cd -
#cleanup
sudo umount $MNT_PATH/$MNT
}
linux_build_and_install
launch_qemu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment