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
- #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.
19 20 21 22 23 |
# File 'lib/vagrant-qemu/driver.rb', line 19 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.
16 17 18 |
# File 'lib/vagrant-qemu/driver.rb', line 16 def data_dir @data_dir end |
#tmp_dir ⇒ Object (readonly)
Returns the value of attribute tmp_dir.
17 18 19 |
# File 'lib/vagrant-qemu/driver.rb', line 17 def tmp_dir @tmp_dir end |
#vm_id ⇒ String (readonly)
Returns VM ID.
15 16 17 |
# File 'lib/vagrant-qemu/driver.rb', line 15 def vm_id @vm_id end |
Instance Method Details
#created? ⇒ Boolean
161 162 163 |
# File 'lib/vagrant-qemu/driver.rb', line 161 def created? result = @data_dir.join(@vm_id).directory? end |
#delete ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/vagrant-qemu/driver.rb', line 36 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
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 207 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 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 |
# File 'lib/vagrant-qemu/driver.rb', line 177 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
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/vagrant-qemu/driver.rb', line 25 def get_current_state case when running? :running when created? :stopped else :not_created end end |
#import(options) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/vagrant-qemu/driver.rb', line 138 def import() new_id = 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" 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) end # Create image execute("qemu-img", "create", "-f", "qcow2", "-F", "qcow2", "-b", [:image_path].to_s, id_dir.join("linked-box.img").to_s) server = { :id => new_id, } end |
#running? ⇒ Boolean
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/vagrant-qemu/driver.rb', line 165 def running? pid_file = @data_dir.join(@vm_id).join("qemu.pid") return false if !pid_file.file? begin Process.getpgid(File.read(pid_file).to_i) true rescue Errno::ESRCH false end end |
#start(options) ⇒ Object
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 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 |
# File 'lib/vagrant-qemu/driver.rb', line 45 def start() if !running? id_dir = @data_dir.join(@vm_id) image_path = id_dir.join("linked-box.img").to_s pid_file = id_dir.join("qemu.pid").to_s id_tmp_dir = @tmp_dir.join(@vm_id) FileUtils.mkdir_p(id_tmp_dir) 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 = [] cmd += %W(qemu-system-#{options[:arch]}) # basic cmd += %W(-machine #{options[:machine]}) cmd += %W(-cpu #{options[:cpu]}) cmd += %W(-smp #{options[:smp]}) cmd += %W(-m #{options[:memory]}) 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}) # drive cmd += %W(-drive if=virtio,format=qcow2,file=#{image_path}) if [:arch] == "aarch64" 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=raw,file=#{fm1_path},readonly=on) cmd += %W(-drive if=pflash,format=raw,file=#{fm2_path}) end # control 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}) cmd += %W(-parallel null -monitor none -display none -vga none) if ![:no_daemonize] cmd += %W(-daemonize) end # user-defined cmd += [:extra_qemu_args] execute(*cmd, {:detach => [:no_daemonize]}) end end |
#stop(options) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/vagrant-qemu/driver.rb', line 118 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 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 end end end end |