Class: OsVm::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/osvm/machine.rb

Direct Known Subclasses

NixosMachine, VpsadminosMachine

Constant Summary collapse

SHELL_INDEX_KEY =
:osvm_machine_shell_index
QEMU_REAP_INTERVAL =
0.1
KERNEL_FAILURE_PATTERN =
Regexp.union(
  /BUG: unable to handle/,
  /BUG: kernel NULL pointer dereference/,
  /kernel BUG at/,
  /Oops:/,
  /general protection fault/,
  /Kernel panic - not syncing:/
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config, tmpdir, sockdir, default_timeout: 900, hash_base: '', interactive_console: false) ⇒ Machine

Returns a new instance of Machine.

Parameters:

  • name (String)
  • config (MachineConfig)
  • tmpdir (String)
  • sockdir (String)
  • default_timeout (Integer) (defaults to: 900)
  • hash_base (String) (defaults to: '')
  • interactive_console (Boolean) (defaults to: false)


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
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/osvm/machine.rb', line 39

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
  @kernel_failure = nil
  @kernel_failure_detected_at = nil
  @allowed_kernel_failure_patterns = []
  @console_output = ''
  @console_scan_buffer = ''

  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

#configObject (readonly, protected)

Returns the value of attribute config.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def config
  @config
end

#console_threadObject (readonly, protected)

Returns the value of attribute console_thread.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def console_thread
  @console_thread
end

#hash_baseObject (readonly, protected)

Returns the value of attribute hash_base.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def hash_base
  @hash_base
end

#logObject (readonly, protected)

Returns the value of attribute log.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def log
  @log
end

#nameString (readonly)

Returns:

  • (String)


26
27
28
# File 'lib/osvm/machine.rb', line 26

def name
  @name
end

#qemu_cvObject (readonly, protected)

Returns the value of attribute qemu_cv.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def qemu_cv
  @qemu_cv
end

#qemu_mutexObject (readonly, protected)

Returns the value of attribute qemu_mutex.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def qemu_mutex
  @qemu_mutex
end

#qemu_pidObject (readonly, protected)

Returns the value of attribute qemu_pid.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def qemu_pid
  @qemu_pid
end

#qemu_readObject (readonly, protected)

Returns the value of attribute qemu_read.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def qemu_read
  @qemu_read
end

#qemu_reaperObject (readonly, protected)

Returns the value of attribute qemu_reaper.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def qemu_reaper
  @qemu_reaper
end

#shared_dirObject (readonly, protected)

Returns the value of attribute shared_dir.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def shared_dir
  @shared_dir
end

#shared_filesystemsObject (readonly, protected)

Returns the value of attribute shared_filesystems.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def shared_filesystems
  @shared_filesystems
end

#shell_instancesObject (readonly, protected)

Returns the value of attribute shell_instances.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def shell_instances
  @shell_instances
end

#sockdirObject (readonly, protected)

Returns the value of attribute sockdir.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def sockdir
  @sockdir
end

#start_kernel_paramsArray<String> (readonly)

Kernel parameters passed to #start

Returns:

  • (Array<String>)


30
31
32
# File 'lib/osvm/machine.rb', line 30

def start_kernel_params
  @start_kernel_params
end

#tmpdirObject (readonly, protected)

Returns the value of attribute tmpdir.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def tmpdir
  @tmpdir
end

#virtiofsd_pidsObject (readonly, protected)

Returns the value of attribute virtiofsd_pids.



509
510
511
# File 'lib/osvm/machine.rb', line 509

def virtiofsd_pids
  @virtiofsd_pids
end

Class Method Details

.with_shell(index) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/osvm/machine.rb', line 17

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

Parameters:

  • cmds (String)

Returns:

  • (Array<Array<[Integer, String]>>)


329
330
331
# File 'lib/osvm/machine.rb', line 329

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

Parameters:

  • cmds (String)

Returns:

  • (Array<Array<[Integer, String]>>)


322
323
324
# File 'lib/osvm/machine.rb', line 322

def all_succeed(*cmds, shell: nil)
  command_shell(shell).all_succeed(*cmds)
