-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmikrotik.sh
79 lines (76 loc) · 2.15 KB
/
mikrotik.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
#!/bin/bash
#vars
version="nil"
vmID="nil"
echo "############## Start of Script ##############
## Checking if temp dir is available..."
if [ -d /root/temp ]
then
echo "-- Directory exists!"
else
echo "-- Creating temp dir!"
mkdir /root/temp
fi
# Ask user for version
echo "## Preparing for image download and VM creation!"
version=""
while [ -z $version ]; do
read -p "Please input CHR version to deploy (6.38.2, 6.40.1, etc):" version
if [ -z $version ]; then
echo "You didn't provide version, please try again"
fi
done
# Check if image is available and download if needed
if [ -f /root/temp/chr-$version.img ]
then
echo "-- CHR image is available."
else
echo "-- Downloading CHR $version image file."
cd /root/temp
echo "---------------------------------------------------------------------------"
wget https://download.mikrotik.com/routeros/$version/chr-$version.img.zip
unzip chr-$version.img.zip
echo "---------------------------------------------------------------------------"
fi
if [ ! -f /root/temp/chr-$version.img ]; then
echo "There is no image, please try again"
exit 1
fi
# List already existing VM's and ask for vmID
echo "== Printing list of VM's on this hypervisor!"
qm list
while [[ ! "$vmID" =~ [0-9]{1,} ]]; do
echo ""
read -p "Please Enter free vm ID to use:" vmID
echo ""
done
# Create storage dir for VM if needed.
if [ -d /var/lib/vz/images/$vmID ]
then
echo "-- VM Directory exists! Ideally try another vm ID!"
read -p "Please Enter free vm ID to use:" vmID
else
echo "-- Creating VM image dir!"
mkdir /var/lib/vz/images/$vmID
fi
# Creating qcow2 image for CHR.
echo "-- Converting image to qcow2 format "
mkdir -p /var/lib/vz/images/$vmID
qemu-img convert \
-f raw \
-O qcow2 \
/root/temp/chr-$version.img \
/var/lib/vz/images/$vmID/vm-$vmID-disk-1.qcow2
# Creating VM
echo "-- Creating new CHR VM"
qm create $vmID \
--name chr-$version \
--net0 virtio,bridge=vmbr0 \
--bootdisk virtio0 \
--ostype l26 \
--memory 256 \
--onboot no \
--sockets 1 \
--cores 1 \
--virtio0 local:$vmID/vm-$vmID-disk-1.qcow2
echo "############## End of Script ##############"