|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# boot-qemu-wic.sh |
| 4 | +# |
| 5 | +# Copyright (C) 2006-2025 wolfSSL Inc. |
| 6 | +# |
| 7 | +# This file is part of wolfProvider. |
| 8 | +# |
| 9 | +# wolfProvider is free software; you can redistribute it and/or modify |
| 10 | +# it under the terms of the GNU General Public License as published by |
| 11 | +# the Free Software Foundation; either version 3 of the License, or |
| 12 | +# (at your option) any later version. |
| 13 | +# |
| 14 | +# wolfProvider is distributed in the hope that it will be useful, |
| 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +# GNU General Public License for more details. |
| 18 | +# |
| 19 | +# You should have received a copy of the GNU General Public License |
| 20 | +# along with wolfProvider. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +# |
| 22 | +# Boot a Yocto WIC image in QEMU and run commands |
| 23 | +# Usage: boot-qemu-wic.sh <wic-file> "command1" "command2" "command3" ... |
| 24 | +set -e |
| 25 | + |
| 26 | +WIC_FILE="$1" |
| 27 | +shift |
| 28 | + |
| 29 | +if [ -z "$WIC_FILE" ] || [ ! -f "$WIC_FILE" ]; then |
| 30 | + echo "ERROR: WIC file not found: $WIC_FILE" |
| 31 | + echo "Usage: $0 <wic-file> <command1> [command2] ..." |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +if [ $# -eq 0 ]; then |
| 36 | + echo "ERROR: No commands provided" |
| 37 | + echo "Usage: $0 <wic-file> <command1> [command2] ..." |
| 38 | + exit 1 |
| 39 | +fi |
| 40 | + |
| 41 | +echo "=== Booting WIC image: $WIC_FILE ===" |
| 42 | +echo "=== Commands to run: $# ===" |
| 43 | + |
| 44 | +# Create expect script dynamically |
| 45 | +EXPECT_SCRIPT=$(mktemp /tmp/qemu-boot.XXXXXX.exp) |
| 46 | + |
| 47 | +cat > "$EXPECT_SCRIPT" <<'EXPECT_HEADER' |
| 48 | +#!/usr/bin/expect |
| 49 | +if {[info exists env(EXPECT_TIMEOUT)]} { |
| 50 | + set timeout $env(EXPECT_TIMEOUT) |
| 51 | +} else { |
| 52 | + set timeout 10800 |
| 53 | +} |
| 54 | +log_user 1 |
| 55 | +
|
| 56 | +# Get WIC path from environment |
| 57 | +set wic_path $env(WIC_FILE) |
| 58 | +
|
| 59 | +puts "=== Starting QEMU with WIC: $wic_path ===" |
| 60 | +
|
| 61 | +# Boot QEMU with SandyBridge CPU (supports AVX for compatibility) |
| 62 | +spawn qemu-system-x86_64 \ |
| 63 | + -cpu SandyBridge \ |
| 64 | + -drive file=$wic_path,if=virtio,format=raw \ |
| 65 | + -m 2048 \ |
| 66 | + -smp 2 \ |
| 67 | + -nographic \ |
| 68 | + -serial mon:stdio |
| 69 | +
|
| 70 | +# Wait for login prompt |
| 71 | +puts "=== Waiting for login prompt ===" |
| 72 | +expect { |
| 73 | + "login:" { |
| 74 | + puts "=== Login prompt detected ===" |
| 75 | + sleep 2 |
| 76 | + send "root\r" |
| 77 | + } |
| 78 | + timeout { |
| 79 | + puts "ERROR: Timeout waiting for login" |
| 80 | + exit 1 |
| 81 | + } |
| 82 | +} |
| 83 | +
|
| 84 | +# Wait for shell prompt |
| 85 | +puts "=== Waiting for shell prompt ===" |
| 86 | +expect { |
| 87 | + -re "#|root@" { |
| 88 | + puts "\n=== LOGGED IN SUCCESSFULLY ===" |
| 89 | + } |
| 90 | + "login:" { |
| 91 | + puts "=== Got login again, retrying ===" |
| 92 | + sleep 1 |
| 93 | + send "root\r" |
| 94 | + exp_continue |
| 95 | + } |
| 96 | + timeout { |
| 97 | + puts "ERROR: Timeout waiting for shell" |
| 98 | + exit 1 |
| 99 | + } |
| 100 | +} |
| 101 | +
|
| 102 | +sleep 2 |
| 103 | +
|
| 104 | +EXPECT_HEADER |
| 105 | + |
| 106 | +# Add each command to the expect script |
| 107 | +CMD_NUM=1 |
| 108 | +for cmd in "$@"; do |
| 109 | + cat >> "$EXPECT_SCRIPT" <<EXPECT_CMD |
| 110 | +
|
| 111 | +# Command $CMD_NUM: $cmd |
| 112 | +puts "=== Running command $CMD_NUM: $cmd ===" |
| 113 | +send "$cmd\r" |
| 114 | +expect -re "#|root@" |
| 115 | +sleep 1 |
| 116 | +
|
| 117 | +EXPECT_CMD |
| 118 | + CMD_NUM=$((CMD_NUM + 1)) |
| 119 | +done |
| 120 | + |
| 121 | +# Add shutdown logic |
| 122 | +cat >> "$EXPECT_SCRIPT" <<'EXPECT_FOOTER' |
| 123 | +
|
| 124 | +# Shutdown |
| 125 | +puts "=== All commands completed, shutting down ===" |
| 126 | +send "poweroff\r" |
| 127 | +
|
| 128 | +expect { |
| 129 | + eof { |
| 130 | + puts "\n=== QEMU SHUTDOWN COMPLETE ===" |
| 131 | + } |
| 132 | + timeout { |
| 133 | + puts "\n=== Timeout on shutdown ===" |
| 134 | + } |
| 135 | +} |
| 136 | +EXPECT_FOOTER |
| 137 | + |
| 138 | +# Make executable and run |
| 139 | +chmod +x "$EXPECT_SCRIPT" |
| 140 | + |
| 141 | +echo "=== Generated expect script ===" |
| 142 | + |
| 143 | +# Export WIC_FILE for expect script |
| 144 | +export WIC_FILE |
| 145 | + |
| 146 | +# Run the expect script |
| 147 | +if "$EXPECT_SCRIPT"; then |
| 148 | + EXIT_CODE=0 |
| 149 | + echo "=== Boot and test completed successfully ===" |
| 150 | +else |
| 151 | + EXIT_CODE=$? |
| 152 | + echo "=== Boot and test failed with exit code $EXIT_CODE ===" |
| 153 | +fi |
| 154 | + |
| 155 | +# Cleanup |
| 156 | +rm -f "$EXPECT_SCRIPT" |
| 157 | + |
| 158 | +exit $EXIT_CODE |
| 159 | + |
0 commit comments