Class: VagrantPlugins::QEMU::Driver
- Inherits:
-
Object
- Object
- VagrantPlugins::QEMU::Driver
- Defined in:
- lib/vagrant-qemu/driver.rb
Instance Attribute Summary collapse
-
#attached_drives ⇒ Object
readonly
Returns the value of attribute attached_drives.
-
#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
- #attach_disk(disk) ⇒ Object
- #attach_dvd(disk) ⇒ Object
- #created? ⇒ Boolean
- #delete ⇒ Object
- #disk_dir ⇒ Object
- #execute(*cmd, **opts, &block) ⇒ Object
- #force_kill ⇒ 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.
22 23 24 25 26 27 28 |
# File 'lib/vagrant-qemu/driver.rb', line 22 def initialize(id, dir, tmp) @vm_id = id @data_dir = dir @tmp_dir = tmp.join("vagrant-qemu") @attached_drives = {disk: [], floppy: [], dvd: []} @logger = Log4r::Logger.new("vagrant_qemu::driver") end |
Instance Attribute Details
#attached_drives ⇒ Object (readonly)
Returns the value of attribute attached_drives.
20 21 22 |
# File 'lib/vagrant-qemu/driver.rb', line 20 def attached_drives @attached_drives end |
#data_dir ⇒ Object (readonly)
Returns the value of attribute data_dir.
18 19 20 |
# File 'lib/vagrant-qemu/driver.rb', line 18 def data_dir @data_dir end |
#tmp_dir ⇒ Object (readonly)
Returns the value of attribute tmp_dir.
19 20 21 |
# File 'lib/vagrant-qemu/driver.rb', line 19 def tmp_dir @tmp_dir end |
#vm_id ⇒ String (readonly)
Returns VM ID.
17 18 19 |
# File 'lib/vagrant-qemu/driver.rb', line 17 def vm_id @vm_id end |
Instance Method Details
#attach_disk(disk) ⇒ Object
404 405 406 |
# File 'lib/vagrant-qemu/driver.rb', line 404 def attach_disk(disk) @attached_drives[:disk] << disk end |
#attach_dvd(disk) ⇒ Object
400 401 402 |
# File 'lib/vagrant-qemu/driver.rb', line 400 def attach_dvd(disk) @attached_drives[:dvd] << disk end |
#created? ⇒ Boolean
295 296 297 |
# File 'lib/vagrant-qemu/driver.rb', line 295 def created? result = @data_dir.join(@vm_id).directory? end |
#delete ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/vagrant-qemu/driver.rb', line 41 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 |
#disk_dir ⇒ Object
408 409 410 |
# File 'lib/vagrant-qemu/driver.rb', line 408 def disk_dir @data_dir.join(@vm_id) end |
#execute(*cmd, **opts, &block) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 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 |
# File 'lib/vagrant-qemu/driver.rb', line 311 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 |
#force_kill ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/vagrant-qemu/driver.rb', line 208 def force_kill pid_file = @tmp_dir.join(@vm_id).join("qemu.pid") return unless pid_file.file? pid = File.read(pid_file).to_i begin Process.kill(0, pid) rescue Errno::ESRCH return # Already dead end $stderr.puts "==> default: Force killing QEMU process (pid=#{pid})" @logger.info("Force killing QEMU process #{pid}") begin Process.kill("KILL", pid) 5.times do begin Process.kill(0, pid) sleep 1 rescue Errno::ESRCH $stderr.puts "==> default: QEMU process #{pid} killed" break end end rescue Errno::ESRCH $stderr.puts "==> default: QEMU process #{pid} killed" end end |
#get_current_state ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/vagrant-qemu/driver.rb', line 30 def get_current_state case when running? :running when created? :stopped else :not_created end end |
#get_ssh_port(ssh_port) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/vagrant-qemu/driver.rb', line 237 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
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 |
# File 'lib/vagrant-qemu/driver.rb', line 249 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}" : '' linked_image = id_dir.join("linked-box#{suffix_index}.img").to_s args = ["create", "-f", "qcow2", "-F", "qcow2", "-b", img.to_s] if ![:extra_image_opts].nil? [:extra_image_opts].each do |opt| args.push("-o") args.push(opt) end end args.push(linked_image) if i == 0 if ![:disk_resize].nil? args.push([:disk_resize]) end end execute("qemu-img", *args) end server = { :id => new_id, } end |
#running? ⇒ Boolean
299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/vagrant-qemu/driver.rb', line 299 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
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/vagrant-qemu/driver.rb', line 50 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=#{[: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=#{[: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-#{[:arch]}) else if [:qemu_bin].kind_of?(Array) cmd += [:qemu_bin] else cmd += %W(#{[:qemu_bin]}) end end # basic cmd += %W(-machine #{[:machine]}) if ![:machine].nil? cmd += %W(-cpu #{[:cpu]}) if ![:cpu].nil? cmd += %W(-smp #{[:smp]}) if ![:smp].nil? cmd += %W(-m #{[:memory]}) if ![:memory].nil? # network if ![:net_device].nil? # net device cmd += %W(-device #{[:net_device]},netdev=net0) # ports hostfwd = "hostfwd=tcp::#{[:ssh_port]}-:22" [:ports].each do |v| hostfwd += ",hostfwd=#{v}" end extra_netdev = "" if ![:extra_netdev_args].nil? extra_netdev = ",#{[:extra_netdev_args]}" end cmd += %W(-netdev user,id=net0,#{hostfwd}#{extra_netdev}) end # drive diskid = 0 extra_drive_args = "" if ![:extra_drive_args].nil? extra_drive_args = ",#{[:extra_drive_args]}" end if ![:drive_interface].nil? image_path.each do |img| cmd += %W(-drive if=#{[:drive_interface]},id=disk#{diskid},format=qcow2,file=#{img}#{extra_drive_args}) diskid += 1 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=#{[:firmware_format]},file=#{fm1_path},readonly=on) cmd += %W(-drive if=pflash,format=#{[:firmware_format]},file=#{fm2_path}) end dvd_index = 1 @attached_drives[:dvd].each do |disk| cmd += %W(-drive file=#{disk[:Path]},index=#{dvd_index},media=cdrom) dvd_index += 1 end if ![:drive_interface].nil? @attached_drives[:disk].each do |disk| cmd += %W(-drive if=#{[:drive_interface]},id=disk#{diskid},format=qcow2,file=#{disk[:Path]}#{extra_drive_args}) diskid += 1 end 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
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 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/vagrant-qemu/driver.rb', line 172 def stop() if running? force_kill if running? # Try graceful shutdown first begin 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 # Wait up to 10 seconds for graceful shutdown 10.times do break if !running? sleep 1 end rescue => e @logger.warn("Graceful shutdown failed: #{e.}") end # Force kill if still running force_kill if running? end end |