diff --git a/tools/ubuntu_vm_start.sh b/tools/ubuntu_vm_start.sh index a9fbbcd..085fd6b 100755 --- a/tools/ubuntu_vm_start.sh +++ b/tools/ubuntu_vm_start.sh @@ -119,7 +119,7 @@ generate_password_hash() { wait_for_vm_boot() { local vm_name="$1" local expected_ip="$2" - local max_wait=120 # 3 minutes + local max_wait=180 # 3 minutes, increased from 120 local count=0 log "Waiting for VM '$vm_name' to boot with static IP $expected_ip..." @@ -131,7 +131,7 @@ wait_for_vm_boot() { fi # 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" echo "$expected_ip" return 0 @@ -448,6 +448,10 @@ fi # Create cloud-init image for first boot 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' PASSWORD_HASH=$(generate_password_hash) test_step "Password hash generation" "[ -n '$PASSWORD_HASH' ]" @@ -474,19 +478,26 @@ ssh_authorized_keys: [] # Network configuration with static IP network: - version: 2 - ethernets: - ens3: - dhcp4: false - addresses: - - $VM_STATIC_IP/24 - routes: - - to: default - via: 192.168.100.1 - nameservers: - addresses: - - 8.8.8.8 - - 8.8.4.4 + config: disabled + +write_files: + - path: /etc/netplan/50-cloud-init.yaml + content: | + network: + version: 2 + ethernets: + ens3: + dhcp4: false + addresses: + - $VM_STATIC_IP/24 # Use variable for IP + # gateway4: 192.168.100.1 # Deprecated + routes: # Use modern routes syntax + - to: default + via: 192.168.100.1 + nameservers: + addresses: + - 8.8.8.8 + - 1.1.1.1 # Package updates and installs package_update: true @@ -507,12 +518,16 @@ runcmd: - systemctl start ssh - systemctl status ssh - netplan apply - - sleep 5 - - ip addr flush dev ens3 - - ip addr add $VM_STATIC_IP/24 dev ens3 - - ip route add default via 192.168.100.1 + - chmod 0600 /etc/netplan/50-cloud-init.yaml || echo "Failed to chmod /etc/netplan/50-cloud-init.yaml" + - sleep 2 # Allow netplan apply to settle + #- ip addr flush dev ens3 + #- ip addr add $VM_STATIC_IP/24 dev ens3 + #- ip route add default via 192.168.100.1 - ip addr show ens3 - ip route show + - ping 192.168.100.2 -c 3 + - echo "WERE THERE" + # Final message 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 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 log "Launching Cloud Hypervisor..." @@ -783,6 +795,13 @@ if test_ssh_connection "$VM_IP"; then log "✓ VM has IP address: $VM_IP" log "✓ SSH is working: ssh ubuntu@$VM_IP (password: ubuntu)" 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 "" info "VM $VM_NAME is ready for use!" info "Connect via SSH: ssh ubuntu@$VM_IP"