Mass migrate VM disks?

cshill

Member
May 8, 2024
87
12
8
Good Morning/Evening,

I didn't think I'd ever have to but I have several VMs running on one physical disk that I want to move to another datastore on another disk. I can move the storage one at a time through the web ui but I was wondering if anyone knows of a faster method? This will be especially a pain in the butt if I am using thick provisioning as it's mega slow.
 
I'm not very good with scripting so I did make this via chatgpt but I've tested it and it works. Just replace the vmid's with the ones you have and the target storage name. There's also a line at the bottom referencing --delete, if you want to keep the old disks on the other drive just remove --delete.

TARGET_STORAGE="Datastore2"
DISK_KEYS="virtio scsi sata ide efidisk tpmstate"
IGNORE_STORES="Datastore1 backup-usb"

# Manually define the VMIDs to process:
VMID_LIST="103 101 102" # Add your VM IDs here

for vmid in $VMID_LIST; do
echo "Processing VM $vmid..."

for key in $DISK_KEYS; do
for entry in $(qm config $vmid | grep "^$key" | cut -d':' -f1); do
line=$(qm config $vmid | grep "^$entry")
source_store=$(echo "$line" | cut -d':' -f2)

if echo "$IGNORE_STORES" | grep -qw "$source_store"; then
echo " Skipping $entry (on ignored storage $source_store)"
continue
fi

echo " Moving $entry of VM $vmid from $source_store to $TARGET_STORAGE..."
qm move-disk $vmid $entry $TARGET_STORAGE --format qcow2 --delete
done
done

echo "Done with VM $vmid"
done
 
Last edited: