Commit Graph

744 Commits

Author SHA1 Message Date
Oded Arbel
3ec2128c3c fixed handling of nil volumes 2021-08-18 13:15:12 +03:00
Darragh Bailey
2ae1421421 Ensure shutdown timeout adjusted if graceful halt fails 2021-08-14 00:13:44 +01:00
Chris Roberts
9acfd16dfc Add ShutdownDomain action and use during halt sequence
This adds in a ShutdownDomain action which allows for the
    GracefulHalt action to attempt to shutdown the domain. If
    it does not transition to domain successfully to a shutoff
    state, the ShutdownDomain action is used to "nicely" shutdown
    the domain. Likewise, if that action fails to transition the
    domain, the HaltDomain action will be used to forcibly stop it.
2021-08-14 00:12:58 +01:00
Chris Roberts
64d087d2f9 Use builtin GracefulHalt action on halt
Updates the halt action to use the GracefulHalt builtin action.
    If the GracefulHalt builtin action fails to properly transition
    the state of the guest, it will use the HaltDomain action to
    forcibly stop it.
2021-08-14 00:12:58 +01:00
Chris Roberts
32f69c5361 Update HaltDomain action to only forcibly halt domain 2021-08-14 00:12:58 +01:00
Mikhail Ushanov
5019e282f2 fix(action): fix typo in change domain disk_bus
Signed-off-by: Mikhail Ushanov <gm.mephisto@gmail.com>
2021-08-04 13:41:27 +03:00
Darragh Bailey
18ebb9d9ed Enable frozen string across project (#1319)
Turn on frozen string support in all files by using a comment to avoid
enabling across dependencies where it may not work.

Fixes: #1177
2021-06-30 13:27:03 +01:00
Darragh Bailey
6dac9c9054 Remove debug statement accidentally left behind (#1318)
Prune debug print statement added by #1308.
2021-06-29 13:51:29 +01:00
Darragh Bailey
1174685e7b Basic packaging tests and restructure action (#1307)
Restructure action to remove custom handling around packaging of the
box and instead use more of the built-in provided actions instead.

Includes some packaging tests to cover both simple where the public
key is retained (can't modify the tinycore VM without more complex
provisioning steps), and a more complex one that utilizes a script
and supports triggering regenerating the hosts on subsequent boots.

The use of the upstream packaging helpers means that when the
default insecure ssh key has been replaced, the packaging process
will automatically include the generated key.

Fixes: #759
Fixes: #765
Fixes: #1013
Fixes: #994
2021-06-25 20:01:02 +01:00
Darragh Bailey
8defd28871 Disable removal of host keys by default (#1315)
Testing has shown that most boxes do not have the hosts keys removed as
it is non trivial to get them to regenerate on subsequent bring up.
Additionally vagrant uses `StrictHostKeyChecking=no` to ignore the host
keys so relying on them to provide any kind of security or identity of a
VM is pointless.

In the few cases where it is possible to have keys regenerate it should
be managed by how the box is cleaned up, and otherwise forced by a
provision script on first boot of the VM.

While it is possible to have a script executed via sysprep modify the
boot of the machine to ensure the keys are regenerated on the next boot,
this should either be built-in to occur automatically, and there by
assume the responsibility for ensuring host keys are replaced for each
VM packaged by vagrant-libvirt, or should default to the same behaviour
that appears to be common for many debian based boxes and simply not
attempt to remove the host keys. If removal is useful, it may be
re-enabled as needed. See #851 for a script that can be used to have
openssh-server reconfigured to trigger regenerating the keys.

A number of people have tried to fixed this previously and I failed to
understand then that they were correct. Have attributed co authorship of
this to all of the PRs authors that attempted to fix this over the
years.

Related: #759
Related: #873
Related: #955
Related: #994

Co-authored-by: Guillaume Penaud <gpenaud@xilopix.com>
Co-authored-by: Zak B. Elep <zakame@zakame.net>
2021-06-25 11:30:59 +01:00
Richard Turc
00cd79aa35 Use qemu-img json output and compute virtual size #1308 2021-06-11 16:38:35 +02:00
Darragh Bailey
e9f5f028a3 Ensure open3 required and qemu-utils in docker (#1306)
Depending on load order of libraries as well as version of ruby, need to
require open3 before attempting to reference `Open3`.

There is an additional dependency on qemu-utils to dynamically retrieve
the image information to provide the virtual size and format
automatically. Add this to the docker image to ensure it's available for
users of this distribution format.

Fixes: #1305
2021-06-07 15:31:18 +01:00
Darragh Bailey
0c35a1e275 Vagrant up expects machine data_dir to exist afterwards (#1298)
Ensure the machine data_dir is recreated after removal because vagrant
up expects the directory to exist even if there has been an exception
and the machine has been deleted subsequently.
2021-05-28 15:40:34 +01:00
Darragh Bailey
199bfe19f9 Provide Vagrantfile path in description by default (#1299)
Ensure the source Vagrantfile appears in the virtual machine description
to help people using virsh/virt-manager subsequently to understand where
machines have come from.

Fixes: #496
2021-05-28 14:46:57 +01:00
Darragh Bailey
994f95e24f Handle extraction of keyfile from uri (#1297)
If the keyfile is specified as part of the URI provided, extract it and
set the appropriate option so that it can be provided to the proxy
command subsequently.
2021-05-27 18:28:12 +01:00
Darragh Bailey
daa8f8af55 Handle custom port for remote libvirt (#1296)
Extract the port number from the parsed URI as part of finalizing the
uri and associated options. Don't expose this as a separate item to be
set as it can be passed in as part of the host entry.

Closes: #789
2021-05-27 17:28:25 +01:00
Rui Lopes
dba31833a4 add support for boot 'network' from a vagrant public_network
when use a public_network in vagrant, this is how libvirt domain xml looks like (notice that the interface type is 'direct'):

    <interface type="direct">
      <mac address="08:00:27:00:00:01"/>
      <source dev="br-rpi" mode="bridge"/>
      <model type="virtio"/>
      <boot order="2"/> <!-- this boot xml node will be inserted with this commit -->
      <address type="pci" domain="0x0000" bus="0x00" slot="0x05" function="0x0"/>
    </interface>
2021-05-26 07:59:47 +01:00
Darragh Bailey
7ce85f2216 Make disks attribute path required check for name collisions (#1293)
Ensure the path attribute for disks in the new format is required to
ensure there is always one parameter required for the hash element in
the array of disks. This avoids a certain amount of magical behaviour.

Identify that the name attribute is optional, however add checks for
collisions in volume names generated, as well as ensuring the volumes
are still prefixed in a way to prevent accidental collisions between
boxes utilizing the same names.

Add notes to the README identifying the format as experimental, with
notes on how the format should appear.
2021-05-22 16:59:11 +01:00
Darragh Bailey
1fe5a80516 Ensure state is correct reflected in global-status (#1292)
Vagrant relies on the side effect of checking the machine state to
trigger updating of the state in the global machine index. As a
consequence any action should not inspect the domain state directly and
instead should access the machine state.

Additionally as part of the up/start actions should switch to built-in
WaitForCommunicator which will inspect the machine states by default to
align with the expected state updating side effects that would be in
effect for any internal provider shipped with vagrant.

Closes: #948
Partial-Fix: #193
2021-05-20 13:36:23 +01:00
Darragh Bailey
981642afeb Invert forward ssh port behaviour (#1289)
Multi machines environments will result in collisions until auto
correction is implemented. Therefore invert to keep the forwarding of
the default ssh port disabled by default initially until can handle the
port collisions detection and resolving.

Relates: #1012
2021-05-18 18:30:56 +01:00
Darragh Bailey
878b5ffe61 Ensure state is fully removed for destroy (#1288)
Completely remove state after a destroy, and ensure removal of a
non-existing domain also scrubs the machine folder clean.

Closes: #1132
2021-05-18 18:21:54 +01:00
Matthias Blümel
07391048af Enable forward ssh-port to host
Provides an option to disable the forwarding in case it causes issues.
However based on original PRs adding the forwarded behaviour, it does
not appear to be intentional to exclude it permanently.

Closes: #1011
Closes: #1012
2021-05-16 19:55:54 +01:00
Darragh Bailey
5f09673c2a Basic forward port tests and refactor out @env (#1281)
Adds some basic unit spec tests to validate the main behaviours around
the port forwarding to allow for subsequent behaviour changes.

Additionally removes the dependency on the instance variable @env for
internal functions to allow testing some of the internal functions
without needing to inject an instance variable that is not set on
initialization.

Includes a file contents matcher lifted from the cucumber/aruba project
on github, with some minor modifications instead of including the entire
gem.
2021-05-15 14:37:55 +01:00
Zdenek Zambersky
3b1b17a5c9 Add support for spinlocks hyperv feature 2021-05-12 20:50:23 +02:00
Darragh Bailey
98ff2dfb51 Retrieve disk format and virtual size directly (#1274)
When uploading disks to libvirt storage it is unnecessary to require
that the virtual size or the format be provided as these can be
retrieved by calling qemu-img on the box files to retrieve the required
information.

Update the handle box image support to separate the handling of the two
different formats and remove the need to specify the additional settings
in the case of the V2 format for multi disk boxes.
2021-05-10 23:02:25 +01:00
Wong Hoi Sing Edison
70c4136836 Bugfix Additional Disks Storage File Ownership
For additional disk image created by
`lib/vagrant-libvirt/action/create_domain.rb`, it is now incorrectly
hard-coded with `group: storage_uid(env)`, so even QEMU process is
running with `root:libvirt` but file is still owned by `root:root`.

Signed-off-by: Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
2021-05-10 15:36:35 +01:00
TJ
4c2e59bc3f Allow Vagrantfile to set the CDROM image type
Although it is possible to attach additional images (disk or cdrom) the
cdrom functionality doesn't allow setting the :type - it defaults to
'raw' since vagrant-libvirt is assuming cdrom images are in ISO9660
format.

This assumption breaks down when using vagrant-mutate to convert a box
with multiple storage devices. A common situation is the Ubuntu boxes
built for VirtualBox that contain a cloud-init ISO9660 image with
user-data. vagrant-mutate converts the image to qcow* format (not
ISO9660) in order to not have to deal with what is inside.

This means the resulting libvirt domain needs to be told the type is
'qcow2' in order for the guest to see it as an ISO9660 image.

This patch adds the required option along with a sensible default.
2021-05-10 10:23:12 +01:00
Darragh Bailey
86cf4e08a7 Handle nil volumes when cd-rom attached (#1273)
When a cd-rom is attached as an additional storage, listing the volumes
can include a nil element. Make sure to check that the element is valid
before attempting to access the attribute.

Update the test to better match the observed behaviour.

Fixes: #1209, #1262
2021-05-08 17:27:04 +01:00
Darragh Bailey
fbf32889d7 Support force halt behaviour (#1270)
Vagrant subcommand halt accepts an argument -f that should result in
VMs being halted immediately instead of attempting a graceful shutdown.

Add support for this option and include tests to cover the rest of the
halt behaviour.

Fixes: #1265
2021-05-08 17:14:13 +01:00
Richard Turc
225237b125 Allow to use many disks in vagrant box for libvirt provider
Adds support for a new multi disk box format and handling to upload the
multiple disks to the storage pool.

New format is:
{
  'disks': [
    {
      'name': 'disk1.img',
      'virtual_size': 10,
      'format': 'qcow2'
    },
    {
      'name': 'disk2.img',
      'virtual_size': 15,
      'format': 'qcow2'
    },
    {
      'name': 'disk3.img',
    }
  ],
  'provider': 'libvirt',
  'format': 'qcow2'
}

It is expected to remove format from being set at the top level when
using the new format, with the assuming that qcow2 should be the default
format, and other formats should be permitted to be specified as needed.

Includes tests for handling the box images and creation of domain
volumes. Additionally includes an integration test to ensure a box with
2 disks will work as expected.

Partially fixes: #602
2021-05-08 17:04:10 +01:00
Darragh Bailey
d764a308b4 Revert auto_config change in #1249 (#1253)
The #1249 introduce a change of behaviour on auto_config to skip if it
was any kind of false, where as the correct behaviour is to only skip if
explicitly set to false.
2021-04-19 12:34:20 +01:00
Kirill Yudenok
c387864dd2 Auto configuration for tunnel interfaces 2021-04-19 10:51:57 +01:00
Kirill Yudenok
b9c73a3b99 Fix interaction with reload plugin 2021-04-18 12:07:57 +01:00
Kirill Yudenok
6e763fdb79 Add ability to define multiple private networks 2021-04-18 11:53:38 +01:00
Wong Hoi Sing Edison
e2766d563e Add virtiofs Support to vagrant-libvirt
From <https://virtio-fs.gitlab.io/index.html#overview>:

> Virtio-fs is a shared file system that lets virtual machines access a directory tree on the host. Unlike existing approaches, it is designed to offer local file system semantics and performance.

From <https://virtio-fs.gitlab.io/index.html#faq>:

> Existing solutions to this problem, such as virtio-9p, are based on existing network protocols that are not optimized for virtualization use cases. As a result they do not perform as well as local file systems and do not provide the semantics that some applications rely on.

This PR add `virtiofs` support to vagrant-libvirt, which simply clone-and-hack from our existing 9p implementation. It also tidy up and synchronize naming for 9p implementation.

Tested with:

  - Host: Ubuntu 20.10 + Linux 5.10.30 + QEMU 5.0.0 + Libvirt 6.6.0 + Vagrant 2.2.15
  - Guest: Ubuntu 20.04 + Linux 5.4.0

Signed-off-by: Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
2021-04-17 11:20:34 +00:00
Kirill Yudenok
929654b83a Add ability to manage bridge STP and delay parameters (#1247)
Second part of PR #1038 - ability to manage Bridge STP and Delay parameters

Follows defaults defined at https://libvirt.org/formatnetwork.html#elementsConnect
2021-04-08 11:36:30 +00:00
Jacek Tomasiak
8f65aec9eb Add option to set management network MTU (#1233)
This adds option to specify non-standard MTU for created management
network.
2021-04-06 14:25:14 +00:00
Emilio Cobos Álvarez
28d475ca29 Fix forward_ports with ruby 3.0 (#1242)
Fedora 24 has moved to ruby 3.0, which triggers an exception in 
vagrant-libvirt when installing it with vagrant provided by the distro.

This is caused by a chance in ruby 3.0 that can be applied to earlier
versions without any down sides:
https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/
2021-04-06 15:20:04 +01:00
Darragh Bailey
1c82be1357 Raise expected Timeout to ensure retryable retries (#1241)
With the refactor to where the domain addresses are looked up, a log
message was added in case of timeout, however it was missed that when
this occurs still need to raise the exception to ensure that checks for
this timeout can occur within the original calling function.

Update tests to ensure that the code will retry the expected number of
times before triggering the expected failure message and aborting the
machine bring up.

Additionally to allow running the wait_till_up_spec.rb separately,
needed to ensure the plugin.rb which is loaded by the code pulls in the
action.rb to ensure `Action.remove_libvirt_image` can be correctly
resolved when the rest of the test suite is not running.

Fixes: #1239
2021-04-03 14:57:31 +01:00
Darragh Bailey
4d7c6fd636 Ensure provisioner cleanup run early in destroy (#1235)
Move the provisioner cleanup to both run at the start of the destroy
sequence and ensure it performs the cleanup as part of the in chain.

Fixes: #839
2021-03-22 13:30:12 +00:00
Darragh Bailey
a16c3f7898 Move proxy_command to config and support templating (#1226)
Migrate the proxy_command specification to the config and add support
for user override template to be used for edge cases. Moving it to the
config allows mistakes in the interpolation to be caught before the
machine is brought up.

Note this uses a more restrictive replacement to avoid requiring
escaping of '%' or '$'.

Issue #921 already partially resolved thanks to @ElArtista, this
completes the fix by allowing users to override as needed.

Fixes: #921
2021-03-21 15:19:53 +00:00
Darragh Bailey
188cb5a5f4 Ensure multi machine IP retrieval works (#1232)
When working with multi machine configurations, various provisioners may
query the IP addresses of all of the other machines from the current
provider, in which case it is necessary to ensure that calls to the
driver use the provided context and not it's current instance variables
as these may reference a different machine.
2021-03-21 13:25:06 +00:00
Darragh Bailey
39dc11d2a8 Extract finalizing from the uri to a separate method (#1231)
Finalize settings that may be provided via the URI afterwards.
2021-03-21 12:42:26 +00:00
Darragh Bailey
4548e19be9 Enable ssh connection on keyfile being set (#1225)
Auto enable ssh connections if the ssh keyfile has been explicitly set
and switch a number of settings from being explicitly set until after
attempts to resolve the values have failed in order to allow decisions
to be main on whether to set them based on inferred values only if not
explicitly provided elsewhere.

Add some additional tests and transport modes to expand the coverage of
what is accepted to align as close to what libvirt will accept as
possible.
2021-03-20 16:19:33 +00:00
Darragh Bailey
876e906de0 Improve id_ssh_key_file finalizing (#1230)
Move finalizing the id_ssh_key_file based on how other settings are
currently defined to a separate private function and extend the tests to
accept defining additional expects/allows within the table.

This should apply a consistent set of rules where if the user explicitly
supplies the key, it will attempt to resolve it based on the expected
ssh directory, and will always retain the explicit setting even if it
doesn't exist. Where connect_via_ssh is enabled, it will attempt to
detect if the default key exists, otherwise it will disable the setting.
If the user does not want automatic guesses, they can explicitly disable
by setting it to `nil`.

Fixes: #1228
2021-03-20 15:38:02 +00:00
Darragh Bailey
9f8912d7aa Bugfix some stray machine args after tidy up (#1227)
Clean up the remaining references to the old functions after the
wait-till-up networking code tidy up.
2021-03-17 18:16:06 +00:00
Darragh Bailey
bbbcdaa44b Only set ssh connection params if transport is ssh (#1224)
Skip setting various additional connection params if the transport for
the libvirt connection is not ssh based as these will be ignored and may
cause confusion as to why they do not apply.
2021-03-17 15:36:09 +00:00
Darragh Bailey
4ee40728b3 Make LIBVIRT_DEFAULT_URI a fackball mechanism (#1223)
Switch to only picking up LIBVIRT_DEFAULT_URI if no settings that could
affect the uri have been explicitly set. Some of these may not actually
appear in the URI depending on what is actually set or not set, however
it is important that should only use the env variable if the user has
not explicitly configured options relevant and instead allow them to
configure as needed.
2021-03-17 14:41:14 +00:00
Agorgianitis Loukas
b15489c557 Fix remote libvirt functionality through LIBVIRT_DEFAULT_URI (#1218)
One can now set `LIBVIRT_DEFAULT_URI` to something like 
`qemu+ssh://user@beefy/system` along with `connect_via_ssh` option to 
true and use vagrant on remote machines, without needing to 
duplicate/hardcode remote server username and identity key inside the 
Vagrantfile.

Partial Fixes: #1217
2021-03-17 12:37:16 +00:00
Darragh Bailey
ba4235add9 Change proxy_command to use embedded OpenSSH functionality instead of (#1222)
Solves vagrant not detecting end of ssh connection when a proxy is
used, described in #921

Allows ssh argument population to append arguments only when
respective values are not nil, avoiding creation of invalid
proxy_command when ssh-agent or ssh config based connections are used
2021-03-17 12:19:02 +00:00