end

#allow_kernel_failure(pattern) ⇒ Object

Allow matching fatal kernel console output only while the block runs. Other kernel failure signatures remain fatal.

Parameters:

  • pattern (Regexp)


384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/osvm/machine.rb', line 384

def allow_kernel_failure(pattern)
  raise ArgumentError, 'pattern must be a Regexp' unless pattern.is_a?(Regexp)

  @mutex.synchronize { @allowed_kernel_failure_patterns << pattern }
  yield
ensure
  if pattern.is_a?(Regexp)
    @mutex.synchronize do
      i = @allowed_kernel_failure_patterns.rindex(pattern)
      @allowed_kernel_failure_patterns.delete_at(i) if i
    end
  end
end

#append_console_output(data, flush: false) ⇒ Object (protected)



768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# File 'lib/osvm/machine.rb', line 768

def append_console_output(data, flush: false)
  events = @mutex.synchronize do
    @console_output << data
    @console_scan_buffer << data

    lines = @console_scan_buffer.split("\n", -1)
    @console_scan_buffer = lines.pop || ''

    if flush && !@console_scan_buffer.empty?
      lines << @console_scan_buffer
      @console_scan_buffer = ''
    end

    lines.filter_map do |raw_line|
      line = raw_line.delete_suffix("\r")
      next unless KERNEL_FAILURE_PATTERN.match?(line)

      expected = @allowed_kernel_failure_patterns.any? { |pattern| pattern.match?(line) }

      unless expected || @kernel_failure
        @kernel_failure = { line: }
        @kernel_failure_detected_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      end

      { line:, expected: }
    end
  end

  events.each { |event| log.kernel_failure(event.fetch(:line), expected: event.fetch(:expected)) }
end

#base_kernel_params(kernel_params) ⇒ Object (protected)



525
526
527
528
529
530
# File 'lib/osvm/machine.rb', line 525

def base_kernel_params(kernel_params)
  [
    'console=ttyS0',
    "init=#{config.toplevel}/init"
  ] + config.kernel_params + kernel_params
end

#booted?Boolean

Returns:

  • (Boolean)


259
260
261
# File 'lib/osvm/machine.rb', line 259

def booted?
  current_shell.up?
end

#can_execute?Boolean

Returns:

  • (Boolean)


264
265
266
# File 'lib/osvm/machine.rb', line 264

def can_execute?
  shell_instances.any?(&:up?)
end

#cleanupMachine

Cleanup machine state

Returns:



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/osvm/machine.rb', line 241

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)



858
859
860
# File 'lib/osvm/machine.rb', line 858

def command_shell(name)
  name.nil? ? current_shell : shells.fetch(name)
end

#console_log_pathObject (protected)



821
822
823
# File 'lib/osvm/machine.rb', line 821

def console_log_path
  File.join(tmpdir, "#{name}-console.log")
end

#console_outputString

Return text captured from the machine's console so far

Returns:

  • (String)


426
427
428
# File 'lib/osvm/machine.rb', line 426

def console_output
  @mutex.synchronize { (@console_output || '').dup }
end

#current_shellObject (protected)



854
855
856
# File 'lib/osvm/machine.rb', line 854

def current_shell
  shell_for(current_shell_index)
end

#current_shell_indexObject (protected)



848
849
850
851
852
# File 'lib/osvm/machine.rb', line 848

def current_shell_index
  index = Thread.current[SHELL_INDEX_KEY] || 0
  validate_shell_index(index)
  index
end

#custom_qemu_numa_memory_backend?Boolean (protected)

Returns:

  • (Boolean)


590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/osvm/machine.rb', line 590

def custom_qemu_numa_memory_backend?
  opts = config.extra_qemu_options
  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

#destroyMachine

Destroy the machine

Returns:



216
217
218
219
220
221
# File 'lib/osvm/machine.rb', line 216

def destroy
  log.destroy
  shared_dir.destroy
  destroy_disks
  self
end

#destroy_disksMachine

Destroy file-backed disks

Disks are destroyed automatically or when #destroy is called. #destroy_disks can be used to reset storage between machine runs.

