#!/bin/bash


if [[ $# != 3 ]]; then
  cat <<-END >&2
usage: $0 old-id new-id pool/subvol
END
  exit 1
fi

old_id=$1
new_id=$2
POOL=$3


if ! disks=$(zfs list -r -o name $POOL | grep "vm-${old_id}-disk"); then
  echo "did not find any disks, check old vm id and running zfs" >&2
  exit 1
fi

for disk in $disks; do
  new_disk=$(echo $disk | sed "s/vm-${old_id}-disk/vm-${new_id}-disk/g")
  zfs rename $disk $new_disk
done

sed -i "s/vm-${old_id}-disk/vm-${new_id}-disk/g" /etc/pve/qemu-server/${old_id}.conf
mv /etc/pve/qemu-server/${old_id}.conf /etc/pve/qemu-server/${new_id}.conf
