Skip to content

Instantly share code, notes, and snippets.

@JasonGhent
Last active July 26, 2017 16:48
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JasonGhent/922f38f57c8cb77b10f3 to your computer and use it in GitHub Desktop.
Save JasonGhent/922f38f57c8cb77b10f3 to your computer and use it in GitHub Desktop.
Attempt to update https://gist.github.com/JasonGhent/e7deab904b30cbc08a7d to work a year later
# script insstalls qemu and vagrant, then uses vagrant to build a qemu-
# supported rpi kernel for dev.
# - tested with 2015-02-16-raspbian-wheezy.zip on OSX El Capitan
# Variables
IMAGE=http://downloads.raspberrypi.org/raspbian_latest
IMG_PATH="/vagrant/raspbian_latest.img"
PI_LINUX=https://github.com/raspberrypi/linux
PI_LINUX_BRANCH="rpi-4.2.y"
PI_TOOLS=https://github.com/raspberrypi/tools
LINUX_DIR="/opt/raspberrypi/linux"
AUFS_GIT=git://aufs.git.sourceforge.net/gitroot/aufs/aufs3-standalone.git
CCPREFIX="~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-"
COMPILE="sudo make -j3 ARCH=arm CROSS_COMPILE=\$CCPREFIX"
QEMU_HTTPS=https://download.plop.at/ploplinux/4.3.1/sources/99-ARM/qemu/
QEMU_PATCH=arm-qemu-linux-4.2.8.patch
QEMU_RULES=/mnt/img/linux/etc/udev/rules.d/90-qemu.rules;
YEAR=$(date | rev | cut -f1 -d" " | rev)
RASP_FILE="$YEAR*"
server="pi@localhost"
destiny="~/docker-debs"
src="rpi-kernel/build_results/$YEAR*/*"
ret=1
firstAttempt=0
# remove prior vagrant shell script
rm vagrant_script.sh 2> /dev/null
# create vagrant shell script
function vshell {
echo $1 >> vagrant_script.sh;
}
function clean_deps {
vagrant halt -f && vagrant destroy -f && vagrant up
}
# Use vagrant to customize the kernel for docker compatability
function vagrant_install_dependencies {
# dependencies
vshell "sudo apt-get update -y"
vshell "sudo apt-get install -y libncurses5-dev gcc make git bc libc6:i386"
vshell "sudo apt-get install -y libgcc1:i386 gcc-4.6-base:i386 ia32-libs"
vshell "sudo apt-get install -y libstdc++5:i386 libstdc++6:i386 lib32z1"
vshell "sudo apt-get install -y lib32ncurses5 lib32bz2-1.0 vim unzip"
# clone pi tools and linux kernel base
vshell "sudo mkdir /opt/raspberrypi #2> /dev/null"
vshell "sudo git clone --depth=1 $PI_TOOLS ~/tools #2>1 >/dev/null"
vshell "sudo git clone --depth=1 $PI_LINUX $LINUX_DIR"
vshell "cd $LINUX_DIR && git checkout $PI_LINUX_BRANCH"
vshell "sudo make clean"
vshell "sudo mkdir -p /mnt/img/boot /mnt/img/linux" # mnt dirs
vshell "sudo mkdir -p $LINUX_DIR/../modules && cd $LINUX_DIR"
}
function build_kernel {
# build config && compile kernel
vshell "export KERNEL=kernel7"
vshell "export CCPREFIX=$CCPREFIX"
# vshell "$COMPILE bcmrpi_defconfig #> /dev/null" # rpi
# vshell "$COMPILE #> /dev/null"
vshell "$COMPILE bcm2709_defconfig"
## vshell "$COMPILE menuconfig"
vshell "$COMPILE zImage modules dtbs"
# vshell "$COMPILE modules #> /dev/null" # modules
# vshell "$COMPILE INSTALL_MOD_PATH=$LINUX_DIR/../modules modules_install #> /dev/null" # modules_install
vshell "$COMPILE INSTALL_MOD_PATH=$LINUX_DIR/../modules modules_install" # modules_install
}
function install_osx_deps {
brew install qemu && brew tap phinze/homebrew-cask && brew install brew-cask
brew cask install virtualbox vagrant vagrant-manager
vagrant box add precise64 http://files.vagrantup.com/precise64.box
vagrant init precise64
}
function get_and_unzip_raspbian_image {
vshell "cd /vagrant"
vshell "wget $IMAGE -O raspbian_latest.zip"
vshell "unzip -o raspbian_latest.zip"
vshell "mv $RASP_FILE raspbian_latest.img"
vshell "cd -"
}
# clone kernel patches
function patch_kernel {
# QEMU for ARM
vshell "sudo wget $QEMU_HTTPS$QEMU_PATCH && sudo patch -p1 < $QEMU_PATCH"
}
# scp over hypriot's docker deb packages and ssh onto pi when it has booted
function wait_for_ssh {
echo "Waiting for SSH daemon to begin.. (this can take a couple minutes)"
echo "Once up, we copy the docker deb files built by hypriot to the pi."
echo "When prompted, the default password is 'raspberry'."
while [[ $ret -ne 0 ]]; do
# copy deb pkg over
ssh \
-o StrictHostKeyChecking=no \
-p 5022 "$server" "mkdir -p $destiny" 2>/dev/null \
&& \
scp -r -P 5022 $src $server:$destiny 2>/dev/null
# connect to pi
ssh \
-o StrictHostKeyChecking=no \
-o ConnectTimeout=3 \
-p 5022 $server 2>/dev/null
# did ssh succeed?
ret=$?
if [[ $firstAttempt -eq 0 ]]; then
firstAttempt=1
else
echo "still waiting.."
fi
if [[ $ret -ne 0 ]]; then
sleep 10
fi
done
}
# Use vagrant to initialize the kernel img for raspberry pi emulation
function mount_boot {
# get size of boot partition and specify offset to mount
vshell "export START=\$(sudo /sbin/fdisk -lu $IMG_PATH | tail -n 2 | head -n 1 | awk '{print \$2}')"
vshell "export START=\$(expr \$START \* \`cat /sys/block/sda/queue/hw_sector_size\`)"
vshell "sudo mount -v -o loop,offset=\$START $IMG_PATH /mnt/img/boot"
}
function copy_boot_files {
# copy kernel from boot partiaion into /vagrant/tmp to expose to OSX
vshell "mkdir -p /vagrant/tmp"
vshell "cp /mnt/img/boot/kernel7.img /vagrant/tmp"
vshell "cp /mnt/img/boot/bcm2709-rpi-2-b.dtb ~/tmp"
vshell "sudo umount /mnt/img/boot" # we have what we need. unmount boot partition.
}
function mount_data {
# get size of data partition and specify offset to mount
vshell "export START=\$(sudo /sbin/fdisk -lu $IMG_PATH | tail -n 1 | awk '{print \$2}')"
vshell "export START=\$(expr \$START \* \`cat /sys/block/sda/queue/hw_sector_size\`)"
vshell "sudo mount -v -o offset=\$START -t ext4 $IMG_PATH /mnt/img/linux"
}
function update_data {
# Move the cross-compiled kernel and modules to the pi
vshell "sudo cp $LINUX_DIR/arch/arm/boot/Image /mnt/img/boot/kernel.img #> /dev/null"
vshell "sudo cp -r ../modules/lib/modules /mnt/img/linux/lib"
# comment this out for qemu
vshell "sudo sed -i 's/^/#/' /mnt/img/linux/etc/ld.so.preload"
# Update the qemu rules
vshell "sudo touch $QEMU_RULES"
vshell "sudo sh -c \"echo 'KERNEL==\\\"sda\\\", SYMLINK+=\\\"mmcblk0\\\"' >> $QEMU_RULES\"";
vshell "sudo sh -c \"echo 'KERNEL==\\\"sda?\\\", SYMLINK+=\\\"mmcblk0p%n\\\"' >> $QEMU_RULES\"";
vshell "sudo sh -c \"echo 'KERNEL==\\\"sda2\\\", SYMLINK+=\\\"root\\\"' >> $QEMU_RULES\"";
vshell "sudo sh -c \"echo \\\"cgroup_disable=memory\\\"\" >> /mnt/img/linux/boot/config.txt" # docker stuff
vshell "sudo umount /mnt/img/linux" # unmount
}
function build_docker_debs {
# Build custom pi-docker kernel patching debs in vagrant ubuntu box
git clone git@github.com:hypriot/rpi-kernel.git
cd rpi-kernel
vagrant up
cd -
}
# start the final raspberry pi image and specify a network mapping (5022->22)
function run_qemu {
# DEBUG BEGIN
# until I can get the kernel compiling, this is a way to use a tested prebuild
#curl -OL https://github.com/trojanspike/qbian/blob/master/qemu_boot/kernel-qemu?raw=true
#mv kernel-qemu* tmp/kernel-qemu
#
# (original [used kernel-qemu])
#qemu-system-arm -kernel tmp/kernel-qemu -cpu arm1176 -m 256 -M versatilepb \
# -no-reboot -serial stdio \
# -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
# -hda raspbian_latest.img -redir tcp:5022::22 # 2>/dev/null 1>/dev/null &
# DEBUG END
#(testing)
qemu-system-arm \
-kernel tmp/kernel7.img \
-cpu arm1176 \
-m 256 \
-M versatilepb \
-serial stdio \
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
-hda raspbian_latest.img \
-redir tcp:5022::22
}
#install_osx_deps
clean_deps
get_and_unzip_raspbian_image
vagrant_install_dependencies
mount_boot
copy_boot_files
mount_data
patch_kernel
build_kernel
#build_docker_debs # commented out for now.. this step takes forever and a day
#debug
##vshell "cd $LINUX_DIR" #debug
##vshell "sudo umount /mnt/img/boot" #debug (remove and uncomment below)
##vshell "sudo umount /mnt/img/linux" #debug (remove and uncomment below)
#debug
# run the created vagrant script
vagrant ssh -c "sudo -E bash /vagrant/vagrant_script.sh;"
run_qemu
#wait_for_ssh
# sudo raspi-config # select "Finish" on GUI.. any way to automate this?
# sudo apt-get -f install
# sudo apt-get -y update
# CREDITS:
# http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/
# http://xecdesign.com/compiling-a-kernel/
# http://superuser.com/questions/690060/how-to-enable-network-with-a-raspberry-pi-emulated-on-qemu
# http://sourabhbajaj.com/mac-setup/Vagrant/README.html
# http://stevef1uk.blogspot.co.uk/2014/06/here-be-dragons-how-to-cross-compile.html
# http://stevef1uk.blogspot.co.uk/2014/06/how-to-run-docker-on-raspberry-pi.html
# http://rpitc.blogspot.com/p/kernel-rebuild.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment