mirror of
https://github.com/kidwellj/proxmox-scripts.git
synced 2025-01-09 13:12:22 +00:00
Merge pull request #3 from StevenSeifried/pihole-cloudflared
This commit is contained in:
commit
17e2fbcc06
28
README.md
28
README.md
|
@ -88,6 +88,34 @@ ________________________________________________________________________________
|
|||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary markdown="span">Pi-hole with cloudflared LXC</summary>
|
||||
|
||||
<h1 align="center" id="heading"> Pi-hole with cloudflared LXC </h1>
|
||||
|
||||
**Please note:** I don't want discussions about cloudflared in the Issues.
|
||||
|
||||
To create a new Proxmox Pi-hole with cloudflared LXC, run the following in the Proxmox web shell.
|
||||
|
||||
```
|
||||
bash -c "$(wget -qLO - https://raw.githubusercontent.com/StevenSeifried/proxmox-scripts/main/pihole_cloudflared_container.sh)"
|
||||
```
|
||||
<h3 align="center" id="heading">Default Settings: 1024MiB RAM - 4GB Storage - 1vCPU</h3>
|
||||
|
||||
**Change Pi-hole password:**
|
||||
|
||||
Run from the LXC console
|
||||
|
||||
```
|
||||
pihole -a -p
|
||||
```
|
||||
|
||||
You must configure Pi-hole to use the local cloudflared service as the upstream DNS server by specifying "127.0.0.1#5053" as the "Custom DNS 1 (IPv4)".
|
||||
|
||||
____________________________________________________________________________________________
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary markdown="span">Emby Server LXC</summary>
|
||||
|
||||
|
|
16
cloudflared.service
Normal file
16
cloudflared.service
Normal file
|
@ -0,0 +1,16 @@
|
|||
[Unit]
|
||||
Description=cloudflared DNS over HTTPS proxy
|
||||
After=syslog.target network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=cloudflared
|
||||
EnvironmentFile=/etc/default/cloudflared
|
||||
ExecStart=/usr/local/bin/cloudflared proxy-dns $CLOUDFLARED_OPTS
|
||||
Restart=on-failure
|
||||
RestartSec=10
|
||||
KillMode=process
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
158
pihole_cloudflared_container.sh
Normal file
158
pihole_cloudflared_container.sh
Normal file
|
@ -0,0 +1,158 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
while true; do
|
||||
printf "This will create a new Pi-hole with cloudflared LXC.\n"
|
||||
read -p "Proceed (y/n)?" yn
|
||||
case $yn in
|
||||
[Yy]* ) break;;
|
||||
[Nn]* ) exit;;
|
||||
* ) echo "Please answer yes or no.";;
|
||||
esac
|
||||
done
|
||||
|
||||
set -o errexit
|
||||
set -o errtrace
|
||||
set -o nounset
|
||||
set -o pipefail
|
||||
shopt -s expand_aliases
|
||||
alias die='EXIT=$? LINE=$LINENO error_exit'
|
||||
trap die ERR
|
||||
trap cleanup EXIT
|
||||
|
||||
function error_exit() {
|
||||
trap - ERR
|
||||
local DEFAULT='Unknown failure occured.'
|
||||
local REASON="\e[97m${1:-$DEFAULT}\e[39m"
|
||||
local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
|
||||
msg "$FLAG $REASON"
|
||||
[ ! -z ${CTID-} ] && cleanup_ctid
|
||||
exit $EXIT
|
||||
}
|
||||
function warn() {
|
||||
local REASON="\e[97m$1\e[39m"
|
||||
local FLAG="\e[93m[WARNING]\e[39m"
|
||||
msg "$FLAG $REASON"
|
||||
}
|
||||
function info() {
|
||||
local REASON="$1"
|
||||
local FLAG="\e[36m[INFO]\e[39m"
|
||||
msg "$FLAG $REASON"
|
||||
}
|
||||
function msg() {
|
||||
local TEXT="$1"
|
||||
echo -e "$TEXT"
|
||||
}
|
||||
function cleanup_ctid() {
|
||||
if [ ! -z ${MOUNT+x} ]; then
|
||||
pct unmount $CTID
|
||||
fi
|
||||
if $(pct status $CTID &>/dev/null); then
|
||||
if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
|
||||
pct stop $CTID
|
||||
fi
|
||||
pct destroy $CTID
|
||||
elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
|
||||
pvesm free $ROOTFS
|
||||
fi
|
||||
}
|
||||
function cleanup() {
|
||||
popd >/dev/null
|
||||
rm -rf $TEMP_DIR
|
||||
}
|
||||
function load_module() {
|
||||
if ! $(lsmod | grep -Fq $1); then
|
||||
modprobe $1 &>/dev/null || \
|
||||
die "Failed to load '$1' module."
|
||||
fi
|
||||
MODULES_PATH=/etc/modules
|
||||
if ! $(grep -Fxq "$1" $MODULES_PATH); then
|
||||
echo "$1" >> $MODULES_PATH || \
|
||||
die "Failed to add '$1' module to load at boot."
|
||||
fi
|
||||
}
|
||||
TEMP_DIR=$(mktemp -d)
|
||||
pushd $TEMP_DIR >/dev/null
|
||||
|
||||
wget -qL https://raw.githubusercontent.com/StevenSeifried/proxmox-scripts/main/pihole_cloudflared_setup.sh
|
||||
|
||||
load_module overlay
|
||||
|
||||
while read -r line; do
|
||||
TAG=$(echo $line | awk '{print $1}')
|
||||
TYPE=$(echo $line | awk '{printf "%-10s", $2}')
|
||||
FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
|
||||
ITEM=" Type: $TYPE Free: $FREE "
|
||||
OFFSET=2
|
||||
if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
|
||||
MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
|
||||
fi
|
||||
STORAGE_MENU+=( "$TAG" "$ITEM" "OFF" )
|
||||
done < <(pvesm status -content rootdir | awk 'NR>1')
|
||||
if [ $((${#STORAGE_MENU[@]}/3)) -eq 0 ]; then
|
||||
warn "'Container' needs to be selected for at least one storage location."
|
||||
die "Unable to detect valid storage location."
|
||||
elif [ $((${#STORAGE_MENU[@]}/3)) -eq 1 ]; then
|
||||
STORAGE=${STORAGE_MENU[0]}
|
||||
else
|
||||
while [ -z "${STORAGE:+x}" ]; do
|
||||
STORAGE=$(whiptail --title "Storage Pools" --radiolist \
|
||||
"Which storage pool you would like to use for the container?\n\n" \
|
||||
16 $(($MSG_MAX_LENGTH + 23)) 6 \
|
||||
"${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
|
||||
done
|
||||
fi
|
||||
info "Using '$STORAGE' for storage location."
|
||||
|
||||
CTID=$(pvesh get /cluster/nextid)
|
||||
info "LXC ID is $CTID."
|
||||
|
||||
msg "Updating LXC template list..."
|
||||
pveam update >/dev/null
|
||||
msg "Downloading LXC template..."
|
||||
OSTYPE=debian
|
||||
OSVERSION=${OSTYPE}-11
|
||||
mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($OSVERSION.*\)/\1/p" | sort -t - -k 2 -V)
|
||||
TEMPLATE="${TEMPLATES[-1]}"
|
||||
pveam download local $TEMPLATE >/dev/null ||
|
||||
die "A problem occured while downloading the LXC template."
|
||||
|
||||
STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
|
||||
case $STORAGE_TYPE in
|
||||
dir|nfs)
|
||||
DISK_EXT=".raw"
|
||||
DISK_REF="$CTID/"
|
||||
;;
|
||||
zfspool)
|
||||
DISK_PREFIX="subvol"
|
||||
DISK_FORMAT="subvol"
|
||||
;;
|
||||
esac
|
||||
DISK=${DISK_PREFIX:-vm}-${CTID}-disk-0${DISK_EXT-}
|
||||
ROOTFS=${STORAGE}:${DISK_REF-}${DISK}
|
||||
|
||||
msg "Creating LXC..."
|
||||
DISK_SIZE=4G
|
||||
pvesm alloc $STORAGE $CTID $DISK $DISK_SIZE --format ${DISK_FORMAT:-raw} >/dev/null
|
||||
if [ "$STORAGE_TYPE" == "zfspool" ]; then
|
||||
warn "Some containers may not work properly due to ZFS not supporting 'fallocate'."
|
||||
else
|
||||
mkfs.ext4 $(pvesm path $ROOTFS) &>/dev/null
|
||||
fi
|
||||
ARCH=$(dpkg --print-architecture)
|
||||
HOSTNAME=pi-hole-cf
|
||||
TEMPLATE_STRING="local:vztmpl/${TEMPLATE}"
|
||||
pct create $CTID $TEMPLATE_STRING -arch $ARCH -features nesting=1 \
|
||||
-hostname $HOSTNAME -net0 name=eth0,bridge=vmbr0,ip=dhcp -onboot 1 -cores 1 -memory 1024 \
|
||||
-ostype $OSTYPE -rootfs $ROOTFS,size=$DISK_SIZE -storage $STORAGE >/dev/null
|
||||
|
||||
MOUNT=$(pct mount $CTID | cut -d"'" -f 2)
|
||||
ln -fs $(readlink /etc/localtime) ${MOUNT}/etc/localtime
|
||||
pct unmount $CTID && unset MOUNT
|
||||
|
||||
msg "Starting LXC..."
|
||||
pct start $CTID
|
||||
pct push $CTID pihole_cloudflared_setup.sh /pihole_cloudflared_setup.sh -perms 755
|
||||
pct exec $CTID /pihole_cloudflared_setup.sh
|
||||
|
||||
IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
|
||||
info "Successfully created a Pi-hole with cloudflared LXC to $CTID at IP Address ${IP}"
|
81
pihole_cloudflared_setup.sh
Normal file
81
pihole_cloudflared_setup.sh
Normal file
|
@ -0,0 +1,81 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -o errexit
|
||||
set -o errtrace
|
||||
set -o nounset
|
||||
set -o pipefail
|
||||
shopt -s expand_aliases
|
||||
alias die='EXIT=$? LINE=$LINENO error_exit'
|
||||
trap die ERR
|
||||
trap 'die "Script interrupted."' INT
|
||||
|
||||
function error_exit() {
|
||||
trap - ERR
|
||||
local DEFAULT='Unknown failure occured.'
|
||||
local REASON="\e[97m${1:-$DEFAULT}\e[39m"
|
||||
local FLAG="\e[91m[ERROR:LXC] \e[93m$EXIT@$LINE"
|
||||
msg "$FLAG $REASON"
|
||||
exit $EXIT
|
||||
}
|
||||
function msg() {
|
||||
local TEXT="$1"
|
||||
echo -e "$TEXT"
|
||||
}
|
||||
|
||||
msg "Setting up LXC OS..."
|
||||
sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
|
||||
locale-gen >/dev/null
|
||||
apt-get -y purge openssh-{client,server} >/dev/null
|
||||
apt-get autoremove >/dev/null
|
||||
|
||||
# Update container OS
|
||||
msg "Updating container OS..."
|
||||
apt update &>/dev/null
|
||||
apt-get -qqy upgrade &>/dev/null
|
||||
|
||||
msg "Installing Prerequisites..."
|
||||
apt-get -qqy install \
|
||||
curl \
|
||||
sudo &>/dev/null
|
||||
|
||||
msg "Installing cloudflared.."
|
||||
wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb &>/dev/null
|
||||
sudo apt-get install ./cloudflared-linux-amd64.deb &>/dev/null
|
||||
sudo useradd -s /usr/sbin/nologin -r -M cloudflared &>/dev/null
|
||||
rm cloudflared-linux-amd64.deb
|
||||
|
||||
CLOUDFLARED_CONF="/etc/default/cloudflared"
|
||||
cat << EOF > $CLOUDFLARED_CONF
|
||||
# Commandline args for cloudflared, using Cloudflare DNS
|
||||
CLOUDFLARED_OPTS=--port 5053 --upstream https://1.1.1.1/dns-query --upstream https://1.0.0.1/dns-query
|
||||
EOF
|
||||
|
||||
chown cloudflared:cloudflared /etc/default/cloudflared
|
||||
chown cloudflared:cloudflared /usr/local/bin/cloudflared
|
||||
|
||||
wget -O /etc/systemd/system/cloudflared.service https://raw.githubusercontent.com/StevenSeifried/proxmox-scripts/main/cloudflared.service &>/dev/null
|
||||
|
||||
systemctl daemon-reload
|
||||
|
||||
msg "Installing Pi-hole.."
|
||||
curl -sSL https://install.pi-hole.net | bash
|
||||
|
||||
msg "Enable and start cloudflared..."
|
||||
systemctl enable --now cloudflared
|
||||
|
||||
msg "Customizing LXC..."
|
||||
rm /etc/motd
|
||||
rm /etc/update-motd.d/10-uname
|
||||
touch ~/.hushlogin
|
||||
GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
|
||||
mkdir -p $(dirname $GETTY_OVERRIDE)
|
||||
cat << EOF > $GETTY_OVERRIDE
|
||||
[Service]
|
||||
ExecStart=
|
||||
ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
|
||||
EOF
|
||||
systemctl daemon-reload
|
||||
systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
|
||||
|
||||
msg "Cleanup..."
|
||||
rm -rf /pihole_cloudflared_setup.sh /var/{cache,log}/* /var/lib/apt/lists/*
|
Loading…
Reference in a new issue