parabola-vmbootstrap/start.sh

95 lines
3.5 KiB
Bash
Raw Normal View History

#!/bin/bash
2017-12-31 22:58:55 +01:00
##############################################################################
# parabola-arm-imagebuilder #
# #
# Copyright (C) 2017 Andreas Grapentin #
# #
# 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/>. #
##############################################################################
2017-12-28 16:09:01 +01:00
set -eu
2017-12-28 16:09:01 +01:00
_builddir=build
2018-02-01 10:54:30 +01:00
mkdir -p "$_builddir"
2017-12-28 16:09:01 +01:00
_imagefile=$1
2018-02-01 10:54:30 +01:00
_pidfile="$_builddir"/qemu-$$.pid
_bootdir="$_builddir"/boot-$$
2017-12-28 16:09:01 +01:00
2018-02-01 10:54:30 +01:00
_loopdev=$(sudo losetup -f --show "$_imagefile")
sudo partprobe $_loopdev
2018-02-01 10:54:30 +01:00
touch "$_pidfile"
2017-12-28 16:09:01 +01:00
# register a cleanup error handler
function cleanup {
2018-02-01 10:54:30 +01:00
test -f "$_pidfile" && (sudo kill -9 $(cat "$_pidfile") || true)
rm -f "$_pidfile"
sudo umount ${_loopdev}p1
sudo losetup -d $_loopdev
2018-02-01 10:54:30 +01:00
rm -rf "$_bootdir"
}
2017-12-28 16:09:01 +01:00
trap cleanup ERR
2017-12-28 16:09:01 +01:00
# start the VM
2018-02-01 10:54:30 +01:00
mkdir -p "$_bootdir"
sudo mount ${_loopdev}p1 "$_bootdir"
_board="vexpress-a9"
# FIXME: archlinuxarm rust SIGILLs on cortex-a9 cpus, using cortex-a15 for now
_cpu="cortex-a15"
_memory="1G"
_snapshot=""
[ -z "${PERSISTENT:-}" ] && _snapshot="-snapshot"
_daemonize="-nographic -serial mon:stdio"
[ -z "${FOREGROUND:-}" ] && _daemonize="-daemonize -pidfile \"$_pidfile\" -net user,hostfwd=tcp::2022-:22 -net nic -display none"
if [ -f "$_bootdir"/zImage ]; then
_kernel="$_bootdir"/zImage
_dtb="$_bootdir"/dtbs/vexpress-v2p-ca9.dtb
_initrd="$_bootdir"/initramfs-linux.img
else
_kernel="$_bootdir"/vmlinuz-linux-libre
_dtb="$_bootdir"/dtbs/linux-libre/vexpress-v2p-ca9.dtb
_initrd="$_bootdir"/initramfs-linux-libre.img
fi
2017-12-28 16:09:01 +01:00
QEMU_AUDIO_DRV=none qemu-system-arm \
2018-02-01 10:54:30 +01:00
-M $_board \
-cpu $_cpu \
-m $_memory \
-kernel "$_kernel" \
-dtb "$_dtb" \
-initrd "$_initrd" \
2018-01-31 13:10:24 +01:00
--append "root=/dev/mmcblk0p3 rw roottype=ext4 console=ttyAMA0" \
2018-02-01 10:54:30 +01:00
-drive if=sd,driver=raw,cache=writeback,file="$_imagefile" \
$_daemonize \
$_snapshot
2018-01-31 13:10:24 +01:00
2018-02-01 10:54:30 +01:00
if [ -z "${FOREGROUND:-}" ]; then
2018-01-31 13:10:24 +01:00
# wait for ssh to be up
2018-02-01 10:54:30 +01:00
_sshopts="-o StrictHostKeyChecking=no -o ConnectTimeout=5"
while ! ssh -p 2022 -i keys/id_rsa root@localhost $_sshopts true 2>/dev/null; do
echo -n . && sleep 5
done && echo
2017-12-28 16:09:01 +01:00
2018-02-01 10:54:30 +01:00
# open a session
ssh -p 2022 -i keys/id_rsa parabola@localhost
2017-12-28 16:09:01 +01:00
2018-02-01 10:54:30 +01:00
# shutdown the VM
ssh -p 2022 -i keys/id_rsa root@localhost "nohup shutdown -h now &>/dev/null & exit"
while sudo kill -0 $(cat "$_pidfile") 2> /dev/null; do echo -n . && sleep 5; done && echo
fi
2017-12-28 16:09:01 +01:00
# cleanup
sudo umount ${_loopdev}p1
sudo losetup -d $_loopdev
2018-02-01 10:54:30 +01:00
rm -rf "$_bootdir" "$_pidfile"