bash-скрипт для управления виртуальными машинами VirtualBox. В процессе написания...

  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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
#VAR
ISO_LOCATION=/home/qiwichupa/ISO/
################################
# == functions ==
# HEADS
function mainHead {
clear
echo "=================================="
echo "========== Main options =========="
echo "=================================="
echo ""
}
function newVmHead {
clear
echo "=================================="
echo "======== Creating new VM ========="
echo "=================================="
echo ""
}
function listHead {
clear
echo "=================================="
echo "========= Your machines =========="
echo "=================================="
echo ""
}
function helpHead {
clear
echo "=================================="
echo "============== HELP =============="
echo "=================================="
echo ""
}
# creating CD selector
function cdSelector {
# mystical action with iso list
# http://www.linux.org.ru/view-message.jsp?msgid=3128616#comment-3132277
temp=$IFS
IFS=$'\n'
ls=$(ls -1 "$ISO_LOCATION")
arrFILES=($(echo "$ls"))
IFS=$temp
for line in "${arrFILES[@]}"; do
tmp="$tmp"$(echo "\"""$line""\""" ")
done
# mystical menu building
# http://www.linuxquestions.org/questions/programming-9/bash-script-using-select-to-show-multi-word-options-like-option-1o-317671/#post1613089
PS3='Choose ISO: '
eval set $tmp
select opt in "$@"
do
echo $opt
VM_CD=$opt
break
done
}
# MAIN FUNCTIONS
# registred machines
function list {
listHead
VBoxManage list vms | grep \"| awk '{print $1}'
echo ""
}
# main level actions
function mainact {
echo "Enter action, '?' for action list or 'x' for exit."
echo -n " act sel > "
read ACT
}
# help
function help {
helpHead
echo "list — show the list of machines"
echo "new — create new machine"
echo "del — delete machine"
echo "start — start machine"
echo "stop — stop machine"
echo ""
}
# creating new VM
function new {
newVmHead
echo "Machines:"
VBoxManage list vms | grep \"| awk '{print $1}'
echo ""
echo "Enter new machine name."
echo -n " new, step 1 - name > "
read VM_NAME # vm name
NAME_TEST=$(VBoxManage list vms | grep "$VM_NAME") # check registred names
if [ -z "$NAME_TEST" ]; then # if unique...
newVmHead
echo "Enter memory size (Mb)."
echo -n " new, step 2 - memory > "
read VM_MEM # vm memory size
newVmHead
echo "Enter HardDrive size (Mb) or leave blank to skip HD creating."
echo -n " new, step 3 - HardDrive > "
read VM_HD # vm hd size
newVmHead
echo "ISO location: $ISO_LOCATION"
echo "Available ISOs:"
cdSelector
clear # if correct...
echo "========================="
echo "=======Creating VM======="
echo "========================="
VM_MEM=$(echo "$VM_MEM""MB")
VM_HD_NAME=$(echo "$VM_NAME"".vdi")
# creating vm
VBoxManage createvm -name "$VM_NAME" -register
# setup memory size, acpi, boot device, network options
VBoxManage modifyvm "$VM_NAME" -memory "$VM_MEM" -acpi on -boot1 dvd -nic1 bridged --bridgeadapter1 eth0
if [ -z "$VM_HD" ]; then # if HD size is not set
echo "Without HardDrive..."
else # else...
# creating HD and adding into VM
VBoxManage createvdi -filename "$VM_HD_NAME" -size "$VM_HD" -register
VBoxManage modifyvm "$VM_NAME" -hda "$VM_HD_NAME"
fi
# registring CD and adding into VM
FULL_CD=$(echo $ISO_LOCATION$VM_CD)
echo $FULL_CD
VBoxManage registerimage dvd "$FULL_CD"
VBoxManage modifyvm "$VM_NAME" -dvd "$FULL_CD"
else # if name is not unique
clear
list
echo "Error: choose another name"
break
fi
}
################################
# == main level ==
mainHead
while [ true ]
do
mainact
case $ACT in
'list')
list
;;
'?')
help
;;
'x')
clear
exit
;;
'new')
new
;;
'')
mainHead
;;
esac
done