forked from 3wordchant/capsul-flask
41 lines
1.3 KiB
Bash
41 lines
1.3 KiB
Bash
|
#!/bin/sh -e
|
||
|
#
|
||
|
# start.sh - starts a capsul
|
||
|
# if it is already started, do nothing
|
||
|
# if it is stopping, do nothing
|
||
|
|
||
|
# State Key
|
||
|
# NOSTATE: 0 - no state
|
||
|
# RUNNING: 1 - the domain is running
|
||
|
# BLOCKED: 2 - the domain is blocked on resource
|
||
|
# PAUSED: 3 - the domain is paused by user
|
||
|
# SHUTDOWN: 4 - the domain is being shut down
|
||
|
# SHUTOFF: 5 - the domain is shut off
|
||
|
# CRASHED: 6 - the domain is crashed
|
||
|
# PMSUSPENDED: 7 - the domain is suspended by guest power management
|
||
|
# LAST: 8 NB: this enum value will increase over time as new events are added to the libvirt API. It reflects the last state supported by this version of the libvirt API.
|
||
|
|
||
|
vmname="$1"
|
||
|
|
||
|
[ "$vmname" = '' ] && printf 'you must set $vmname\n' && exit 1
|
||
|
|
||
|
if printf "$vmname" | grep -vqE '^(cvm|capsul)-[a-z0-9]{10}$'; then
|
||
|
printf 'vmname %s must match ^capsul-[a-z0-9]{10}$\n' "$vmname"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
state="$(virsh domstats $vmname | grep state.state | cut -d '=' -f 2)"
|
||
|
|
||
|
[ "$state" = '' ] && printf 'state was not detected. must match ^[0-8]$\n' && exit 1
|
||
|
|
||
|
if printf "$state" | grep -vqE '^[0-8]$'; then
|
||
|
printf 'state %s must match ^[0-8]$\n' "$state"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
case "$state" in
|
||
|
1) printf '%s is already running\n' "$vmname" ;;
|
||
|
4) printf '%s cannot be started while it is shutting down\n' "$vmname" ;;
|
||
|
[235-7]) virsh start "$vmname" > /dev/null ;;
|
||
|
esac
|