summaryrefslogtreecommitdiff
path: root/scripts/testing/setup-testing-env.sh
blob: e682553244d05f20ce04e5bead84a655e2b1c3a3 (plain)
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
#!/bin/bash
# Setup testing environment for archsetup
# Author: Craig Jennings <craigmartinjennings@gmail.com>
# License: GNU GPLv3
#
# This script performs one-time setup of the testing infrastructure:
# - Installs QEMU/KVM and libvirt
# - Configures libvirt networking
# - Adds user to libvirt group
# - Verifies KVM support
# - Creates directories for test artifacts

set -e

# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

# Source utilities
source "$SCRIPT_DIR/lib/logging.sh"

# Initialize logging
LOGFILE="$PROJECT_ROOT/test-results/setup-$(date +'%Y%m%d-%H%M%S').log"
init_logging "$LOGFILE"

section "ArchSetup Testing Environment Setup"

# Check if running on Arch Linux
if [ ! -f /etc/arch-release ]; then
    fatal "This script is designed for Arch Linux"
fi

# Check if user has sudo
if ! sudo -n true 2>/dev/null; then
    warn "This script requires sudo access"
    info "You may be prompted for your password"
fi

# Install required packages
section "Installing Required Packages"

PACKAGES=(
    qemu-full
    libvirt
    virt-manager
    dnsmasq
    bridge-utils
    iptables
    virt-install
    libguestfs
)

for pkg in "${PACKAGES[@]}"; do
    if pacman -Qi "$pkg" &>/dev/null; then
        info "$pkg is already installed"
    else
        step "Installing $pkg"
        if sudo pacman -S --noconfirm "$pkg" >> "$LOGFILE" 2>&1; then
            success "$pkg installed"
        else
            error "Failed to install $pkg"
            fatal "Package installation failed"
        fi
    fi
done

# Enable and start libvirt service
section "Configuring libvirt Service"

step "Enabling libvirtd service"
if sudo systemctl enable libvirtd.service >> "$LOGFILE" 2>&1; then
    success "libvirtd service enabled"
else
    warn "Failed to enable libvirtd service (may already be enabled)"
fi

step "Starting libvirtd service"
if sudo systemctl start libvirtd.service >> "$LOGFILE" 2>&1; then
    success "libvirtd service started"
else
    if sudo systemctl is-active --quiet libvirtd.service; then
        info "libvirtd service is already running"
    else
        error "Failed to start libvirtd service"
        fatal "Service startup failed"
    fi
fi

# Add user to libvirt group
section "Configuring User Permissions"

if groups | grep -q libvirt; then
    success "User $USER is already in libvirt group"
else
    step "Adding user $USER to libvirt group"
    if sudo usermod -a -G libvirt "$USER" >> "$LOGFILE" 2>&1; then
        success "User added to libvirt group"
        warn "You must log out and back in for group membership to take effect"
        warn "After logging back in, re-run this script to continue"
        exit 0
    else
        error "Failed to add user to libvirt group"
        fatal "User configuration failed"
    fi
fi

# Verify KVM support
section "Verifying KVM Support"

if [ -e /dev/kvm ]; then
    success "KVM is available"
else
    error "KVM is not available"
    info "Check if virtualization is enabled in BIOS"
    info "Load kvm module: sudo modprobe kvm-intel (or kvm-amd)"
    fatal "KVM not available"
fi

# Check which KVM module is loaded
if lsmod | grep -q kvm_intel; then
    info "Using Intel KVM"
elif lsmod | grep -q kvm_amd; then
    info "Using AMD KVM"
else
    warn "No KVM module detected"
    info "Load with: sudo modprobe kvm-intel (or kvm-amd)"
fi

# Create directory structure
section "Creating Directory Structure"

DIRS=(
    "$PROJECT_ROOT/vm-images"
    "$PROJECT_ROOT/test-results"
)

for dir in "${DIRS[@]}"; do
    if [ -d "$dir" ]; then
        info "Directory exists: $dir"
    else
        step "Creating directory: $dir"
        if mkdir -p "$dir" 2>> "$LOGFILE"; then
            success "Directory created: $dir"
        else
            error "Failed to create directory: $dir"
        fi
    fi
done

# Configure default libvirt network
section "Configuring libvirt Network"

if virsh net-info default &>/dev/null; then
    info "Default network exists"

    if virsh net-info default | grep -q "Active:.*yes"; then
        success "Default network is active"
    else
        step "Starting default network"
        if virsh net-start default >> "$LOGFILE" 2>&1; then
            success "Default network started"
        else
            error "Failed to start default network"
        fi
    fi

    if virsh net-info default | grep -q "Autostart:.*yes"; then
        info "Default network autostart is enabled"
    else
        step "Enabling default network autostart"
        if virsh net-autostart default >> "$LOGFILE" 2>&1; then
            success "Default network autostart enabled"
        else
            warn "Failed to enable default network autostart"
        fi
    fi
else
    error "Default network not found"
    info "This is unusual - libvirt should create it automatically"
fi

# Summary
section "Setup Complete"

success "Testing environment is ready"
info ""
info "Next steps:"
info "  1. Create base VM: ./scripts/testing/create-base-vm.sh"
info "  2. Run a test: ./scripts/testing/run-test.sh"
info ""
info "Log file: $LOGFILE"