diff --git a/lib/vagrant_utm/action.rb b/lib/vagrant_utm/action.rb index b620efc..68c6a37 100644 --- a/lib/vagrant_utm/action.rb +++ b/lib/vagrant_utm/action.rb @@ -10,6 +10,7 @@ module Action # rubocop:disable Metrics/ModuleLength # Autoloading action blocks action_root = Pathname.new(File.expand_path("action", __dir__)) autoload :Boot, action_root.join("boot") + autoload :BootDisposable, action_root.join("boot_disposable") autoload :CheckAccessible, action_root.join("check_accessible") autoload :CheckCreated, action_root.join("check_created") autoload :CheckGuestAdditions, action_root.join("check_guest_additions") @@ -239,6 +240,23 @@ def self.action_start end end + # This action start VM in disposable mode. + # UTM equivalent of `utmctl start --disposable` + def self.action_start_disposable + Vagrant::Action::Builder.new.tap do |b| + b.use CheckUtm + b.use ConfigValidate + b.use Call, IsRunning do |env1, b2| + if env1[:result] + b2.use MessageAlreadyRunning + next + end + # If the VM is NOT running, then start in disposable mode + b2.use BootDisposable + end + end + end + # This action is primarily responsible for suspending the VM. # UTM equivalent of `utmctl suspend ` def self.action_suspend diff --git a/lib/vagrant_utm/action/boot_disposable.rb b/lib/vagrant_utm/action/boot_disposable.rb new file mode 100644 index 0000000..7c1c787 --- /dev/null +++ b/lib/vagrant_utm/action/boot_disposable.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module VagrantPlugins + module Utm + module Action + # Action to start the virtual machine in disposable mode. + class BootDisposable + def initialize(app, _env) + @app = app + end + + def call(env) + # Start up the VM in disposable mode. + env[:ui].warn I18n.t("vagrant_utm.actions.vm.boot.disposable") + env[:machine].provider.driver.start_disposable + + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant_utm/disposable.rb b/lib/vagrant_utm/disposable.rb new file mode 100644 index 0000000..0224047 --- /dev/null +++ b/lib/vagrant_utm/disposable.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module VagrantPlugins + module Utm + # Run VM as a snapshot and do not save changes to disk. + class Disposable < Vagrant.plugin(2, :command) + def execute + with_target_vms do |machine| + machine.action(:start_disposable) + end + + 0 + end + end + end +end diff --git a/lib/vagrant_utm/driver/base.rb b/lib/vagrant_utm/driver/base.rb index 107fe95..1157dcf 100644 --- a/lib/vagrant_utm/driver/base.rb +++ b/lib/vagrant_utm/driver/base.rb @@ -68,6 +68,10 @@ def set_name(name); end # rubocop:disable Naming/AccessorMethodName # @return [void] def start; end + # Starts the virtual machine in disposable mode. + # @return [void] + def start_disposable; end + # Deletes the virtual machine references by this driver. # @return [void] def delete; end diff --git a/lib/vagrant_utm/driver/meta.rb b/lib/vagrant_utm/driver/meta.rb index b7cae3d..7b2d961 100644 --- a/lib/vagrant_utm/driver/meta.rb +++ b/lib/vagrant_utm/driver/meta.rb @@ -90,6 +90,7 @@ def initialize(uuid = nil) # rubocop:disable Metrics/CyclomaticComplexity,Metric :list, :read_state, :start, + :start_disposable, :vm_exists?, :halt, :suspend, diff --git a/lib/vagrant_utm/driver/version_4_5.rb b/lib/vagrant_utm/driver/version_4_5.rb index a409fd3..5d1d9f9 100644 --- a/lib/vagrant_utm/driver/version_4_5.rb +++ b/lib/vagrant_utm/driver/version_4_5.rb @@ -59,6 +59,10 @@ def start execute("start", @uuid) end + def start_disposable + execute("start", @uuid, "--disposable") + end + def halt execute("stop", @uuid) end diff --git a/lib/vagrant_utm/plugin.rb b/lib/vagrant_utm/plugin.rb index d85110b..abe7324 100644 --- a/lib/vagrant_utm/plugin.rb +++ b/lib/vagrant_utm/plugin.rb @@ -38,6 +38,12 @@ class Plugin < Vagrant.plugin("2") Provider end + # Register the command + command "disposable" do + require_relative "disposable" + Disposable + end + # Load the translation files def self.setup_i18n I18n.load_path << File.expand_path("locales/en.yml", Utm.source_root) diff --git a/lib/vagrant_utm/provider.rb b/lib/vagrant_utm/provider.rb index db3dc75..c266926 100644 --- a/lib/vagrant_utm/provider.rb +++ b/lib/vagrant_utm/provider.rb @@ -48,8 +48,11 @@ def initialize(machine) machine_id_changed end - # Execute the action with the given name. + # @see Vagrant::Plugin::V1::Provider#action def action(name) + # Attempt to get the action method from the Action class if it + # exists, otherwise return nil to show that we don't support the + # given action. action_method = "action_#{name}" return Action.send(action_method) if Action.respond_to?(action_method) diff --git a/locales/en.yml b/locales/en.yml index 49cad4c..1ed76d2 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -97,3 +97,7 @@ en: 1. Headover to UTM. 2. Open the action menu of VM by a secondary click on a virtual machine %{name} in the list. 3. Select 'Share' and save the file to a location. + boot: + disposable: |- + WARNING: The UTM virtual machine is booting in disposable mode. + Changes made to the VM will be lost when the VM is powered off.