environment/lib/virtual-machine.rb

61 lines
1.6 KiB
Ruby
Raw Normal View History

2025-08-11 10:20:07 +02:00
require 'downloader'
2025-08-11 14:18:59 +02:00
require 'system'
2025-08-11 10:20:07 +02:00
require_relative 'data/resources/iso-images'
2025-08-11 17:36:55 +02:00
require 'vm/qemu'
2025-08-11 10:20:07 +02:00
module VirtualMachine
def self.distro(name, arch, type = :install)
ISO_URLS[:debian][:arm64][:install]
end
def self.setup(options)
2025-08-11 14:18:59 +02:00
if options[:name] == nil
options[:name] = options[:distro]
end
if options[:arch] == nil
options[:arch] = System::ARCH
else
options[:arch] = System.arch_to_symbol(options[:arch])
end
puts options
2025-08-11 10:20:07 +02:00
distro = options[:distro]
type = :install
2025-08-11 14:18:59 +02:00
arch = options[:arch]
2025-08-11 10:20:07 +02:00
url = distro(name, arch, type)
2025-08-11 14:18:59 +02:00
Downloader.get(url) do |path|
disk_img_path = File.join(User.cache_path, "vm", distro.to_s, arch.to_s, options[:name], "root.img")
create_disk_image(disk_img_path, 15000)
2025-08-11 14:18:59 +02:00
puts path
puts disk_img_path
2025-08-11 17:36:55 +02:00
Qemu.launch(
arch,
disk_img_path,
cpus: [1, System.cpus - 2].max,
cdrom: path,
detach: false
)
2025-08-11 14:18:59 +02:00
end
end
# size in MB
2025-08-11 17:36:55 +02:00
# lsof /Users/artur/.cache/dat/vm/debian/arm64/debian/root.img
2025-08-11 14:18:59 +02:00
def self.create_disk_image(path, size)
size_in_bytes = 1024 * 1024 * size
# Ensure the directory exists
dir = File.dirname(path)
FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
File.open(path, "wb") do |f|
f.truncate(size_in_bytes)
# f.seek(size_in_bytes - 1)
# f.write("\0")
end
2025-08-11 10:20:07 +02:00
end
end