| 1 |
.. _setup_vagrant: |
| 2 |
|
| 3 |
Image Description for Vagrant |
| 4 |
============================= |
| 5 |
|
| 6 |
.. sidebar:: Abstract |
| 7 |
|
| 8 |
This page provides further information for handling |
| 9 |
Vagrant-controlled disk images built with {kiwi} and references |
| 10 |
the following article: |
| 11 |
|
| 12 |
* :ref:`simple_disk` |
| 13 |
|
| 14 |
`Vagrant <https://www.vagrantup.com>`_ is a framework to |
| 15 |
implement consistent processing/testing work environments based on |
| 16 |
Virtualization technologies. To run a system, Vagrant needs so-called |
| 17 |
**boxes**. A box is a TAR archive containing a virtual disk image and |
| 18 |
some metadata. |
| 19 |
|
| 20 |
To build Vagrant boxes, you can use `Packer <https://www.packer.io>`_, which |
| 21 |
is provided by Hashicorp itself. Packer is based on the official |
| 22 |
installation media (DVDs) as shipped by the distribution vendor. |
| 23 |
|
| 24 |
The {kiwi} way of building images might be helpful if such a media does not |
| 25 |
exist or does not suit your needs. For example, if the distribution is |
| 26 |
still under development or you want to use a collection of your own |
| 27 |
repositories. Note that in contrast to Packer, {kiwi} only supports the |
| 28 |
libvirt and VirtualBox providers. Other providers require a different box |
| 29 |
layout that is currently not supported by {kiwi}. |
| 30 |
|
| 31 |
In addition, you can use the {kiwi} image description as a source for the |
| 32 |
`Open Build Service <https://openbuildservice.org>`_, which allows |
| 33 |
building and maintaining boxes. |
| 34 |
|
| 35 |
Vagrant expects boxes to be set up in a specific way (for details refer to |
| 36 |
the `Vagrant box documentation |
| 37 |
<https://www.vagrantup.com/docs/boxes/base.html>`_.). applied to the |
| 38 |
referenced {kiwi} image description from :ref:`simple_disk`, the following |
| 39 |
steps are required: |
| 40 |
|
| 41 |
1. Update the image type setup. |
| 42 |
|
| 43 |
.. code:: xml |
| 44 |
|
| 45 |
<type image="oem" filesystem="ext4" format="vagrant"> |
| 46 |
<bootloader name="grub2" timeout="0"/> |
| 47 |
<vagrantconfig provider="libvirt" virtualsize="42"/> |
| 48 |
<size unit="G">42</size> |
| 49 |
<oemconfig> |
| 50 |
<oem-resize>false</oem-resize> |
| 51 |
</oemconfig> |
| 52 |
</type> |
| 53 |
|
| 54 |
This modifies the type to build a Vagrant box for the libvirt |
| 55 |
provider, including a pre-defined disk size. The disk size is |
| 56 |
optional but recommended to provide some free space on disk. |
| 57 |
|
| 58 |
For the VirtualBox provider, the additional attribute |
| 59 |
`virtualbox_guest_additions_present` can be set to `true` when the |
| 60 |
VirtualBox guest additions are installed in the {kiwi} image: |
| 61 |
|
| 62 |
.. code:: xml |
| 63 |
|
| 64 |
<type image="oem" filesystem="ext4" format="vagrant"> |
| 65 |
<bootloader name="grub2" timeout="0"/> |
| 66 |
<vagrantconfig |
| 67 |
provider="virtualbox" |
| 68 |
virtualbox_guest_additions_present="true" |
| 69 |
virtualsize="42" |
| 70 |
/> |
| 71 |
<size unit="G">42</size> |
| 72 |
<oemconfig> |
| 73 |
<oem-resize>false</oem-resize> |
| 74 |
</oemconfig> |
| 75 |
</type> |
| 76 |
|
| 77 |
The resulting Vagrant box then uses the `vboxfs` module for the |
| 78 |
synchronized folder instead of `rsync`, which is used by default. |
| 79 |
|
| 80 |
2. Add mandatory packages. |
| 81 |
|
| 82 |
.. code:: xml |
| 83 |
|
| 84 |
<package name="sudo"/> |
| 85 |
<package name="openssh"/> |
| 86 |
|
| 87 |
3. Add additional packages. |
| 88 |
|
| 89 |
If you have set the attribute `virtualbox_guest_additions_present` to |
| 90 |
`true`, add the VirtualBox guest additions. For openSUSE, the following |
| 91 |
packages are required: |
| 92 |
|
| 93 |
.. code:: xml |
| 94 |
|
| 95 |
<package name="virtualbox-guest-tools"/> |
| 96 |
<package name="virtualbox-guest-x11"/> |
| 97 |
<package name="virtualbox-guest-kmp-default"/> |
| 98 |
|
| 99 |
Otherwise, you must add `rsync`: |
| 100 |
|
| 101 |
.. code:: xml |
| 102 |
|
| 103 |
<package name="rsync"/> |
| 104 |
|
| 105 |
Note that {kiwi} cannot verify whether these packages are installed. If |
| 106 |
they are missing, the resulting Vagrant box will be broken. |
| 107 |
|
| 108 |
4. Add Vagrant user. |
| 109 |
|
| 110 |
.. code:: xml |
| 111 |
|
| 112 |
<users> |
| 113 |
<user name='vagrant' password='vh4vw1N4alxKQ' home='/home/vagrant' groups='vagrant'/> |
| 114 |
</users> |
| 115 |
|
| 116 |
This adds the **vagrant** user to the system and applies the |
| 117 |
name of the user as the password for login. |
| 118 |
|
| 119 |
5. Configure SSH, the default shared folder, and sudo permissions. |
| 120 |
|
| 121 |
Vagrant expects that it can log in as the user `vagrant` using an |
| 122 |
insecure public key [#f1]_. Furthermore, Vagrant also usually uses |
| 123 |
:file:`/vagrant` as the default shared folder and assumes that the |
| 124 |
`vagrant` user can invoke commands via `sudo` without having |
| 125 |
to enter a password. |
| 126 |
|
| 127 |
This can be achieved using the function `baseVagrantSetup` in |
| 128 |
:file:`config.sh`: |
| 129 |
|
| 130 |
.. code:: bash |
| 131 |
|
| 132 |
baseVagrantSetup |
| 133 |
|
| 134 |
6. Additional customizations: |
| 135 |
|
| 136 |
In addition to `baseVagrantSetup`, you might want to also ensure the |
| 137 |
following: |
| 138 |
|
| 139 |
- If you have installed the VirtualBox guest additions into your box, |
| 140 |
then also load the `vboxsf` kernel module. |
| 141 |
- When building boxes for libvirt, ensure that the default wired |
| 142 |
networking interface is called `eth0` and uses DHCP. This is |
| 143 |
necessary since libvirt uses `dnsmasq` to issue IPs to the VMs. This |
| 144 |
step can be omitted for VirtualBox boxes. |
| 145 |
|
| 146 |
An image built with the above setup creates a Vagrant box file with the |
| 147 |
extension :file:`.vagrant.libvirt.box` or |
| 148 |
:file:`.vagrant.virtualbox.box`. Add the box file to Vagrant with the |
| 149 |
command: |
| 150 |
|
| 151 |
.. code:: bash |
| 152 |
|
| 153 |
vagrant box add my-box image-file.vagrant.libvirt.box |
| 154 |
|
| 155 |
.. note:: |
| 156 |
|
| 157 |
Using the box with the libvirt provider requires, alongside a correct |
| 158 |
Vagrant installation: |
| 159 |
|
| 160 |
- the plugin `vagrant-libvirt` to be installed |
| 161 |
- a running libvirtd daemon |
| 162 |
|
| 163 |
Once added to Vagrant, boot the box and log in |
| 164 |
with the following sequence of `vagrant` commands: |
| 165 |
|
| 166 |
.. code:: bash |
| 167 |
|
| 168 |
vagrant init my-box |
| 169 |
vagrant up --provider libvirt |
| 170 |
vagrant ssh |
| 171 |
|
| 172 |
|
| 173 |
Customizing the embedded Vagrantfile |
| 174 |
------------------------------------ |
| 175 |
|
| 176 |
.. warning:: This is an advanced topic and not required for most users. |
| 177 |
|
| 178 |
|
| 179 |
Vagrant ships with an embedded :file:`Vagrantfile` that carries settings |
| 180 |
specific to this box, for instance, the synchronization mechanism for the |
| 181 |
shared folder. {kiwi} generates such a file automatically for you, and it |
| 182 |
should be sufficient for most use cases. |
| 183 |
|
| 184 |
If a box requires different settings in the embedded :file:`Vagrantfile`, |
| 185 |
then the user can provide {kiwi} with a path to an alternative via the |
| 186 |
attribute `embedded_vagrantfile` of the `vagrantconfig` element: it |
| 187 |
specifies a relative path to the :file:`Vagrantfile` that will be included |
| 188 |
in the finished box. |
| 189 |
|
| 190 |
In the following example snippet from :file:`config.xml`, we add a custom |
| 191 |
:file:`MyVagrantfile` into the box (the file should be in the image |
| 192 |
description directory next to :file:`config.sh`): |
| 193 |
|
| 194 |
.. code:: xml |
| 195 |
|
| 196 |
<type image="oem" filesystem="ext4" format="vagrant"> |
| 197 |
<bootloader name="grub2" timeout="0"/> |
| 198 |
<vagrantconfig |
| 199 |
provider="libvirt" |
| 200 |
virtualsize="42" |
| 201 |
embedded_vagrantfile="MyVagrantfile" |
| 202 |
/> |
| 203 |
<size unit="G">42</size> |
| 204 |
<oemconfig> |
| 205 |
<oem-resize>false</oem-resize> |
| 206 |
</oemconfig> |
| 207 |
</type> |
| 208 |
|
| 209 |
|
| 210 |
The option to provide a custom :file:`Vagrantfile` can be combined with the |
| 211 |
usage of *profiles* (see :ref:`image-profiles`), so that |
| 212 |
certain builds can use the automatically generated :file:`Vagrantfile` (in |
| 213 |
the following example, that is the VirtualBox build) and others get a |
| 214 |
customized one (the libvirt profile in the following example): |
| 215 |
|
| 216 |
.. code:: xml |
| 217 |
|
| 218 |
<?xml version="1.0" encoding="utf-8"?> |
| 219 |
|
| 220 |
<image schemaversion="{schema_version}" name="{exc_image_base_name}"> |
| 221 |
<!-- description goes here --> |
| 222 |
<profiles> |
| 223 |
<profile name="libvirt" description="Vagrant Box for Libvirt"/> |
| 224 |
<profile name="virtualbox" description="Vagrant Box for VirtualBox"/> |
| 225 |
</profiles> |
| 226 |
|
| 227 |
<!-- general preferences go here --> |
| 228 |
|
| 229 |
<preferences profiles="libvirt"> |
| 230 |
<type |
| 231 |
image="oem" |
| 232 |
filesystem="ext4" |
| 233 |
format="vagrant"> |
| 234 |
<bootloader name="grub2" timeout="0"/> |
| 235 |
<vagrantconfig |
| 236 |
provider="libvirt" |
| 237 |
virtualsize="42" |
| 238 |
embedded_vagrantfile="LibvirtVagrantfile" |
| 239 |
/> |
| 240 |
<size unit="G">42</size> |
| 241 |
<oemconfig> |
| 242 |
<oem-resize>false</oem-resize> |
| 243 |
</oemconfig> |
| 244 |
</type> |
| 245 |
</preferences> |
| 246 |
<preferences profiles="virtualbox"> |
| 247 |
<type |
| 248 |
image="oem" |
| 249 |
filesystem="ext4" |
| 250 |
format="vagrant"> |
| 251 |
<bootloader name="grub2" timeout="0"/> |
| 252 |
<vagrantconfig |
| 253 |
provider="virtualbox" |
| 254 |
virtualbox_guest_additions_present="true" |
| 255 |
virtualsize="42" |
| 256 |
/> |
| 257 |
<size unit="G">42</size> |
| 258 |
<oemconfig> |
| 259 |
<oem-resize>false</oem-resize> |
| 260 |
</oemconfig> |
| 261 |
</type> |
| 262 |
</preferences> |
| 263 |
|
| 264 |
<!-- remaining box description --> |
| 265 |
</image> |
| 266 |
|
| 267 |
|
| 268 |
.. [#f1] The insecure key is removed from the box when it is first |
| 269 |
booted via Vagrant. |