This commit is contained in:
Artur Gurgul 2025-08-26 08:52:28 +02:00
parent c8b057e6a2
commit 54d4dfcb33
3 changed files with 68 additions and 8 deletions

8
bin/vm
View file

@ -23,6 +23,14 @@ OptionParser.new do |opt|
options.name = name options.name = name
end end
opt.on('--cdrom CDROM', 'Use local cdrom image') do |cdrom|
options.cdrom = cdrom
end
opt.on('--tpm', 'Use tpm') do
options.tpm = true
end
opt.on('--detached') do opt.on('--detached') do
options.detached = true options.detached = true
end end

View file

@ -62,6 +62,10 @@ module VirtualMachine
if options[:detached] == nil if options[:detached] == nil
options[:detached] = false options[:detached] = false
end end
if options[:tpm] == nil
options[:tpm] = false
end
end end
def self.archive(options) def self.archive(options)
@ -98,14 +102,16 @@ module VirtualMachine
) )
end end
# vm setup windows --arch arm64 --iso /Users/agurgul/Downloads/win.iso --tpm
# vm setup windows --tpm --cdrom /Users/agurgul/Downloads/win.iso
def self.setup(options) def self.setup(options)
fill_defaults(options) fill_defaults(options)
url = distro(options[:name], options[:arch], :install) # puts options
disk_img_path = root_img_path(options) # exit -1
Downloader.get(url) do |path| get_cdrom_image(options) do |path|
disk_img_path = root_img_path(options)
create_disk_image(disk_img_path, 64000) create_disk_image(disk_img_path, 64000)
Qemu.launch( Qemu.launch(
@ -114,11 +120,23 @@ module VirtualMachine
cpus: [1, System.cpus - 2].max, cpus: [1, System.cpus - 2].max,
cdrom: path, cdrom: path,
detach: options[:detached], detach: options[:detached],
display: DisplayMode.window display: DisplayMode.window,
tpm: options[:tpm]
) )
end end
end end
def self.get_cdrom_image(options)
if options[:cdrom] == nil
url = distro(options[:name], options[:arch], :install)
Downloader.get(url) do |path|
yield path
end
else
yield options[:cdrom]
end
end
# size in MB # size in MB
# lsof /Users/artur/.cache/dat/vm/debian/arm64/debian/root.img # lsof /Users/artur/.cache/dat/vm/debian/arm64/debian/root.img
def self.create_disk_image(path, size) def self.create_disk_image(path, size)

View file

@ -81,7 +81,7 @@ module Qemu
arch: System::ARCH, arch: System::ARCH,
cdrom: nil, cdrom: nil,
detach: true, detach: true,
ram: 2048, ram: 2048 * 8,
cpus: 1, cpus: 1,
display: DisplayMode::none display: DisplayMode::none
} }
@ -102,7 +102,9 @@ module Qemu
if System::OS == :macos && arch == :arm64 if System::OS == :macos && arch == :arm64
# args += ["-bios", "/opt/homebrew/share/qemu/edk2-aarch64-code.fd"] # args += ["-bios", "/opt/homebrew/share/qemu/edk2-aarch64-code.fd"]
# cp /opt/homebrew/share/qemu/edk2-arm-vars.fd ~/edk2-arm-vars.fd
args += ["-drive", "if=pflash,format=raw,unit=0,readonly=on,file=/opt/homebrew/share/qemu/edk2-aarch64-code.fd"] args += ["-drive", "if=pflash,format=raw,unit=0,readonly=on,file=/opt/homebrew/share/qemu/edk2-aarch64-code.fd"]
args += ["-drive", "if=pflash,format=raw,unit=1,file=/Users/agurgul/edk2-arm-vars.fd"]
if opts[:display] == DisplayMode::none if opts[:display] == DisplayMode::none
port = 2222 port = 2222
@ -156,10 +158,40 @@ module Qemu
args += machine_args_for(arch) args += machine_args_for(arch)
args += ["-m", opts[:ram].to_s, "-smp", opts[:cpus].to_s] args += ["-m", opts[:ram].to_s, "-smp", opts[:cpus].to_s]
args += ["-name", "FirstVM", "-boot", "order=d"] # boot from CD first args += ["-name", "FirstVM", "-boot", "order=d"] # boot from CD first
args += ["-drive", "file=#{disk_path},if=virtio,cache=writeback,format=raw"] args += ["-drive", "file=#{disk_path},if=virtio,cache=writeback,format=raw,id=nvme0"]
#args += ["-device", "nvme,serial=nvme0,drive=nvme0,bootindex=2"]
if opts[:cdrom] != nil if opts[:cdrom] != nil
args += ["-cdrom", opts[:cdrom]] #args += ["-cdrom", opts[:cdrom]]
# args += ["-device", "virtio-scsi-pci,id=scsi"]
# args += ["-drive", "if=none,id=cd,format=raw,file=#{opts[:cdrom]},media=cdrom"]
# args += ["-device", "scsi-cd,drive=cd,bootindex=1"]
args += ["-drive", "id=cd,format=raw,file=#{opts[:cdrom]},media=cdrom"]
args += ["-device", "usb-storage,drive=cd,bootindex=1"]
args += ["-device", "ramfb"]
# args += ["-device", "virtio-gpu-pci"]
# args += ["-display", "default,show-cursor=on"]
end end
if opts[:tpm]
# brew install swtpm
# swtpm socket --tpm2 --ctrl type=unixio,path=./tpm/tpm.sock --tpmstate dir=./tpm --daemon
# args += ["-chardev", "socket,id=chrtpm,path=/Users/agurgul/Downloads/tpm/tpm.sock"]
# args += ["-tpmdev", "emulator,id=tpm0,chardev=chrtpm"]
# args += ["-device", "tpm-crb-device,tpmdev=tpm0"]
args += ["-chardev", "socket,id=chrtpm,path=/Users/agurgul/Downloads/tpm/tpm.sock"]
args += ["-tpmdev", "emulator,id=tpm0,chardev=chrtpm"]
#args += ["-device", "tpm-tis,tpmdev=tpm0"]
args += ["-device", "tpm-tis-device,tpmdev=tpm0"]
# nic user,ipv6=off,model=rtl8139,mac=84:1b:77:c9:03:a6
end
# args += ["-device", "virtio-net,netdev=n0", "-netdev", "user,id=n0"] # user-mode NAT # args += ["-device", "virtio-net,netdev=n0", "-netdev", "user,id=n0"] # user-mode NAT
# optional: uncomment to run headless with VNC on :5901 # optional: uncomment to run headless with VNC on :5901
# args += ["-display", "none", "-vnc", "127.0.0.1:1"] # args += ["-display", "none", "-vnc", "127.0.0.1:1"]
@ -181,3 +213,5 @@ module Qemu
end end
end end
end end