»Plugin Development: Guests
This page documents how to add new guest OS detection to Vagrant, allowing Vagrant to properly configure new operating systems. Prior to reading this, you should be familiar with the plugin development basics.
Warning: Advanced Topic! Developing plugins is an advanced topic that only experienced Vagrant users who are reasonably comfortable with Ruby should approach.
Vagrant has many features that requires doing guest OS-specific actions, such as mounting folders, configuring networks, etc. These tasks vary from operating system to operating system. If you find that one of these does not work for your operating system, then maybe the guest implementation is incomplete or incorrect.
Within the context of a plugin definition, new guests can be defined like so:
guest "ubuntu" do require_relative "guest" Guestend
Guests are defined with the
guest method. The first argument is the
name of the guest. This name is not actually used anywhere, but may in the
future, so choose something helpful. Then, the block argument returns a
class that implements the
Vagrant.plugin(2, :guest) interface.
Implementations of guests subclass
Vagrant.plugin("2", "guest"). Within
this implementation, only the
detect? method needs to be implemented.
detect? method is called by Vagrant at some point after the machine
is booted in order to determine what operating system the guest is running.
If you detect that it is your operating system, return
Communication channels to the machine are guaranteed to be running at this point, so the most common way to detect the operating system is to do some basic testing:
class MyGuest < Vagrant.plugin("2", "guest") def detect?(machine) machine.communicate.test("cat /etc/myos-release") endend
After detecting an OS, that OS is used for various guest capabilities that may be required.
Vagrant also supports a form of inheritance for guests, since sometimes operating systems stem from a common root. A good example of this is Linux is the root of Debian, which further is the root of Ubuntu in many cases. Inheritance allows guests to share a lot of common behavior while allowing distro-specific overrides.
Inheritance is not done via standard Ruby class inheritance because Vagrant uses a custom capability-based system. Vagrant handles inheritance dispatch for you.
To subclass another guest, specify that guest's name as a second parameter in the guest definition:
guest "ubuntu", "debian" do require_relative "guest" Guestend
With the above component, the "ubuntu" guest inherits from "debian." When a capability is looked up for "ubuntu", all capabilities from "debian" are also available, and any capabilities in "ubuntu" override parent capabilities.
When detecting operating systems with
detect?, Vagrant always does a
depth-first search by searching the children operating systems before
checking their parents. Therefore, it is guaranteed in the above example
detect? method on "ubuntu" will be called before "debian."