#!/bin/bash # Capture a screenshot of a libvirt VM and convert to PNG for viewing. # Usage: vm-screenshot.sh [VM_NAME] [OUTPUT_PATH] VM_NAME="${1:-lab-pxe-test}" OUTPUT="${2:-/tmp/vm-screenshot.png}" PPM="/tmp/vm-screenshot-$$.ppm" if ! sudo virsh domstate "$VM_NAME" &>/dev/null; then echo "ERROR: VM '$VM_NAME' not found or not running" >&2 exit 1 fi sudo virsh screenshot "$VM_NAME" "$PPM" --screen 0 2>/dev/null if [ ! -f "$PPM" ]; then echo "ERROR: screenshot failed" >&2 exit 1 fi # Convert to PNG (ppm -> png) if command -v convert &>/dev/null; then convert "$PPM" "$OUTPUT" elif command -v ffmpeg &>/dev/null; then ffmpeg -y -i "$PPM" "$OUTPUT" 2>/dev/null elif command -v pnmtopng &>/dev/null; then pnmtopng "$PPM" > "$OUTPUT" else # fallback: just copy the PPM (Read tool can handle it) cp "$PPM" "${OUTPUT%.png}.ppm" OUTPUT="${OUTPUT%.png}.ppm" fi rm -f "$PPM" echo "$OUTPUT"