/home/alex/dev/podman/pihole.sh (1)

From RaySoft
#!/bin/bash -
# ------------------------------------------------------------------------------
# pihole.sh
# =========
#
# Project   Replace Docker by Podman (Pi-hole)
# Scope     Linux
# Copyright (C) 2024 by RaySoft, Zurich, Switzerland
# License   GNU General Public License (GPL) 2.0
#           https://www.gnu.org/licenses/gpl2.txt
#
# ------------------------------------------------------------------------------

set -o 'errexit' -o 'noglob' -o 'nounset' -o 'pipefail' # -o 'xtrace'

# ------------------------------------------------------------------------------

POD_NAME='pihole'

DNSMASQ_CONFIG_DIR='etc/dnsmasq.d'
LIGHTTPD_CONFIG_DIR='etc/lighttpd'
PIHOLE_CONFIG_DIR='etc/pihole'

STORAGE_PATH='/var/lib/containers/volumes'

# ------------------------------------------------------------------------------

# Define paths for the storage directories
dnsmasq_storage_path="${STORAGE_PATH}/${POD_NAME}/${DNSMASQ_CONFIG_DIR////-}"
lighttpd_storage_path="${STORAGE_PATH}/${POD_NAME}/${LIGHTTPD_CONFIG_DIR////-}"
pihole_storage_path="${STORAGE_PATH}/${POD_NAME}/${PIHOLE_CONFIG_DIR////-}"

# Create storage directories
for dir in "${dnsmasq_storage_path}" "${lighttpd_storage_path}/cert" \
           "${pihole_storage_path}"
do
  [[ ! -d "${dir}" ]] && sudo mkdir -p "${dir}"
done

# Create a file for the external Lighttpd configuration
sudo touch "${lighttpd_storage_path}/external.conf"

# Set permissions for the main storage directorie
sudo find "${STORAGE_PATH}" \
           '(' -type 'd' -execdir chmod --changes 0755 '{}' '+' ')' \
       -or '(' -type 'f' -execdir chmod --changes 0644 '{}' '+' ')'

# Create a new pod
sudo podman pod create \
  --name="${POD_NAME}" \
  --publish='53:53/tcp' \
  --publish='53:53/udp' \
  --publish='80:80/tcp' \
  --publish='443:443/tcp' \
  --share='net'

# Create a new container in the pod
sudo podman run \
  --detach \
  --dns='127.0.0.1' \
  --dns='192.168.1.1' \
  --env='PIHOLE_DNS_=192.168.1.1' \
  --env='ServerIP=127.0.0.1' \
  --env='VIRTUAL_HOST=pi.hole' \
  --label 'io.containers.autoupdate=registry' \
  --name="${POD_NAME}-main" \
  --pod="${POD_NAME}" \
  --restart='always' \
  --volume="${dnsmasq_storage_path}/:/${DNSMASQ_CONFIG_DIR}/:z" \
  --volume="${lighttpd_storage_path}/cert/:/${LIGHTTPD_CONFIG_DIR}/cert/:z" \
  --volume="${lighttpd_storage_path}/external.conf:/${LIGHTTPD_CONFIG_DIR}/external.conf:z" \
  --volume="${pihole_storage_path}/:/${PIHOLE_CONFIG_DIR}/:z" \
  'docker.io/pihole/pihole:latest'

# Change to the script's directory
cd "${0%/*}"

# Generate the start scripts (one for the pod and one for each container)
sudo podman generate systemd --files --name --new "${POD_NAME}"

# Copy the start scripts
find . -maxdepth 1 -type 'f' -name "*${POD_NAME}*.service" \
  -execdir sudo cp --target-directory='/etc/systemd/system' '{}' '+'

# Reload the start scripts
sudo systemctl daemon-reload

# Stop the pod and its containers
sudo podman pod stop "${POD_NAME}"

# Remove the pod and its containers
sudo podman pod rm "${POD_NAME}"

# Enable and start the pod and its containers
sudo systemctl enable --now "pod-${POD_NAME}.service"

# Verify if the pod and its containers are running
sudo systemctl status "pod-${POD_NAME}.service"

# ------------------------------------------------------------------------------

exit 0

Usage

See Pi-hole Howto