particle-os/scripts/test-virsh.sh
2025-08-10 20:24:11 -07:00

184 lines
5.9 KiB
Bash
Executable file

#!/bin/bash
set -euo pipefail
echo "Starting virsh bootloader test..."
# Check if Phase 2 image exists
if ! podman image exists particle-os:phase2; then
echo "ERROR: Phase 2 image not found. Run 'just build-phase2' first."
exit 1
fi
echo "Phase 2 image found, proceeding with test..."
# Create test directory
mkdir -p test
mkdir -p test/particle-os-test
# Create disk image
echo "Creating 4GB disk image..."
qemu-img create -f raw test/particle-os-test/particle-os-test.img 4G
# Partition the disk
echo "Partitioning disk..."
echo "yes" | sudo /usr/sbin/parted test/particle-os-test/particle-os-test.img mklabel gpt
sudo /usr/sbin/parted test/particle-os-test/particle-os-test.img mkpart primary fat32 1MiB 512MiB
sudo /usr/sbin/parted test/particle-os-test/particle-os-test.img mkpart primary ext4 512MiB 100%
sudo /usr/sbin/parted test/particle-os-test/particle-os-test.img set 1 boot on
sudo /usr/sbin/parted test/particle-os-test/particle-os-test.img set 1 esp on
echo "Disk image created and partitioned successfully!"
# Create loop device
echo "Creating loop device..."
loop_dev=$(sudo losetup --find --show /tmp/particle-os-test/particle-os-test.img)
echo "Using loop device: $loop_dev"
# Wait for partition devices
echo "Waiting for partition devices..."
sleep 2
# Check if partition devices exist
if [ ! -b "${loop_dev}p1" ] || [ ! -b "${loop_dev}p2" ]; then
echo "Partition devices not found, running partprobe..."
sudo partprobe "$loop_dev"
sleep 3
# Check again and try to recreate loop device with partition scanning
if [ ! -b "${loop_dev}p1" ] || [ ! -b "${loop_dev}p2" ]; then
echo "Partition devices still not found, recreating loop device..."
sudo losetup -d "$loop_dev"
sleep 1
loop_dev=$(sudo losetup --find --show --partscan /tmp/particle-os-test/particle-os-test.img)
echo "New loop device: $loop_dev"
sleep 2
fi
fi
echo "Partition devices:"
ls -la "${loop_dev}"* 2>/dev/null || echo "No partition devices found"
# Create filesystems
echo "Creating filesystems..."
sudo mkfs.fat -F32 "${loop_dev}p1"
echo "y" | sudo mkfs.ext4 "${loop_dev}p2"
echo "Filesystems created successfully!"
# Keep the same loop device for mounting
echo "Keeping loop device for mounting..."
# Mount partitions
echo "Mounting partitions..."
mkdir -p test/particle-os-test/mount/boot test/particle-os-test/mount/rootfs
sudo mount "${loop_dev}p1" test/particle-os-test/mount/boot
sudo mount "${loop_dev}p2" test/particle-os-test/mount/rootfs
# Extract Phase 2 container to rootfs
echo "Extracting Phase 2 container..."
# Clean up any existing container with this name
podman rm temp-phase2-virsh 2>/dev/null || true
podman create --name temp-phase2-virsh particle-os:phase2
podman export temp-phase2-virsh | sudo tar -x -C test/particle-os-test/mount/rootfs
podman rm temp-phase2-virsh
# Set up bootloader
echo "Setting up bootloader..."
sudo /usr/sbin/grub-install --target=x86_64-efi --efi-directory=test/particle-os-test/mount/boot --boot-directory=test/particle-os-test/mount/boot --removable "$loop_dev"
# Create GRUB config
echo "Creating GRUB configuration..."
sudo tee test/particle-os-test/mount/boot/grub/grub.cfg > /dev/null <<EOF
set timeout=5
set default=0
menuentry "Particle-OS Phase 2" {
search --set=root --file /vmlinuz-6.12.38+deb13-amd64
linux /vmlinuz-6.12.38+deb13-amd64 root=/dev/vda2 rw console=ttyS0
initrd /initrd.img-6.12.38+deb13-amd64
}
EOF
# Cleanup mounts
echo "Cleaning up mounts..."
sudo umount test/particle-os-test/mount/boot test/particle-os-test/mount/rootfs
sudo losetup -d "$loop_dev"
# Create libvirt VM definition
echo "Creating libvirt VM definition..."
VM_NAME="particle-os-test"
# Remove existing VM if it exists
if virsh list --all --name | grep -q "^$VM_NAME$"; then
echo "Removing existing VM: $VM_NAME"
virsh destroy "$VM_NAME" 2>/dev/null || true
virsh undefine "$VM_NAME" 2>/dev/null || true
echo "Old VM removed"
fi
# Create new VM using virt-install
echo "Creating VM: $VM_NAME"
sudo virt-install \
--name "$VM_NAME" \
--memory 2048 \
--vcpus 2 \
--disk test/particle-os-test/particle-os-test.img,format=raw,bus=virtio \
--graphics vnc,listen=0.0.0.0,port=5900 \
--video qxl \
--boot hd \
--os-variant debian12 \
--noautoconsole \
--import
echo "VM created: $VM_NAME"
# Test bootloader
echo "Testing bootloader..."
echo "Starting VM: $VM_NAME"
# Check if VM is already running (it might be after creation)
current_status=$(sudo virsh domstate "$VM_NAME")
if [ "$current_status" != "running" ]; then
sudo virsh start "$VM_NAME"
# Wait for VM to start
sleep 5
else
echo "VM is already running"
fi
# Check VM status
status=$(sudo virsh domstate "$VM_NAME")
echo "VM status: $status"
if [ "$status" = "running" ]; then
echo "✅ VM is running successfully!"
echo ""
echo "🌐 To view the VM console from another PC:"
echo "1. Connect to this machine via VNC on port 5900"
echo "2. Or use virt-manager on another PC and connect to:"
echo " qemu+ssh://$USER@$(hostname -I | awk '{print $1}')/system"
echo "3. Or use VNC viewer to connect to: $(hostname -I | awk '{print $1}'):5900"
echo ""
echo "🔧 VM management commands:"
echo " sudo virsh start $VM_NAME - Start VM"
echo " sudo virsh shutdown $VM_NAME - Shutdown VM"
echo " sudo virsh destroy $VM_NAME - Force stop VM"
echo " sudo virsh undefine $VM_NAME - Remove VM definition"
echo ""
echo "🌐 Remote access from virt-manager on another PC:"
echo " Connection URI: qemu+ssh://joe@192.168.122.76/system"
echo " VNC access: 192.168.122.76:5900"
else
echo "❌ VM failed to start properly"
exit 1
fi
echo "Next steps would be:"
echo "1. ✅ Create loop device - COMPLETE"
echo "2. ✅ Format partitions - COMPLETE"
echo "3. ✅ Mount and extract container - COMPLETE"
echo "4. ✅ Install bootloader - COMPLETE"
echo "5. ✅ Create VM with virsh - COMPLETE"
echo "Test completed successfully!"