Skip to content

Commit

Permalink
QemuHCK: Keep pressing return key
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiko Odaki <[email protected]>
  • Loading branch information
akihikodaki committed Nov 20, 2023
1 parent bab34bc commit 15d17db
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
13 changes: 13 additions & 0 deletions lib/auxiliary/closable_thread.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require './lib/exceptions'

module AutoHCK
# ClosableThread is a Thread with close method
class ClosableThread < Thread
def close
kill
join
end
end
end
26 changes: 19 additions & 7 deletions lib/setupmanagers/qemuhck/qemu_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_relative 'qmp'
require_relative 'exceptions'

require_relative '../../auxiliary/closable_thread'
require_relative '../../auxiliary/json_helper'
require_relative '../../auxiliary/host_helper'
require_relative '../../auxiliary/resource_scope'
Expand Down Expand Up @@ -66,8 +67,10 @@ def initialize(scope, logger, machine, run_name, run_opts)
@keep_alive = run_opts[:keep_alive]
@delete_snapshot = run_opts[:create_snapshot]
@machine.run_config_commands
run_vm
@machine.dump_config
@qemu_thread = run_vm
scope << self
scope << @qemu_thread
end

def keep_snapshot
Expand All @@ -91,16 +94,28 @@ def run_qemu(scope)
qemu
end

def run_vm
@machine.dump_config
def run_ret
ClosableThread.new do
loop do
sleep 1
try_with_qmp(&:ret)
end
end
end

@qemu_thread = Thread.new do
def run_vm
ClosableThread.new do
loop do
qemu = nil
ResourceScope.open do |scope|
@machine.run_pre_start_commands

qemu = run_qemu(scope)

# The installer requires to press some key to start for UEFI
# systems.
scope << run_ret

qemu.wait_no_fail
qemu = nil
ensure
Expand Down Expand Up @@ -152,9 +167,6 @@ def vm_abort
return if hard_abort

@logger.info("#{@run_name} hard abort failed, force aborting...")

@qemu_thread.kill
@qemu_thread.join
end

def close
Expand Down
28 changes: 24 additions & 4 deletions lib/setupmanagers/qemuhck/qmp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,47 @@ def initialize(scope, name, logger)

def quit
@logger.info("Sending quit signal to #{@name} via QMP")
run_cmd('quit')
run_cmd 'execute' => 'quit'
end

def powerdown
@logger.info("Sending powerdown signal to #{@name} via QMP")
run_cmd('system_powerdown')
run_cmd 'execute' => 'system_powerdown'
end

def ret
run_cmd(
'execute' => 'input-send-event',
'arguments' => {
'events' => [
{
'type' => 'key',
'data' => {
'down' => true,
'key' => {
'type' => 'qcode',
'data' => 'ret'
}
}
}
]
}
)
end

private

def run_cmd(cmd)
unless @negotiated
send_cmd 'qmp_capabilities'
send_cmd 'execute' => 'qmp_capabilities'
@negotiated = true
end

send_cmd cmd
end

def send_cmd(cmd)
@socket_internal.write JSON.dump({ 'execute' => cmd })
@socket_internal.write JSON.dump(cmd)
@socket_internal.flush

loop do
Expand Down

0 comments on commit 15d17db

Please sign in to comment.