-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_vm
executable file
·120 lines (101 loc) · 4.14 KB
/
create_vm
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
#!/usr/bin/env bash
# make SURE httpd is installed and running
# yum install -y qemu-kvm qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer httpd
# download boot.iso curl http://mirror.centos.org/centos/7/os/x86_64/images/boot.iso -o /srv/boot.iso
DOMAIN="mynet.net.org"
WWW_LOCATION="/var/www/html/"
if [ $# -eq 1 ]
then
VM_NAME=$1
else
echo -en "Enter vm name: "
read VM_NAME
fi
CHANGE_NS_DEFAULT='8.8.8.8'
CHANGE_GATE_DEFAULT='192.168.122.1'
# Update kickstart file
echo -en "FQDN. default ${VM_NAME}.$DOMAIN : "
read FQDN ; if [[ -z $FQDN ]] ; then FQDN=${VM_NAME}.$DOMAIN ; fi
echo "FQDN: $FQDN"
echo -en "Enter IP Address, must be valid IP: "
read IP_ADDRESS ; if ipcalc -c $IP_ADDRESS 2>/dev/null ; then echo "IP_ADDRESS: $IP_ADDRESS" ; else echo "$IP_ADDRESS invalid IP adress" ; exit 1 ; fi
echo -en "Enter Netmask, default 255.255.255.0: "
read CHANGE_NETMASK ; if [[ -z $CHANGE_NETMASK ]] ; then CHANGE_NETMASK=255.255.255.0 ; fi
echo "NETMASK: $CHANGE_NETMASK"
echo -en "Enter GW, default $CHANGE_GATE_DEFAULT: "
read CHANGE_GATE ; if [[ -z $CHANGE_GATE ]] ; then CHANGE_GATE=$CHANGE_GATE_DEFAULT ; fi
if ipcalc -c $CHANGE_GATE 2>/dev/null ; then echo "Default GW: $CHANGE_GATE" ; else echo "$CHANGE_GATE invalid IP adress" ; exit 1 ; fi
echo -en "Enter DNS, default $CHANGE_NS_DEFAULT: "
read CHANGE_NS ; if [[ -z $CHANGE_NS ]] ; then CHANGE_NS=$CHANGE_NS_DEFAULT ; fi
if ipcalc -c $CHANGE_NS 2>/dev/null ; then echo "DNS: $CHANGE_NS" ; else echo "$CHANGE_NS invalid IP adress" ; exit 1 ; fi
sudo /bin/cp -f /srv/vm_ks_template.ks ${WWW_LOCATION}/vm_ks.ks
echo "CHANGE FQDN to $FQDN"
sudo sed -i 's/CHANGE_HOSTNAME/'$FQDN'/g' ${WWW_LOCATION}/vm_ks.ks
echo "CHANGE IP_ADDRESS to $IP_ADDRESS"
sudo sed -i 's/CHANGE_IP/'$IP_ADDRESS'/g' ${WWW_LOCATION}/vm_ks.ks
echo "CHANGE CHANGE_GATE to $CHANGE_GATE"
sudo sed -i 's/CHANGE_GATE/'$CHANGE_GATE'/g' ${WWW_LOCATION}/vm_ks.ks
echo "CHANGE NETMASK to $CHANGE_NETMASK"
sudo sed -i 's/CHANGE_NETMASK/'$CHANGE_NETMASK'/g' ${WWW_LOCATION}/vm_ks.ks
echo "CHANGE NS SERVERS to $CHANGE_NS"
sudo sed -i 's/CHANGE_NS/'$CHANGE_NS'/g' ${WWW_LOCATION}/vm_ks.ks
## Pre-defined variables
echo ""
MEM_SIZE_DEFAULT=2048 # DEFAULT RHEL MINIMUM
VCPUS_DEFAULT=1
OS_VARIANT_DEFAULT="rhel7"
ISO_FILE="/srv/boot.iso"
echo -en "Enter MEM_SIZE, default $MEM_SIZE_DEFAULT: "
read MEM_SIZE ; if [[ -z $MEM_SIZE ]] ; then MEM_SIZE=$MEM_SIZE_DEFAULT ; fi
echo -en "Enter VCPUS, default $VCPUS_DEFAULT: "
read VCPUS ; if [[ -z $VCPUS ]] ; then VCPUS=$VCPUS_DEFAULT ; fi
echo -en "Enter OS_VARIANT, default $OS_VARIANT_DEFAULT: "
read OS_VARIANT ; if [[ -z $OS_VARIANT ]] ; then OS_VARIANT=$OS_VARIANT_DEFAULT ; fi
if [[ -f $ISO_FILE ]] ;
then
echo "disk ok"
else
echo "missing boot.iso, run curl http://mirror.centos.org/centos/7/os/x86_64/images/boot.iso -O"
exit 1
fi
OS_TYPE="linux"
echo -en "Enter virtual disk size in GB:"
read DISK_SIZE ; if [[ $DISK_SIZE -gt 0 ]] ; then echo "DISK SIZE ${DISK_SIZE}G" ; else echo "DISK_SIZE must be valid INT" ; exit 1 ; fi
echo "Running virt-install with following params \
sudo virt-install \
--name ${VM_NAME} \
--memory=${MEM_SIZE} \
--vcpus=${VCPUS} \
--os-type ${OS_TYPE} \
--location ${ISO_FILE} \
--disk size=${DISK_SIZE} \
--network bridge=virbr0 \
--graphics=none \
--os-variant=${OS_VARIANT} \
--console pty,target_type=serial \
-x 'console=ttyS0,115200n8 serial' \
-x "ks=http://$CHANGE_GATE/vm_ks.ks" \
-x "ip=$IP_ADDRESS"\
-x "netmask=$CHANGE_NETMASK"\
-x "gateway=$CHANGE_GATE"\
-x "dns=$CHANGE_NS"\
-x "biosdevname=0 net.ifnames=0"
"
sudo virt-install \
--name ${VM_NAME} \
--memory=${MEM_SIZE} \
--vcpus=${VCPUS} \
--os-type ${OS_TYPE} \
--location ${ISO_FILE} \
--disk size=${DISK_SIZE} \
--network bridge=virbr0 \
--graphics=none \
--os-variant=${OS_VARIANT} \
--console pty,target_type=serial \
-x 'console=ttyS0,115200n8 serial' \
-x "ks=http://$CHANGE_GATE/vm_ks.ks" \
-x "ip=$IP_ADDRESS"\
-x "netmask=$CHANGE_NETMASK"\
-x "gateway=$CHANGE_GATE"\
-x "dns=$CHANGE_NS"\
-x "biosdevname=0 net.ifnames=0"