Created
April 17, 2026 22:22
-
-
Save peterhellberg/bc4e267276e1cd96534851b353b0a54a to your computer and use it in GitHub Desktop.
Run script for my alpine VM created with https://github.com/keygenqt/skill-qemu-alpine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env sh | |
| set -eu | |
| PORT="2224" | |
| SCRIPT="$0" | |
| while [ -L "$SCRIPT" ]; do | |
| DIR="$(cd -P "$(dirname "$SCRIPT")" && pwd)" | |
| SCRIPT="$(readlink "$SCRIPT")" | |
| case "$SCRIPT" in | |
| /*) ;; # absolute | |
| *) SCRIPT="$DIR/$SCRIPT" ;; | |
| esac | |
| done | |
| SCRIPT_DIR="$(cd -P "$(dirname "$SCRIPT")" && pwd)" | |
| DISK_NAME="$SCRIPT_DIR/alpine.x86_64.qcow2" | |
| [ -f "$DISK_NAME" ] || { | |
| echo "Disk not found: $DISK_NAME" | |
| exit 1 | |
| } | |
| if nc -z localhost "$PORT" 2>/dev/null; then | |
| echo "VM already running → connecting via SSH" | |
| exec ssh -p "$PORT" localhost | |
| fi | |
| echo "Starting the VM..." | |
| qemu-system-x86_64 -enable-kvm \ | |
| -machine pc \ | |
| -cpu host -smp cores=4 \ | |
| -m 8192 \ | |
| -display none \ | |
| -daemonize \ | |
| -drive if=virtio,id=hd,format=qcow2,file=$DISK_NAME \ | |
| -nic user,hostfwd=tcp::$PORT-:22,model=virtio \ | |
| -device virtio-rng-pci \ | |
| -rtc base=utc,clock=host | |
| echo "Waiting for SSH..." | |
| until ssh -p "$PORT" \ | |
| -o ConnectTimeout=1 \ | |
| -o StrictHostKeyChecking=no \ | |
| -o UserKnownHostsFile=/dev/null \ | |
| localhost true 2>/dev/null; do | |
| sleep 0.3 | |
| done | |
| echo "Connecting..." | |
| ssh -p $PORT localhost |
Author
Author
The disk image was created via the install.sh found at https://github.com/keygenqt/skill-qemu-alpine/tree/main/x86_64
#!/bin/bash
ALP_ARCH=x86_64
DISK_NAME="alpine.$ALP_ARCH.qcow2"
DISK_SIZE="8G"
#Get last release and iso file name
ALP_VIRT_LAST_ISO=$(wget -qO- "https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/$ALP_ARCH/latest-releases.yaml" | grep "iso: alpine-virt" | cut -d':' -f2- | tr -d '[:space:]')
ALP_VIRT_VERSION=$(echo $ALP_VIRT_LAST_ISO | cut -d'-' -f3)
#Display detected last-version of virt
echo "Last Alpine release for $ALP_ARCH version: $ALP_VIRT_VERSION iso:$ALP_VIRT_LAST_ISO"
echo "Starting download.."
URL_DISTR="https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/$ALP_ARCH/$ALP_VIRT_LAST_ISO"
# Download Alpine
if [ ! -f $(basename $URL_DISTR) ]; then
wget $URL_DISTR
fi
# Create qemu disc
if [ ! -f "$DISK_NAME" ]; then
qemu-img create -f qcow2 "$DISK_NAME" "$DISK_SIZE"
fi
# Run qemu
qemu-system-x86_64 -enable-kvm \
-machine pc \
-cpu host -smp cores=4 \
-m 2048 \
-cdrom $(basename $URL_DISTR) \
-drive format=qcow2,file=$DISK_NAME \
-nographic
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have this symlinked in my
$PATHasalpine, allowing me to both start the VM, and to SSH into it if already running.