Returns:



229
230
231
232
233
234
235
236
237
# File 'lib/osvm/machine.rb', line 229

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)



825
826
827
828
829
830
831
832
833
# File 'lib/osvm/machine.rb', line 825

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

Parameters:

  • cmd (String)
  • timeout (Integer) (defaults to: @default_timeout)

Returns:

  • (Array<Integer, String>)

    exit status and output

Raises:



279
280
281
# File 'lib/osvm/machine.rb', line 279

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

Parameters:

  • cmd (String)
  • timeout (Integer) (defaults to: @default_timeout)

Returns:

  • (Array<Integer, String>)


305
306
307
# File 'lib/osvm/machine.rb', line 305

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

Parameters:

  • cmd (String)
  • attempts (Integer)
  • retry_delay (Numeric) (defaults to: 1)
  • timeout (Integer) (defaults to: @default_timeout)

    timeout for each attempt

Returns:

  • (Array<Integer, String>)


315
316
317
# File 'lib/osvm/machine.rb', line 315

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

#finalizeObject



80
81
82
83
# File 'lib/osvm/machine.rb', line 80

def finalize
  log.close
  shell_instances.each(&:finalize)
end

#inspectObject



503
504
505
# File 'lib/osvm/machine.rb', line 503

def inspect
  "#<#{self.class.name}:#{object_id} name=#{name}>"
end

#join(timeout: @default_timeout) ⇒ Object

Block until the machine stops



154
155
156
157
158
159
# File 'lib/osvm/machine.rb', line 154

def join(timeout: @default_timeout)
  _pid, reaper, = qemu_state
  wait_for_reaper(reaper, timeout:) if reaper
  raise_if_kernel_failed!
  nil
end

#kernel_failed?Boolean

Returns:

  • (Boolean)


398
399
400
# File 'lib/osvm/machine.rb', line 398

def kernel_failed?
  @mutex.synchronize { !@kernel_failure.nil? }
end

#kill(signal: 'TERM') ⇒ Machine

Kill the machine

Parameters:

  • signal ('TERM', 'KILL') (defaults to: 'TERM')

Returns:



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/osvm/machine.rb', line 185

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

#kill_after_kernel_failure(drain_timeout: 1) ⇒ Object



413
414
415
416
417
418
419
420
421
422
# File 'lib/osvm/machine.rb', line 413

def kill_after_kernel_failure(drain_timeout: 1)
  detected_at = @mutex.synchronize { @kernel_failure_detected_at }

  if detected_at
    remaining = drain_timeout - (Process.clock_gettime(Process::CLOCK_MONOTONIC) - detected_at)
    sleep(remaining) if remaining > 0
  end

  kill(signal: 'KILL')
end

#mkdir(path) ⇒ Machine

Create a directory inside the machine

Parameters:

  • path (String)

    path within the machine

Returns:



466
467
468
469
# File 'lib/osvm/machine.rb', line 466

def mkdir(path)
  succeeds("mkdir \"#{path}\"")
  self
end

#mkdir_p(path) ⇒ Machine

Create a directory inside the machine

Parameters:

  • path (String)

    path within the machine

Returns:



474
475
476
477
# File 'lib/osvm/machine.rb', line 474

def mkdir_p(path)
  succeeds("mkdir -p \"#{path}\"")
  self
end

#mount_shared_dir_onceObject (protected)



873
874
875
876
877
878
879
880
881
882
# File 'lib/osvm/machine.rb', line 873

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_commandObject (protected)



521
522
523
# File 'lib/osvm/machine.rb', line 521

def poweroff_command
  'poweroff -f'
end

#prepare_disksObject (protected)



799
800
801
802
803
804
805
# File 'lib/osvm/machine.rb', line 799

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

Parameters:

  • src (String)

    file within the machine

Returns:

  • (String)

    path to the file on the host



494
495
496
# File 'lib/osvm/machine.rb', line 494

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

Parameters:

  • src (String)

    file on the host

  • dst (String)

    file within the machine

  • preserve (Boolean) (defaults to: false)
  • mkpath (Boolean) (defaults to: false)

