-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-vm
More file actions
executable file
·212 lines (181 loc) · 4.88 KB
/
create-vm
File metadata and controls
executable file
·212 lines (181 loc) · 4.88 KB
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
#!/bin/bash
# SPDX-FileCopyrightText: The vmnet-broker authors
# SPDX-License-Identifier: Apache-2.0
set -e
CACHE_DIR=".cache"
VMS_DIR=".vms"
ARCH=$(uname -m | sed s/x86_64/amd64/)
IMAGE_URL="https://cloud-images.ubuntu.com/plucky/current/plucky-server-cloudimg-${ARCH}.img"
KERNEL_URL="https://cloud-images.ubuntu.com/plucky/current/unpacked/plucky-server-cloudimg-${ARCH}-vmlinuz-generic"
INITRD_URL="https://cloud-images.ubuntu.com/plucky/current/unpacked/plucky-server-cloudimg-${ARCH}-initrd-generic"
# Generate Random Unicast Locally Administered MAC
generate_mac_address() {
# -tu1: Type unsigned decimal, 1-byte units
local bytes=($(od -An -N6 -tu1 /dev/urandom))
# Make it Unicast Locally Administered
# (Value AND 254) clears bit 0 (Unicast)
# (Value OR 2) sets bit 1 (Locally Administered)
bytes[0]=$(( (bytes[0] & 254) | 2 ))
printf "%02x:%02x:%02x:%02x:%02x:%02x" "${bytes[@]}"
}
download_image() {
local url="$1"
local filename="$2"
if [ ! -f "$CACHE_DIR/$filename" ]; then
echo "Downloading $url"
curl --fail --show-error --location "$url" -o "$CACHE_DIR/$filename.qcow2"
echo "Converting image to raw format"
qemu-img convert -f qcow2 -O raw "$CACHE_DIR/$filename.qcow2" "$CACHE_DIR/$filename.tmp"
rm "$CACHE_DIR/$filename.qcow2"
echo "Resize image to 20g"
qemu-img resize "$CACHE_DIR/$filename.tmp" 20g
mv "$CACHE_DIR/$filename.tmp" "$CACHE_DIR/$filename"
fi
}
download_kernel() {
local url="$1"
local filename="$2"
if [ ! -f "$CACHE_DIR/kernel" ]; then
echo "Downloading $url"
curl --fail --show-error --location "$url" -o "$CACHE_DIR/$filename.tmp.gz"
echo "Decompressing kernel"
gzip -d "$CACHE_DIR/$filename.tmp.gz"
mv "$CACHE_DIR/$filename.tmp" "$CACHE_DIR/$filename"
fi
}
download_initrd() {
local url="$1"
local filename="$2"
if [ ! -f "$CACHE_DIR/$filename" ]; then
echo "Downloading $url"
curl --fail --show-error --location "$url" -o "$CACHE_DIR/$filename.tmp"
mv "$CACHE_DIR/$filename.tmp" "$CACHE_DIR/$filename"
fi
}
create_cloud_init_iso() {
cidata_dir="$VM_DIR/cidata"
mkdir "$cidata_dir"
cat <<EOF > "$cidata_dir/user-data"
#cloud-config
password: pass
chpasswd:
expire: false
EOF
cat <<EOF > "$cidata_dir/network-config"
version: 2
ethernets:
eth0:
match:
macaddress: ${MAC_ADDRESS}
set-name: eth0
dhcp4: true
dhcp-identifier: mac
EOF
cat <<EOF > "$cidata_dir/meta-data"
instance-id: ${VM_UUID}
local-hostname: ${VM_NAME}
EOF
echo "Generating cloud-init ISO..."
hdiutil makehybrid \
-o "$VM_DIR/cidata.iso" \
-joliet \
-iso \
-joliet-volume-name cidata \
-quiet \
"$cidata_dir/"
}
create_config_json() {
cat <<EOF > "${VM_DIR}/config.json"
{
"cpus": 2,
"memory": 1024,
"mac": "${MAC_ADDRESS}",
"bootloader": {
EOF
if [ "$BOOTLOADER" = "linux" ]; then
cat <<EOF >> "${VM_DIR}/config.json"
"type": "linux",
"kernel": "${VM_DIR}/kernel",
"initrd": "${VM_DIR}/initrd"
EOF
elif [ "$BOOTLOADER" = "efi" ]; then
cat <<EOF >> "${VM_DIR}/config.json"
"type": "efi",
"variable-store": "${VM_DIR}/efi_vars.fd"
EOF
fi
cat <<EOF >> "${VM_DIR}/config.json"
},
"disks": [
{
"path": "${VM_DIR}/disk.img",
"readonly": false
},
{
"path": "${VM_DIR}/cidata.iso",
"readonly": true
}
]
}
EOF
}
usage() {
echo "Usage: create-vm [--bootloader=linux|efi] vm-name"
exit 1
}
BOOTLOADER="linux"
VM_NAME=""
while [[ $# -gt 0 ]]; do
case $1 in
--bootloader)
BOOTLOADER="$2"
shift 2
;;
--bootloader=*)
BOOTLOADER="${1#*=}"
shift
;;
-*)
echo "Unknown option: $1"
usage
;;
*)
if [ -z "$VM_NAME" ]; then
VM_NAME="$1"
else
echo "Error: Multiple VM names specified"
usage
fi
shift
;;
esac
done
if [ "$BOOTLOADER" != "efi" ] && [ "$BOOTLOADER" != "linux" ]; then
usage
fi
if [ -z "$VM_NAME" ]; then
usage
fi
VM_DIR="$VMS_DIR/${VM_NAME}"
if [ -d "$VM_DIR" ]; then
echo "Error: VM directory '${VM_DIR}' already exists"
exit 1
fi
MAC_ADDRESS=$(generate_mac_address)
VM_UUID=$(uuidgen | tr "A-F" "a-f")
mkdir -p "$CACHE_DIR"
mkdir -p "$VM_DIR"
download_image "$IMAGE_URL" disk.img
if [ "$BOOTLOADER" = "linux" ]; then
download_kernel "$KERNEL_URL" kernel
download_initrd "$INITRD_URL" initrd
fi
echo "Creating VM directory..."
cp -c "$CACHE_DIR/disk.img" "$VM_DIR/disk.img"
if [ "$BOOTLOADER" = "linux" ]; then
cp -c "$CACHE_DIR/kernel" "$VM_DIR/kernel"
cp -c "$CACHE_DIR/initrd" "$VM_DIR/initrd"
fi
create_cloud_init_iso
create_config_json
echo "Created ${VM_DIR}"