This commit is contained in:
kristof 2025-06-15 20:41:33 +02:00
parent dc9e75704d
commit e701615e53

View File

@ -119,7 +119,7 @@ generate_password_hash() {
wait_for_vm_boot() { wait_for_vm_boot() {
local vm_name="$1" local vm_name="$1"
local expected_ip="$2" local expected_ip="$2"
local max_wait=120 # 3 minutes local max_wait=180 # 3 minutes, increased from 120
local count=0 local count=0
log "Waiting for VM '$vm_name' to boot with static IP $expected_ip..." log "Waiting for VM '$vm_name' to boot with static IP $expected_ip..."
@ -131,7 +131,7 @@ wait_for_vm_boot() {
fi fi
# Try to ping the expected static IP # Try to ping the expected static IP
if ping -c 1 -W 2 "$expected_ip" >/dev/null 2>&1; then if ping -c 2 -W 3 "$expected_ip" >/dev/null 2>&1; then
log "VM is responding at static IP address: $expected_ip" log "VM is responding at static IP address: $expected_ip"
echo "$expected_ip" echo "$expected_ip"
return 0 return 0
@ -448,6 +448,10 @@ fi
# Create cloud-init image for first boot # Create cloud-init image for first boot
log "Creating cloud-init configuration..." log "Creating cloud-init configuration..."
# Generate a random MAC address for the VM (used in cloud-init and hypervisor)
VM_MAC="52:54:00:$(printf '%02x:%02x:%02x' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)))"
log "Generated MAC address for VM: $VM_MAC"
# Generate proper password hash for 'ubuntu' # Generate proper password hash for 'ubuntu'
PASSWORD_HASH=$(generate_password_hash) PASSWORD_HASH=$(generate_password_hash)
test_step "Password hash generation" "[ -n '$PASSWORD_HASH' ]" test_step "Password hash generation" "[ -n '$PASSWORD_HASH' ]"
@ -474,19 +478,26 @@ ssh_authorized_keys: []
# Network configuration with static IP # Network configuration with static IP
network: network:
config: disabled
write_files:
- path: /etc/netplan/50-cloud-init.yaml
content: |
network:
version: 2 version: 2
ethernets: ethernets:
ens3: ens3:
dhcp4: false dhcp4: false
addresses: addresses:
- $VM_STATIC_IP/24 - $VM_STATIC_IP/24 # Use variable for IP
routes: # gateway4: 192.168.100.1 # Deprecated
routes: # Use modern routes syntax
- to: default - to: default
via: 192.168.100.1 via: 192.168.100.1
nameservers: nameservers:
addresses: addresses:
- 8.8.8.8 - 8.8.8.8
- 8.8.4.4 - 1.1.1.1
# Package updates and installs # Package updates and installs
package_update: true package_update: true
@ -507,12 +518,16 @@ runcmd:
- systemctl start ssh - systemctl start ssh
- systemctl status ssh - systemctl status ssh
- netplan apply - netplan apply
- sleep 5 - chmod 0600 /etc/netplan/50-cloud-init.yaml || echo "Failed to chmod /etc/netplan/50-cloud-init.yaml"
- ip addr flush dev ens3 - sleep 2 # Allow netplan apply to settle
- ip addr add $VM_STATIC_IP/24 dev ens3 #- ip addr flush dev ens3
- ip route add default via 192.168.100.1 #- ip addr add $VM_STATIC_IP/24 dev ens3
#- ip route add default via 192.168.100.1
- ip addr show ens3 - ip addr show ens3
- ip route show - ip route show
- ping 192.168.100.2 -c 3
- echo "WERE THERE"
# Final message # Final message
final_message: "Cloud-init setup complete. VM is ready for SSH access!" final_message: "Cloud-init setup complete. VM is ready for SSH access!"
@ -653,9 +668,6 @@ VM_LOG_FILE="/tmp/cloud-hypervisor-vm$VM_NUMBER.log"
# Remove existing socket and log file if they exist # Remove existing socket and log file if they exist
rm -f "$VM_SOCKET" "$VM_LOG_FILE" rm -f "$VM_SOCKET" "$VM_LOG_FILE"
# Generate a random MAC address for the VM
VM_MAC="52:54:00:$(printf '%02x:%02x:%02x' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)))"
log "Generated MAC address for VM: $VM_MAC"
# Start Cloud Hypervisor in background with error handling # Start Cloud Hypervisor in background with error handling
log "Launching Cloud Hypervisor..." log "Launching Cloud Hypervisor..."
@ -783,6 +795,13 @@ if test_ssh_connection "$VM_IP"; then
log "✓ VM has IP address: $VM_IP" log "✓ VM has IP address: $VM_IP"
log "✓ SSH is working: ssh ubuntu@$VM_IP (password: ubuntu)" log "✓ SSH is working: ssh ubuntu@$VM_IP (password: ubuntu)"
log "✓ VM info saved to: $VM_INFO_FILE" log "✓ VM info saved to: $VM_INFO_FILE"
# Display relevant IP configuration lines from VM log
if [ -f "$VM_LOG_FILE" ]; then
info "Relevant IP configuration from VM log ($VM_LOG_FILE):"
grep -E "inet .*ens3|default via" "$VM_LOG_FILE" | tail -n 5 || true
fi
echo "" echo ""
info "VM $VM_NAME is ready for use!" info "VM $VM_NAME is ready for use!"
info "Connect via SSH: ssh ubuntu@$VM_IP" info "Connect via SSH: ssh ubuntu@$VM_IP"