Skip to content

Instantly share code, notes, and snippets.

@tomdavidson
Created April 8, 2025 02:11
Show Gist options
  • Save tomdavidson/5e8ecd1f83cb7ff668b94fe9f930828f to your computer and use it in GitHub Desktop.
Save tomdavidson/5e8ecd1f83cb7ff668b94fe9f930828f to your computer and use it in GitHub Desktop.
Run a vm kata style but with a full kernel.
#!/bin/bash
# qemu-run - A Podman-like interface for QEMU
#
# podman run --rm \
# --runtime=kata \
# -v "$PWD:/mnt/host:Z" \
# alpine sh -c \
# 'ls -la /mnt/host > /mnt/host/ls.txt'
#
# ./qemu-run alpine -v "$PWD:/mnt/host" 'ls -la /mnt/host > /mnt/host/ls.txt'
# Parse arguments
IMAGE="$1"
shift
VOLUME=""
CMD=""
while [ $# -gt 0 ]; do
case "$1" in
-v|--volume)
VOLUME="$2"
shift 2
;;
*)
CMD="$1"
shift
break
;;
esac
done
# Parse volume mapping
HOST_PATH=$(echo "$VOLUME" | cut -d: -f1)
GUEST_PATH=$(echo "$VOLUME" | cut -d: -f2 | cut -d: -f1)
# Create cloud-init config
cat > user-data <<EOF
#cloud-config
runcmd:
- mkdir -p $GUEST_PATH
- mount -t 9p -o trans=virtio hostdir $GUEST_PATH
- $CMD
- sync
- poweroff
EOF
# Create cloud-init ISO
cloud-localds seed.iso user-data
# Run VM
qemu-system-x86_64 \
-m 512 \
-nographic \
-drive file=$IMAGE.qcow2,if=virtio \
-drive file=seed.iso,if=virtio \
-virtfs local,path="$HOST_PATH",mount_tag=hostdir,security_model=mapped,id=hostshare
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment