#!/bin/bash
# qemu-sync - sync RAM qcow2 back to disk
# usage: ./qemu-sync [disk.qcow2]

set -e

DISK="${1:-disk.qcow2}"       # default se non passi nulla
RAMDISK="/dev/shm/$(basename "$DISK")"

if [ ! -f "$RAMDISK" ]; then
  echo "❌ RAM copy not found: $RAMDISK"
  exit 1
fi

cp "$DISK" "${DISK}.bak"
echo "💾 Saving RAM copy as new $DISK"
rsync -a --inplace "$RAMDISK" "$DISK"
echo "✅ Sync complete"
