From 54d4dfcb33b3b92b163b6a9080373c961520a411 Mon Sep 17 00:00:00 2001 From: Artur Gurgul Date: Tue, 26 Aug 2025 08:52:28 +0200 Subject: [PATCH] save --- bin/vm | 8 ++++++++ lib/virtual-machine.rb | 28 +++++++++++++++++++++++----- lib/vm/qemu.rb | 40 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/bin/vm b/bin/vm index 609384e..322c850 100755 --- a/bin/vm +++ b/bin/vm @@ -23,6 +23,14 @@ OptionParser.new do |opt| options.name = name 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 options.detached = true end diff --git a/lib/virtual-machine.rb b/lib/virtual-machine.rb index ccb8606..874a9fe 100644 --- a/lib/virtual-machine.rb +++ b/lib/virtual-machine.rb @@ -62,6 +62,10 @@ module VirtualMachine if options[:detached] == nil options[:detached] = false end + + if options[:tpm] == nil + options[:tpm] = false + end end def self.archive(options) @@ -98,14 +102,16 @@ module VirtualMachine ) 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) fill_defaults(options) - url = distro(options[:name], options[:arch], :install) - disk_img_path = root_img_path(options) + # puts 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) Qemu.launch( @@ -114,11 +120,23 @@ module VirtualMachine cpus: [1, System.cpus - 2].max, cdrom: path, detach: options[:detached], - display: DisplayMode.window + display: DisplayMode.window, + tpm: options[:tpm] ) 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 # lsof /Users/artur/.cache/dat/vm/debian/arm64/debian/root.img def self.create_disk_image(path, size) diff --git a/lib/vm/qemu.rb b/lib/vm/qemu.rb index 5d4fd15..50140d3 100644 --- a/lib/vm/qemu.rb +++ b/lib/vm/qemu.rb @@ -81,7 +81,7 @@ module Qemu arch: System::ARCH, cdrom: nil, detach: true, - ram: 2048, + ram: 2048 * 8, cpus: 1, display: DisplayMode::none } @@ -102,7 +102,9 @@ module Qemu if System::OS == :macos && arch == :arm64 # 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=1,file=/Users/agurgul/edk2-arm-vars.fd"] if opts[:display] == DisplayMode::none port = 2222 @@ -156,10 +158,40 @@ module Qemu args += machine_args_for(arch) args += ["-m", opts[:ram].to_s, "-smp", opts[:cpus].to_s] 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 - 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 + + 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 # optional: uncomment to run headless with VNC on :5901 # args += ["-display", "none", "-vnc", "127.0.0.1:1"] @@ -181,3 +213,5 @@ module Qemu end end end + +