This commit is contained in:
Artur Gurgul 2025-08-26 17:50:38 +02:00
parent 54d4dfcb33
commit 605cfd38f9
8 changed files with 130 additions and 6 deletions

49
bin/vm-cmd Executable file
View file

@ -0,0 +1,49 @@
#!/usr/bin/env bash
# brew install qemu swtpm
# qemu-system-aarch64 -drive if=pflash,format=raw,unit=0,readonly=on,file=/Volumes/Cache/vms/image/win11-arm64/win11/code.fd -drive if=pflash,format=raw,unit=1,file=/Volumes/Cache/vms/image/win11-arm64/win11/vars.fd -display cocoa -device qemu-xhci,id=xhci -device usb-kbd -device usb-tablet -device virtio-keyboard-device -device virtio-mouse-device -device virtio-gpu -device virtio-net,netdev=n0 -netdev user,id=n0 -accel hvf -machine virt -cpu max -m 16384 -smp 6 -name FirstVM -boot order=d -drive file=/Volumes/Cache/vms/image/win11-arm64/win11/root.img,if=virtio,cache=writeback,format=raw,id=nvme0 -drive id=cd,format=raw,file=/Volumes/Cache/downloads/win11arm64.iso,media=cdrom -device usb-storage,drive=cd,bootindex=1 -device ramfb -chardev socket,id=chrtpm,path=/Users/agurgul/Downloads/tpm/tpm.sock -tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-tis-device,tpmdev=tpm0
# vm setup win11 --tpm --cdrom /Volumes/Cache/downloads/win11arm64.iso
# qemu-system-aarch64 -drive if=pflash,format=raw,unit=0,readonly=on,file=/Volumes/Cache/vms/image/win11-arm64/win11/code.fd -drive if=pflash,format=raw,unit=1,file=/Volumes/Cache/vms/image/win11-arm64/win11/vars.fd -display cocoa -device qemu-xhci,id=xhci -device usb-kbd -device usb-tablet -device virtio-keyboard-device -device virtio-mouse-device -device virtio-gpu -device virtio-net,netdev=n0 -netdev user,id=n0 -accel hvf -machine virt -cpu max -m 16384 -smp 6 -name FirstVM -boot order=d -drive file=/Volumes/Cache/vms/image/win11-arm64/win11/root.img,if=virtio,cache=writeback,format=raw,id=nvme0 -drive id=cd,format=raw,file=/Volumes/Cache/downloads/win11arm64.iso,media=cdrom -device usb-storage,drive=cd,bootindex=1 -device ramfb -chardev socket,id=chrtpm,path=/Users/agurgul/Downloads/tpm/tpm.sock -tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-tis-device,tpmdev=tpm0
# -netdev user,id=net0,hostfwd=tcp::33890-:3389,hostfwd=tcp::59850-:5985 \
# -machine virt,accel=hvf,highmem=off \
# -device virtio-keyboard-pci \
# -device virtio-tablet-pci \
qemu-system-aarch64 \
-boot order=d \
-machine virt,accel=hvf \
-cpu host -smp 6 -m 8G \
-drive if=pflash,format=raw,file=/Volumes/Cache/vms/image/win11-arm64/win11/code.fd,readonly=on \
-drive if=pflash,format=raw,file=/Volumes/Cache/vms/image/win11-arm64/win11/vars.fd \
-drive file=/Volumes/Cache/vms/image/win11-arm64/win11/root.img,if=none,format=raw,id=drv0 \
-device ich9-ahci,id=ahci0 \
-device nvme,drive=drv0,serial=nvme0 \
-device virtio-scsi-pci,id=scsi0 \
-drive file=/Volumes/Cache/downloads/win11arm64.iso,if=none,media=cdrom,id=cd0 \
-device scsi-cd,drive=cd0,bus=scsi0.0,bootindex=1 \
-device ramfb \
-device qemu-xhci,id=xhci \
-device usb-kbd,bus=xhci.0,port=1 \
-device usb-tablet,bus=xhci.0,port=2 \
-netdev user,id=net0,hostfwd=tcp::33890-:3389 \
-device virtio-net-pci,netdev=net0 \
-chardev socket,id=chrtpm,path=/Users/artur/Downloads/tpm/tpm.sock \
-tpmdev emulator,id=tpm0,chardev=chrtpm \
-device tpm-tis-device,tpmdev=tpm0
# in Shell
# FS0:
# cd EFI\BOOT
# BOOTAA64.EFI
# Disk driver: https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/
# Used: https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.271-1/

View file

@ -37,6 +37,13 @@ module System
puts os_name
end
def self.qemu_paths
{
code_fd: qemu_code_fd_path,
vars_fd: qemu_vars_fd_path
}
end
def self.arch_to_symbol(arch)
normalize_architecture_string(arch)
end

View file

@ -54,4 +54,12 @@ module DebianSystem
puts "Failed to uninstall some packages."
end
end
def qemu_code_fd_path()
raise "not supported yet"
end
def qemu_vars_fd_path()
raise "not supported yet"
end
end

View file

@ -54,4 +54,23 @@ module MacOSSystem
puts "Failed to uninstall some packages."
end
end
def qemu_code_fd_path()
case arch_to_symbol(arch)
when :arm64
"/opt/homebrew/share/qemu/edk2-aarch64-code.fd"
else
raise "not supported yet"
end
end
def qemu_vars_fd_path()
case arch_to_symbol(arch)
when :arm64
"/opt/homebrew/share/qemu/edk2-arm-vars.fd"
else
raise "not supported yet"
end
end
end

View file

@ -36,6 +36,6 @@ module User
def self.cache_path
"#{ENV["HOME"]}/.cache/dat/"
ENV["DAT_CACHE_PATH"] || "#{ENV["HOME"]}/.cache/dat/"
end
end

View file

@ -66,6 +66,14 @@ module VirtualMachine
if options[:tpm] == nil
options[:tpm] = false
end
unless options[:vars_fd]
options[:vars_fd] = File.join(vm_dir(options), "vars.fd")
end
unless options[:code_fd]
options[:code_fd] = File.join(vm_dir(options), "code.fd")
end
end
def self.archive(options)
@ -97,8 +105,11 @@ module VirtualMachine
Qemu.launch(
options[:arch],
disk_img_path,
code_fd: options[:code_fd],
vars_fd: options[:vars_fd],
cpus: [1, System.cpus - 2].max,
detach: options[:detached]
detach: options[:detached],
tpm: options[:tpm]
)
end
@ -117,6 +128,8 @@ module VirtualMachine
Qemu.launch(
options[:arch],
disk_img_path,
code_fd: options[:code_fd],
vars_fd: options[:vars_fd],
cpus: [1, System.cpus - 2].max,
cdrom: path,
detach: options[:detached],

View file

@ -91,20 +91,33 @@ module Qemu
defaults[:display] = DisplayMode.fullscreen
defaults[:display] = DisplayMode.window
defaults[:display] = DisplayMode.none
# defaults[:display] = DisplayMode.none
opts = defaults.merge(options)
puts options
puts opts
qemu = qemu_bin_for(arch)
args = []
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"]
unless File.exist?(opts[:vars_fd])
#System.qemu_paths
FileUtils.cp(System.qemu_vars_fd_path, opts[:vars_fd])
end
unless File.exist?(opts[:code_fd])
FileUtils.cp(System.qemu_code_fd_path, opts[:code_fd])
end
args += ["-drive", "if=pflash,format=raw,unit=0,readonly=on,file=#{opts[:code_fd]}"]
args += ["-drive", "if=pflash,format=raw,unit=1,file=#{opts[:vars_fd]}"]
if opts[:display] == DisplayMode::none
port = 2222
@ -181,6 +194,9 @@ module Qemu
# brew install swtpm
# swtpm socket --tpm2 --ctrl type=unixio,path=./tpm/tpm.sock --tpmstate dir=./tpm --daemon
["swtpm", "socket", "--tpm2", "--ctrl", "type=unixio,path=./tpm/tpm.sock", "--tpmstate", "dir=./tpm"]
# 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"]
@ -190,6 +206,9 @@ module Qemu
#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
# TODO: Shared network on macOS
# -netdev vmnet-shared,id=net0
end
# args += ["-device", "virtio-net,netdev=n0", "-netdev", "user,id=n0"] # user-mode NAT

View file

@ -4,3 +4,12 @@
```bash
curl -sSL https://gurgul.pro/artur/environment/raw/branch/main/install | bash
```
Config:
| Variable Name | Default Value | Description |
|---------------------------|--------------------------------|-------------|
| `DAT_CACHE_PATH` | `${HOME}/.cache/dat/` | Path where cached data is stored. |
| `DEFAULT_INTERNET_LOGIN` | not set | |
| `DAT_VM_DATA` | `${DAT_CACHE_PATH}/vm` | Directory for storing VM-related data. |