Class: OsVm::NixosMachine

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

Overview

NixOS-specific machine driver

Instance Attribute Summary

Attributes inherited from Machine

#config, #console_thread, #hash_base, #log, #name, #qemu_pid, #qemu_read, #qemu_reaper, #shared_dir, #shared_filesystems, #shell, #shell_server, #sockdir, #start_kernel_params, #tmpdir, #virtiofsd_pids

Instance Method Summary collapse

Methods inherited from Machine

#all_fail, #all_succeed, #base_kernel_params, #booted?, #can_execute?, #cleanup, #console_log_path, #destroy, #disk_path, #execute, #fails, #finalize, #initialize, #inspect, #join, #kill, #mkdir, #mkdir_p, #pull_file, #push_file, #qemu_virtiofs_options, #read_nonblock, #read_shell_output, #run_console_thread, #run_qemu_reaper, #running?, #shell_socket_path, #shell_up?, #socket_path, #start, #start_virtiofs, #stop, #stop_virtiofs, #succeeds, #virtiofs_log_path, #virtiofs_socket_path, #wait_for_boot, #wait_for_console_text, #wait_for_service, #wait_for_shell, #wait_for_shutdown, #wait_until_fails, #wait_until_online, #wait_until_succeeds

Constructor Details

This class inherits a constructor from OsVm::Machine

Instance Method Details

#destroy_disksObject



4
5
6
7
# File 'lib/osvm/nixos_machine.rb', line 4

def destroy_disks
  FileUtils.rm_f(root_disk_path)
  super
end

#prepare_disksObject (protected)



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/osvm/nixos_machine.rb', line 52

def prepare_disks
  super

  # Copy the NixOS disk image into a writable location for the VM.
  # Always refresh to ensure a clean state between runs.
  FileUtils.rm_f(root_disk_path)
  FileUtils.cp(config.disk_image, root_disk_path)
  begin
    File.chmod(0o644, root_disk_path)
  rescue StandardError
    # ignore chmod failures
  end
end

#qemu_command(kernel_params: []) ⇒ Object (protected)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/osvm/nixos_machine.rb', line 15

def qemu_command(kernel_params: [])
  all_kernel_params = base_kernel_params(kernel_params)

  [
    "#{config.qemu}/bin/qemu-kvm",
    '-name', "os-vm-#{name}",
    '-m', config.memory.to_s,
    '-cpu', 'host',
    '-smp', "cpus=#{config.cpus},cores=#{config.cpu.cores},threads=#{config.cpu.threads},sockets=#{config.cpu.sockets}",
    '--no-reboot',
    '-device', 'ahci,id=ahci'
  ] + config.networks.map(&:qemu_options).flatten + [
    '-chardev', "socket,id=shell,path=#{shell_socket_path}",
    '-device', 'virtio-serial',
    '-device', 'virtconsole,chardev=shell',
    '-kernel', config.kernel,
    '-initrd', config.initrd,
    '-append', all_kernel_params.join(' '),
    '-nographic'
  ] + qemu_disk_options + qemu_virtiofs_options + config.extra_qemu_options
end

#qemu_disk_optionsObject (protected)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/osvm/nixos_machine.rb', line 37

def qemu_disk_options
  ret = []

  ret << '-drive' << "id=diskroot,file=#{root_disk_path},if=none,format=raw"
  ret << '-device' << 'ide-hd,drive=diskroot,bus=ahci.0'

  config.disks.each_with_index do |disk, i|
    idx = i + 1
    ret << '-drive' << "id=disk#{idx},file=#{disk_path(disk.device)},if=none,format=raw"
    ret << '-device' << "ide-hd,drive=disk#{idx},bus=ahci.#{idx}"
  end

  ret
end

#root_disk_pathObject (protected)



66
67
68
# File 'lib/osvm/nixos_machine.rb', line 66

def root_disk_path
  @root_disk_path ||= disk_path("#{name}-root.img")
end

#service_check_command(name) ⇒ Object (protected)



11
12
13
# File 'lib/osvm/nixos_machine.rb', line 11

def service_check_command(name)
  "systemctl is-active --quiet #{name}"
end