Skip to content

Instantly share code, notes, and snippets.

@mawillcockson
Last active April 13, 2021 22:12
Show Gist options
  • Select an option

  • Save mawillcockson/4dd70d94f8ef1c997184664e42ab13d8 to your computer and use it in GitHub Desktop.

Select an option

Save mawillcockson/4dd70d94f8ef1c997184664e42ab13d8 to your computer and use it in GitHub Desktop.
Install virt-bootstrap on debian
#!/bin/sh
# This is a very dumb script: run it more than once, and it will mess things up.
#
# This is meant to be run from as a regular user.
#
# At the end, when the command virt-manager is run from bash, virt-manager will
# be launched with access to virt-bootstrap.
#
# To uninstall, remove the following directories and files:
# ~/.local/lib/virt-bootstrap
# ~/.local/bin/virt-manager
# ~/.local/bin/virt-bootstrap
# ~/.config/containers/policy.json
#
# The following packages would also no longer be required:
# skopeo
# virt-sandbox
# libguestfs-dev
# build-essential
# python3-dev
# python3-venv
#
# The skopeo package is currently in debian testing.
# To be able to install it, add the testing repositories with priorities set
# low. For example:
# https://serverfault.com/a/382101
#
# It appears to be in Ubuntu's normal repos, but only for 20.10 onwards. Skopeo's
# installation notes have information on installing it on earlier LTS releases:
# https://github.com/containers/skopeo/blob/master/install.md#ubuntu
#
# If this script falls out of date, and no longer works,
# feel free to fork it or leave a comment.
set -eux
sudo apt update -y
sudo apt install --no-install-recommends -y git curl virt-sandbox skopeo \
python3-venv libguestfs-dev build-essential python3-dev
SKOPEO_POLICY_FILE="${HOME}/.config/containers/policy.json"
VIRT_BOOTSTRAP_DIR="${HOME}/.local/lib/virt-bootstrap"
# skopeo needs a policy in order to be run by virt-bootstrap
# Please create an appropriate policy instead of using the default, insecure one:
# https://github.com/containers/skopeo/blob/master/docs/skopeo.1.md#files
if ! [ -f "${SKOPEO_POLICY_FILE}" ]; then
mkdir -p "$(dirname -- "${SKOPEO_POLICY_FILE}")"
curl -L https://github.com/containers/skopeo/raw/master/default-policy.json --output "${SKOPEO_POLICY_FILE}"
fi
# Create virtual environment for virt-bootstrap
mkdir -p "${VIRT_BOOTSTRAP_DIR}"
python3 -m venv --system-site-packages "${VIRT_BOOTSTRAP_DIR}/venv"
VENV_PYTHON="${VIRT_BOOTSTRAP_DIR}/venv/bin/python3"
[ -f "${VENV_PYTHON}" ]
# Update packaging tools in the environment
"${VENV_PYTHON}" -m pip install --upgrade pip setuptools wheel
# Download virt-bootstrap's source
if ! [ -d "${VIRT_BOOTSTRAP_DIR}/virt-bootstrap/.git" ]; then
git clone https://github.com/virt-manager/virt-bootstrap.git "${VIRT_BOOTSTRAP_DIR}/virt-bootstrap"
fi
git -C "${VIRT_BOOTSTRAP_DIR}/virt-bootstrap" fetch --all
git -C "${VIRT_BOOTSTRAP_DIR}/virt-bootstrap" fetch --tags
TAG="$(git -C "${VIRT_BOOTSTRAP_DIR}/virt-bootstrap" describe --abbrev=0 origin/master)"
git -C "${VIRT_BOOTSTRAP_DIR}/virt-bootstrap" checkout "${TAG}"
# virt-bootstrap's requirements.txt lists itself as a dependency, which causes
# problems when trying to install its dependencies, since its installation
# script, setup.py, ends up also trying to import the whole package during
# installation, which requires virt-bootstrap's dependencies to already be
# present. This problem is avoided by dropping the line at the end of
# virt-bootstrap's requirements.txt, where it lists itself as a dependency:
[ -f "${VIRT_BOOTSTRAP_DIR}/virt-bootstrap/requirements.txt" ]
# Makes sure the last line of the file is still "-e ."
[ X"-e ." = X"$(tail -n 1 "${VIRT_BOOTSTRAP_DIR}/virt-bootstrap/requirements.txt")" ]
head -n -1 "${VIRT_BOOTSTRAP_DIR}/virt-bootstrap/requirements.txt" \
> "${VIRT_BOOTSTRAP_DIR}/fixed-requirements.txt"
# Install virt-bootstrap's dependencies in the virtual environment
"${VENV_PYTHON}" -m pip install --upgrade -r "${VIRT_BOOTSTRAP_DIR}/fixed-requirements.txt"
# Install virt-bootstrap
"${VENV_PYTHON}" -m pip install "${VIRT_BOOTSTRAP_DIR}/virt-bootstrap"
[ -f "${VIRT_BOOTSTRAP_DIR}/venv/bin/virt-bootstrap" ]
# Configure a wrapper command for virt-manager
mkdir -p "${HOME}/.local/bin"
case ":${PATH}:" in
*":${HOME}/.local/bin:"*) ;; # ~/.local/bin is already in PATH
*)
mkdir -p "{$HOME}/.local/bin"
ADD_LOCAL_BIN="export PATH=\"${HOME}/.local/bin:\${PATH}\""
for file in .bashrc .zhsrc .profile ; do
if [ -f "${HOME}/${file}" ]; then
echo "${ADD_LOCAL_BIN}" >> "${HOME}/${file}"
fi
done
;;
esac
cat > "${HOME}"/.local/bin/virt-manager << EOF
"${VENV_PYTHON}" /usr/share/virt-manager/virt-manager "\$@"
EOF
chmod a+x "${HOME}"/.local/bin/virt-manager
if [ -f "${HOME}"/.local/bin/virt-bootstrap ]; then
if [ -L "${HOME}"/.local/bin/virt-bootstrap ]; then
:
else
echo "${HOME}/.local/bin/virt-bootstrap already exists and isn't a symlink"
exit 1
fi
else
ln -s "${VIRT_BOOTSTRAP_DIR}/venv/bin/virt-bootstrap" "${HOME}"/.local/bin/virt-bootstrap
fi
set +eux
echo "Re-login or restart the shell, and virt-bootstrap should be available
It was installed at ${VIRT_BOOTSTRAP_DIR}/venv/bin/virt-bootstrap"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment