创建虚拟机脚本
#!/bin/bash
. /etc/init.d/functions
CONF_DIR=/etc/libvirt/qemu
IMG_DIR=/var/lib/libvirt/images
BASEVM=node_tedu
createvm(){
qemu-img create -f qcow2 -b
I
M
G
D
I
R
/
.
{IMG_DIR}/.
IMGDIR/.{BASEVM}.qcow2
I
M
G
D
I
R
/
IMG_DIR/
IMGDIR/{1}.img 30G &> /dev/null
sed “s/
B
A
S
E
V
M
/
{BASEVM}/
BASEVM/{1}/”
I
M
G
D
I
R
/
.
{IMG_DIR}/.
IMGDIR/.{BASEVM}.xml >
C
O
N
F
D
I
R
/
{CONF_DIR}/
CONFDIR/{1}.xml
sudo virsh define
C
O
N
F
D
I
R
/
{CONF_DIR}/
CONFDIR/{1}.xml &> /dev/null
echo_success
echo “VM ${1} Create”
}
read -p "Enter VM number: " VMNUM
if [ -z $VMNUM ];then
echo Please enter parameters
exit
fi
if [ -z “${VMNUM}” ]; then
echo “You must input a number.”
exit 65
elif [ $(echo ${VMNUM}*1 | bc) = 0 ]; then
echo “You must input a number.”
exit 66
elif [ ${VMNUM} -lt 1 -o ${VMNUM} -gt 99 ]; then
echo “Input out of range”
exit 67
fi
if [
V
M
N
U
M
−
l
e
9
]
;
t
h
e
n
V
M
N
U
M
=
0
{VMNUM} -le 9 ];then VMNUM=0
VMNUM−le9];thenVMNUM=0{VMNUM}
fi
if [ $USER == student ];then
NEWVM=tedu_node
elif [
U
S
E
R
=
=
w
e
e
k
e
n
d
]
;
t
h
e
n
N
E
W
V
M
=
t
m
o
o
c
n
o
d
e
e
l
s
e
N
E
W
V
M
=
t
e
d
u
n
o
d
e
f
i
v
m
n
a
m
e
=
USER == weekend ];then NEWVM=tmooc_node else NEWVM=tedu_node fi vm_name=
USER==weekend];thenNEWVM=tmoocnodeelseNEWVM=tedunodefivmname={NEWVM}${VMNUM}
if [ -e
I
M
G
D
I
R
/
IMG_DIR/
IMGDIR/{vm_name}.img ]; then
echo_warning
echo “vm ${vm_name} is exists”
exit 38
else
createvm ${vm_name}
fi
~