Class: OsVm::Machine
- Inherits:
-
Object
- Object
- OsVm::Machine
- Defined in:
- lib/osvm/machine.rb
Direct Known Subclasses
Constant Summary collapse
- SHELL_INDEX_KEY =
:osvm_machine_shell_index- QEMU_REAP_INTERVAL =
0.1
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
protected
Returns the value of attribute config.
-
#console_thread ⇒ Object
readonly
protected
Returns the value of attribute console_thread.
-
#hash_base ⇒ Object
readonly
protected
Returns the value of attribute hash_base.
-
#log ⇒ Object
readonly
protected
Returns the value of attribute log.
- #name ⇒ String readonly
-
#qemu_cv ⇒ Object
readonly
protected
Returns the value of attribute qemu_cv.
-
#qemu_mutex ⇒ Object
readonly
protected
Returns the value of attribute qemu_mutex.
-
#qemu_pid ⇒ Object
readonly
protected
Returns the value of attribute qemu_pid.
-
#qemu_read ⇒ Object
readonly
protected
Returns the value of attribute qemu_read.
-
#qemu_reaper ⇒ Object
readonly
protected
Returns the value of attribute qemu_reaper.
-
#shared_dir ⇒ Object
readonly
protected
Returns the value of attribute shared_dir.
-
#shared_filesystems ⇒ Object
readonly
protected
Returns the value of attribute shared_filesystems.
-
#shell_instances ⇒ Object
readonly
protected
Returns the value of attribute shell_instances.
-
#sockdir ⇒ Object
readonly
protected
Returns the value of attribute sockdir.
-
#start_kernel_params ⇒ Array<String>
readonly
Kernel parameters passed to #start.
-
#tmpdir ⇒ Object
readonly
protected
Returns the value of attribute tmpdir.
-
#virtiofsd_pids ⇒ Object
readonly
protected
Returns the value of attribute virtiofsd_pids.
Class Method Summary collapse
Instance Method Summary collapse
-
#all_fail(*cmds, shell: nil) ⇒ Array<Array<[Integer, String]>>
Execute all commands and check that they all fail.
-
#all_succeed(*cmds, shell: nil) ⇒ Array<Array<[Integer, String]>>
Execute all commands and check that they all succeed.
- #base_kernel_params(kernel_params) ⇒ Object protected
- #booted? ⇒ Boolean
- #can_execute? ⇒ Boolean
-
#cleanup ⇒ Machine
Cleanup machine state.
- #command_shell(name) ⇒ Object protected
- #console_log_path ⇒ Object protected
-
#console_output ⇒ String
Return text captured from the machine's console so far.
- #current_shell ⇒ Object protected
- #current_shell_index ⇒ Object protected
- #custom_qemu_numa_memory_backend? ⇒ Boolean protected
-
#destroy ⇒ Machine
Destroy the machine.
-
#destroy_disks ⇒ Machine
Destroy file-backed disks.
- #disk_path(path) ⇒ Object protected
-
#execute(cmd, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Execute a command.
-
#fails(cmd, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Execute command and check that it fails.
-
#fails_with_retries(cmd, attempts:, retry_delay: 1, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Execute a command repeatedly until it fails or all attempts are used.
- #finalize ⇒ Object
-
#initialize(name, config, tmpdir, sockdir, default_timeout: 900, hash_base: '', interactive_console: false) ⇒ Machine
constructor
A new instance of Machine.
- #inspect ⇒ Object
-
#join(timeout: @default_timeout) ⇒ Object
Block until the machine stops.
-
#kill(signal: 'TERM') ⇒ Machine
Kill the machine.
-
#mkdir(path) ⇒ Machine
Create a directory inside the machine.
-
#mkdir_p(path) ⇒ Machine
Create a directory inside the machine.
- #mount_shared_dir_once ⇒ Object protected
- #poweroff_command ⇒ Object protected
- #prepare_disks ⇒ Object protected
-
#pull_file(src, preserve: false) ⇒ String
Pull file from the machine to the host.
-
#push_file(src, dst, preserve: false, mkpath: false) ⇒ Machine
Push file from the host to the machine.
- #qemu_boot_media_options ⇒ Object protected
- #qemu_boot_options(kernel_params) ⇒ Object protected
- #qemu_command(kernel_params: []) ⇒ Object protected
- #qemu_disk_options ⇒ Object protected
- #qemu_shell_options ⇒ Object protected
- #qemu_state ⇒ Object protected
- #qemu_virtiofs_options ⇒ Object protected
- #read_nonblock(io) ⇒ Object protected
- #run_console_thread ⇒ Object protected
- #run_qemu_reaper(pid) ⇒ Object protected
- #running? ⇒ Boolean
- #service_check_command(_name) ⇒ Object protected
- #shell_chardev_id(index) ⇒ Object protected
- #shell_for(index) ⇒ Object protected
- #shell_log_path(index = 0) ⇒ Object protected
- #shell_socket_path(index = 0) ⇒ Object protected
- #shells ⇒ ShellCollection
- #signal_qemu(signal, pid) ⇒ Object protected
- #socket_path(socket) ⇒ Object protected
-
#start(kernel_params: [], wait_for_boot: false) ⇒ Machine
Start the machine.
- #start_virtiofs ⇒ Object protected
-
#stop(timeout: @default_timeout) ⇒ Machine
Stop the machine.
- #stop_virtiofs ⇒ Object protected
-
#succeeds(cmd, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Execute command and check that it succeeds.
-
#succeeds_with_retries(cmd, attempts:, retry_delay: 1, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Execute a command repeatedly until it succeeds or all attempts are used.
- #validate_shell_index(index) ⇒ Object protected
- #virtiofs_log_path(mount_name) ⇒ Object protected
- #virtiofs_socket_path(mount_name) ⇒ Object protected
-
#wait_for_boot(timeout: @default_timeout) ⇒ Object
Wait until the system has booted.
-
#wait_for_console_text(regex, timeout: @default_timeout) ⇒ Machine
Wait for text to appear in console output.
- #wait_for_qemu(pid) ⇒ Object protected
- #wait_for_qemu_exit(pid, timeout) ⇒ Object protected
-
#wait_for_service(name) ⇒ Machine
Wait for a system service to start.
-
#wait_for_shutdown(timeout: @default_timeout) ⇒ Machine
Wait until the machine shuts down.
-
#wait_until_fails(cmd, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Wait until command fails.
-
#wait_until_online(timeout: @default_timeout) ⇒ Machine
Wait until network is operational, including DNS.
-
#wait_until_succeeds(cmd, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Wait until command succeeds.
Constructor Details
#initialize(name, config, tmpdir, sockdir, default_timeout: 900, hash_base: '', interactive_console: false) ⇒ Machine
Returns a new instance of Machine.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/osvm/machine.rb', line 31 def initialize(name, config, tmpdir, sockdir, default_timeout: 900, hash_base: '', interactive_console: false) @name = name @config = config @tmpdir = tmpdir @sockdir = sockdir @default_timeout = default_timeout || 900 @hash_base = hash_base @interactive_console = interactive_console @start_kernel_params = [] @running = false @shared_dir = SharedDir.new(self) @shared_filesystems = { shared_dir.fs_name => shared_dir.host_path }.merge(config.shared_filesystems) @virtiofsd_pids = [] @mutex = Mutex.new @start_mutex = Mutex.new @qemu_mutex = Mutex.new @qemu_cv = ConditionVariable.new @shared_dir_mutex = Mutex.new @shared_dir_mounted = false FileUtils.mkdir_p(tmpdir) FileUtils.mkdir_p(sockdir) @log = MachineLog.new(File.join(tmpdir, "#{name}-log.log")) named_shells = {} worker_shell_count = config.test_shells - config.shell_names.length @shell_instances = Array.new(config.test_shells) do |i| shell_name = i >= worker_shell_count ? config.shell_names[i - worker_shell_count] : nil shell = Shell.new(self, i, shell_socket_path(i), shell_log_path(i), default_timeout:, name: shell_name) named_shells[shell_name] = shell if shell_name shell end @shell_collection = ShellCollection.new(self, named_shells) end |
Instance Attribute Details
#config ⇒ Object (readonly, protected)
Returns the value of attribute config.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def config @config end |
#console_thread ⇒ Object (readonly, protected)
Returns the value of attribute console_thread.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def console_thread @console_thread end |
#hash_base ⇒ Object (readonly, protected)
Returns the value of attribute hash_base.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def hash_base @hash_base end |
#log ⇒ Object (readonly, protected)
Returns the value of attribute log.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def log @log end |
#name ⇒ String (readonly)
18 19 20 |
# File 'lib/osvm/machine.rb', line 18 def name @name end |
#qemu_cv ⇒ Object (readonly, protected)
Returns the value of attribute qemu_cv.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def qemu_cv @qemu_cv end |
#qemu_mutex ⇒ Object (readonly, protected)
Returns the value of attribute qemu_mutex.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def qemu_mutex @qemu_mutex end |
#qemu_pid ⇒ Object (readonly, protected)
Returns the value of attribute qemu_pid.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def qemu_pid @qemu_pid end |
#qemu_read ⇒ Object (readonly, protected)
Returns the value of attribute qemu_read.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def qemu_read @qemu_read end |
#qemu_reaper ⇒ Object (readonly, protected)
Returns the value of attribute qemu_reaper.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def qemu_reaper @qemu_reaper end |
#shared_dir ⇒ Object (readonly, protected)
Returns the value of attribute shared_dir.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def shared_dir @shared_dir end |
#shared_filesystems ⇒ Object (readonly, protected)
Returns the value of attribute shared_filesystems.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def shared_filesystems @shared_filesystems end |
#shell_instances ⇒ Object (readonly, protected)
Returns the value of attribute shell_instances.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def shell_instances @shell_instances end |
#sockdir ⇒ Object (readonly, protected)
Returns the value of attribute sockdir.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def sockdir @sockdir end |
#start_kernel_params ⇒ Array<String> (readonly)
Kernel parameters passed to #start
22 23 24 |
# File 'lib/osvm/machine.rb', line 22 def start_kernel_params @start_kernel_params end |
#tmpdir ⇒ Object (readonly, protected)
Returns the value of attribute tmpdir.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def tmpdir @tmpdir end |
#virtiofsd_pids ⇒ Object (readonly, protected)
Returns the value of attribute virtiofsd_pids.
447 448 449 |
# File 'lib/osvm/machine.rb', line 447 def virtiofsd_pids @virtiofsd_pids end |
Class Method Details
.with_shell(index) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/osvm/machine.rb', line 9 def self.with_shell(index) original_index = Thread.current[SHELL_INDEX_KEY] Thread.current[SHELL_INDEX_KEY] = index yield ensure Thread.current[SHELL_INDEX_KEY] = original_index end |
Instance Method Details
#all_fail(*cmds, shell: nil) ⇒ Array<Array<[Integer, String]>>
Execute all commands and check that they all fail
313 314 315 |
# File 'lib/osvm/machine.rb', line 313 def all_fail(*cmds, shell: nil) command_shell(shell).all_fail(*cmds) end |
#all_succeed(*cmds, shell: nil) ⇒ Array<Array<[Integer, String]>>
Execute all commands and check that they all succeed
306 307 308 |
# File 'lib/osvm/machine.rb', line 306 def all_succeed(*cmds, shell: nil) command_shell(shell).all_succeed(*cmds) end |
#base_kernel_params(kernel_params) ⇒ Object (protected)
463 464 465 466 467 468 |
# File 'lib/osvm/machine.rb', line 463 def base_kernel_params(kernel_params) [ 'console=ttyS0', "init=#{config.toplevel}/init" ] + config.kernel_params + kernel_params end |
#booted? ⇒ Boolean
243 244 245 |
# File 'lib/osvm/machine.rb', line 243 def booted? current_shell.up? end |
#can_execute? ⇒ Boolean
248 249 250 |
# File 'lib/osvm/machine.rb', line 248 def can_execute? shell_instances.any?(&:up?) end |
#cleanup ⇒ Machine
Cleanup machine state
225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/osvm/machine.rb', line 225 def cleanup shell_instances.each(&:cleanup) shared_filesystems.each_key do |fs_name| File.unlink(virtiofs_socket_path(fs_name)) rescue Errno::ENOENT # ignore end self end |
#command_shell(name) ⇒ Object (protected)
741 742 743 |
# File 'lib/osvm/machine.rb', line 741 def command_shell(name) name.nil? ? current_shell : shells.fetch(name) end |
#console_log_path ⇒ Object (protected)
704 705 706 |
# File 'lib/osvm/machine.rb', line 704 def console_log_path File.join(tmpdir, "#{name}-console.log") end |
#console_output ⇒ String
Return text captured from the machine's console so far
366 367 368 |
# File 'lib/osvm/machine.rb', line 366 def console_output @mutex.synchronize { (@console_output || '').dup } end |
#current_shell ⇒ Object (protected)
737 738 739 |
# File 'lib/osvm/machine.rb', line 737 def current_shell shell_for(current_shell_index) end |
#current_shell_index ⇒ Object (protected)
731 732 733 734 735 |
# File 'lib/osvm/machine.rb', line 731 def current_shell_index index = Thread.current[SHELL_INDEX_KEY] || 0 validate_shell_index(index) index end |
#custom_qemu_numa_memory_backend? ⇒ Boolean (protected)
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/osvm/machine.rb', line 528 def custom_qemu_numa_memory_backend? opts = config. has_memory_backend = false has_numa_memdev = false opts.each_cons(2) do |arg, value| if arg == '-object' && value.start_with?('memory-backend-') has_memory_backend = true elsif arg == '-numa' && value.start_with?('node,') && value.include?('memdev=') has_numa_memdev = true end end has_memory_backend && has_numa_memdev end |
#destroy ⇒ Machine
Destroy the machine
200 201 202 203 204 205 |
# File 'lib/osvm/machine.rb', line 200 def destroy log.destroy shared_dir.destroy destroy_disks self end |
#destroy_disks ⇒ Machine
Destroy file-backed disks
Disks are destroyed automatically or when #destroy is called. #destroy_disks can be used to reset storage between machine runs.
213 214 215 216 217 218 219 220 221 |
# File 'lib/osvm/machine.rb', line 213 def destroy_disks config.disks.each do |disk| next if disk.type != 'file' || !disk.create FileUtils.rm_f(disk_path(disk.device)) end self end |
#disk_path(path) ⇒ Object (protected)
708 709 710 711 712 713 714 715 716 |
# File 'lib/osvm/machine.rb', line 708 def disk_path(path) resolved = path.gsub('{machine}', name) if resolved.start_with?('/') resolved else File.join(tmpdir, resolved) end end |
#execute(cmd, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Execute a command
263 264 265 |
# File 'lib/osvm/machine.rb', line 263 def execute(cmd, timeout: @default_timeout, shell: nil) command_shell(shell).execute(cmd, timeout:) end |
#fails(cmd, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Execute command and check that it fails
289 290 291 |
# File 'lib/osvm/machine.rb', line 289 def fails(cmd, timeout: @default_timeout, shell: nil) command_shell(shell).fails(cmd, timeout:) end |
#fails_with_retries(cmd, attempts:, retry_delay: 1, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Execute a command repeatedly until it fails or all attempts are used
299 300 301 |
# File 'lib/osvm/machine.rb', line 299 def fails_with_retries(cmd, attempts:, retry_delay: 1, timeout: @default_timeout, shell: nil) command_shell(shell).fails_with_retries(cmd, attempts:, retry_delay:, timeout:) end |
#finalize ⇒ Object
67 68 69 70 |
# File 'lib/osvm/machine.rb', line 67 def finalize log.close shell_instances.each(&:finalize) end |
#inspect ⇒ Object
441 442 443 |
# File 'lib/osvm/machine.rb', line 441 def inspect "#<#{self.class.name}:#{object_id} name=#{name}>" end |
#join(timeout: @default_timeout) ⇒ Object
Block until the machine stops
140 141 142 143 144 |
# File 'lib/osvm/machine.rb', line 140 def join(timeout: @default_timeout) _pid, reaper, = qemu_state reaper&.join(timeout) nil end |
#kill(signal: 'TERM') ⇒ Machine
Kill the machine
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/osvm/machine.rb', line 169 def kill(signal: 'TERM') pid, reaper, running = qemu_state unless running log.kill('NONE') return self end if pid.nil? log.kill('NONE') reaper&.join return self end log.kill(signal) signal_qemu(signal, pid) if signal == 'KILL' || wait_for_qemu_exit(pid, 60) reaper&.join return self end log.kill('KILL') signal_qemu('KILL', pid) reaper&.join self end |
#mkdir(path) ⇒ Machine
Create a directory inside the machine
404 405 406 407 |
# File 'lib/osvm/machine.rb', line 404 def mkdir(path) succeeds("mkdir \"#{path}\"") self end |
#mkdir_p(path) ⇒ Machine
Create a directory inside the machine
412 413 414 415 |
# File 'lib/osvm/machine.rb', line 412 def mkdir_p(path) succeeds("mkdir -p \"#{path}\"") self end |
#mount_shared_dir_once ⇒ Object (protected)
756 757 758 759 760 761 762 763 764 765 |
# File 'lib/osvm/machine.rb', line 756 def mount_shared_dir_once return if @shared_dir_mounted @shared_dir_mutex.synchronize do return if @shared_dir_mounted shared_dir.mount @shared_dir_mounted = true end end |
#poweroff_command ⇒ Object (protected)
459 460 461 |
# File 'lib/osvm/machine.rb', line 459 def poweroff_command 'poweroff -f' end |
#prepare_disks ⇒ Object (protected)
682 683 684 685 686 687 688 |
# File 'lib/osvm/machine.rb', line 682 def prepare_disks config.disks.each do |disk| next if disk.type != 'file' || !disk.create || File.exist?(disk_path(disk.device)) `truncate -s#{disk.size} #{disk_path(disk.device)}` end end |
#pull_file(src, preserve: false) ⇒ String
Pull file from the machine to the host
432 433 434 |
# File 'lib/osvm/machine.rb', line 432 def pull_file(src, preserve: false) shared_dir.pull_file(src, preserve:) end |
#push_file(src, dst, preserve: false, mkpath: false) ⇒ Machine
Push file from the host to the machine
423 424 425 426 427 |
# File 'lib/osvm/machine.rb', line 423 def push_file(src, dst, preserve: false, mkpath: false) mkdir_p(File.dirname(dst)) if mkpath shared_dir.push_file(src, dst, preserve:) self end |
#qemu_boot_media_options ⇒ Object (protected)
484 485 486 487 488 |
# File 'lib/osvm/machine.rb', line 484 def return [] if config.iso.nil? ['-cdrom', config.iso] end |
#qemu_boot_options(kernel_params) ⇒ Object (protected)
470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/osvm/machine.rb', line 470 def (kernel_params) if config.boot_mode == 'direct' [ '-kernel', config.kernel, '-initrd', config.initrd, '-append', base_kernel_params(kernel_params).join(' ') ] else ret = [] ret += ['-boot', "order=#{config.boot_order}"] if config.boot_order ret end end |
#qemu_command(kernel_params: []) ⇒ Object (protected)
451 452 453 |
# File 'lib/osvm/machine.rb', line 451 def qemu_command(kernel_params: []) raise NotImplementedError, "#{self.class} must implement #qemu_command" end |
#qemu_disk_options ⇒ Object (protected)
500 501 502 503 504 505 506 507 508 509 |
# File 'lib/osvm/machine.rb', line 500 def ret = [] config.disks.each_with_index do |disk, i| ret << '-drive' << "id=disk#{i},file=#{disk_path(disk.device)},if=none,format=raw" ret << '-device' << "ide-hd,drive=disk#{i},bus=ahci.#{i}" end ret end |
#qemu_shell_options ⇒ Object (protected)
490 491 492 493 494 495 496 497 498 |
# File 'lib/osvm/machine.rb', line 490 def ret = ['-device', 'virtio-serial'] shell_instances.each do |shell| ret += shell. end ret end |
#qemu_state ⇒ Object (protected)
628 629 630 |
# File 'lib/osvm/machine.rb', line 628 def qemu_state qemu_mutex.synchronize { [@qemu_pid, @qemu_reaper, @running] } end |
#qemu_virtiofs_options ⇒ Object (protected)
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/osvm/machine.rb', line 511 def ret = [] shared_filesystems.each_with_index do |fs, i| name, = fs ret << '-chardev' << "socket,id=char#{i},path=#{virtiofs_socket_path(name)}" ret << '-device' << "vhost-user-fs-pci,queue-size=1024,chardev=char#{i},tag=#{name}" end if ret.any? && !custom_qemu_numa_memory_backend? ret << '-object' << "memory-backend-file,id=m0,size=#{config.memory}M,mem-path=/dev/shm,share=on" ret << '-numa' << 'node,memdev=m0' end ret end |
#read_nonblock(io) ⇒ Object (protected)
767 768 769 770 771 |
# File 'lib/osvm/machine.rb', line 767 def read_nonblock(io) io.read_nonblock(4096) rescue IO::WaitReadable '' end |
#run_console_thread ⇒ Object (protected)
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 |
# File 'lib/osvm/machine.rb', line 657 def run_console_thread @console_output = '' @console_thread = Thread.new do console_log = File.open(console_log_path, 'w') begin loop do rs = qemu_read.wait_readable next unless rs data = read_nonblock(qemu_read) @mutex.synchronize { @console_output << data } console_log.write(data) console_log.flush end rescue EOFError console_log.close rescue IOError # pass end end end |
#run_qemu_reaper(pid) ⇒ Object (protected)
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
# File 'lib/osvm/machine.rb', line 576 def run_qemu_reaper(pid) Thread.new do status = wait_for_qemu(pid) begin log.exit(status) if @qemu_read @qemu_read.close @qemu_read = nil end if @console_thread console_thread.join @console_thread = nil end shell_instances.each(&:close) stop_virtiofs cleanup ensure qemu_mutex.synchronize do @qemu_reaper = nil if @qemu_reaper == Thread.current @running = false @stopped_at = Time.now qemu_cv.broadcast end end end end |
#running? ⇒ Boolean
238 239 240 |
# File 'lib/osvm/machine.rb', line 238 def running? qemu_mutex.synchronize { @running } end |
#service_check_command(_name) ⇒ Object (protected)
455 456 457 |
# File 'lib/osvm/machine.rb', line 455 def service_check_command(_name) nil end |
#shell_chardev_id(index) ⇒ Object (protected)
690 691 692 |
# File 'lib/osvm/machine.rb', line 690 def shell_chardev_id(index) shell_for(index).chardev_id end |
#shell_for(index) ⇒ Object (protected)
745 746 747 748 |
# File 'lib/osvm/machine.rb', line 745 def shell_for(index) validate_shell_index(index) @shell_instances[index] end |
#shell_log_path(index = 0) ⇒ Object (protected)
699 700 701 702 |
# File 'lib/osvm/machine.rb', line 699 def shell_log_path(index = 0) suffix = index == 0 ? 'shell' : "shell#{index}" File.join(tmpdir, "#{name}-#{suffix}.log") end |
#shell_socket_path(index = 0) ⇒ Object (protected)
694 695 696 697 |
# File 'lib/osvm/machine.rb', line 694 def shell_socket_path(index = 0) suffix = index == 0 ? 'shell' : "shell#{index}" socket_path("#{name}-#{suffix}.sock") end |
#shells ⇒ ShellCollection
437 438 439 |
# File 'lib/osvm/machine.rb', line 437 def shells @shell_collection end |
#signal_qemu(signal, pid) ⇒ Object (protected)
647 648 649 650 651 652 653 654 655 |
# File 'lib/osvm/machine.rb', line 647 def signal_qemu(signal, pid) qemu_mutex.synchronize do return unless @qemu_pid == pid Process.kill(signal, pid) end rescue Errno::ESRCH warn "Unable to kill machine #{name} using SIG#{signal}" end |
#socket_path(socket) ⇒ Object (protected)
726 727 728 729 |
# File 'lib/osvm/machine.rb', line 726 def socket_path(socket) @socket_hash ||= Digest::SHA256.hexdigest([hash_base, name].join)[0..7] File.join(sockdir, "#{@socket_hash}-#{socket}") end |
#start(kernel_params: [], wait_for_boot: false) ⇒ Machine
Start the machine
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/osvm/machine.rb', line 76 def start(kernel_params: [], wait_for_boot: false) @start_mutex.synchronize do if running? unless start_kernel_params == kernel_params raise 'Machine already started with different kernel parameters' end self.wait_for_boot if wait_for_boot return self end # virtiofsd cannot be relaunched right away, it needs some time settle # for unknown reasons, so we ensure there's a 5 second gap between stop # and start of this machine if @stopped_at diff = Time.now - @stopped_at delay = 5 sleep([delay - diff, delay].min) if diff <= delay end log.start prepare_disks shell_instances.each(&:prepare) shared_dir.setup @shared_dir_mounted = false start_virtiofs sleep(1) qemu_kwargs = {} unless @interactive_console @qemu_read, w = IO.pipe qemu_kwargs = { in: :close, out: w, err: w } end @start_kernel_params = kernel_params pid = Process.spawn( *qemu_command(kernel_params:), **qemu_kwargs ) w.close unless @interactive_console qemu_mutex.synchronize do @qemu_pid = pid @running = true @qemu_reaper = run_qemu_reaper(pid) end run_console_thread unless @interactive_console self.wait_for_boot if wait_for_boot self end end |
#start_virtiofs ⇒ Object (protected)
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/osvm/machine.rb', line 544 def start_virtiofs shared_filesystems.each do |name, path| f = File.open(virtiofs_log_path(name), 'w') virtiofsd_pids << Process.spawn( File.join(config.virtiofsd, 'bin/virtiofsd'), '--socket-path', virtiofs_socket_path(name), '--shared-dir', path, '--cache', 'auto', in: :close, out: f, err: f ) f.close end end |
#stop(timeout: @default_timeout) ⇒ Machine
Stop the machine
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/osvm/machine.rb', line 149 def stop(timeout: @default_timeout) log.stop begin execute(poweroff_command) rescue MachineShellClosed # The shell logs the failed command. end _pid, reaper, = qemu_state if reaper && reaper.join(timeout).nil? raise UnrecoverableTimeoutError, "Timeout while stopping machine #{name}" end self end |
#stop_virtiofs ⇒ Object (protected)
562 563 564 565 566 567 568 569 570 571 572 573 574 |
# File 'lib/osvm/machine.rb', line 562 def stop_virtiofs virtiofsd_pids.delete_if do |pid| Process.kill('TERM', pid) false rescue Errno::ESRCH true end virtiofsd_pids.delete_if do |pid| Process.wait(pid) true end end |
#succeeds(cmd, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Execute command and check that it succeeds
271 272 273 |
# File 'lib/osvm/machine.rb', line 271 def succeeds(cmd, timeout: @default_timeout, shell: nil) command_shell(shell).succeeds(cmd, timeout:) end |
#succeeds_with_retries(cmd, attempts:, retry_delay: 1, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Execute a command repeatedly until it succeeds or all attempts are used
281 282 283 |
# File 'lib/osvm/machine.rb', line 281 def succeeds_with_retries(cmd, attempts:, retry_delay: 1, timeout: @default_timeout, shell: nil) command_shell(shell).succeeds_with_retries(cmd, attempts:, retry_delay:, timeout:) end |
#validate_shell_index(index) ⇒ Object (protected)
750 751 752 753 754 |
# File 'lib/osvm/machine.rb', line 750 def validate_shell_index(index) return if index.is_a?(Integer) && index >= 0 && index < config.test_shells raise ArgumentError, "invalid shell index #{index.inspect}" end |
#virtiofs_log_path(mount_name) ⇒ Object (protected)
722 723 724 |
# File 'lib/osvm/machine.rb', line 722 def virtiofs_log_path(mount_name) File.join(tmpdir, "#{name}-fs-#{mount_name}.log") end |
#virtiofs_socket_path(mount_name) ⇒ Object (protected)
718 719 720 |
# File 'lib/osvm/machine.rb', line 718 def virtiofs_socket_path(mount_name) socket_path("#{name}-fs-#{mount_name}.sock") end |
#wait_for_boot(timeout: @default_timeout) ⇒ Object
Wait until the system has booted
254 255 256 |
# File 'lib/osvm/machine.rb', line 254 def wait_for_boot(timeout: @default_timeout) current_shell.wait(timeout:) end |
#wait_for_console_text(regex, timeout: @default_timeout) ⇒ Machine
Wait for text to appear in console output
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/osvm/machine.rb', line 373 def wait_for_console_text(regex, timeout: @default_timeout) t1 = Time.now cur_timeout = timeout log_started_at = log.console_wait_begin(regex) loop do if regex =~ console_output log.console_wait_end(true, nil, log_started_at) return self end cur_timeout = timeout - (Time.now - t1) if cur_timeout <= 0 log.console_wait_end(false, 'timeout', log_started_at) raise TimeoutError, "Timeout occurred while waiting for #{regex.inspect} on the console" elsif !running? log.console_wait_end(false, 'machine not running', log_started_at) raise Error, 'Machine is not running' end sleep(1) end self end |
#wait_for_qemu(pid) ⇒ Object (protected)
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
# File 'lib/osvm/machine.rb', line 609 def wait_for_qemu(pid) loop do result = qemu_mutex.synchronize do ret = Process.wait2(pid, Process::WNOHANG) if ret @qemu_pid = nil if @qemu_pid == pid qemu_cv.broadcast end ret end return result.last if result sleep(QEMU_REAP_INTERVAL) end end |
#wait_for_qemu_exit(pid, timeout) ⇒ Object (protected)
632 633 634 635 636 637 638 639 640 641 642 643 644 645 |
# File 'lib/osvm/machine.rb', line 632 def wait_for_qemu_exit(pid, timeout) deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout qemu_mutex.synchronize do while @qemu_pid == pid remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC) return false if remaining <= 0 qemu_cv.wait(qemu_mutex, remaining) end end true end |
#wait_for_service(name) ⇒ Machine
Wait for a system service to start
356 357 358 359 360 361 362 |
# File 'lib/osvm/machine.rb', line 356 def wait_for_service(name) cmd = service_check_command(name) raise NotImplementedError, "#{self.class} does not implement service checks" if cmd.nil? wait_until_succeeds(cmd) self end |
#wait_for_shutdown(timeout: @default_timeout) ⇒ Machine
Wait until the machine shuts down
339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/osvm/machine.rb', line 339 def wait_for_shutdown(timeout: @default_timeout) t1 = Time.now loop do return self unless running? if t1 + timeout < Time.now raise TimeoutError, 'Timeout occurred while waiting for shutdown' end sleep(1) end end |
#wait_until_fails(cmd, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Wait until command fails
325 326 327 |
# File 'lib/osvm/machine.rb', line 325 def wait_until_fails(cmd, timeout: @default_timeout, shell: nil) command_shell(shell).wait_until_fails(cmd, timeout:) end |
#wait_until_online(timeout: @default_timeout) ⇒ Machine
Wait until network is operational, including DNS
331 332 333 334 |
# File 'lib/osvm/machine.rb', line 331 def wait_until_online(timeout: @default_timeout) wait_until_succeeds('curl --head https://check-online.vpsadminos.org', timeout:) self end |
#wait_until_succeeds(cmd, timeout: @default_timeout, shell: nil) ⇒ Array<Integer, String>
Wait until command succeeds
319 320 321 |
# File 'lib/osvm/machine.rb', line 319 def wait_until_succeeds(cmd, timeout: @default_timeout, shell: nil) command_shell(shell).wait_until_succeeds(cmd, timeout:) end |