refactor: many changes
This commit is contained in:
145
scripts/run-vm
Executable file
145
scripts/run-vm
Executable file
@@ -0,0 +1,145 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Euo pipefail
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# This script sets up and launches a QEMU virtual machine with OVMF (UEFI).
|
||||
# It checks for the necessary files, creates directories/images as needed,
|
||||
# and provides clear, user-friendly output along the way.
|
||||
# Usage:
|
||||
# ./run-vm.sh [install]
|
||||
# - Pass "install" to attach the ISO as a CD-ROM for installation.
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
# Paths to OVMF firmware (pflash)
|
||||
OVMF_CODE="/run/libvirt/nix-ovmf/OVMF_CODE.fd"
|
||||
OVMF_VARS_DEFAULT="/run/libvirt/nix-ovmf/OVMF_VARS.fd"
|
||||
|
||||
# Determine where this script lives and compute related paths
|
||||
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
|
||||
TARGET_DIR=$(readlink -f "$SCRIPT_DIR/../.vm")
|
||||
OVMF_VARS_PATH=$(readlink -f "$SCRIPT_DIR/../.vm/OVMF_VARS-myvm.fd")
|
||||
IMG_PATH=$(readlink -f "$SCRIPT_DIR/../.vm/disk.img")
|
||||
ISO_DIR=$(readlink -f "$SCRIPT_DIR/../iso/result/iso")
|
||||
|
||||
echo
|
||||
echo "============================================================"
|
||||
echo " QEMU VM Setup and Launch Script"
|
||||
echo "============================================================"
|
||||
echo
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 1. Locate the ISO file
|
||||
# -----------------------------------------------------------------------------
|
||||
echo "[1/6] Locating ISO file in: $ISO_DIR"
|
||||
ISO_FILE=$(ls "$ISO_DIR"/*.iso 2>/dev/null | head -n 1 || true)
|
||||
|
||||
if [ -z "$ISO_FILE" ]; then
|
||||
echo " ❌ Error: No ISO found in $ISO_DIR"
|
||||
echo " Please verify the directory and try again."
|
||||
exit 1
|
||||
else
|
||||
echo " ✅ Found ISO: $ISO_FILE"
|
||||
fi
|
||||
echo
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 2. Ensure target VM directory exists
|
||||
# -----------------------------------------------------------------------------
|
||||
echo "[2/6] Checking VM target directory: $TARGET_DIR"
|
||||
if [ ! -d "$TARGET_DIR" ]; then
|
||||
echo " 📁 Directory does not exist; creating: $TARGET_DIR"
|
||||
mkdir -p "$TARGET_DIR"
|
||||
echo " ✅ Directory created."
|
||||
else
|
||||
echo " ✅ Directory already exists."
|
||||
fi
|
||||
echo
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 3. Prepare OVMF variables file (OVMF_VARS-myvm.fd)
|
||||
# -----------------------------------------------------------------------------
|
||||
echo "[3/6] Preparing OVMF variables file"
|
||||
if [ ! -f "$OVMF_VARS_PATH" ]; then
|
||||
echo " Copying default vars to: $OVMF_VARS_PATH"
|
||||
cp "$OVMF_VARS_DEFAULT" "$OVMF_VARS_PATH"
|
||||
chmod 600 "$OVMF_VARS_PATH"
|
||||
echo " ✅ OVMF_VARS-myvm.fd created and permissions set."
|
||||
else
|
||||
echo " ✅ OVMF_VARS-myvm.fd already exists."
|
||||
fi
|
||||
echo
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 4. Create QCOW2 disk image if missing
|
||||
# -----------------------------------------------------------------------------
|
||||
echo "[4/6] Ensuring QCOW2 disk image exists at: $IMG_PATH"
|
||||
if [ ! -f "$IMG_PATH" ]; then
|
||||
echo " 💾 Creating a new 64G QCOW2 image..."
|
||||
qemu-img create -f qcow2 "$IMG_PATH" "64G"
|
||||
echo " ✅ Disk image created: $IMG_PATH (64G)"
|
||||
else
|
||||
echo " ✅ Disk image already exists."
|
||||
fi
|
||||
echo
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 5. Parse arguments (install mode or normal boot)
|
||||
# -----------------------------------------------------------------------------
|
||||
INSTALL_MODE=0
|
||||
if [ "${1-}" = "install" ]; then
|
||||
INSTALL_MODE=1
|
||||
fi
|
||||
|
||||
if [ "$INSTALL_MODE" -eq 1 ]; then
|
||||
echo "[5/6] Install mode enabled: CD-ROM will be attached"
|
||||
CDROM_OPTS="-drive file=\"$ISO_FILE\",format=raw,if=none,media=cdrom,id=cd1,readonly=on -device ahci,id=ahci0 -device ide-cd,bus=ahci0.0,drive=cd1,bootindex=1"
|
||||
else
|
||||
echo "[5/6] Normal boot mode: No CD-ROM attached"
|
||||
CDROM_OPTS=""
|
||||
fi
|
||||
echo
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 6. Launch QEMU
|
||||
# -----------------------------------------------------------------------------
|
||||
echo "[6/6] Launching QEMU VM now..."
|
||||
echo "------------------------------------------------------------"
|
||||
echo " • Machine: q35, KVM acceleration"
|
||||
echo " • Memory: 4096 MB"
|
||||
echo " • CPU: host"
|
||||
echo " • OVMF_CODE (readonly): $OVMF_CODE"
|
||||
echo " • OVMF_VARS: $OVMF_VARS_PATH"
|
||||
echo " • Disk image: $IMG_PATH"
|
||||
if [ "$INSTALL_MODE" -eq 1 ]; then
|
||||
echo " • CD-ROM (ISO): $ISO_FILE"
|
||||
fi
|
||||
echo " • Networking: user-mode with hostfwd ssh (host:2222 → guest:22)"
|
||||
echo " • Connect to VM via: ssh -p 2222 root@localhost"
|
||||
echo " • VGA: virtio"
|
||||
echo "------------------------------------------------------------"
|
||||
echo
|
||||
|
||||
# Construct network options
|
||||
NET_OPTS="-netdev user,id=net0,hostfwd=tcp::2222-:22 -device e1000,netdev=net0"
|
||||
|
||||
# Run QEMU using eval to allow variable expansion in CDROM_OPTS
|
||||
eval qemu-system-x86_64 \
|
||||
-machine type=q35,accel=kvm \
|
||||
-m 4096 \
|
||||
-cpu host \
|
||||
\
|
||||
-drive if=pflash,format=raw,readonly=on,file="$OVMF_CODE" \
|
||||
\
|
||||
-drive if=pflash,format=raw,file="$OVMF_VARS_PATH" \
|
||||
\
|
||||
-drive file="$IMG_PATH",format=qcow2 \
|
||||
\
|
||||
$CDROM_OPTS \
|
||||
\
|
||||
$NET_OPTS \
|
||||
-vga virtio
|
||||
|
||||
echo
|
||||
echo "============================================================"
|
||||
echo " QEMU VM has exited"
|
||||
echo "============================================================"
|
||||
60
scripts/test-configuration
Executable file
60
scripts/test-configuration
Executable file
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Euo pipefail
|
||||
|
||||
VERBOSE=false
|
||||
SHOW_TRACE_OPT=""
|
||||
|
||||
# Parse options
|
||||
#
|
||||
if [ "$#" -gt 0 ]; then
|
||||
if [[ "$1" == "-v" || "$1" == "--verbose" ]]; then
|
||||
VERBOSE=true
|
||||
SHOW_TRACE_OPT="--show-trace"
|
||||
shift # Remove the verbose flag from arguments
|
||||
fi
|
||||
fi
|
||||
|
||||
# Check if 'nixos-rebuild' command is available
|
||||
if ! command -v nixos-rebuild > /dev/null; then
|
||||
echo "ERROR: 'nixos-rebuild' command not found. Please ensure it is installed and in your PATH." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Determine the absolute directory where the script itself is located
|
||||
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
|
||||
|
||||
# Construct the absolute path to the configuration file
|
||||
# and resolve it to a canonical path
|
||||
CONFIG_PATH=$(readlink -f "$SCRIPT_DIR/../example/configuration.nix")
|
||||
CHANNEL=$(cat "$SCRIPT_DIR/../example/channel")
|
||||
|
||||
# Verify that the CONFIG_PATH exists and is a regular file
|
||||
if [ ! -f "$CONFIG_PATH" ]; then
|
||||
echo "ERROR: Configuration file not found at '$CONFIG_PATH'." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "INFO: Attempting dry-build for using configuration '$CONFIG_PATH'..."
|
||||
if [ "$VERBOSE" = true ]; then
|
||||
echo "INFO: Verbose mode enabled, --show-trace will be used."
|
||||
fi
|
||||
|
||||
# Execute nixos-rebuild dry-build
|
||||
# Store the output and error streams, and the exit code
|
||||
NIX_OUTPUT_ERR=$(nixos-rebuild dry-build $SHOW_TRACE_OPT -I nixpkgs=$CHANNEL/nixexprs.tar.xz -I nixos-config="$CONFIG_PATH" 2>&1)
|
||||
NIX_EXIT_STATUS=$?
|
||||
|
||||
# Check the exit status
|
||||
if [ "$NIX_EXIT_STATUS" -eq 0 ]; then
|
||||
echo "INFO: Dry-build for completed successfully."
|
||||
if [ "$VERBOSE" = true ]; then
|
||||
echo "Output from nixos-rebuild:"
|
||||
echo "$NIX_OUTPUT_ERR"
|
||||
fi
|
||||
exit 0
|
||||
else
|
||||
echo "ERROR: Dry-build for failed. 'nixos-rebuild' exited with status $NIX_EXIT_STATUS." >&2
|
||||
echo "Output from nixos-rebuild:" >&2
|
||||
echo "$NIX_OUTPUT_ERR" >&2
|
||||
exit "$NIX_EXIT_STATUS"
|
||||
fi
|
||||
Reference in New Issue
Block a user