#!/bin/bash
# Filename: pve8-setup.sh
# Perform basic config/tuning on a newly installed Proxmox v8.x host
# Run this script only once!
#
# To run to run use one of the below commands:
# wget -O - -q https://go.porna.net/go/pve8-setup.sh | bash
# curl -s      https://go.poorna.net/go/pve8-setup.sh | bash

# Ensure that we are running on a proxmox host
if [ ! -f /usr/bin/pveversion ]; then
  echo 'This script is intended to run only on a Proxmox host'
  exit 1
fi

set -eoux pipefail

echo 'Setting up a new proxmox host'

# Speed up ZFS at the cost of possible risk data loss on power loss
echo 'Setting sync=disabled on rpool'
zfs get sync rpool
zfs set sync=disabled rpool
zfs get sync rpool

echo 'Setting atime=off on rpool'
zfs get atime rpool
zfs set atime=off rpool
zfs get atime rpool

echo 'Setting relatime=off on rpool'
zfs get relatime rpool
zfs set relatime=off rpool
zfs get relatime rpool

echo 'Updating PVE no-subscription lists'
rm /etc/apt/sources.list.d/{pve-*,ceph*}.list 

cat > /etc/apt/sources.list.d/pve-no-subscription.list <<EOT
# Filename: /etc/apt/sources.list.d/pve-no-subscription.list
# This is for Proxmox 8.x only
# Proxmox VE pve-no-subscription repository provided by proxmox.com,
# NOT recommended for production use
deb http://download.proxmox.com/debian/pve bookworm pve-no-subscription
EOT

cat > /etc/apt/sources.list.d/ceph.list <<EOT
# Filename: /etc/apt/sources.list.d/ceph.list
# This is for Proxmox 8.x only
deb http://download.proxmox.com/debian/ceph-quincy bookworm no-subscription
EOT

echo 'Updating installed packages'
apt update; apt -y full-upgrade; apt clean

echo 'Setting optimal CPU settings'
sed -i ' 1 s/$/ cpufreq.default_governor=powersave mitigations=off nomodeset/' /etc/kernel/cmdline
echo 'New /etc/kernel/cmdline'
cat /etc/kernel/cmdline
proxmox-boot-tool refresh 

echo 'Updating LXC templates'
pveam update

echo 'Downloading Ubuntu LXC templates'
pveam download local ubuntu-24.04-standard_24.04-2_amd64.tar.zst
pveam download local ubuntu-22.04-standard_22.04-1_amd64.tar.zst

echo 'Please reboot the Proxmox host now for the new settings to take effect.'
