Creating wrapper script

This commit is contained in:
vandomej 2025-09-29 22:51:08 -07:00
parent 2547c73dc2
commit 2c85175912

View file

@ -81,25 +81,42 @@ create_vm_script() {
local script_path="$vm_path/start.sh" local script_path="$vm_path/start.sh"
cat > "$script_path" << EOF cat > "$script_path" << 'EOF'
#!/bin/bash #!/bin/bash
qemu-system-aarch64 \\ VM_NAME="$1"
-name "$vm_name" \\ MAC_ADDRESS="$2"
-machine virt,highmem=off \\ ISO_PATH="$3"
-accel hvf \\ DISK_PATH="$4"
-cpu host \\ CPUS="$5"
-smp "$CPUS" \\ MEMORY="$6"
-m "${MEMORY}M" \\
-drive file="$disk_path",if=virtio,format=qcow2 \\ qemu-system-aarch64 \
-cdrom "$iso_path" \\ -name "$VM_NAME" \
-netdev user,id=net0 \\ -machine virt,highmem=off \
-device virtio-net-pci,netdev=net0,mac=$mac_address \\ -accel hvf \
-nographic \\ -cpu host \
-smp "$CPUS" \
-m "${MEMORY}M" \
-drive file="$DISK_PATH",if=virtio,format=qcow2 \
-cdrom "$ISO_PATH" \
-netdev user,id=net0 \
-device virtio-net-pci,netdev=net0,mac="$MAC_ADDRESS" \
-nographic \
-serial mon:stdio -serial mon:stdio
EOF EOF
# Make the script executable and create a wrapper that passes the parameters
chmod +x "$script_path" chmod +x "$script_path"
echo "$script_path"
# Create a wrapper script that calls the main script with proper arguments
local wrapper_path="$vm_path/run.sh"
cat > "$wrapper_path" << EOF
#!/bin/bash
"$script_path" "$vm_name" "$mac_address" "$iso_path" "$disk_path" "$CPUS" "$MEMORY"
EOF
chmod +x "$wrapper_path"
echo "$wrapper_path"
} }
# Create systemd service file (optional) # Create systemd service file (optional)
@ -148,13 +165,13 @@ main() {
create_service_file "$controlplane_path" "$CONTROLPLANE_NAME" "$controlplane_script" create_service_file "$controlplane_path" "$CONTROLPLANE_NAME" "$controlplane_script"
local manage_script="$VM_DIR/manage-vm.sh" local manage_script="$VM_DIR/manage-vm.sh"
cat > "$manage_script" << 'EOF' cat > "$manage_script" << EOF
#!/bin/bash #!/bin/bash
VM_DIR="$(cd "$(dirname "$0")" && pwd)" VM_DIR="\$(cd "\$(dirname "\$0")" && pwd)"
start_vm() { start_vm() {
echo "Starting Talos VM..." echo "Starting Talos VM..."
"$VM_DIR/talos-controlplane/start.sh" & "\$VM_DIR/talos-controlplane/run.sh" &
} }
stop_vm() { stop_vm() {
@ -162,7 +179,7 @@ stop_vm() {
pkill -f "qemu-system-aarch64.*talos-controlplane" pkill -f "qemu-system-aarch64.*talos-controlplane"
} }
case "$1" in case "\$1" in
start) start)
start_vm start_vm
;; ;;
@ -173,7 +190,7 @@ case "$1" in
pgrep -f "qemu-system-aarch64.*talos-controlplane" > /dev/null && echo "VM is running" || echo "VM is stopped" pgrep -f "qemu-system-aarch64.*talos-controlplane" > /dev/null && echo "VM is running" || echo "VM is stopped"
;; ;;
*) *)
echo "Usage: $0 {start|stop|status}" echo "Usage: \$0 {start|stop|status}"
exit 1 exit 1
;; ;;
esac esac