environment/lib/virtual-machine.rb

51 lines
No EOL
1.3 KiB
Ruby

require 'downloader'
require 'system'
require_relative 'data/resources/iso-images'
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, 500)
puts path
puts disk_img_path
end
end
# size in MB
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