Hashicorp Vagrant

Siehe auch

  • KVM

  • GNOME Boxes

Links:

Installation auf Fedora

Mit libvirt/KVM als Hypervisor (Fedora 36+), aus dem Hashicorp Hersteller-Repo - liefert aber unter Umständen nicht immer die neueste Vagrant-Version für das laufende Fedora:

sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
sudo dnf -y install vagrant libvirt-devel
sudo vagrant plugin install vagrant-libvirt

Alternativ aktuellste Vagrant-Version ohne Repo installieren:

sudo dnf -y remove vagrant*
sudo rm -f /etc/yum.repos.d/hashicorp.repo
sudo rm -rf ~/.vagrant.d /opt/vagrant /usr/share/vagrant

VAGRANT_LATEST_VERSION=$(curl -s https://checkpoint-api.hashicorp.com/v1/check/vagrant | jq -r -M '.current_version')
sudo dnf -y install https://releases.hashicorp.com/vagrant/$VAGRANT_LATEST_VERSION/vagrant-$VAGRANT_LATEST_VERSION-1.x86_64.rpm

# libvirt
sudo dnf -y install gcc libvirt-daemon-kvm qemu-kvm libvirt-devel make rdesktop
sudo vagrant plugin install vagrant-libvirt

Egal ob aus dem Repo oder von der Homepage installiert - wer Windows-VMs mit Vagrant betreiben möchte, benötigt diese Plugins:

sudo dnf -y install rubygem-archive-tar-minitar
sudo vagrant plugin install winrm --debug
sudo vagrant plugin install winrm-elevated --debug
sudo vagrant plugin install winrm-fs --debug

Probleme mit den Plugins?

sudo vagrant plugin list

sudo vagrant plugin repair
# or
sudo vagrant plugin expunge --reinstall

Tipp

Immer noch Probleme mit den Plugins, z.B. mit WinRM? Kann auch ein Bug in Vagrant sein, was nicht so ungewöhnlich ist. Eventuell eine neuere Development-Version von https://github.com/hashicorp/vagrant/releases/ ausprobieren.

Umgang mit Hashicorp Vagrant

Best Practice: Pro Vagrant-Maschine ein Verzeichnis anlegen.

mkdir ~/vagrant
cd ~/vagrant

Beispiel: Ziel ist es, ein Arch-Linux zu deployen. Dafür ein Vagrantfile anlegen lassen:

mkdir generic-arch
cd generic-arch
vagrant init generic/arch

Optional das Vagrantfile bearbeiten, um die eigenen SSH-Keys zu deployen, Software zu installieren, Anpassungen vorzunehmen usw. Hier am Beispiel eines Rocky 8:

Vagrantfile
Vagrant.configure("2") do |config|

  config.vm.box = "generic/rocky8"
  config.vm.hostname = "walle.linuxfabrik.loc"
  config.vm.network  "private_network", ip: "192.0.2.226"

  config.vm.provider "libvirt" do |v|
    v.memory = 4096
    v.cpus = 2
  end

  config.vm.provision "shell" do |s|
    ssh_prv_key = ""
    ssh_pub_key = ""
    if File.file?("/home/user/.ssh/id_rsa")
      ssh_prv_key = File.read("/home/user/.ssh/id_rsa")
      ssh_pub_key = File.readlines("/home/user/.ssh/id_rsa.pub").first.strip
    else
      puts "No SSH key found. You will need to remedy this before pushing to the repository."
    end
    s.inline = <<-SHELL
      if grep -sq "
        echo "SSH keys already provisioned."
        exit 0;
      fi
      echo "SSH key provisioning."
      mkdir -p /home/vagrant/.ssh/
      touch /home/vagrant/.ssh/authorized_keys
      chmod 644 /home/vagrant/.ssh/id_rsa.pub
      chmod 600 /home/vagrant/.ssh/id_rsa
      chown -R vagrant:vagrant /home/vagrant
      systemctl disable --now firewalld
      dnf -y install nano
      exit 0
    SHELL
  end
end

Tipp

Windows? VM braucht ewig zum Starten, und vagrant up bricht irgendwann ab? Vagrantfile um folgende Zeilen ergänzen:

config.winrm.max_tries = 300 # default is 20 seconds
config.winrm.retry_delay = 2 #seconds. This is the defaul value and just here for documentation.

Mehr CPU und Memory vergeben:

Vagrantfile
config.vm.provider "libvirt" do |v|
  v.memory = 4096
  v.cpus = 2
end

Fixe IP-Adresse:

Vagrantfile
config.vm.network "private_network", ip: "192.0.2.226"

Maschine hochfahren:

sudo vagrant up

# after changing the Vagrantfile for an already-running Vagrant box:
sudo vagrant reload

Tipp

There was an error talking to Libvirt. The error message is shown below:

Call to virDomainCreateWithFlags failed: internal error: /usr/libexec/qemu-bridge-helper --use-vnet --br=virbr0 --fd=27: failed to communicate with bridge helper: Transport endpoint is not connected
stderr=failed to get mtu of bridge `virbr0': No such device

Vorher den „Virtual Machine Manager“ von Hand starten. Damit werden alle später für Vagrant notwendigen libvirt-Komponenten gestartet.

Stati aller hochgefahrenen Boxes einsehen:

sudo vagrant status

SSH-Konfiguration einsehen:

vagrant ssh-config

SSH-Zugriff mit Benutzer „vagrant“:

# using the vagrant tool:
vagrant ssh

# or doing the same with SSH (run `vagrant ssh-config` before to get the SSH details):
ssh -i ~/vagrant/$BOXNAME/.vagrant/machines/default/libvirt/private_key -o PasswordAuthentication=no -o PubkeyAcceptedKeyTypes=+ssh-rsa -p 22 vagrant@192.0.2.226

# or by using your ssh key deployed above (run `vagrant ssh-config` before to get the SSH details)::
ssh vagrant@192.0.2.226

RDP-Zugriff auf eine Windows-Box:

vagrant rdp

Troubleshooting

SSH-Zugang zu alten Boxen (Debian 8, CentOS 6 etc.) scheitert mit „sign_and_send_pubkey: no mutual signature supported“

Ist kein Vagrant-Problem, sondern das moderner SSH-Clients - diese haben alte Encryption-Algorithmen abgeschaltet. In dem Fall den SSH-Befehl um -o PubkeyAcceptedKeyTypes=+ssh-rsa erweitern.

Can’t vagrant up or destroy „domain about to create is already taken“

virsh list –all

MACHINE=generic-rocky8_default virsh destroy $MACHINE virsh undefine $MACHINE

# are found in ~/.local/share/libvirt/images virsh vol-list default VOLUME=generic-arch_default.img virsh vol-delete –pool default $VOLUME

Built on 2024-04-18