-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm_install.sh
158 lines (119 loc) · 3.06 KB
/
vm_install.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
#!/bin/bash
# [2023021601]
#
# Script to provision a new VM
#
# VM configuration, should be saved separately in a different file.
#
vm_n=2
vm_i=171
ip='192.168.1.%d'
netmask='255.255.255.0'
gateway='192.168.1.254'
nameserver='1.1.1.1'
hostname="centos-%3d"
root_pw='password'
user_name='admin'
user_pw='password'
#
# VM settings
name="CentOS-%d"
title="CentOS-%d (.%d)"
#
# Common settings of all VM's
#
# Use `virt-install --osinfo list` to get the list of the accepted OS variants.
#
osname="centos7"
cdrom="/home/terence/Downloads/CentOS-7-x86_64-DVD-2009.iso"
path="/var/lib/libvirt/images/%s.qcow2"
# hardware resources
#
sockets=1
cores=1
ram=1024 # 2048 MB
disk=10 # 20 GB
autostart="1"
# Use `virt-install --network=?` to see a list of all available sub options.
#
net_type="direct"
net_source="eno2" # mode=bridge assumed
net_model="virtio"
if [ ! -e /sys/class/net/$net_source ]; then
echo "$net_source: host interface not found, exiting.."
exit 1
fi
network="type=$net_type,source=$net_source,source.mode=bridge,model=$net_model"
#
# unattended installation
#
ks_dir="ks_cfg"
ks_file="centos7-%3d.ks"
ks_template="centos7.ks"
# user_login="admin"
# user_password_file="/root/bin/kvm/pass.txt"
product_key="" # Windows 10
# --------------------------------------------------------------------------------
#
vcpus="sockets=$sockets,cores=$cores"
if [ -n "$autostart" ]; then
autostart="--autostart"
fi
vm_id=$vm_i
for ((i = 0; i < $vm_n; i++)); do
vm_name=`printf "$name" $vm_id`
vm_title=`printf "$title" $vm_id $vm_id`
vm_path=`printf "$path" $vm_name`
vm_ks_file=`printf "$ks_file" $vm_id`
vm_ks_path="$ks_dir/$vm_ks_file"
vm_hostname=`printf "$hostname" $vm_id`
vm_ip=`printf "$ip" $vm_id`
# check if the VM file exists (has been provisioned)
#
if [ -e "$vm_path" ]; then
echo "$vm_path: VM found, skipping to the next one.."
continue
fi
if [ ! -e "$ks_template" ]; then
echo "$ks_template: KS template not found, exiting.."
exit 1
fi
# generate the ks file
#
cat $ks_template > $vm_ks_path
cat <<EOF >> $vm_ks_path
rootpw --plaintext $root_pw
user --name=$user_name --shell=/bin/bash --homedir=/home/$user_name --password=$user_pw --plaintext
# Network information
network --bootproto=static --device=eth0 --gateway=$gateway --ip=$vm_ip --nameserver=$nameserver --netmask=$netmask --hostname=$vm_hostname
EOF
echo --------------------------------------------------------------------------------
date
echo
virt-install \
--name $vm_name \
--metadata title="$vm_title" \
--osinfo detect=on,name="$osname" \
--cdrom $cdrom \
--location $cdrom \
--disk "path=$vm_path,size=$disk,bus=virtio" \
--ram=$ram \
--vcpus=$vcpus \
--network "$network" \
--initrd-inject="$vm_ks_path" --extra-args "ks=file:/$vm_ks_file" \
$autostart
# --unattended "user-login=$user_login,user-password-file=$user_password_file" \
# --unattended product-key="product_key"
if [ "$debug" = "yes" ]; then
break
fi
((vm_id++))
done
exit
#
# End
#
--description "Test VM with RHEL 6" \
--graphics none \
--os-type=Linux \
--os-variant=rhel6 \