-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathausilio_en_v0.1.sh
More file actions
369 lines (308 loc) · 11.3 KB
/
ausilio_en_v0.1.sh
File metadata and controls
369 lines (308 loc) · 11.3 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/bin/bash
#=================================================
# SCRIPT: Create empty VM on Proxmox + Download ARC ISO from https://github.com/AuxXxilium/arc
# AUTHOR: Gianluca (gianlucaf81)
#=================================================
# Colors
GREEN="\e[32m"
RED="\e[31m"
YELLOW="\e[33m"
CYAN="\e[36m"
RESET="\e[0m"
# Functions
print_success() { echo -e "${GREEN}[✔]${RESET} $1"; }
print_error() { echo -e "${RED}[✘]${RESET} $1"; }
print_info() { echo -e "${CYAN}[i]${RESET} $1"; }
print_warn() { echo -e "${YELLOW}[!]${RESET} $1"; }
#=================================================
# Constants
#=================================================
ISO_STORAGE="/var/lib/vz/template/iso"
REPO_URL="https://github.com/AuxXxilium/arc"
DEFAULT_MEMORY="4096"
DEFAULT_CORES="2"
TITLE="ARC VM Creator"
#=================================================
# Check whiptail
#=================================================
check_dependencies() {
# Check for whiptail (for the interface)
if ! command -v whiptail &>/dev/null; then
print_info "Installing whiptail..."
if command -v apt &>/dev/null; then apt-get update -qq && apt-get install -y whiptail >/dev/null 2>&1
elif command -v yum &>/dev/null; then yum install -y newt >/dev/null 2>&1
elif command -v dnf &>/dev/null; then dnf install -y newt >/dev/null 2>&1
else print_error "Cannot install whiptail: no supported package manager found" && exit 1
fi
command -v whiptail &>/dev/null || { print_error "Failed to install whiptail."; exit 1; }
fi
# Check for unzip
if ! command -v unzip &>/dev/null; then
print_info "Installing unzip..."
if command -v apt &>/dev/null; then apt-get update -qq && apt-get install -y unzip >/dev/null 2>&1
elif command -v yum &>/dev/null; then yum install -y unzip >/dev/null 2>&1
elif command -v dnf &>/dev/null; then dnf install -y unzip >/dev/null 2>&1
else print_error "Cannot install unzip: no supported package manager found" && exit 1
fi
command -v unzip &>/dev/null || { print_error "Failed to install unzip."; exit 1; }
fi
}
#=================================================
# Cleanup
#=================================================
cleanup() {
echo -e "${YELLOW}Cleaning temp files...${RESET}"
rm -rf "$ISO_STORAGE/$(basename "$URL_DOWNLOAD")" "$ISO_STORAGE/${URL_DOWNLOAD##*/%.zip}" 2>/dev/null
exit 1
}
trap cleanup ERR
#=================================================
# GUI Functions
#=================================================
# Get a list of available bridges
get_available_bridges() {
ip link show | grep -oP '(?<=: )vmbr\d+' | sort
}
# Show a message with progress bar
show_progress() {
local message="$1"
local command="$2"
local total="$3"
(
echo "0"
eval "$command" &>/dev/null
local exit_code=$?
echo "100"
exit $exit_code
) | whiptail --gauge "$message" 8 70 0
return $?
}
#=================================================
# MAIN
#=================================================
# Check dependencies
check_dependencies
# Verify if user is root
if [[ $(id -u) -ne 0 ]]; then
whiptail --title "$TITLE" --msgbox "This script must be run as root!" 8 60
exit 1
fi
# Welcome screen
if ! whiptail --title "$TITLE" --yesno "This script will create a new Proxmox VM with the AuxXxilium ARC image.\n\nContinue?" 10 60; then
exit 0
fi
# Get VMID
NEXT_VMID=$(pvesh get /cluster/nextid)
VMID=$(whiptail --title "$TITLE" --inputbox "Enter VM ID (e.g., 100):" 8 60 "$NEXT_VMID" --title "VM ID" 3>&1 1>&2 2>&3)
exitstatus=$?
if [[ $exitstatus -ne 0 ]]; then
exit 1
fi
# If blank use next free value
if [[ -z "$VMID" ]]; then
VMID="$NEXT_VMID"
fi
# Check if VMID already exists
if qm list | awk '{print $1}' | grep -q "^$VMID$"; then
whiptail --title "$TITLE" --msgbox "VM ID $VMID already exists. Choose another ID." 8 60
exit 1
fi
# Get VM Name
VMNAME=$(whiptail --title "$TITLE" --inputbox "Enter VM name:" 8 60 --title "VM Name" 3>&1 1>&2 2>&3)
exitstatus=$?
if [[ $exitstatus -ne 0 || -z "$VMNAME" ]]; then
exit 1
fi
# Hardware settings
MEMORY=$(whiptail --title "$TITLE" --inputbox "Memory (MB):" 8 60 "$DEFAULT_MEMORY" --title "Memory" 3>&1 1>&2 2>&3)
exitstatus=$?
if [[ $exitstatus -ne 0 || -z "$MEMORY" ]]; then
MEMORY="$DEFAULT_MEMORY"
fi
CORES=$(whiptail --title "$TITLE" --inputbox "Number of CPU cores:" 8 60 "$DEFAULT_CORES" --title "CPU Cores" 3>&1 1>&2 2>&3)
exitstatus=$?
if [[ $exitstatus -ne 0 || -z "$CORES" ]]; then
CORES="$DEFAULT_CORES"
fi
# Bridge selection
BRIDGES=$(get_available_bridges)
if [[ -z "$BRIDGES" ]]; then
BRIDGE="vmbr0" # Default
else
BRIDGE=$(whiptail --title "$TITLE" --menu "Select network bridge:" 15 60 6 $(echo "$BRIDGES" | awk '{print $0, "Bridge"}') 3>&1 1>&2 2>&3)
exitstatus=$?
if [[ $exitstatus -ne 0 || -z "$BRIDGE" ]]; then
BRIDGE="vmbr0" # Default
fi
fi
# Option to add physical disks
ADD_DISKS=false
if whiptail --title "$TITLE" --yesno "Do you want to add physical disks to the VM?" 8 60; then
ADD_DISKS=true
fi
# Variable to store disk information
DISK_INFO=""
DISK_COUNT=0
SATA_PORTS=()
# If user wants to add physical disks
if [[ "$ADD_DISKS" == "true" ]]; then
# Ottieni la lista dei dischi disponibili
AVAILABLE_DISKS=$(ls -la /dev/disk/by-id/ | grep -v "wwn\|part\|^$" | grep "ata\|scsi" | awk '{print $9,$11}' | sed 's/\.\.\/\.\.\//\/dev\//g' | sort)
if [[ -z "$AVAILABLE_DISKS" ]]; then
whiptail --title "$TITLE" --msgbox "No available physical disks found." 8 60
else
# Prepara l'array per la checklist
disk_checklist_options=()
while IFS= read -r line; do
disk_id=$(echo "$line" | awk '{print $1}')
disk_device=$(echo "$line" | awk '{print $2}')
# Ottieni info disco
if command -v lsblk &>/dev/null; then
disk_size=$(lsblk -d -n -o SIZE "$disk_device" 2>/dev/null || echo "Unknown")
disk_model=$(lsblk -d -n -o MODEL "$disk_device" 2>/dev/null || echo "Unknown")
disk_info="${disk_size} ${disk_model} (${disk_device})"
else
disk_info="$disk_device"
fi
disk_checklist_options+=("$disk_id" "$disk_info" "OFF")
done <<< "$AVAILABLE_DISKS"
# Mostra checklist per selezione multipla
SELECTED_DISKS_IDS=$(whiptail --title "$TITLE" --checklist \
"Select disks to add (Space to select, Enter to confirm):" \
20 80 10 "${disk_checklist_options[@]}" 3>&1 1>&2 2>&3)
exitstatus=$?
if [[ $exitstatus -ne 0 ]]; then
print_info "Disk selection cancelled."
ADD_DISKS=false
else
# Processa i dischi selezionati
SELECTED_DISKS=()
SATA_PORTS=()
DISK_INFO=""
DISK_COUNT=0
# Convert selected list to array
SELECTED_ARRAY=()
for id in $SELECTED_DISKS_IDS; do
id_clean=$(echo "$id" | sed 's/"//g')
SELECTED_ARRAY+=("$id_clean")
done
# Assegna a ciascun disco una porta SATA
for disk_id in "${SELECTED_ARRAY[@]}"; do
PORT=$((DISK_COUNT + 1))
SELECTED_DISKS+=("/dev/disk/by-id/$disk_id")
SATA_PORTS+=("$PORT")
DISK_INFO="${DISK_INFO}SATA${PORT}: /dev/disk/by-id/$disk_id\n"
DISK_COUNT=$((DISK_COUNT + 1))
done
# Mostra riepilogo selezione
if [[ $DISK_COUNT -gt 0 ]]; then
whiptail --title "$TITLE" --msgbox "Dischi selezionati:\n\n$DISK_INFO" 20 70
else
ADD_DISKS=false
fi
fi
fi
fi
# Settings summary including disks
DISKS_SUMMARY=""
if [[ -n "$DISK_INFO" ]]; then
DISKS_SUMMARY="\n\nPhysical disks:\n$DISK_INFO"
fi
if ! whiptail --title "$TITLE" --yesno "Settings summary:\n\nVM ID: $VMID\nName: $VMNAME\nMemory: $MEMORY MB\nCPU Cores: $CORES\nBridge: $BRIDGE$DISKS_SUMMARY\n\nProceed with creation?" 20 70; then
exit 0
fi
# Create VM
whiptail --title "$TITLE" --infobox "Creating VM $VMNAME with ID $VMID..." 8 60
if ! qm create "$VMID" \
--name "$VMNAME" \
--ostype l26 \
--memory "$MEMORY" \
--cores "$CORES" \
--net0 "virtio,bridge=$BRIDGE" \
--boot 'order=sata0' \
--scsihw virtio-scsi-pci \
--machine pc \
--agent enabled=1 \
--onboot 0 \
--ide2 none,media=cdrom \
--serial0 socket; then
whiptail --title "$TITLE" --msgbox "Error creating VM." 8 60
exit 1
fi
# Remove default SCSI disk if it exists
whiptail --title "$TITLE" --infobox "Removing SCSI0 disk if present..." 8 60
qm set "$VMID" --delete scsi0 2>/dev/null
# Download ISO
URL_DOWNLOAD=$(curl -s https://api.github.com/repos/AuxXxilium/arc/releases/latest | grep "browser_download_url" | head -1 | cut -d '"' -f 4)
if [[ -z "$URL_DOWNLOAD" ]]; then
whiptail --title "$TITLE" --msgbox "Unable to get download URL from GitHub API." 8 60
exit 1
fi
ZIP_NAME="$(basename "$URL_DOWNLOAD")"
# Download with progress bar
(
echo "10"
echo "# Preparing download..."
sleep 1
echo "20"
echo "# Downloading image from GitHub..."
if ! wget -q -O "$ISO_STORAGE/$ZIP_NAME" "$URL_DOWNLOAD"; then
echo "100"
exit 1
fi
echo "50"
echo "# Extracting ZIP file..."
if ! unzip -o -d "$ISO_STORAGE" "$ISO_STORAGE/$ZIP_NAME" >/dev/null; then
echo "100"
exit 1
fi
echo "75"
echo "# Setting permissions..."
chmod 644 "$ISO_STORAGE"/*
echo "90"
echo "# Importing disk to local-lvm..."
if ! qm importdisk "$VMID" "$ISO_STORAGE/arc.img" local-lvm; then
echo "100"
exit 1
fi
sleep 2
echo "95"
echo "# Configuring disk as IDE0..."
if ! qm set "$VMID" --sata0 "local-lvm:vm-${VMID}-disk-0,cache=writeback"; then
echo "100"
exit 1
fi
# Add selected physical disks
if [[ ${#SELECTED_DISKS[@]} -gt 0 ]]; then
echo "96"
echo "# Adding physical disks..."
for i in "${!SELECTED_DISKS[@]}"; do
disk_path="${SELECTED_DISKS[i]}"
sata_port="${SATA_PORTS[i]}"
echo "# Connecting physical disk to SATA${sata_port}..."
if ! qm set "$VMID" --"sata${sata_port}" "$disk_path"; then
echo "100"
exit 1
fi
sleep 1
done
fi
echo "100"
echo "# Completed!"
sleep 1
) | whiptail --title "$TITLE" --gauge "Initializing..." 10 70 0
# Check if operation completed successfully
if [[ $? -ne 0 ]]; then
whiptail --title "$TITLE" --msgbox "An error occurred during download or import." 8 60
cleanup
fi
# Final message
DISK_MSG=""
if [[ ${#SELECTED_DISKS[@]} -gt 0 ]]; then
DISK_MSG="\n\nPhysical disks connected:"
for i in "${!SELECTED_DISKS[@]}"; do
DISK_MSG="${DISK_MSG}\nSATA${SATA_PORTS[i]}: ${SELECTED_DISKS[i]}"
done
fi
whiptail --title "$TITLE" --msgbox "VM '$VMNAME' (ID: $VMID) created successfully!\n\nThe ARC image has been imported and configured as IDE0 with writeback cache.${DISK_MSG}\n\nYou can start the VM from the Proxmox web interface." 16 76
exit 0