Skip to content

Instantly share code, notes, and snippets.

@xucian
Forked from riblo/ovf2kvm.sh
Last active December 5, 2023 14:04
Show Gist options
  • Save xucian/11444ee0c0951acd1bc22c0e87283537 to your computer and use it in GitHub Desktop.
Save xucian/11444ee0c0951acd1bc22c0e87283537 to your computer and use it in GitHub Desktop.
Import VMware images (ovf based) to Proxmox VE
#!/bin/bash
# Script that imports VMware images (ovf based) to Proxmox VE
# Your VM_NAME needs to be the same for: VM_NAME (directory) and VM_NAME.ovf
set -euo pipefail
PROXMOX_USER=root
PROXMOX_PASS='???'
PROXMOX_HOST='???'
PROXMOX_PORT=22
PROXMOX_STORAGE='???'
VM_PATH=./
VM_NAME=${1:?VM_NAME not set as the first argument}
echo "Copying VM files to Proxmox..."
## Copy VM (-3 actually means "copy through this machine", so in case you're copying between 2 remote machines, this option is fine. it's the only way to see a decent progress bar, otherwise have to look at rsync: https://askubuntu.com/questions/17275/how-to-show-the-transfer-progress-and-speed-when-copying-files-with-cp#comment1916044_201250)
#sshpass -p $PROXMOX_PASS scp -3 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r -P$PROXMOX_PORT $VM_PATH/$VM_NAME $PROXMOX_USER@$PROXMOX_HOST:.
# Update: rsync to show progress
# Enabling rsync compression and disabling ssh's default compression, as per: https://unix.stackexchange.com/a/746573
# Update on compression: 3x speedup without it. Need to dump any compression
# Using aes128-ctr for faster encryption (we don't need any, but ssh requires it)
sshpass -p $PROXMOX_PASS rsync -a --progress -e "ssh -c aes128-ctr -p $PROXMOX_PORT -o Compression=no -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" $VM_PATH/$VM_NAME $PROXMOX_USER@$PROXMOX_HOST:.
# Check if VMID is provided as the second argument, if not fetch it from Proxmox. Doing this after copy so that we'll make sure other parallel executions of this script won't interfere
set +u
if [[ -z "$2" ]]; then
set -u
VMID=$(sshpass -p $PROXMOX_PASS ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p$PROXMOX_PORT $PROXMOX_USER@$PROXMOX_HOST "pvesh get /cluster/nextid")
else
set -u
VMID=$2
fi
echo "Importing VM to Proxmox..."
# Set additional options for Windows VMs
REMOTE_EXTRA_OPTS=""
if grep -q 'vmw:osType="windows' "$VM_PATH/$VM_NAME/$VM_NAME.ovf"; then
REMOTE_EXTRA_OPTS="--bios ovmf"
echo "Windows detected, will add extra opts: $REMOTE_EXTRA_OPTS"
fi
# Import and start VM
sshpass -p $PROXMOX_PASS ssh -A -v -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p$PROXMOX_PORT $PROXMOX_USER@$PROXMOX_HOST bash -s << END
cd $VM_NAME
qm importovf $VMID $VM_NAME.ovf $PROXMOX_STORAGE
# Major fix 04.12.2023 @ xucian: Only -scsihw virtio-scsi-pci seems to work
#qm set $VMID -scsihw pvscsi -net0 model=vmxnet3,bridge=vmbr0
qm set $VMID -scsihw virtio-scsi-pci -net0 model=vmxnet3,bridge=vmbr0 $EXTRA_OPTS
qm start $VMID
END
# Print a message indicating the completion of the import process
echo "VM import completed"
# Remove VM files (uncomment if you want to delete VM files after import)
# if [ "$?" -eq 0 ]; then
# rm -rf $HOME/$VM_NAME
# fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment