proxmox-zfs-postinstall/proxmox-zfs-postinstall.sh

350 lines
15 KiB
Bash
Raw Normal View History

2021-04-12 22:07:17 +02:00
#!/bin/bash
2021-04-24 17:35:54 +02:00
###### CONFIG SECTION ######
2021-04-12 22:07:17 +02:00
# Define basic tools to install
2022-12-14 02:31:07 +01:00
TOOLS="sudo vim ifupdown2 libpve-network-perl net-tools dnsutils ethtool git curl unzip screen tmux iftop lshw smartmontools nvme-cli lsscsi sysstat zfs-auto-snapshot htop mc rpl lsb-release"
2021-04-12 22:07:17 +02:00
2021-04-28 14:46:24 +02:00
#### PVE CONF BACKUP CONFIGURATION ####
# Define target dataset for backup of /etc
# IMPORTANT NOTE: Don't type in the leading /, this will be set where needed
PVE_CONF_BACKUP_TARGET=rpool/pveconf
2022-12-14 02:31:07 +01:00
# Define timer for your backup cronjob (default: every 15 minutes from 3 through 59)
PVE_CONF_BACKUP_CRON_TIMER="3,18,33,48 * * * *"
2021-04-28 14:46:24 +02:00
# Get Debian version info
source /etc/os-release
2021-04-28 14:46:24 +02:00
2021-04-24 17:35:54 +02:00
###### SYSTEM INFO AND INTERACTIVE CONFIGURATION SECTION ######
2021-04-28 23:29:35 +02:00
ROUND_FACTOR=512
roundup(){
echo $(((($1 + $ROUND_FACTOR) / $ROUND_FACTOR) * $ROUND_FACTOR))
}
roundoff(){
echo $((($1 / $ROUND_FACTOR) * $ROUND_FACTOR))
}
2021-04-24 17:35:54 +02:00
#### L1ARC SIZE CONFIGURATION ####
# get total size of all zpools
ZPOOL_SIZE_SUM_BYTES=0
for line in $(zpool list -o size -Hp); do ZPOOL_SIZE_SUM_BYTES=$(($ZPOOL_SIZE_SUM_BYTES+$line)); done
2021-04-24 17:35:54 +02:00
# get information about available ram
2021-04-24 23:01:44 +02:00
MEM_TOTAL_BYTES=$(($(awk '/MemTotal/ {print $2}' /proc/meminfo) * 1024))
2021-04-24 17:35:54 +02:00
# get values if defaults are set
2021-04-24 20:39:58 +02:00
ARC_MAX_DEFAULT_BYTES=$(($MEM_TOTAL_BYTES / 2))
ARC_MIN_DEFAULT_BYTES=$(($MEM_TOTAL_BYTES / 32))
2021-04-24 17:35:54 +02:00
# get current settings
2021-04-24 22:45:22 +02:00
ARC_MIN_CUR_BYTES=$(cat /sys/module/zfs/parameters/zfs_arc_min)
ARC_MAX_CUR_BYTES=$(cat /sys/module/zfs/parameters/zfs_arc_max)
2021-04-12 22:07:17 +02:00
2021-04-24 17:35:54 +02:00
# calculate suggested l1arc sice
2021-04-28 23:39:01 +02:00
ZFS_ARC_MIN_MEGABYTES=$(roundup $(($ZPOOL_SIZE_SUM_BYTES / 2048 / 1024 / 1024)))
ZFS_ARC_MAX_MEGABYTES=$(roundoff $(($ZPOOL_SIZE_SUM_BYTES / 1024 / 1024 / 1024)))
2021-04-24 17:35:54 +02:00
echo -e "######## CONFIGURE ZFS L1ARC SIZE ########\n"
echo "System Summary:"
2021-04-24 20:39:58 +02:00
echo -e "\tSystem Memory:\t\t$(($MEM_TOTAL_BYTES / 1024 / 1024))\tMB"
echo -e "\tZpool size (sum):\t$(($ZPOOL_SIZE_SUM_BYTES / 1024 / 1024))\tMB"
2021-04-24 17:35:54 +02:00
echo -e "Calculated l1arc if set to defaults:"
if [ $ARC_MIN_DEFAULT_BYTES -lt 33554432 ]; then
2021-04-24 20:39:58 +02:00
echo -e "\tDefault zfs_arc_min:\t32\tMB"
2021-04-24 17:35:54 +02:00
else
2021-04-24 20:39:58 +02:00
echo -e "\tDefault zfs_arc_min:\t$(($ARC_MIN_DEFAULT_BYTES / 1024 / 1024))\tMB"
2021-04-24 17:35:54 +02:00
fi
2021-04-24 20:39:58 +02:00
echo -e "\tDefault zfs_arc_max:\t$(($ARC_MAX_DEFAULT_BYTES / 1024 / 1024))\tMB"
2021-04-24 17:35:54 +02:00
echo -e "Current l1arc configuration:"
2021-04-24 22:45:22 +02:00
if [ $ARC_MIN_CUR_BYTES -gt 0 ]; then
echo -e "\tCurrent zfs_arc_min:\t$(($ARC_MIN_CUR_BYTES / 1024 / 1024))\tMB"
2021-04-24 17:35:54 +02:00
else
echo -e "\tCurrent zfs_arc_min:\t0"
fi
2021-04-24 22:45:22 +02:00
if [ $ARC_MAX_CUR_BYTES -gt 0 ]; then
echo -e "\tCurrent zfs_arc_max:\t$(($ARC_MAX_CUR_BYTES / 1024 / 1024))\tMB"
2021-04-24 17:35:54 +02:00
else
echo -e "\tCurrent zfs_arc_max:\t0"
fi
echo -e "Note: If your current values are 0, the calculated values above will apply."
echo ""
2021-04-24 20:39:58 +02:00
echo -e "The l1arc cache will be set relative to the size (sum) of your zpools by policy"
2021-04-28 23:29:35 +02:00
echo -e "zfs_arc_min:\t\t\t$(($ZFS_ARC_MIN_MEGABYTES))\tMB\t\t= 512 MB RAM per 1 TB ZFS storage (round off in 512 MB steps)"
echo -e "zfs_arc_max:\t\t\t$(($ZFS_ARC_MAX_MEGABYTES))\tMB\t\t= 1 GB RAM per 1 TB ZFS storage (round up in 512 MB steps)"
2021-04-24 17:35:54 +02:00
echo ""
RESULT=not_set
2021-04-24 20:39:58 +02:00
while [ "$(echo $RESULT | awk '{print tolower($0)}')" != "y" ] && [ "$(echo $RESULT | awk '{print tolower($0)}')" != "n" ] && [ "$(echo $RESULT | awk '{print tolower($0)}')" != "" ]; do
read -p "If you want to apply the values by script policy type 'y', type 'n' to adjust the values yourself [Y/n]? "
2021-04-24 17:35:54 +02:00
RESULT=${REPLY}
done
if [[ "$(echo $RESULT | awk '{print tolower($0)}')" == "n" ]]; then
read -p "Please type in the desired value in MB for 'zfs_arc_min' [$(($ZFS_ARC_MIN_MEGABYTES))]: "
2021-04-24 17:35:54 +02:00
if [[ ${REPLY} -gt 0 ]]; then
2021-04-28 23:29:35 +02:00
ZFS_ARC_MIN_MEGABYTES=$((${REPLY}))
2021-04-24 17:35:54 +02:00
fi
read -p "Please type in the desired value in MB for 'zfs_arc_max' [$(($ZFS_ARC_MAX_MEGABYTES))]: "
2021-04-24 17:35:54 +02:00
if [[ ${REPLY} -gt 0 ]]; then
2021-04-28 23:29:35 +02:00
ZFS_ARC_MAX_MEGABYTES=$((${REPLY}))
2021-04-24 17:35:54 +02:00
fi
fi
#### Create Swapfile ####
echo -e "######## Configure Swapfile ########\n"
read -p "With the default PVE-root on ZFS installation, no swap is created. Do you want to create a swapfile/zfs swap volume dataset? (y/n)" swapfile_userinput_bool
if [ $swapfile_userinput_bool == "y" ]; then
read -p "Name for the swap dataset? Leave empty to use default 'swap'" swapfile_userinput_name
if [ x$swapfile_userinput_name == "x" ]; then
swapfile_name="swap";
else
swapfile_name=$swapfile_userinput_name;
fi
read -p "Size for the swap volume in G, only numbers" swapfile_userinput_size
if echo "$swapfile_userinput_size" | grep -qE '^[0-9]+$'; then
swapfile_size=$swapfile_userinput_size;
else
echo "No valid input detected, falling back to 20G Swap Size";
swapfile_size="20";
fi
else
echo "No swapfile will be created"
fi
2021-04-30 23:35:23 +02:00
#### SWAPPINESS ####
echo -e "######## CONFIGURE SWAPPINESS ########\n"
SWAPPINESS=$(cat /proc/sys/vm/swappiness)
echo "The current swappiness is configured to '$SWAPPINESS %' of free memory until using swap."
2023-04-20 14:45:44 +02:00
read -p "If you want to change the swappiness, please type in the percentage as number (0 = disabled):" user_input
2021-04-30 23:35:23 +02:00
if echo "$user_input" | grep -qE '^[0-9]+$'; then
echo "Changing swappiness from '$SWAPPINESS %' to '$user_input %'"
SWAPPINESS=$user_input
else
echo "No input - swappiness unchanged at '$SWAPPINESS %'."
fi
2021-04-24 17:35:54 +02:00
#### ZFS AUTO SNAPSHOT CONFIGURATION ####
# get information about zfs-auto-snapshot and ask for snapshot retention
2021-04-24 20:39:58 +02:00
declare -A auto_snap_keep=( ["frequent"]="8" ["hourly"]="48" ["daily"]="31" ["weekly"]="8" ["monthly"]="3" )
dpkg -l zfs-auto-snapshot > /dev/null
if [ $? -eq 0 ]; then
echo "'zfs-auto-snapshot' already installed. Reading config..."
for interval in "${!auto_snap_keep[@]}"; do
2021-04-24 17:35:54 +02:00
if [[ "$interval" == "frequent" ]]; then
2021-04-24 20:39:58 +02:00
auto_snap_keep[$interval]=$(cat /etc/cron.d/zfs-auto-snapshot | grep keep | cut -d' ' -f19 | cut -d '=' -f2)
2021-04-24 17:35:54 +02:00
else
2021-04-24 20:39:58 +02:00
auto_snap_keep[$interval]=$(cat /etc/cron.$interval/zfs-auto-snapshot | grep keep | cut -d' ' -f6 | cut -d'=' -f2)
2021-04-24 17:35:54 +02:00
fi
done
2021-04-24 20:39:58 +02:00
else
2021-04-24 23:31:10 +02:00
echo "'zfs-auto-snapshot' not installed yet, using script defaults..."
2021-04-24 17:35:54 +02:00
fi
echo -e "######## CONFIGURE ZFS AUTO SNAPSHOT ########\n"
2021-04-24 20:39:58 +02:00
for interval in "${!auto_snap_keep[@]}"; do
read -p "Please set how many $interval snapshots to keep (current: keep=${auto_snap_keep[$interval]})" user_input
if echo "$user_input" | grep -qE '^[0-9]+$'; then
echo "Changing $interval from ${auto_snap_keep[$interval]} to $user_input"
auto_snap_keep[$interval]=$user_input
else
2021-04-28 14:21:05 +02:00
echo "No input - $interval unchanged at ${auto_snap_keep[$interval]}."
2021-04-24 17:35:54 +02:00
fi
done
#### CHECKMK AGENT CONFIGURATION ####
2022-12-14 02:31:07 +01:00
read -p "Do you want to install checkmk agent on this machine? [y/N] " install_checkmk
if [[ "$install_checkmk" == "y" ]]; then
read -p "Please specify the base url to your checkmk server (e.g. https://check.zmb.rocks/bashclub): " cmk_agent_url
read -p "Enable agent encryption (requires setup of Agent Encryption on your checkmk instance). Do you want to activate agent encryption? [y/N] " cmk_encrypt
if [[ "$cmk_encrypt" == "y" ]]; then
read -p "Please enter the encryption passphrase: " cmk_enc_pass
fi
read -p "Register your machine on your checkmk server (requires preconfigured automation secret)? [y/N] " cmk_register
if [[ "$cmk_register" == "y" ]]; then
read -p "Please enter your automation secret: " cmk_secret
read -p "Please enter the folder where to store the host: " cmk_folder
cmk_site=$(echo $cmk_agent_url | cut -d'/' -f4)
read -p "Please enter the checkmk site name: [$cmk_site]" user_input
if [[ $(echo -n "$user_input") != "" ]]; then
cmk_site=$user_input
fi
echo "Please select which agent ip address to register:"
select ip in $(ip a | grep "inet " | cut -d ' ' -f6 | cut -d/ -f1); do
cmk_reg_ip=$ip
break
done
fi
fi
2021-04-24 17:35:54 +02:00
###### INSTALLER SECTION ######
2021-04-12 22:07:17 +02:00
# disable pve-enterprise repo and add pve-no-subscription repo
2022-10-17 16:27:27 +02:00
#Not tested, yet!
read -p "Do you want to disable pve-enterprise repo and add pve-no-subscription repo (y/N)? " response
2022-10-17 16:27:27 +02:00
if [ "${response,,}" == "y" ]; then
2022-10-17 16:27:27 +02:00
if [[ "$(uname -r)" == *"-pve" ]]; then
echo "Deactivating pve-enterprise repository"
mv /etc/apt/sources.list.d/pve-enterprise.list /etc/apt/sources.list.d/pve-enterprise.list.bak > /dev/null 2>&1
echo "Activating pve-no-subscription repository"
q=$(cat /etc/apt/sources.list | grep "pve-no-subscription")
if [ $? -gt 0 ]; then
echo "deb http://download.proxmox.com/debian/pve $VERSION_CODENAME pve-no-subscription" >> /etc/apt/sources.list
fi
rm -f /etc/apt/sources.list.d/pve-no-subscription.list
fi
2021-04-24 23:31:10 +02:00
fi
2022-10-17 16:27:27 +02:00
2021-04-28 14:21:05 +02:00
echo "Getting latest package lists"
apt update > /dev/null 2>&1
2021-04-12 22:07:17 +02:00
# include interfaces.d to enable SDN features
q=$(cat /etc/network/interfaces | grep "source /etc/network/interfaces.d/*")
if [ $? -gt 0 ]; then
echo "source /etc/network/interfaces.d/*" >> /etc/network/interfaces
fi
2021-04-12 22:07:17 +02:00
# update system and install basic tools
2021-04-28 14:21:05 +02:00
echo "Upgrading system to latest version - Depending on your version this could take a while..."
DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical apt -y -qq dist-upgrade > /dev/null 2>&1
echo "Installing toolset - Depending on your version this could take a while..."
DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical apt -y -qq install $TOOLS > /dev/null 2>&1
2021-04-12 22:07:17 +02:00
# configure zfs-auto-snapshot
2021-04-24 20:39:58 +02:00
for interval in "${!auto_snap_keep[@]}"; do
2021-04-28 14:46:24 +02:00
echo "Setting zfs-auto-snapshot retention: $interval = ${auto_snap_keep[$interval]}"
2021-04-24 17:35:54 +02:00
if [[ "$interval" == "frequent" ]]; then
CURRENT=$(cat /etc/cron.d/zfs-auto-snapshot | grep keep | cut -d' ' -f19 | cut -d '=' -f2)
if [[ "${auto_snap_keep[$interval]}" != "$CURRENT" ]]; then
2021-04-28 14:21:05 +02:00
rpl "keep=$CURRENT" "keep=${auto_snap_keep[$interval]}" /etc/cron.d/zfs-auto-snapshot > /dev/null 2>&1
2021-04-24 17:35:54 +02:00
fi
else
CURRENT=$(cat /etc/cron.$interval/zfs-auto-snapshot | grep keep | cut -d' ' -f6 | cut -d'=' -f2)
2021-04-24 20:39:58 +02:00
if [[ "${auto_snap_keep[$interval]}" != "$CURRENT" ]]; then
2021-04-28 14:21:05 +02:00
rpl "keep=$CURRENT" "keep=${auto_snap_keep[$interval]}" /etc/cron.$interval/zfs-auto-snapshot > /dev/null 2>&1
2021-04-24 17:35:54 +02:00
fi
fi
done
2021-04-12 22:07:17 +02:00
if [ $swapfile_userinput_bool -eq "y" ]; then
echo "Creating swapfile"
zfs create -V ${swapfile_size}G -b $(getconf PAGESIZE) -o logbias=throughput -o sync=always -o primarycache=metadata -o com.sun:auto-snapshot=false rpool/$swapfile_name
echo "Formatting swapfile"
mkswap -f /dev/zvol/rpool/$swapfile_name
echo "Turning on swap"
swapon /dev/zvol/rpool/$swapfile_name
echo "Adding swapfile to fstab"
echo "/dev/zvol/rpool/$swapfile_name none swap discard 0 0" >> /etc/fstab
fi
2021-04-30 23:35:23 +02:00
echo "Configuring swappiness"
echo "vm.swappiness=$SWAPPINESS" > /etc/sysctl.d/swappiness.conf
sysctl -w vm.swappiness=$SWAPPINESS
2021-04-28 14:21:05 +02:00
echo "Configuring pve-conf-backup"
# create backup jobs of /etc
zfs list $PVE_CONF_BACKUP_TARGET > /dev/null 2>&1
if [ $? -ne 0 ]; then
zfs create $PVE_CONF_BACKUP_TARGET
fi
2022-01-17 21:42:03 +01:00
if [[ "$(df -h -t zfs | grep /$ | cut -d ' ' -f1)" == "rpool/ROOT/pve-1" ]] ; then
echo "$PVE_CONF_BACKUP_CRON_TIMER root rsync -va --delete /etc /$PVE_CONF_BACKUP_TARGET > /$PVE_CONF_BACKUP_TARGET/pve-conf-backup.log" > /etc/cron.d/pve-conf-backup
fi
2021-04-28 14:21:05 +02:00
2021-04-28 23:29:35 +02:00
ZFS_ARC_MIN_BYTES=$((ZFS_ARC_MIN_MEGABYTES * 1024 *1024))
ZFS_ARC_MAX_BYTES=$((ZFS_ARC_MAX_MEGABYTES * 1024 *1024))
2021-04-28 14:21:05 +02:00
echo "Adjusting ZFS level 1 arc"
2021-04-12 22:07:17 +02:00
echo $ZFS_ARC_MIN_BYTES > /sys/module/zfs/parameters/zfs_arc_min
echo $ZFS_ARC_MAX_BYTES > /sys/module/zfs/parameters/zfs_arc_max
cat << EOF > /etc/modprobe.d/zfs.conf
2021-06-09 14:42:12 +02:00
options zfs zfs_arc_max=$ZFS_ARC_MAX_BYTES
options zfs zfs_arc_min=$ZFS_ARC_MIN_BYTES
2021-04-12 22:07:17 +02:00
EOF
2021-04-24 17:35:54 +02:00
if [[ "$install_checkmk" == "y" ]]; then
echo "Installing checkmk agent..."
if [[ $( echo -n "$(openssl s_client -connect $(echo $cmk_agent_url | cut -d'/' -f3):443 <<< "Q" 2>/dev/null | grep "Verify return code" | cut -d ' ' -f4)" ) -gt 0 ]]; then
wget_opts="--no-check-certificate"
curl_opts="--insecure"
fi
wget -q -O /usr/local/bin/check_mk_agent $wget_opts $cmk_agent_url/check_mk/agents/check_mk_agent.linux
2021-05-21 13:21:44 +02:00
wget -q -O /usr/local/bin/mk-job $wget_opts $cmk_agent_url/check_mk/agents/mk-job
wget -q -O /usr/local/bin/check_mk_caching_agent $wget_opts $cmk_agent_url/check_mk/agents/check_mk_caching_agent.linux
wget -q -O /usr/local/bin/waitmax $wget_opts $cmk_agent_url/check_mk/agents/waitmax
chmod +x /usr/local/bin/check_mk_agent
2021-05-21 13:21:44 +02:00
chmod +x /usr/local/bin/mk-job
chmod +x /usr/local/bin/check_mk_caching_agent
chmod +x /usr/local/bin/waitmax
/usr/local/bin/check_mk_agent > /dev/null
wget -q -O /etc/systemd/system/check_mk.socket $wget_opts $cmk_agent_url/check_mk/agents/cfg_examples/systemd/check_mk.socket
cat << EOF > /etc/systemd/system/check_mk@.service
# systemd service definition file
[Unit]
Description=Check_MK
[Service]
# "-" path prefix makes systemd record the exit code,
# but the unit is not set to failed.
ExecStart=-/usr/local/bin/check_mk_agent
Type=forking
User=root
Group=root
StandardInput=socket
EOF
2021-05-21 13:21:44 +02:00
mkdir -p /etc/check_mk
if [[ "$cmk_encrypt" == "y" ]]; then
2021-05-21 13:21:44 +02:00
mkdir -p /etc/check_mk
cat << EOF > /etc/check_mk/encryption.cfg
ENCRYPTED=yes
PASSPHRASE='$cmk_enc_pass'
EOF
chmod 600 /etc/check_mk/encryption.cfg
fi
2021-05-21 13:21:44 +02:00
mkdir -p /var/lib/check_mk_agent
mkdir -p /var/lib/check_mk_agent/spool
mkdir -p /var/lib/check_mk_agent/job
mkdir -p /usr/lib/check_mk_agent/local
mkdir -p /usr/lib/check_mk_agent/plugins
wget -q -O /usr/lib/check_mk_agent/plugins/smart $wget_opts $cmk_agent_url/check_mk/agents/plugins/smart
chmod +x /usr/lib/check_mk_agent/plugins/smart
wget -q -O /usr/lib/check_mk_agent/plugins/mk_inventory $wget_opts $cmk_agent_url/check_mk/agents/plugins/mk_inventory.linux
chmod +x /usr/lib/check_mk_agent/plugins/mk_inventory
wget -q -O /usr/lib/check_mk_agent/plugins/mk_apt $wget_opts $cmk_agent_url/check_mk/agents/plugins/mk_apt
chmod +x /usr/lib/check_mk_agent/plugins/mk_apt
#LocalDirectory: /usr/lib/check_mk_agent/local
systemctl daemon-reload
systemctl enable check_mk.socket
systemctl restart sockets.target
if [[ "$cmk_register" == "y" ]]; then
2021-05-21 13:21:44 +02:00
cmk_request="request={\"hostname\":\"$(echo -n $(hostname -f))\",\"folder\":\"$cmk_folder\",\"attributes\":{\"ipaddress\":\"$cmk_reg_ip\",\"site\":\"$cmk_site\",\"tag_agent\":\"cmk-agent\"},\"create_folders\":\"1\"}"
curl $curl_opts "$cmk_agent_url/check_mk/webapi.py?action=add_host&_secret=$cmk_secret&_username=automation" -d $cmk_request
curl $curl_opts "$cmk_agent_url/check_mk/webapi.py?action=activate_changes&_secret=$cmk_secret&_username=automation" -d "request={\"sites\":[\"$cmk_site\"],\"allow_foreign_changes\":\"0\"}"
fi
fi
2021-04-28 14:21:05 +02:00
echo "Updating initramfs - This will take some time..."
update-initramfs -u -k all > /dev/null 2>&1
2021-04-24 17:35:54 +02:00
2021-06-09 14:42:12 +02:00
echo "Proxmox postinstallation finished!"