Add possibility to run without display

This commit is contained in:
Artur Gurgul 2025-08-17 10:30:42 +02:00
parent 73e3f7548b
commit 83a0f81cbe
4 changed files with 51 additions and 20 deletions

View file

@ -129,4 +129,4 @@ module VirtualMachine
# f.write("\0")
end
end
end
end

View file

@ -1,6 +1,20 @@
require "fileutils"
require "ostruct"
module DisplayMode
def self.none
0
end
def self.fullscreen
1
end
def self.window
2
end
end
module Qemu
def self.qemu_bin_for(arch)
{
@ -69,10 +83,15 @@ module Qemu
detach: true,
ram: 2048,
cpus: 1,
fullscreen: true
display: DisplayMode::none
}
# for testing only
defaults[:detach] = false
defaults[:display] = DisplayMode.window
opts = defaults.merge(options)
puts options
puts opts
qemu = qemu_bin_for(arch)
@ -82,22 +101,31 @@ module Qemu
# args += ["-bios", "/opt/homebrew/share/qemu/edk2-aarch64-code.fd"]
args += ["-drive", "if=pflash,format=raw,unit=0,readonly=on,file=/opt/homebrew/share/qemu/edk2-aarch64-code.fd"]
#args += ["-device", "virtio-gpu-device"]
if opts[:fullscreen]
args += ["-display", "cocoa,full-screen=on"]
if opts[:display] == DisplayMode::none
port = 2222
args += ['-nographic']
args += ['-netdev', "user,id=net0,hostfwd=tcp:127.0.0.1:#{port}-:22"]
#args += ['-device', 'e1000,netdev=net0']
args += ['-device', 'virtio-net-pci,netdev=net0']
puts "ssh -p #{port} user@localhost"
else
args += ["-display", "cocoa"]
#args += ["-device", "virtio-gpu-device"]
if opts[:display] == DisplayMode::fullscreen
args += ["-display", "cocoa,full-screen=on"]
else
args += ["-display", "cocoa"]
end
args += ["-device", "qemu-xhci,id=xhci"]
args += ["-device", "usb-kbd"]
args += ["-device", "usb-tablet"]
args += ["-device", "virtio-keyboard-device"]
args += ["-device", "virtio-mouse-device"]
args += ["-device", "virtio-gpu"]
end
args += ["-device", "qemu-xhci,id=xhci"]
args += ["-device", "usb-kbd"]
args += ["-device", "usb-tablet"]
args += ["-device", "virtio-keyboard-device"]
args += ["-device", "virtio-mouse-device"]
args += ["-device", "virtio-gpu"]
# copy to /Users/artur/.cache/dat/vm/debian/arm64/debian/vars.fd
# /opt/homebrew/share/qemu/edk2-aarch64-vars.fd
# args += ["-drive", "if=pflash,format=raw,unit=1,file=/Users/artur/.cache/dat/vm/debian/arm64/debian/vars.fd"]
@ -111,22 +139,24 @@ module Qemu
if opts[:cdrom] != nil
args += ["-cdrom", opts[:cdrom]]
end
args += ["-device", "virtio-net,netdev=n0", "-netdev", "user,id=n0"] # user-mode NAT
# args += ["-device", "virtio-net,netdev=n0", "-netdev", "user,id=n0"] # user-mode NAT
# optional: uncomment to run headless with VNC on :5901
# args += ["-display", "none", "-vnc", "127.0.0.1:1"]
cmd = [qemu, *args]
puts "Launching: #{cmd.join(' ')}"
pid = Process.spawn(*cmd, pgroup: true, out: $stdout, err: $stderr)
if opts[:detach]
log = File.open("log.txt", "w")
pid = Process.spawn(*cmd, pgroup: false, out: log, err: log)
Process.detach(pid)
puts "QEMU pid=#{pid}"
else
pid = Process.spawn(*cmd, pgroup: true, out: $stdout, err: $stderr)
Process.wait(pid)
status = $?
puts "Exit status: #{status.exitstatus}"
end
end
end
end