diff --git a/pve-zfs-postinstall.sh b/postinstall old mode 100644 new mode 100755 similarity index 51% rename from pve-zfs-postinstall.sh rename to postinstall index d2e7f64..87435f4 --- a/pve-zfs-postinstall.sh +++ b/postinstall @@ -8,6 +8,8 @@ # - Install and configure zfs-auto-snapshot # - Switch pve-enterprise/pve-no-subscription repo # - Disable "No subscription message" in webinterface in no-subscription mode +# - Add pve-enterprise subscription key +# - Configure ceph repo # - Update system to the latest version # - Install common tools # - Install Proxmox SDN Extensions @@ -60,6 +62,10 @@ SWAPPINESS=$(cat /proc/sys/vm/swappiness) # zfs-auto-snapshot default values declare -A auto_snap_keep=( ["frequent"]="12" ["hourly"]="96" ["daily"]="14" ["weekly"]="6" ["monthly"]="3" ) +# gather proxmox subscription info +serverid=$(pvesubscription get | grep serverid | cut -d' ' -f2) +sub_status=$(pvesubscription get | grep status | cut -d' ' -f2) + #### FUNCTIONS #### roundup(){ @@ -99,7 +105,6 @@ cancel_dialog() { } arc_suggestion(){ - ZFS_ARC_MIN_MEGABYTES=$(roundoff $(($ZPOOL_SIZE_SUM_BYTES / 2048 / 1024 / 1024))) ZFS_ARC_MAX_MEGABYTES=$(roundup $(($ZPOOL_SIZE_SUM_BYTES / 1024 / 1024 / 1024))) @@ -125,8 +130,11 @@ Suggested values: \n \ } arc_set_manual() { - if ! ZFS_ARC_MIN_MEGABYTES=$(inputbox_int 'CONFIGURE ZFS L1ARC MIN SIZE' 'Please enter zfs_arc_min in MB' 7 $ZFS_ARC_MIN_MEGABYTES) ; then cancel_dialog ; fi - if ! ZFS_ARC_MAX_MEGABYTES=$(inputbox_int 'CONFIGURE ZFS L1ARC MAX SIZE' 'Please enter zfs_arc_max in MB' 7 $ZFS_ARC_MAX_MEGABYTES) ; then cancel_dialog ; fi + if [ $ARC_MIN_CURRENT_MB -gt 0 ]; then MIN_VALUE=$ARC_MIN_CURRENT_MB; else $ZFS_ARC_MIN_MEGABYTES; fi + if [ $ARC_MAX_CURRENT_MB -gt 0 ]; then MAX_VALUE=$ARC_MAX_CURRENT_MB; else $ZFS_ARC_MAX_MEGABYTES; fi + + if ! ZFS_ARC_MIN_MEGABYTES=$(inputbox_int 'CONFIGURE ZFS L1ARC MIN SIZE' 'Please enter zfs_arc_min in MB' 7 $MIN_VALUE) ; then cancel_dialog ; fi + if ! ZFS_ARC_MAX_MEGABYTES=$(inputbox_int 'CONFIGURE ZFS L1ARC MAX SIZE' 'Please enter zfs_arc_max in MB' 7 $MAX_VALUE) ; then cancel_dialog ; fi } vm_swappiness () { @@ -148,36 +156,69 @@ auto_snapshot(){ done } -check_subscription(){ - serverid=$(pvesh get nodes/px1/subscription --output-format yaml | grep serverid | cut -d' ' -f2) - sub_status=$(pvesh get nodes/px1/subscription --output-format yaml | grep status | cut -d' ' -f2) - if [[ $sub_status == "notfound" ]]; then +select_subscription(){ + suppress_warning=0 + if [[ $sub_status == "notfound" ]] || [[ $sub_status == "invalid" ]]; then if [[ $repo_selection == "pve-enterprise" ]]; then if whiptail --title "NO PROXMOX SUBSCRIPTION FOUND" \ --backtitle $PROG \ --yes-button "ADD" \ --no-button "SKIP" \ - --yesno "Server ID: $serverid\nDo you want to add a subscription key?" 17 76 ; then - add_subscription + --yesno "Server ID: $serverid\n\nDo you want to add a subscription key?" 9 76 ; then + input_subscription fi else if whiptail --title "NO PROXMOX SUBSCRIPTION FOUND" \ --backtitle $PROG \ --yes-button "SUPPRESS WARNING" \ --no-button "SKIP" \ - --yesno "Do you want to suppress the no subscription warning in WebGUI?" 17 76 ; then - suppress_no_subscription_warning + --yesno "Do you want to suppress the no subscription warning in WebGUI?" 9 76 ; then + suppress_warning=1 fi fi fi } -add_subscription(){ +ask_locales(){ + locales=$(whiptail --title "SET LOCALES" --backtitle "$PROG" --inputbox "Please enter a space separated list of locales to generate." 9 76 "$(echo $(grep -vE '#|^$' /etc/locale.gen | cut -d ' ' -f1))" 3>&1 1>&2 2>&3) +} +input_subscription(){ + key="" + cancel=0 + while [[ $key == "" ]]; do + if ! key=$(whiptail --title "ADD PROXMOX SUBSCRIPTION KEY" --backtitle "$PROG" \ + --inputbox "Server ID: $serverid\n\nAdd your subscription key" 9 76 3>&1 1>&2 2>&3) ; then + cancel=1 ; break + fi + done + if [ $cancel -eq 0 ]; then + set_subscription $key + fi + return $cancel +} + +set_subscription(){ + if ! pvesubscription set $1; then + input_subscription + elif [[ $(pvesubscription get | grep status | cut -d' ' -f2) == "invalid" ]]; then + input_subscription + fi } suppress_no_subscription_warning(){ + if [ $suppress_warning -gt 0 ]; then + # remove old no-sub-hack + if [ -f /opt/bashclub/no-sub-hack.sh ] ; then rm -r /opt/bashclub ; fi + if [ -f /etc/apt/apt.conf.d/80bashclubapthook ] ; then rm /etc/apt/apt.conf.d/80bashclubapthook ; fi + wget --no-cache -O /usr/local/bin/suppress_no_subscription_warning https://github.com/bashclub/no-sub-hack/raw/main/no-sub-hack.sh + chmod +x /usr/local/bin/suppress_no_subscription_warning + /usr/local/bin/suppress_no_subscription_warning + cat << EOF > /etc/apt/apt.conf.d/80-suppress_no_subscription_warning +DPkg::Post-Invoke {"/usr/local/bin/suppress_no_subscription_warning";}; +EOF + fi } select_pve_repos(){ @@ -185,13 +226,13 @@ select_pve_repos(){ pvenosubscription=OFF pvetest=OFF if [ -f /etc/apt/sources.list.d/pve-enterprise.list ]; then - if $(grep -v '#' /etc/apt/sources.list.d/pve-enterprise.list | grep "pve-enterprise") ; then + if grep -v '#' /etc/apt/sources.list.d/pve-enterprise.list | grep "pve-enterprise" > /dev/null ; then pveenterprise=ON else if [ -f /etc/apt/sources.list ]; then - if $(grep -v '#' /etc/apt/sources.list | grep "pve-no-subscription") ; then + if grep -v '#' /etc/apt/sources.list | grep "pve-no-subscription" > /dev/null ; then pvenosubscription=ON - elif $(grep -v '#' /etc/apt/sources.list | grep "pvetest") ; then + elif grep -v '#' /etc/apt/sources.list | grep "pvetest" > /dev/null ; then pvetest=ON else pveenterprise=ON @@ -205,6 +246,125 @@ select_pve_repos(){ "pve-no-subscription" "Proxmox VE No Subscription repository" "$pvenosubscription" \ "pvetest" "Proxmox VE Testing repository" "$pvetest" 3>&1 1>&2 2>&3) +} + +set_locales(){ + for locale in $locales; do + line=$(grep $locale /etc/locale.gen) + if echo $line | grep "#" ; then + sed -i "s/$line/$(echo $line | cut -d' ' -f2-)" /etc/locale.gen + fi + done + locale-gen +} + +set_pve_repo(){ + echo "" +} + +update_system(){ + echo "Getting latest package lists" + apt update > /dev/null 2>&1 + 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 +} + +install_tools(){ + echo "Installing toolset - Depending on your version this could take a while..." + DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical apt -y -qq install $REQUIRED_TOOLS $OPTIONAL_TOOLS > /dev/null 2>&1 +} + +enable_sdn(){ + # 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 +} + +set_arc_cache(){ + ZFS_ARC_MIN_BYTES=$((ZFS_ARC_MIN_MEGABYTES * 1024 *1024)) + ZFS_ARC_MAX_BYTES=$((ZFS_ARC_MAX_MEGABYTES * 1024 *1024)) + echo "Adjusting ZFS level 1 arc" + 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 +options zfs zfs_arc_max=$ZFS_ARC_MAX_BYTES +options zfs zfs_arc_min=$ZFS_ARC_MIN_BYTES +EOF +} + +set_auto_snapshot(){ + # configure zfs-auto-snapshot + for interval in "${!auto_snap_keep[@]}"; do + echo "Setting zfs-auto-snapshot retention: $interval = ${auto_snap_keep[$interval]}" + 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 + rpl "keep=$CURRENT" "keep=${auto_snap_keep[$interval]}" /etc/cron.d/zfs-auto-snapshot > /dev/null 2>&1 + fi + else + CURRENT=$(cat /etc/cron.$interval/zfs-auto-snapshot | grep keep | cut -d' ' -f6 | cut -d'=' -f2) + if [[ "${auto_snap_keep[$interval]}" != "$CURRENT" ]]; then + rpl "keep=$CURRENT" "keep=${auto_snap_keep[$interval]}" /etc/cron.$interval/zfs-auto-snapshot > /dev/null 2>&1 + fi + fi + done +} + +set_swappiness(){ + echo "Configuring swappiness" + echo "vm.swappiness=$SWAPPINESS" > /etc/sysctl.d/swappiness.conf + sysctl -w vm.swappiness=$SWAPPINESS +} + +pve_conf_backup(){ + 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 + + 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 +} + +harden_ssh(){ + echo "" +} + +set_mail_delivery(){ + echo "" +} + +create_swap_pool(){ + echo "" +} + +set_default_volblocksize(){ + echo "" +} + +installation_task(){ + set_locales + set_pve_repo + update_system + install_tools + enable_sdn + set_arc_cache + set_swappiness + set_auto_snapshot + pve_conf_backup + suppress_no_subscription_warning + harden_ssh + set_mail_delivery + create_swap_pool + set_default_volblocksize + + echo "Updating initramfs - This will take some time..." + update-initramfs -u -k all > /dev/null 2>&1 } @@ -216,6 +376,9 @@ arc_suggestion # Set swapping behaviour vm_swappiness +# Ask for additional locales +ask_locales + # Configure count per interval of zfs-auto-snapshot auto_snapshot @@ -223,4 +386,6 @@ auto_snapshot select_pve_repos # subscription related actions -select_subscription \ No newline at end of file +select_subscription + +echo "Proxmox postinstallation finished!"