-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_arm64_vm.sh
119 lines (99 loc) · 2.42 KB
/
create_arm64_vm.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
#!/usr/bin/env bash
# Set strict error handling
set -euo pipefail
# Configuration variables
VM_NAME="arm64-vm"
CPU_LIMIT=8
MEMORY="16GiB"
IMAGE="images:ubuntu/oracular"
DISK_SIZE="50GiB"
NEW_USER="tmeijn"
NEW_USER_PASSWORD="test"
# Function to log messages
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
# Function to check if VM exists
check_vm_exists() {
incus info "$VM_NAME" > /dev/null 2>&1
}
# Function to delete VM if it exists
delete_existing_vm() {
if check_vm_exists; then
log "Deleting existing VM: $VM_NAME"
incus delete "$VM_NAME" --force || {
log "Error: Failed to delete VM"
exit 1
}
fi
}
# Function to create and configure VM
create_vm() {
log "Creating new VM: $VM_NAME"
incus create "$IMAGE" "$VM_NAME" --vm \
-c "limits.cpu=$CPU_LIMIT" \
-c "limits.memory=$MEMORY" || {
log "Error: Failed to create VM"
exit 1
}
incus config device override "$VM_NAME" root "size=${DISK_SIZE}" || {
log "Error: Failed to set disk size for VM"
exit 1
}
incus start "$VM_NAME" || {
log "Error: Failed to start VM"
exit 1
}
}
# Function to install desktop environment
install_desktop() {
log "Updating system packages"
incus exec "$VM_NAME" -- bash -c "apt update && apt upgrade -y" || {
log "Error: Failed to update packages"
exit 1
}
log "Installing Ubuntu desktop"
incus exec "$VM_NAME" -- bash -c "DEBIAN_FRONTEND=noninteractive apt install -y ubuntu-desktop" || {
log "Error: Failed to install desktop environment"
exit 1
}
}
# Function to setup user
setup_user() {
log "Setting up new user: $NEW_USER"
incus exec "$VM_NAME" -- bash -c "useradd -m -G sudo -s /bin/bash -c 'Tyrone Meijn' $NEW_USER && echo '$NEW_USER:$NEW_USER_PASSWORD' | chpasswd" || {
log "Error: Failed to create user or set password"
exit 1
}
}
# Function to reboot VM
reboot_vm() {
log "Rebooting VM"
incus restart "$VM_NAME" || {
log "Error: Failed to reboot VM"
exit 1
}
}
# Function to create snapshot of VM
create_snapshot() {
log "Creating snapshot of VM"
incus snapshot create "$VM_NAME" initial-state || {
log "Error: Failed to create snapshot"
exit 1
}
}
# Main execution
main() {
log "Starting VM creation process"
delete_existing_vm
create_vm
# Wait for VM to be ready
sleep 10
install_desktop
setup_user
reboot_vm
create_snapshot
log "VM setup completed successfully"
}
# Execute main function
main