Returns:



485
486
487
488
489
# File 'lib/osvm/machine.rb', line 485

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_optionsObject (protected)



546
547
548
549
550
# File 'lib/osvm/machine.rb', line 546

def qemu_boot_media_options
  return [] if config.iso.nil?

  ['-cdrom', config.iso]
end

#qemu_boot_options(kernel_params) ⇒ Object (protected)



532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/osvm/machine.rb', line 532

def qemu_boot_options(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)

Raises:

  • (NotImplementedError)


513
514
515
# File 'lib/osvm/machine.rb', line 513

def qemu_command(kernel_params: [])
  raise NotImplementedError, "#{self.class} must implement #qemu_command"
end

#qemu_disk_optionsObject (protected)



562
563
564
565
566
567
568
569
570
571
# File 'lib/osvm/machine.rb', line 562

def qemu_disk_options
  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_optionsObject (protected)



552
553
554
555
556
557
558
559
560
# File 'lib/osvm/machine.rb', line 552

def qemu_shell_options
  ret = ['-device', 'virtio-serial']

  shell_instances.each do |shell|
    ret += shell.qemu_options
  end

  ret
end

#qemu_stateObject (protected)



690
691
692
# File 'lib/osvm/machine.rb', line 690

def qemu_state
  qemu_mutex.synchronize { [@qemu_pid, @qemu_reaper, @running] }
end

#qemu_virtiofs_optionsObject (protected)



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/osvm/machine.rb', line 573

def qemu_virtiofs_options
  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

#raise_if_kernel_failed!Object

Raises:



402
403
404
405
406
407
408
409
410
411
# File 'lib/osvm/machine.rb', line 402

def raise_if_kernel_failed!
  failure = @mutex.synchronize { @kernel_failure&.dup }
  return self unless failure

  raise KernelFailure.new(
    machine_name: name,
    console_line: failure.fetch(:line),
    console_log_path:
  )
end

#read_nonblock(io) ⇒ Object (protected)



884
885
886
887
888
# File 'lib/osvm/machine.rb', line 884

def read_nonblock(io)
  io.read_nonblock(4096)
rescue IO::WaitReadable
  ''
end

#reset_kernel_failureObject (protected)



759
760
761
762
763
764
765
766
# File 'lib/osvm/machine.rb', line 759

def reset_kernel_failure
  @mutex.synchronize do
    @kernel_failure = nil
    @kernel_failure_detected_at = nil
    @console_output = ''
    @console_scan_buffer = ''
  end
end

#run_console_threadObject (protected)



734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# File 'lib/osvm/machine.rb', line 734

def run_console_thread
  @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)
        append_console_output(data)

        console_log.write(data)
        console_log.flush
      end
    rescue EOFError
    rescue IOError
      # pass
    ensure
      append_console_output('', flush: true)
      console_log.close unless console_log.closed?
    end
  end
end

#run_qemu_reaper(pid) ⇒ Object (protected)



638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
# File 'lib/osvm/machine.rb', line 638

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

Returns:

  • (Boolean)


254
255
256
# File 'lib/osvm/machine.rb', line 254

def running?
  qemu_mutex.synchronize { @running }
end

#service_check_command(_name) ⇒ Object (protected)



517
518
519
# File 'lib/osvm/machine.rb', line 517

def service_check_command(_name)
  nil
end

#shell_chardev_id(index) ⇒ Object (protected)



807
808
809
# File 'lib/osvm/machine.rb', line 807

def shell_chardev_id(index)
  shell_for(index).chardev_id
end

#shell_for(index) ⇒ Object (protected)



862
863
864
865
# File 'lib/osvm/machine.rb', line 862

def shell_for(index)
  validate_shell_index(index)
  @shell_instances[index]
end

#shell_log_path(index = 0) ⇒ Object (protected)



816
817
818
819
# File 'lib/osvm/machine.rb', line 816

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)



811
812
813
814
# File 'lib/osvm/machine.rb', line 811

def shell_socket_path(index = 0)
  suffix = index == 0 ? 'shell' : "shell#{index}"
  socket_path("#{name}-#{suffix}.sock")
