Module: Bsdkrun::Args

Defined in:
lib/bsdkrun/args.rb

Overview

Builds the bsdkrun argv (minus the binary and global flags) for a detached create. Every path ends with -d so create yields a handle.

Options are a Hash with Symbol keys mirroring the TypeScript SDK's CreateOptions, discriminated on :os.

Class Method Summary collapse

Class Method Details

.build_create_args(opts) ⇒ Array<String>

Build the full detached create argv for the given options.

Parameters:

  • opts (Hash)

    create options, discriminated on :os.

Returns:

  • (Array<String>)

Raises:

  • (ArgumentError)

    on an unknown :os.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bsdkrun/args.rb', line 72

def build_create_args(opts)
  case opts[:os].to_s
  when "linux"    then linux_args(opts)
  when "freebsd"  then freebsd_args(opts)
  when "netbsd"   then netbsd_args(opts)
  when "firmware" then firmware_args(opts)
  when "kernel"   then kernel_args(opts)
  when "unikraft" then unikraft_args(opts)
  when "solo5"    then solo5_args(opts)
  when "nanos"    then nanos_args(opts)
  when "osv"      then osv_args(opts)
  else raise ArgumentError, "unknown os: #{opts[:os].inspect}"
  end
end

.disk_args(o) ⇒ Array<String>

Disk-persistence flags shared by BSD / firmware / kernel guests.

Parameters:

  • o (Hash)

Returns:

  • (Array<String>)


59
60
61
62
63
64
65
# File 'lib/bsdkrun/args.rb', line 59

def disk_args(o)
  a = []
  a.push("--persist") if o[:persist]
  a.push("-v", o[:volume]) if o[:volume]
  Array(o[:attach_disk]).each { |d| a.push("--attach-disk", d) }
  a
end

.env_args(env) ⇒ Array<String>

--name flag if a name is set. -e K=V per entry, sorted by key.

A hash has no argv order of its own, so sorting is what makes the command line — and the tests that assert on it — deterministic. The guest sees the same environment either way.

Parameters:

  • o (Hash)

Returns:

  • (Array<String>)


36
37
38
39
40
# File 'lib/bsdkrun/args.rb', line 36

def env_args(env)
  return [] if env.nil? || env.empty?

  env.to_h.sort_by { |k, _| k.to_s }.flat_map { |k, v| ["-e", "#{k}=#{v}"] }
end

.name_args(o) ⇒ Object



42
43
44
# File 'lib/bsdkrun/args.rb', line 42

def name_args(o)
  o[:name] ? ["--name", o[:name].to_s] : []
end

.net_args(net) ⇒ Array<String>

Networking flags shared by every guest kind.

Parameters:

  • net (Hash, nil)

Returns:

  • (Array<String>)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bsdkrun/args.rb', line 15

def net_args(net)
  a = []
  return a unless net

  a.push("--no-net") if net[:disabled]
  Array(net[:ports]).each do |p|
    a.push("--port", p.is_a?(String) ? p : "#{p[:host]}:#{p[:guest]}")
  end
  a.push("--mac", net[:mac]) if net[:mac]
  a.push("--network", net[:network]) if net[:network]
  a
end

.vm_args(o) ⇒ Array<String>

--cpus / --mem sizing flags.

Parameters:

  • o (Hash)

Returns:

  • (Array<String>)


49
50
51
52
53
54
# File 'lib/bsdkrun/args.rb', line 49

def vm_args(o)
  a = []
  a.push("--cpus", o[:cpus].to_s) unless o[:cpus].nil?
  a.push("--mem", o[:mem].to_s) unless o[:mem].nil?
  a
end