require 'downloader' require 'system' require_relative 'data/resources/iso-images' require 'vm/qemu' module VirtualMachine def self.distro(name, arch, type = :install) ISO_URLS[:debian][:arm64][:install] end def self.setup(options) 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 distro = options[:distro] type = :install arch = options[:arch] url = distro(name, arch, type) 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) puts path puts disk_img_path Qemu.launch( arch, disk_img_path, cpus: [1, System.cpus - 2].max, cdrom: path, detach: false ) end end # size in MB # lsof /Users/artur/.cache/dat/vm/debian/arm64/debian/root.img 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 end end