Class: VagrantPlugins::QEMU::Driver
- Inherits:
-
Object
- Object
- VagrantPlugins::QEMU::Driver
- Defined in:
- lib/vagrant-qemu/driver.rb
Instance Attribute Summary collapse
-
#data_dir ⇒ Object
readonly
Returns the value of attribute data_dir.
-
#tmp_dir ⇒ Object
readonly
Returns the value of attribute tmp_dir.
-
#vm_id ⇒ String
readonly
VM ID.
Instance Method Summary collapse
- #created? ⇒ Boolean
- #delete ⇒ Object
- #execute(*cmd, **opts, &block) ⇒ Object
- #get_current_state ⇒ Object
- #get_ssh_port(ssh_port) ⇒ Object
- #import(options) ⇒ Object
-
#initialize(id, dir, tmp) ⇒ Driver
constructor
A new instance of Driver.
- #running? ⇒ Boolean
- #start(options) ⇒ Object
- #stop(options) ⇒ Object
Constructor Details
#initialize(id, dir, tmp) ⇒ Driver
Returns a new instance of Driver.
20 21 22 23 24 |
# File 'lib/vagrant-qemu/driver.rb', line 20 def initialize(id, dir, tmp) @vm_id = id @data_dir = dir @tmp_dir = tmp.join("vagrant-qemu") end |
Instance Attribute Details
#data_dir ⇒ Object (readonly)
Returns the value of attribute data_dir.
17 18 19 |
# File 'lib/vagrant-qemu/driver.rb', line 17 def data_dir @data_dir end |
#tmp_dir ⇒ Object (readonly)
Returns the value of attribute tmp_dir.
18 19 20 |
# File 'lib/vagrant-qemu/driver.rb', line 18 def tmp_dir @tmp_dir end |
#vm_id ⇒ String (readonly)
Returns VM ID.
16 17 18 |
# File 'lib/vagrant-qemu/driver.rb', line 16 def vm_id @vm_id end |
Instance Method Details
#created? ⇒ Boolean
208 209 210 |
# File 'lib/vagrant-qemu/driver.rb', line 208 def created? result = @data_dir.join(@vm_id).directory? end |
#delete ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/vagrant-qemu/driver.rb', line 37 def delete if created? id_dir = @data_dir.join(@vm_id) FileUtils.rm_rf(id_dir) id_tmp_dir = @tmp_dir.join(@vm_id) FileUtils.rm_rf(id_tmp_dir) end end |
#execute(*cmd, **opts, &block) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/vagrant-qemu/driver.rb', line 224 def execute(*cmd, **opts, &block) result = nil if opts && opts[:detach] # give it some time to startup timeout = 5 # edit version of "Subprocess.execute" for detach workdir = Dir.pwd process = ChildProcess.build(*cmd) stdout, stdout_writer = ::IO.pipe stderr, stderr_writer = ::IO.pipe process.io.stdout = stdout_writer process.io.stderr = stderr_writer process.leader = true process.detach = true ::Vagrant::Util::SafeChdir.safe_chdir(workdir) do process.start end if RUBY_PLATFORM != "java" stdout_writer.close stderr_writer.close end io_data = { stdout: "", stderr: "" } start_time = Time.now.to_i open_readers = [stdout, stderr] while true results = ::IO.select(open_readers, nil, nil, 0.1) results ||= [] readers = results[0] # Check if we have exceeded our timeout return if (Time.now.to_i - start_time) > timeout if readers && !readers.empty? readers.each do |r| data = ::Vagrant::Util::IO.read_until_block(r) next if data.empty? io_name = r == stdout ? :stdout : :stderr io_data[io_name] += data end end break if process.exited? end if RUBY_PLATFORM == "java" stdout_writer.close stderr_writer.close end result = ::Vagrant::Util::Subprocess::Result.new(process.exit_code, io_data[:stdout], io_data[:stderr]) else # Append in the options for subprocess cmd << { notify: [:stdout, :stderr, :stdin] } interrupted = false int_callback = ->{ interrupted = true } result = ::Vagrant::Util::Busy.busy(int_callback) do ::Vagrant::Util::Subprocess.execute(*cmd, &block) end end result.stderr.gsub!("\r\n", "\n") result.stdout.gsub!("\r\n", "\n") if result.exit_code != 0 && !interrupted raise Errors::ExecuteError, command: cmd.inspect, stderr: result.stderr, stdout: result.stdout end if opts if opts[:with_stderr] return result.stdout + " " + result.stderr else return result.stdout end end end |
#get_current_state ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/vagrant-qemu/driver.rb', line 26 def get_current_state case when running? :running when created? :stopped else :not_created end end |
#get_ssh_port(ssh_port) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/vagrant-qemu/driver.rb', line 169 def get_ssh_port(ssh_port) id_tmp_dir = @tmp_dir.join(@vm_id) = id_tmp_dir.join("options.yml") if .file? = YAML.load_file() rescue nil ssh_port = [:ssh_port] if !.nil? && .key?(:ssh_port) end ssh_port end |
#import(options) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/vagrant-qemu/driver.rb', line 181 def import() new_id = "vq_" + SecureRandom.urlsafe_base64(8) # Make dir id_dir = @data_dir.join(new_id) FileUtils.mkdir_p(id_dir) id_tmp_dir = @tmp_dir.join(new_id) FileUtils.mkdir_p(id_tmp_dir) # Prepare firmware if [:arch] == "aarch64" && ![:firmware_format].nil? execute("cp", [:qemu_dir].join("edk2-aarch64-code.fd").to_s, id_dir.join("edk2-aarch64-code.fd").to_s) execute("cp", [:qemu_dir].join("edk2-arm-vars.fd").to_s, id_dir.join("edk2-arm-vars.fd").to_s) execute("chmod", "644", id_dir.join("edk2-arm-vars.fd").to_s) end # Create image [:image_path].each_with_index do |img, i| suffix_index = i > 0 ? "-#{i}" : '' execute("qemu-img", "create", "-f", "qcow2", "-F", "qcow2", "-b", img.to_s, id_dir.join("linked-box#{suffix_index}.img").to_s) end server = { :id => new_id, } end |
#running? ⇒ Boolean
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/vagrant-qemu/driver.rb', line 212 def running? pid_file = @tmp_dir.join(@vm_id).join("qemu.pid") return false if !pid_file.file? begin Process.kill(0, File.read(pid_file).to_i) true rescue Errno::ESRCH false end end |
#start(options) ⇒ Object
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 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 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/vagrant-qemu/driver.rb', line 46 def start() if !running? id_dir = @data_dir.join(@vm_id) image_path = Array.new image_count = id_dir.glob("linked-box*.img").count for i in 0..image_count-1 do suffix_index = i > 0 ? "-#{i}" : '' image_path.append(id_dir.join("linked-box#{suffix_index}.img").to_s) end id_tmp_dir = @tmp_dir.join(@vm_id) FileUtils.mkdir_p(id_tmp_dir) # dump options = id_tmp_dir.join("options.yml") File.write(, .to_yaml) control_socket = "" if ![:control_port].nil? control_socket = "port=#{options[:control_port]},host=localhost,ipv4=on" else unix_socket_path = id_tmp_dir.join("qemu_socket").to_s control_socket = "path=#{unix_socket_path}" end debug_socket = "" if ![:debug_port].nil? debug_socket = "port=#{options[:debug_port]},host=localhost,ipv4=on" else unix_socket_serial_path = id_tmp_dir.join("qemu_socket_serial").to_s debug_socket = "path=#{unix_socket_serial_path}" end cmd = [] if [:qemu_bin].nil? cmd += %W(qemu-system-#{options[:arch]}) else if [:qemu_bin].kind_of?(Array) cmd += [:qemu_bin] else cmd += %W(#{options[:qemu_bin]}) end end # basic cmd += %W(-machine #{options[:machine]}) if ![:machine].nil? cmd += %W(-cpu #{options[:cpu]}) if ![:cpu].nil? cmd += %W(-smp #{options[:smp]}) if ![:smp].nil? cmd += %W(-m #{options[:memory]}) if ![:memory].nil? # network if ![:net_device].nil? # net device cmd += %W(-device #{options[:net_device]},netdev=net0) # ports hostfwd = "hostfwd=tcp::#{options[:ssh_port]}-:22" [:ports].each do |v| hostfwd += ",hostfwd=#{v}" end extra_netdev = "" if ![:extra_netdev_args].nil? extra_netdev = ",#{options[:extra_netdev_args]}" end cmd += %W(-netdev user,id=net0,#{hostfwd}#{extra_netdev}) end # drive if ![:drive_interface].nil? image_path.each do |img| cmd += %W(-drive if=#{options[:drive_interface]},format=qcow2,file=#{img}) end end if [:arch] == "aarch64" && ![:firmware_format].nil? fm1_path = id_dir.join("edk2-aarch64-code.fd").to_s fm2_path = id_dir.join("edk2-arm-vars.fd").to_s cmd += %W(-drive if=pflash,format=#{options[:firmware_format]},file=#{fm1_path},readonly=on) cmd += %W(-drive if=pflash,format=#{options[:firmware_format]},file=#{fm2_path}) end # control pid_file = id_tmp_dir.join("qemu.pid").to_s cmd += %W(-chardev socket,id=mon0,#{control_socket},server=on,wait=off) cmd += %W(-mon chardev=mon0,mode=readline) cmd += %W(-chardev socket,id=ser0,#{debug_socket},server=on,wait=off) cmd += %W(-serial chardev:ser0) cmd += %W(-pidfile #{pid_file}) if ![:no_daemonize] cmd += %W(-daemonize) end # other default cmd += [:other_default] # user-defined cmd += [:extra_qemu_args] opts = {:detach => [:no_daemonize]} execute(*cmd, **opts) end end |
#stop(options) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/vagrant-qemu/driver.rb', line 149 def stop() if running? if ![:control_port].nil? Socket.tcp("localhost", [:control_port], connect_timeout: 5) do |sock| sock.print "system_powerdown\n" sock.close_write sock.read rescue nil end else id_tmp_dir = @tmp_dir.join(@vm_id) unix_socket_path = id_tmp_dir.join("qemu_socket").to_s Socket.unix(unix_socket_path) do |sock| sock.print "system_powerdown\n" sock.close_write sock.read rescue nil end end end end |