Class: QEMU::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/qemu/command.rb

Defined Under Namespace

Classes: Disk, Disks

Constant Summary collapse

@@command_system_aliases =
{
  "amd64" => "x86_64"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#alsa_adc_devObject

Returns the value of attribute alsa_adc_dev.



79
80
81
# File 'lib/qemu/command.rb', line 79

def alsa_adc_dev
  @alsa_adc_dev
end

#alsa_dac_devObject

Returns the value of attribute alsa_dac_dev.



79
80
81
# File 'lib/qemu/command.rb', line 79

def alsa_dac_dev
  @alsa_dac_dev
end

#architectureObject

Returns the value of attribute architecture.



12
13
14
# File 'lib/qemu/command.rb', line 12

def architecture
  @architecture
end

#audio_driverObject

Returns the value of attribute audio_driver.



74
75
76
# File 'lib/qemu/command.rb', line 74

def audio_driver
  @audio_driver
end

#disksObject (readonly)

Returns the value of attribute disks.



18
19
20
# File 'lib/qemu/command.rb', line 18

def disks
  @disks
end

#mac_addressObject

Returns the value of attribute mac_address.



52
53
54
# File 'lib/qemu/command.rb', line 52

def mac_address
  @mac_address
end

#memoryObject

Returns the value of attribute memory.



10
11
12
# File 'lib/qemu/command.rb', line 10

def memory
  @memory
end

#monitorObject

Returns the value of attribute monitor.



61
62
63
# File 'lib/qemu/command.rb', line 61

def monitor
  @monitor
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/qemu/command.rb', line 4

def name
  @name
end

#sound_hardwareObject

Returns the value of attribute sound_hardware.



73
74
75
# File 'lib/qemu/command.rb', line 73

def sound_hardware
  @sound_hardware
end

#telnet_portObject

Returns the value of attribute telnet_port.



62
63
64
# File 'lib/qemu/command.rb', line 62

def telnet_port
  @telnet_port
end

#usbObject

Returns the value of attribute usb.



81
82
83
# File 'lib/qemu/command.rb', line 81

def usb
  @usb
end

#vde_optionsObject

Returns the value of attribute vde_options.



54
55
56
# File 'lib/qemu/command.rb', line 54

def vde_options
  @vde_options
end

#vncObject

Returns the value of attribute vnc.



59
60
61
# File 'lib/qemu/command.rb', line 59

def vnc
  @vnc
end

Instance Method Details

#commandObject



120
121
122
123
# File 'lib/qemu/command.rb', line 120

def command
  system = @@command_system_aliases.fetch(architecture.to_s, architecture.to_s)
  "/usr/bin/qemu-system-#{system}"
end

#command_argumentsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/qemu/command.rb', line 83

def command_arguments
  [].tap do |args|
    args << "-enable-kvm"

    args << "-m" << "#{memory}m" if memory
    disks.each_with_index do |disk, index|
      args << "-drive" << disk.qemu_drive(index)
    end
    args << "-net" << "nic,macaddr=#{mac_address}"
    unless vde_options.empty?
      args << "-net" << "vde," + vde_options.map { |k,v| "#{k}=#{v}" }.join(',')
    end
    if vnc
      args << "-vnc" << vnc
    else
      args << "-nographic"
    end

    args << "-usb" if usb

    args << "-monitor" << monitor if monitor
    args << "-soundhw" << sound_hardware if sound_hardware
  end
end

#command_envObject



108
109
110
111
112
113
114
# File 'lib/qemu/command.rb', line 108

def command_env
  {}.tap do |env|
    env["QEMU_AUDIO_DRV"] = audio_driver
    env["QEMU_ALSA_DAC_DEV"] = alsa_dac_dev if alsa_dac_dev
    env["QEMU_ALSA_ADC_DEV"] = alsa_adc_dev if alsa_adc_dev
  end
end

#daemonObject



130
131
132
133
134
135
136
# File 'lib/qemu/command.rb', line 130

def daemon
  @daemon ||=
    begin
      QEMU.logger.debug "Prepare daemon with '#{command_arguments}'"
      Daemon.new :command => command, :name => name, :arguments => command_arguments, :env => command_env
    end
end

#qemu_monitorObject



69
70
71
# File 'lib/qemu/command.rb', line 69

def qemu_monitor
  @qemu_monitor ||= QEMU::Monitor.new :port => telnet_port if telnet_port
end

#runObject



125
126
127
128
# File 'lib/qemu/command.rb', line 125

def run
  shell_env = command_env.map { |k,v| "#{k}=#{v}" }.join(' ')
  system "#{shell_env} #{command} #{command_arguments.join(' ')}"
end