Skip to content

Instantly share code, notes, and snippets.

@slazarov
Last active June 23, 2025 13:38
Show Gist options
  • Save slazarov/697ab562def68e6d448a89ddff1bbd23 to your computer and use it in GitHub Desktop.
Save slazarov/697ab562def68e6d448a89ddff1bbd23 to your computer and use it in GitHub Desktop.
#!/bin/bash
# --- Configuration Variables ---
REPO_URL="http://download.opensuse.org/repositories/home:/alvistack/xUbuntu_24.04/"
REPO_NAME="home_alvistack" # A short, unique name for the repository
PACKAGE_NAME="podman"
PIN_PRIORITY=900 # A priority higher than default (500) and ESM (510) to prefer this repo
# --- Script Logic ---
# Check if the script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root."
echo "Please use: sudo"
exit 1
fi
echo "--- Podman Installation from Custom Repository ---"
echo "This script will add the '$REPO_URL' repository and install '$PACKAGE_NAME' from it."
echo "It will set a pinning priority of $PIN_PRIORITY to prefer this repository."
echo ""
read -p "Do you want to proceed? (y/N): " confirm
if [[ ! "$confirm" =~ ^[yY]$ ]]; then
echo "Installation aborted."
exit 0
fi
# --- Check for GPG and install if not present ---
echo "Checking for 'gpg' (GnuPG) installation..."
if ! command -v gpg &>/dev/null; then
echo "'gpg' is not installed. Installing 'gnupg' package..."
sudo apt update # Ensure package lists are fresh before installing gnupg
sudo apt install -y gnupg
if [ $? -ne 0 ]; then
echo "Error: Failed to install 'gnupg'. Please install it manually and try again."
exit 1
fi
echo "'gnupg' installed successfully."
else
echo "'gpg' is already installed."
fi
# --- Extract the domain name for the 'Pin: origin' directive ---
# This is the corrected part
REPO_DOMAIN=$(echo "$REPO_URL" | awk -F'[/:]' '{print $4}')
if [ -z "$REPO_DOMAIN" ]; then
echo "Error: Could not extract domain from REPO_URL: $REPO_URL. Aborting."
exit 1
fi
echo "Extracted repository domain for pinning: $REPO_DOMAIN"
echo "1. Adding the GPG key for the repository..."
# Fetch the GPG key from the repository and add it to apt's trusted keys
# This usually comes from the Release.key or Release.gpg file at the repo root
# For OpenSUSE Build Service repos, it's often available at the base URL + Release.key
echo "Downloading ${REPO_URL}/Release.key and inserting into /etc/apt/trusted.gpg.d/${REPO_NAME}.gpg"
wget -qO- "${REPO_URL}/Release.key" | gpg --dearmor | sudo tee "/etc/apt/trusted.gpg.d/${REPO_NAME}.gpg" >/dev/null
if [ $? -ne 0 ]; then
echo "Error: Failed to add GPG key. Aborting."
exit 1
fi
echo "GPG key added."
echo "Fix permissions of GPG"
chmod 644 /etc/apt/trusted.gpg.d/${REPO_NAME}.gpg
echo "2. Adding the repository to APT sources..."
# Create the .sources file for the repository
cat <<EOF | sudo tee "/etc/apt/sources.list.d/${REPO_NAME}.sources" >/dev/null
Types: deb
URIs: ${REPO_URL}
Suites: /
Components:
Signed-By: /etc/apt/trusted.gpg.d/${REPO_NAME}.gpg
EOF
if [ $? -ne 0 ]; then
echo "Error: Failed to add repository to sources. Aborting."
exit 1
fi
echo "Repository added: /etc/apt/sources.list.d/${REPO_NAME}.sources"
echo "3. Creating APT preferences file for package pinning..."
# Create the preferences file to prioritize the custom repository for podman
cat <<EOF | sudo tee "/etc/apt/preferences.d/${REPO_NAME}-${PACKAGE_NAME}.pref" >/dev/null
Package: ${PACKAGE_NAME} buildah
Pin: origin "${REPO_DOMAIN}"
Pin-Priority: ${PIN_PRIORITY}
EOF
if [ $? -ne 0 ]; then
echo "Error: Failed to create preferences file. Aborting."
exit 1
fi
echo "Preferences file created: /etc/apt/preferences.d/${REPO_NAME}-${PACKAGE_NAME}.pref"
echo "4. Updating APT package lists..."
sudo apt update
if [ $? -ne 0 ]; then
echo "Error: APT update failed. Check your network or repository URL."
exit 1
fi
echo "APT package lists updated."
echo "5. Checking available versions of ${PACKAGE_NAME}..."
apt-cache policy ${PACKAGE_NAME}
read -p "Does the output above show a candidate version from '${REPO_URL}' with priority ${PIN_PRIORITY}? (y/N): " verify_policy
if [[ ! "$verify_policy" =~ ^[yY]$ ]]; then
echo "It seems the pinning did not work as expected. Please review the output and the script."
echo "Installation aborted."
exit 1
fi
echo "7. Installing dependencies"
sudo apt install -y \
uidmap \
containernetworking-dnsname \
containernetworking-podman-machine \
containers-storage \
podman-aardvark-dns \
podman-gvproxy \
podman-netavark \
python3-podman \
python3-podman-compose \
passt \
fuse-overlayfs \
buildah
echo "8. Installing ${PACKAGE_NAME}..."
sudo apt install -y ${PACKAGE_NAME}
if [ $? -ne 0 ]; then
echo "Error: Failed to install ${PACKAGE_NAME}. See output above for details."
exit 1
fi
echo "${PACKAGE_NAME} installed successfully."
echo "9. Verifying the installed version..."
if command -v ${PACKAGE_NAME} &>/dev/null; then
${PACKAGE_NAME} --version
else
echo "Could not find ${PACKAGE_NAME} command. Please check installation manually."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment