-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmctl.sh
166 lines (159 loc) · 4.79 KB
/
vmctl.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
# variables
declare HOST # whether a localhost or remote
declare VM_LIST # list of vm
declare VM_COUNT=0 # total count of vm available
declare VM_NAME # selected vm name
declare VM_STATUS # contain status of selected vm
declare SNAPSHOT_COUNT=0 # total count of snapshot
declare SNAPSHOT_LIST # list of snapshot
declare SNAPSHOT_NAME # selected snapshot name
declare RESULT # temp variable to store result of command executed
GET_VM_LIST()
{
# function to populate vm list
RESULT=$(ssh $HOST -C "virsh -c qemu:///system list --all --name | tr -s '\n' ' '")
for VM in $RESULT
do
VM_LIST[VM_COUNT]=$VM_COUNT" "$VM
((VM_COUNT=VM_COUNT+1))
done
}
GET_VM_STATUS()
{
# function to get vm status
VM_NAME=$(echo ${VM_LIST[$RESULT]} | cut -d ' ' -f2)
VM_STATUS=$(ssh $HOST -C "virsh -c qemu:///system domstate $VM_NAME | tr -d '\n'")
}
GET_SNAPSHOT_LIST()
{
# function to populate snapshot list
RESULT=$(ssh $HOST -C "virsh -c qemu:///system snapshot-list $VM_NAME --name | tr -s '\n' ' '")
for SNAPSHOT in $RESULT
do
SNAPSHOT_LIST[SNAPSHOT_COUNT]=$SNAPSHOT_COUNT" "$SNAPSHOT
((SNAPSHOT_COUNT=SNAPSHOT_COUNT+1))
done
}
MANAGE_VM_STATE()
{
case $(whiptail --title "VM Control: $HOST" --menu "Manage VM State: $VM_NAME : $VM_STATUS" 30 60 10 \
"1." "PowerON" \
"2." "Shutdown" \
"3." "Reboot" \
"4." "ForceOFF" 3>&1 1>&2 2>&3) in
'1.')
# PowerON VM
RESULT=$(ssh $HOST -C "virsh -c qemu:///system start $VM_NAME")
whiptail --title "Status" --msgbox "$RESULT" 8 60
;;
'2.')
# Shutdown VM
RESULT=$(ssh $HOST -C "virsh -c qemu:///system shutdown $VM_NAME")
whiptail --title "Status" --msgbox "$RESULT" 8 60
;;
'3.')
# Reboot VM
RESULT=$(ssh $HOST -C "virsh -c qemu:///system reboot $VM_NAME")
whiptail --title "Status" --msgbox "$RESULT" 8 60
;;
'4.')
# ForceOff VM
RESULT=$(ssh $HOST -C "virsh -c qemu:///system destroy $VM_NAME")
whiptail --title "Status" --msgbox "$RESULT" 8 60
;;
esac
}
MANAGE_VM_SNAPSHOT()
{
SNAPSHOT_COUNT=0
SNAPSHOT_LIST=()
GET_SNAPSHOT_LIST
case $(whiptail --title "VM Control: $HOST" --menu "Manage Snapshot: $VM_NAME : $VM_STATUS" 30 60 10 \
"1." "Create" \
"2." "Restore" \
"3." "Delete" 3>&1 1>&2 2>&3) in
'1.')
# create a snapshot
SNAPSHOT_NAME=$(whiptail --inputbox "Enter VM Snapshot Name: " 30 60 --title "Create Snapshot: $VM_NAME : $VM_STATUS" 3>&1 1>&2 2>&3)
if [ ! -z $SNAPSHOT_NAME ]
then
RESULT=$(ssh $HOST -C "virsh -c qemu:///system snapshot-create-as $VM_NAME --name $SNAPSHOT_NAME")
whiptail --title "Status" --msgbox "$RESULT" 8 60
fi
;;
'2.')
# restore a snapshot
RESULT=$(whiptail --title "VM Control: $HOST" --menu "Manage Snapshot: $VM_NAME : $VM_STATUS" 30 60 10 ${SNAPSHOT_LIST[*]} 3>&1 1>&2 2>&3)
SNAPSHOT_NAME=$(echo ${SNAPSHOT_LIST[$RESULT]} | cut -d ' ' -f2)
RESULT=$(ssh $HOST -C "virsh -c qemu:///system snapshot-revert $VM_NAME --snapshotname $SNAPSHOT_NAME")
whiptail --title "Status" --msgbox "The domain $VM_NAME is being restored." 8 60
;;
'3.')
# delete a snapshot
RESULT=$(whiptail --title "VM Control: $HOST" --menu "Manage Snapshot: $VM_NAME : $VM_STATUS" 30 60 10 ${SNAPSHOT_LIST[*]} 3>&1 1>&2 2>&3)
if [ ! -z $RESULT ]
then
SNAPSHOT_NAME=$(echo ${SNAPSHOT_LIST[$RESULT]} | cut -d ' ' -f2)
RESULT=$(ssh $HOST -C "virsh -c qemu:///system snapshot-delete $VM_NAME --snapshotname $SNAPSHOT_NAME")
whiptail --title "Status" --msgbox "$RESULT" 8 60
fi
;;
esac
}
MAIN()
{
# MAIN MENU
while true
do
# display list of vm and store selected vm in RESULT
RESULT=$(whiptail --title "VM Control: $HOST" --menu "Select a VM" 30 60 10 ${VM_LIST[*]} 3>&1 1>&2 2>&3)
if [ -z $RESULT ]
then
# nothing in RESULT then exit
exit 0
else
# function call to get VM status
GET_VM_STATUS
case $(whiptail --title "VM Control: $HOST" --menu "Manage VM: $VM_NAME : $VM_STATUS" 30 60 10 \
"1." "Manage VM State: PowerON/Shutdown/Reboot/ForceOFF" \
"2." "Manage VM Snapshot: Create/Restore/Delete" 3>&1 1>&2 2>&3) in
'1.')
MANAGE_VM_STATE
;;
'2.')
MANAGE_VM_SNAPSHOT
;;
esac
fi
done
}
HELP()
{
echo "Usage: $0 (FOR MANAGING LOCAL VM ONLY)"
echo "Usage: $0 [user@[FQDN/IP]] (FOR MANAGING REMOTE VM)"
echo "Dependencies: [Warning]"
echo " Machine on which this tool is running requires: whiptail & libvirt-clients"
echo " Remote Machine on which this tool will connect to only need libvirt-clients"
echo "Description: "
echo " VMCTL is a TUI based tool which will help you to manage local and remote vm using ssh, virsh, whiptail."
echo " It is preferred to use password-less authentication in order to prevent entering ssh password multiple times."
}
if [ $# -eq 0 ]
then
HOST=localhost
GET_VM_LIST
MAIN
elif [ $# -eq 1 ]
then
if [ $1 == '--help' ] || [ $1 == 'help' ]
then
HELP
else
HOST=$1
GET_VM_LIST
MAIN
fi
else
HELP
fi