end

#shellsShellCollection

Returns:



499
500
501
# File 'lib/osvm/machine.rb', line 499

def shells
  @shell_collection
end

#signal_qemu(signal, pid) ⇒ Object (protected)



724
725
726
727
728
729
730
731
732
# File 'lib/osvm/machine.rb', line 724

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)



843
844
845
846
# File 'lib/osvm/machine.rb', line 843

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

Parameters:

  • kernel_params (Array<String>) (defaults to: [])
  • wait_for_boot (Boolean) (defaults to: false)

Returns:



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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/osvm/machine.rb', line 89

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
    reset_kernel_failure

    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_virtiofsObject (protected)



606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# File 'lib/osvm/machine.rb', line 606

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

Parameters:

  • timeout (Integer) (defaults to: @default_timeout)

Returns:



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/osvm/machine.rb', line 164

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 && !wait_for_reaper(reaper, timeout:)
    raise UnrecoverableTimeoutError, "Timeout while stopping machine #{name}"
  end

  raise_if_kernel_failed!
  self
end

#stop_virtiofsObject (protected)



624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/osvm/machine.rb', line 624

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

Parameters:

  • cmd (String)
  • timeout (Integer) (defaults to: @default_timeout)

Returns:

  • (Array<Integer, String>)


287
288
289
# File 'lib/osvm/machine.rb', line 287

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

Parameters:

  • cmd (String)
  • attempts (Integer)
  • retry_delay (Numeric) (defaults to: 1)
  • timeout (Integer) (defaults to: @default_timeout)

    timeout for each attempt

Returns:

  • (Array<Integer, String>)


297
298
299
# File 'lib/osvm/machine.rb', line 297

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)

Raises:

  • (ArgumentError)


867
868
869
870
871
# File 'lib/osvm/machine.rb', line 867

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)



839
840
841
# File 'lib/osvm/machine.rb', line 839

def virtiofs_log_path(mount_name)
  File.join(tmpdir, "#{name}-fs-#{mount_name}.log")
end

#virtiofs_socket_path(mount_name) ⇒ Object (protected)



835
836
837
# File 'lib/osvm/machine.rb', line 835

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

Parameters:

  • timeout (Integer) (defaults to: @default_timeout)


270
271
272
# File 'lib/osvm/machine.rb', line 270

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

Parameters:

  • regex (Regexp)

Returns:



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/osvm/machine.rb', line 433

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
    raise_if_kernel_failed!

    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)



671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/osvm/machine.rb', line 671

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)



694
695
696
697
698
699
700
701
702
703
704
705
706
707
# File 'lib/osvm/machine.rb', line 694

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_reaper(reaper, timeout:) ⇒ Object (protected)



709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'lib/osvm/machine.rb', line 709

def wait_for_reaper(reaper, timeout:)
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout

  loop do
    raise_if_kernel_failed!
    remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
    return false if remaining <= 0

    if reaper.join([remaining, 1].min)
      raise_if_kernel_failed!
      return true
    end
  end
end

#wait_for_service(name) ⇒ Machine

Wait for a system service to start

Parameters:

  • name (String)

Returns:

Raises:

  • (NotImplementedError)


373
374
375
376
377
378
379
# File 'lib/osvm/machine.rb', line 373

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

Parameters:

  • timeout (Integer) (defaults to: @default_timeout)

Returns:



355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/osvm/machine.rb', line 355

def wait_for_shutdown(timeout: @default_timeout)
  t1 = Time.now

  loop do
    raise_if_kernel_failed!
    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

Returns:

  • (Array<Integer, String>)


341
342
343
# File 'lib/osvm/machine.rb', line 341

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

Returns:



347
348
349
350
# File 'lib/osvm/machine.rb', line 347

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

Returns:

  • (Array<Integer, String>)


335
336
337
# File 'lib/osvm/machine.rb', line 335

def wait_until_succeeds(cmd, timeout: @default_timeout, shell: nil)
  command_shell(shell).wait_until_succeeds(cmd, timeout:)
end