-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_vms.sh
executable file
·122 lines (103 loc) · 3.62 KB
/
build_vms.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
#!/bin/bash
display_usage() {
echo -e "\nUsage: $0 [vm1 vm2 vm3] [normal netmap]\n"
}
# check whether user had supplied -h or --help . If yes display usage
if [[ ($# == "--help") || $# == "-h" ]]; then
display_usage
exit 0
fi
# display usage if the script is not run as root user
if [[ "$EUID" -ne 0 ]]; then
echo "This script must be run as root!"
exit 1
fi
# if less than one arguments supplied, display usage
if [ $# -le 1 ]; then
echo "This script must be run with at least one argument."
display_usage
exit 1
fi
CLOUD_BASE_IMG="ubuntu-20.04-server-cloudimg-amd64.img"
CUR_PATH=$(pwd)
MISSING=""
FOUND=""
VM_NAME="$1"
NUM="${VM_NAME: -1}"
BACK_IFNAME="$3"
checkdep() {
local exe="$1" package="$2" upstream="$3"
if command -v "$1" >/dev/null 2>&1; then
FOUND="${FOUND:+${FOUND} }$exe"
return "0"
fi
MISSING=${MISSING:+${MISSING}$package}
echo "missing $exe."
echo " It can be installed in package: $package"
[ -n "$upstream" ] &&
echo " Upstream project url: $upstream"
return 1
}
checkdep cloud-localds cloud-image-utils http://launchpad.net/cloud-utils
# checkdep genisoimage genisoimage
checkdep qemu-img qemu-utils http://qemu.org/
checkdep qemu-system-x86_64 qemu-system-x86 http://qemu.org/
checkdep wget wget
if [ -n "$MISSING" ]; then
echo
[ -n "${FOUND}" ] && echo "found: ${FOUND}"
echo "install missing deps with:"
echo " apt-get update && apt-get install ${MISSING}"
else
echo "All needed dependencies properly installed. (${FOUND})"
fi
# Check if base image exists
if [ ! -f "${CUR_PATH}/${CLOUD_BASE_IMG}" ]; then
echo "Base image ${CLOUD_BASE_IMG} not found in ${CUR_PATH}, donwloading..."
wget -O "${CUR_PATH}/${CLOUD_BASE_IMG}" \
"https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64.img"
fi
# Create an overlay image
qemu-img create -f qcow2 -b "$CLOUD_BASE_IMG" "$VM_NAME".img
qemu-img resize "$VM_NAME".img +22G
# Build seed image with the user data and the networking config
# TODO This net conf is not working
# cloud-localds -v --network-config="$CUR_PATH"/net_conf_vm2.yaml \
# "$CUR_PATH"/seed_"$VM_NAME".img "$CUR_PATH"/user-data.yaml
cloud-localds "$CUR_PATH"/seed_"$VM_NAME".img "$CUR_PATH"/user-data.yaml
if [ "$2" == "normal" ]; then
NET_FRONTEND="virtio-net-pci"
NET_BACKEND="tap"
BACK_IFNAME=""$VM_NAME".cp"
IFUP_SCRIPTS=",script=no,downscript=no"
elif [ "$2" == "netmap" ]; then
# Make sure netmap module is loaded
if ! lsmod | grep "netmap" &>/dev/null; then
echo "netmap module is not loaded. Loading."
if modprobe netmap; then
echo "netmap module loaded."
else
echo "Failed to load netmap module, please compile and install it for your current kernel version: $(uname -r)."
exit 1
fi
fi
NET_FRONTEND="ptnet-pci"
NET_BACKEND="netmap"
if [ ! -n "$BACK_IFNAME" ]; then
BACK_IFNAME="vale2:1}2"
echo "Backend interface name not specified, using default: $BACK_IFNAME"
fi
IFUP_SCRIPTS=",passthrough=on" # The comma here is on purpose and mandatory
else
echo "Unknown network type"
display_usage
exit 1
fi
# Boot the vm
sudo qemu-system-x86_64 \
-hda "$CUR_PATH"/"$VM_NAME".img \
-hdb "$CUR_PATH"/seed_"$VM_NAME".img \
-m 2G --enable-kvm -pidfile $VM_NAME.pid \
-serial file:"$VM_NAME".log \
-device e1000,netdev=mgmt,mac=00:AA:BB:CC:01:99 -netdev user,id=mgmt,hostfwd=tcp::202"$NUM"-:22 \
-device "$NET_FRONTEND",netdev=data1,mac=00:0a:0a:0a:0"$NUM":01 -netdev $NET_BACKEND,ifname="$BACK_IFNAME",id=data1"$IFUP_SCRIPTS" &