Skip to content

Instantly share code, notes, and snippets.

@trinitronx
Created December 30, 2024 22:38
Show Gist options
  • Save trinitronx/fbbff8e8cdd1eb34ad2afd3c09cfda9b to your computer and use it in GitHub Desktop.
Save trinitronx/fbbff8e8cdd1eb34ad2afd3c09cfda9b to your computer and use it in GitHub Desktop.
Workaround script to install vagrant-libvirt + vagrant 2.4.3 on Arch Linux & Arch-based distros
#!/bin/bash
export VAGRANT_DEBUG=1
export VAGRANT_DEBUG_LAUNCHER=1
export VAGRANT_LOG=debug
## Setting above debug options reveals vagrant substrate's default environment:
#2024/12/17 09:02:16 launcher: env "CFLAGS" = "-I/opt/vagrant/embedded/include -I/opt/vagrant/embedded/include/libxml2 -lcurl -I/opt/vagrant/embedded/include -I/opt/vagrant/embedded/include/libxml2"
#2024/12/17 09:02:16 launcher: env "CONFIGURE_ARGS" = "with-libvirt-include=/usr/include/libvirt with-libvirt-lib=/usr/lib with-curl-include=/usr/include/curl with-curl-lib=/usr/lib "
#2024/12/17 09:02:16 launcher: env "CPPFLAGS" = "-I/opt/vagrant/embedded/include -I/opt/vagrant/embedded/include/libxml2"
#2024/12/17 09:02:16 launcher: env "CURL_CA_BUNDLE" = "/opt/vagrant/embedded/cacert.pem"
#2024/12/17 09:02:16 launcher: env "CXXFLAGS" = ""
#2024/12/17 09:02:16 launcher: env "GEMRC" = "/opt/vagrant/embedded/etc/gemrc"
#2024/12/17 09:02:16 launcher: env "GEM_HOME" = "/opt/vagrant/embedded/gems"
#2024/12/17 09:02:16 launcher: env "GEM_PATH" = "/opt/vagrant/embedded/gems"
#2024/12/17 09:02:16 launcher: env "LDFLAGS" = "-L/opt/vagrant/embedded/lib -L/opt/vagrant/embedded/lib64"
#2024/12/17 09:02:16 launcher: env "NOKOGIRI_USE_SYSTEM_LIBRARIES" = "true"
#2024/12/17 09:02:16 launcher: env "PATH" = "/opt/vagrant/bin/vagrant"
#2024/12/17 09:02:16 launcher: env "PKG_CONFIG_PATH" = "/opt/vagrant/embedded/lib/pkgconfig:/opt/vagrant/embedded/lib64/pkgconfig"
#2024/12/17 09:02:16 launcher: env "RUBYLIB" = ""
#2024/12/17 09:02:16 launcher: env "RUBYOPT" = ""
# Set a strict path to avoid RVM, rbenv, and other possibly confounding situations
export PATH="/opt/vagrant/embedded/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
export VAGRANT_ARCHLINUX_PKG='vagrant-2.4.3-1-x86_64.pkg.tar.zst'
export VAGRANT_ARCHLINUX_PKG_URL='https://releases.hashicorp.com/vagrant/2.4.3/vagrant-2.4.3-1-x86_64.pkg.tar.zst'
## Compile flags to build against system libcurl and libvirt
## Note: gem install might sometimes ignore PKG_CONFIG_PATH ?? ¯\_(ツ)_/¯
## Reference: https://stackoverflow.com/a/53841639/645491
## Embedded vagrant's default pkg-config path only includes /opt/vagrant location
# We will override this here for this gem, so it can link against system libvirt + libcurl
export PKG_CONFIG_PATH='/usr/lib/pkgconfig:/opt/vagrant/embedded/lib64/pkgconfig:/opt/vagrant/embedded/lib/pkgconfig'
export CFLAGS="-I/opt/vagrant/embedded/include -I/opt/vagrant/embedded/include/libxml2 -lcurl -lssl -lcrypto"
export CPPFLAGS="-I/opt/vagrant/embedded/include -I/opt/vagrant/embedded/include/libxml2 -lcurl -lssl -lcrypto"
export CXXFLAGS=""
export GEMRC="/opt/vagrant/embedded/etc/gemrc"
export GEM_HOME="${HOME}/.vagrant.d/gems/3.3.6"
export GEM_PATH="$GEM_HOME:/opt/vagrant/embedded/gems"
export LD_LIBRARY_PATH=".:/usr/lib:/opt/vagrant/embedded/lib"
export LDFLAGS="-L/usr/lib -L/opt/vagrant/embedded/lib -L/opt/vagrant/embedded/lib64"
export NOKOGIRI_USE_SYSTEM_LIBRARIES="true"
# If pkg-config is not available, try hardcoding the paths like this:
#export CONFIGURE_ARGS="--with-cflags='$CFLAGS' with-ldflags='$LDFLAGS' with-libvirt-include=/usr/include/libvirt with-libvirt-lib=/usr/lib with-curl-include=/usr/include/curl with-curl-lib=/usr/lib"
# Vagrant's "substrate" golang entrypoint sanitizes the environment
# vagrant-libvirt plugin links against system's libvirt and thus openssl + libcurl
# Meanwhile, vagrant links against its' embedded openssl & libcurl
# So, the following does not work on Arch Linux because only PKG_CONFIG_PATH and
# CONFIGURE_ARGS are exported and propagated down to the resulting gem install
# environment:
## VAGRANT_LOG=debug vagrant plugin install vagrant-libvirt
if pacman -Qi vagrant > /dev/null 2>&1 && \
pacman -Qi vagrant-git > /dev/null 2>&1 && \
pacman -Qi hashicorp-vagrant > /dev/null 2>&1 ; then
echo "Vagrant package already installed... moving on."
else
echo "Vagrant package not installed... Will download and install official hashicorp-vagrant."
curl -Lo "/tmp/$VAGRANT_ARCHLINUX_PKG" "$VAGRANT_ARCHLINUX_PKG_URL" && sync "/tmp/$VAGRANT_ARCHLINUX_PKG"
if [ -e "/tmp/$VAGRANT_ARCHLINUX_PKG" ]; then
sudo pacman -U /tmp/$VAGRANT_ARCHLINUX_PKG
else
echo "Could not install /tmp/$VAGRANT_ARCHLINUX_PKG ... Download failed? Exiting..." >&2
exit 1
fi
fi
if pkg-config --exists libvirt; then
echo "libvirt found via pkg-config"
echo "Installing gems into $GEM_HOME"
export CURL_CA_BUNDLE="/opt/vagrant/embedded/cacert.pem"
export SSL_CERT_FILE="/opt/vagrant/embedded/cacert.pem"
# Set paths for libvirt & curl to link against system libs
export CONFIGURE_ARGS="with-cflags='$CFLAGS' with-ldflags='$LDFLAGS' \
with-libvirt-lib=$(pkg-config --variable=libdir libvirt) \
with-libvirt-include=$(pkg-config --variable=includedir libvirt) \
with-libcurl-lib=$(pkg-config --variable=libdir libcurl) \
with-libcurl-include=$(pkg-config --variable=includedir libcurl)"
vagrant plugin install vagrant-libvirt
## Alternative method
## NOTE: may not work for vagrant to find the gems properly in the bundle
# /opt/vagrant/embedded/bin/gem install vagrant-libvirt -- \
# --with-libvirt-lib=$(pkg-config --variable=libdir libvirt) \
# --with-libvirt-include=$(pkg-config --variable=includedir libvirt) \
# --with-libcurl-lib=$(pkg-config --variable=libdir libcurl) \
# --with-libcurl-include=$(pkg-config --variable=includedir libcurl) \
# --with-cflags="$CFLAGS" \
# --with-ldflags="$LDFLAGS"
else
echo "libvirt NOT found in PKG_CONFIG_PATH ($PKG_CONFIG_PATH)" >&2
echo "OR pkg-config is not installed" >&2
echo "Install pkg-config and libvirt from your distro's package manager" >&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment