-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_ex_virtio.sh
270 lines (248 loc) · 6.76 KB
/
make_ex_virtio.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/sh
rootpassword=root123
echo "-----------------------------------------------------"
# Usage function to display help
usage() {
echo "Usage: $0 [-no-synchronize] [-no-ztp] [-no-vxlan-default]"
echo "Options:"
echo " -no-synchronize Disable dual boot enable - do not add: system>commit synchronize"
echo " -no-ztp Disable auto upgrade - do not add: chassis>auto-image-upgrade"
echo " -no-vxlan-default Disable evpn-vxlan-default-switch-support - do not add chassis>evpn-vxlan-default-switch-support"
exit 1
}
# Initialize variables
NOSYNCHRONIZE=false
NOZTP=false
NOVXLANDEFAULT=false
# Parse arguments
while [ $# -gt 0 ]; do
case $1 in
-no-synchronize)
NOSYNCHRONIZE=true
;;
-no-ztp)
NOZTP=true
;;
-no-vxlan-default)
NOVXLANDEFAULT=true
;;
*)
# Unknown option
echo "Error: Unknown option: $1"
usage
;;
esac
shift
done
# Check if the system is running Ubuntu
if [ -f /etc/lsb-release ]; then
. /etc/lsb-release
if [ "$DISTRIB_ID" = "Ubuntu" ]; then
echo "This is Ubuntu."
else
echo "This is not Ubuntu."
exit 1
fi
else
echo "This is not Ubuntu."
exit 1
fi
# Check if 'pwgen' is installed and install it if it's not
if ! dpkg -l | grep -q "pwgen"; then
echo "'pwgen' is not installed. Installing..."
sudo apt-get update -y
sudo apt-get install -y pwgen
else
echo "'pwgen' is already installed."
fi
# Check if 'qemu-img' is installed and install it if it's not
if ! dpkg -l | grep -q "qemu-utils"; then
echo "'qemu-img' (qemu-utils) is not installed. Installing..."
sudo apt-get install -y qemu-utils
else
echo "'qemu-img' (qemu-utils) is already installed."
fi
echo "-----------------------------------------------------"
echo "Prepere..."
mkdir /mnt/virtioc
rm -f virtioc.qcow2
rm -f virtiob.qcow2
rm -Rf config_drive
echo "-----------------------------------------------------"
echo "Creating config drive..."
mkdir config_drive
mkdir config_drive/etc
mkdir config_drive/etc/cron.d
mkdir config_drive/boot
mkdir config_drive/var
mkdir config_drive/var/db
mkdir config_drive/var/db/vmm
mkdir config_drive/var/db/vmm/etc
mkdir config_drive/var/db/vmm/yang
mkdir config_drive/var/db/scripts
mkdir config_drive/var/db/scripts/event
mkdir config_drive/config
mkdir config_drive/config/license
echo "-----------------------------------------------------"
cat > config_drive/etc/cron.d/xdpc <<EOF
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
* * * * * root /bin/sh /var/db/scripts/event/xdpc.sh >> /var/log/script.log 2>&1
EOF
echo "-----------------------------------------------------"
echo "Creating loader file..."
cat > config_drive/boot/loader.conf <<EOF
boot_noveriexec=yes
hw.pci.link.0x60.irq=10
vmtype=0
vm_retype=RE-VMX
vm_i2cid=0xBAA
vm_chassis_i2cid=151
vm_instance=1
vm_is_virtual=1
kern.timecounter.invariant_tsc=1
kern.timecounter.smp_tsc=1
kern.timecounter.vm_guest_tsc=1
EOF
echo "-----------------------------------------------------"
echo "Creating additional config file..."
SALT=$(pwgen 8 1)
HASH=$(openssl passwd -1 -salt $SALT $rootpassword)
echo "Root password: $rootpassword"
cat > config_drive/config/juniper.conf <<EOF
system {
root-authentication {
encrypted-password "$HASH";
}
services {
ssh {
root-login allow;
protocol-version v2;
}
netconf {
ssh;
}
}
arp {
aging-timer 5;
}
syslog {
file interactive-commands {
interactive-commands any;
}
file messages {
any notice;
authorization info;
}
}
processes {
dhcp-service {
traceoptions {
file dhcp_logfile size 10m;
level all;
flag packet;
}
}
}
}
interfaces {
fxp0 {
unit 0 {
family inet {
dhcp {
vendor-id Juniper-ex9214-VM;
force-discover;
}
}
}
}
}
multi-chassis {
mc-lag {
consistency-check;
}
}
protocols {
router-advertisement {
interface fxp0.0;
}
lldp {
port-id-subtype interface-name;
port-description-type interface-description;
neighbour-port-info-display port-id;
interface all;
}
lldp-med {
interface all;
}
}
EOF
if [ "$NOSYNCHRONIZE" = false ]; then
echo "Adding system>commit synchronize"
cat >> config_drive/config/juniper.conf <<EOF
system {
commit synchronize;
}
EOF
fi
if [ "$NOZTP" = false ]; then
echo "Adding chassis>auto-image-upgrade"
cat >> config_drive/config/juniper.conf <<EOF
chassis {
auto-image-upgrade;
}
EOF
fi
if [ "$NOVXLANDEFAULT" = false ]; then
echo "Adding chassis> evpn-vxlan-default-switch-support"
cat >> config_drive/config/juniper.conf <<EOF
chassis {
evpn-vxlan-default-switch-support;
}
EOF
fi
echo "-----------------------------------------------------"
cat > config_drive/var/db/scripts/event/xdpc.sh <<EOF
#!/bin/sh
#
# (crontab -l; echo "* * * * * /bin/sh /var/db/scripts/event/xdpc.sh >> /var/log/script.log 2>&1") | crontab -
current_datetime=\$(date '+%Y-%m-%d %H:%M:%S')
output=\$(cli -c "show chassis alarm")
echo "\$output" | grep "XDPC" > /dev/null 2>&1
if [ \$? -eq 0 ]; then
echo "\${current_datetime} - Found XDPC. Restarting FPC slot 0..."
cli -c "request chassis fpc slot 0 restart"
else
lldp_count=\$(cli -c "show lldp neighbors" | tail -n +2 | wc -l | tr -d ' ')
echo "\${current_datetime} - alarm XDPC not found / LLDP Neighbors: \${lldp_count}"
fi
EOF
echo "-----------------------------------------------------"
echo "Creating vmm-config.tgz..."
cd config_drive
tar zcf vmm-config.tgz *
rm -rf boot config var etc
cd ..
echo "-----------------------------------------------------"
echo "Creating virtiob (vmxhdd.img) for VCP ..."
# vmxhdd.img
# Create empty disk emulate routing-engine-hdd
qemu-img create -f qcow2 -o compat=1.1,refcount_bits=64 virtiob.qcow2 4G >/dev/null
ls -l virtiob.qcow2
echo "-----------------------------------------------------"
echo "Creating virtioc (metadata-usb-re.img)..."
# metadata-usb-re.img
# Create our own metadrive image, so we can use a junos config file
dd if=/dev/zero of=virtioc.img bs=1M count=50 >/dev/null 2>&1
mkfs.vfat virtioc.img >/dev/null
mount -o loop virtioc.img /mnt/virtioc
cp config_drive/vmm-config.tgz /mnt/virtioc
umount /mnt/virtioc
rm -R /mnt/virtioc
qemu-img convert -O qcow2 -o compat=1.1,refcount_bits=64 virtioc.img virtioc.qcow2
ls -l virtioc.qcow2
echo "-----------------------------------------------------"
echo "Clean..."
rm virtioc.img
rm -R config_drive/
echo "DONE"