SOLVED How start VM from shell or script?

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399
You'll need the VM ID of the VM. Run midclt call vm.query | jq to find the ID of the VM. To start the VM, run midclt call vm.start <ID>.
 

sretalla

Powered by Neutrality
Moderator
Joined
Jan 1, 2016
Messages
9,703
curl --user root:password -X POST -H "Content-Type: application/json" http://freenas.local/api/v2.0/vm/id/30/restart where 30 is the ID of the VM you want to restart/start/stop/poweroff/shutdown.

You can find the ID by doing this:
curl --user root:password -X GET -H "Content-Type: application/json" http://freenas.local/api/v2.0/vm

Of course you can find the example here:

including many different scripting/programming language examples.

Or @Samuel Tai 's example works too if you're running the script locally.
 

blanchet

Guru
Joined
Apr 17, 2018
Messages
516
This is a script to start VM by name

Code:
#!/usr/local/bin/bash
# start VM by name
# platform: TrueNAS-12

PATH=/bin:/usr/bin:/usr/local/bin

if [ $# -eq 0 ] ; then
    echo "Syntax: `basename $0` vmname"
    exit 0
fi

# get VM name
VMNAME=$1

# get VM ID
ID=`midclt call vm.query |
     jq --raw-output '[.[] | { (.name): .id } ]' |
     grep $VMNAME |
     sed 's/.*\:\(.*\)/\1/'`

echo "Start '$VMNAME': id=$ID"
midclt call vm.start $ID
 
Last edited:
Top