Unify install logic and cleanup LXC for all OS types

Refactored build.func to use a unified install.func for all supported OS types, simplifying OS detection and package installation. Added cleanup_lxc step to all install scripts. Removed support for runit, pacman, and nix-env from install.func, and improved SSH server installation logic for containers. Updated template pattern matching for additional OS types.
This commit is contained in:
CanbiZ 2025-12-04 15:00:10 +01:00
parent 8aecfce5a3
commit 19a2d83c5a
11 changed files with 97 additions and 81 deletions

View File

@ -15,3 +15,4 @@ update_os
motd_ssh
customize
cleanup_lxc

View File

@ -15,3 +15,4 @@ update_os
motd_ssh
customize
cleanup_lxc

View File

@ -15,3 +15,4 @@ update_os
motd_ssh
customize
cleanup_lxc

View File

@ -15,3 +15,4 @@ update_os
motd_ssh
customize
cleanup_lxc

View File

@ -15,3 +15,4 @@ update_os
motd_ssh
customize
cleanup_lxc

View File

@ -15,3 +15,4 @@ update_os
motd_ssh
customize
cleanup_lxc

View File

@ -15,3 +15,4 @@ update_os
motd_ssh
customize
cleanup_lxc

View File

@ -15,3 +15,4 @@ update_os
motd_ssh
customize
cleanup_lxc

View File

@ -2419,11 +2419,9 @@ build_container() {
TEMP_DIR=$(mktemp -d)
pushd "$TEMP_DIR" >/dev/null
if [ "$var_os" == "alpine" ]; then
export FUNCTIONS_FILE_PATH="$(curl -fsSL https://git.community-scripts.org/community-scripts/ProxmoxVED/raw/branch/main/misc/alpine-install.func)"
else
export FUNCTIONS_FILE_PATH="$(curl -fsSL https://git.community-scripts.org/community-scripts/ProxmoxVED/raw/branch/main/misc/install.func)"
fi
# Unified install.func automatically detects OS type (debian, alpine, fedora, etc.)
export FUNCTIONS_FILE_PATH="$(curl -fsSL https://git.community-scripts.org/community-scripts/ProxmoxVED/raw/branch/main/misc/install.func)"
# Core exports for install.func
export DIAGNOSTICS="$DIAGNOSTICS"
@ -2826,39 +2824,78 @@ EOF
# install_gpu_userland "NVIDIA"
# fi
# Continue with standard container setup
if [ "$var_os" == "alpine" ]; then
sleep 3
# Continue with standard container setup - install core dependencies based on OS
sleep 3
case "$var_os" in
alpine)
pct exec "$CTID" -- /bin/sh -c 'cat <<EOF >/etc/apk/repositories
http://dl-cdn.alpinelinux.org/alpine/latest-stable/main
http://dl-cdn.alpinelinux.org/alpine/latest-stable/community
EOF'
pct exec "$CTID" -- ash -c "apk add bash newt curl openssh nano mc ncurses jq >/dev/null"
else
sleep 3
pct exec "$CTID" -- bash -c "sed -i '/$LANG/ s/^# //' /etc/locale.gen"
pct exec "$CTID" -- bash -c "locale_line=\$(grep -v '^#' /etc/locale.gen | grep -E '^[a-zA-Z]' | awk '{print \$1}' | head -n 1) && \
echo LANG=\$locale_line >/etc/default/locale && \
locale-gen >/dev/null && \
export LANG=\$locale_line"
;;
debian | ubuntu | devuan)
# Locale setup for Debian-based
pct exec "$CTID" -- bash -c "sed -i '/$LANG/ s/^# //' /etc/locale.gen 2>/dev/null || true"
pct exec "$CTID" -- bash -c "locale_line=\$(grep -v '^#' /etc/locale.gen 2>/dev/null | grep -E '^[a-zA-Z]' | awk '{print \$1}' | head -n 1) && \
[[ -n \"\$locale_line\" ]] && echo LANG=\$locale_line >/etc/default/locale && \
locale-gen >/dev/null 2>&1 && \
export LANG=\$locale_line || true"
# Timezone setup
if [[ -z "${tz:-}" ]]; then
tz=$(timedatectl show --property=Timezone --value 2>/dev/null || echo "Etc/UTC")
fi
if pct exec "$CTID" -- test -e "/usr/share/zoneinfo/$tz"; then
# Set timezone using symlink (Debian 13+ compatible)
# Create /etc/timezone for backwards compatibility with older scripts
pct exec "$CTID" -- bash -c "tz='$tz'; ln -sf \"/usr/share/zoneinfo/\$tz\" /etc/localtime && echo \"\$tz\" >/etc/timezone || true"
else
msg_warn "Skipping timezone setup zone '$tz' not found in container"
fi
# Core dependencies
pct exec "$CTID" -- bash -c "apt-get update >/dev/null && apt-get install -y sudo curl mc gnupg2 jq >/dev/null" || {
msg_error "apt-get base packages installation failed"
exit 1
}
fi
;;
fedora | rockylinux | almalinux | centos)
# RHEL-based: Fedora, Rocky, AlmaLinux, CentOS
pct exec "$CTID" -- bash -c "dnf install -y curl sudo mc jq procps-ng >/dev/null 2>&1 || yum install -y curl sudo mc jq procps-ng >/dev/null 2>&1" || {
msg_error "dnf/yum base packages installation failed"
exit 1
}
;;
opensuse)
# openSUSE
pct exec "$CTID" -- bash -c "zypper --non-interactive install curl sudo mc jq >/dev/null" || {
msg_error "zypper base packages installation failed"
exit 1
}
;;
gentoo)
# Gentoo - emerge is slow, only install essentials
pct exec "$CTID" -- bash -c "emerge --quiet app-misc/jq net-misc/curl app-misc/mc >/dev/null 2>&1" || {
msg_warn "Gentoo base packages installation incomplete - may need manual setup"
}
;;
openeuler)
# openEuler (RHEL-compatible)
pct exec "$CTID" -- bash -c "dnf install -y curl sudo mc jq >/dev/null" || {
msg_error "dnf base packages installation failed"
exit 1
}
;;
*)
msg_warn "Unknown OS '$var_os' - skipping core dependency installation"
;;
esac
msg_ok "Customized LXC Container"
@ -3394,11 +3431,15 @@ create_lxc_container() {
# ------------------------------------------------------------------------------
# Template discovery & validation
# Supported OS types (pveam available): alpine, almalinux, centos, debian,
# devuan, fedora, gentoo, openeuler, opensuse, rockylinux, ubuntu
# ------------------------------------------------------------------------------
TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}"
case "$PCT_OSTYPE" in
debian | ubuntu) TEMPLATE_PATTERN="-standard_" ;;
alpine | fedora | rocky | centos) TEMPLATE_PATTERN="-default_" ;;
debian | ubuntu | devuan) TEMPLATE_PATTERN="-standard_" ;;
alpine | fedora | rocky | rockylinux | centos | almalinux | openeuler) TEMPLATE_PATTERN="-default_" ;;
gentoo) TEMPLATE_PATTERN="-current_" ;;
opensuse) TEMPLATE_PATTERN="-default_" ;;
*) TEMPLATE_PATTERN="" ;;
esac

View File

@ -795,7 +795,6 @@ is_verbose_mode() {
# ------------------------------------------------------------------------------
cleanup_lxc() {
msg_info "Cleaning up"
# OS-specific package manager cleanup
if is_alpine; then
$STD apk cache clean 2>/dev/null || true

View File

@ -24,7 +24,7 @@
# Features:
# - Automatic OS detection
# - Unified package manager abstraction
# - Init system abstraction (systemd/OpenRC/runit/sysvinit)
# - Init system abstraction (systemd/OpenRC/sysvinit)
# - Network connectivity verification
# - MOTD and SSH configuration
# - Container customization
@ -39,8 +39,8 @@
OS_TYPE="" # debian, ubuntu, devuan, alpine, fedora, rocky, alma, centos, opensuse, gentoo, openeuler
OS_FAMILY="" # debian, alpine, rhel, suse, gentoo
OS_VERSION="" # Version number
PKG_MANAGER="" # apt, apk, dnf, yum, pacman, zypper, emerge, nix-env
INIT_SYSTEM="" # systemd, openrc, runit, sysvinit
PKG_MANAGER="" # apt, apk, dnf, yum, zypper, emerge
INIT_SYSTEM="" # systemd, openrc, sysvinit
# ------------------------------------------------------------------------------
# detect_os()
@ -143,8 +143,6 @@ detect_os() {
INIT_SYSTEM="systemd"
elif command -v rc-service &>/dev/null || [[ -d /etc/init.d && -f /sbin/openrc ]]; then
INIT_SYSTEM="openrc"
elif command -v sv &>/dev/null && [[ -d /etc/sv ]]; then
INIT_SYSTEM="runit"
elif [[ -f /etc/inittab ]]; then
INIT_SYSTEM="sysvinit"
else
@ -167,8 +165,6 @@ _bootstrap() {
dnf install -y curl &>/dev/null
elif command -v yum &>/dev/null; then
yum install -y curl &>/dev/null
elif command -v pacman &>/dev/null; then
pacman -Sy --noconfirm curl &>/dev/null
elif command -v zypper &>/dev/null; then
zypper install -y curl &>/dev/null
elif command -v emerge &>/dev/null; then
@ -210,18 +206,12 @@ pkg_update() {
yum)
$STD yum makecache
;;
pacman)
$STD pacman -Sy
;;
zypper)
$STD zypper refresh
;;
emerge)
$STD emerge --sync
;;
nix-env)
$STD nix-channel --update
;;
*)
msg_error "Unknown package manager: $PKG_MANAGER"
return 1
@ -248,18 +238,12 @@ pkg_upgrade() {
yum)
$STD yum -y update
;;
pacman)
$STD pacman -Syu --noconfirm
;;
zypper)
$STD zypper -n update
;;
emerge)
$STD emerge --quiet --update --deep @world
;;
nix-env)
$STD nix-env -u
;;
*)
msg_error "Unknown package manager: $PKG_MANAGER"
return 1
@ -291,20 +275,12 @@ pkg_install() {
yum)
$STD yum install -y "${packages[@]}"
;;
pacman)
$STD pacman -S --noconfirm "${packages[@]}"
;;
zypper)
$STD zypper install -y "${packages[@]}"
;;
emerge)
$STD emerge --quiet "${packages[@]}"
;;
nix-env)
for pkg in "${packages[@]}"; do
$STD nix-env -iA "nixos.$pkg"
done
;;
*)
msg_error "Unknown package manager: $PKG_MANAGER"
return 1
@ -334,20 +310,12 @@ pkg_remove() {
yum)
$STD yum remove -y "${packages[@]}"
;;
pacman)
$STD pacman -Rs --noconfirm "${packages[@]}"
;;
zypper)
$STD zypper remove -y "${packages[@]}"
;;
emerge)
$STD emerge --quiet --unmerge "${packages[@]}"
;;
nix-env)
for pkg in "${packages[@]}"; do
$STD nix-env -e "$pkg"
done
;;
*)
msg_error "Unknown package manager: $PKG_MANAGER"
return 1
@ -376,18 +344,12 @@ pkg_clean() {
yum)
$STD yum clean all
;;
pacman)
$STD pacman -Scc --noconfirm
;;
zypper)
$STD zypper clean
;;
emerge)
$STD emerge --quiet --depclean
;;
nix-env)
$STD nix-collect-garbage -d
;;
*)
return 0
;;
@ -414,9 +376,6 @@ svc_enable() {
openrc)
$STD rc-update add "$service" default
;;
runit)
[[ -d "/etc/sv/$service" ]] && ln -sf "/etc/sv/$service" "/var/service/"
;;
sysvinit)
if command -v update-rc.d &>/dev/null; then
$STD update-rc.d "$service" defaults
@ -447,9 +406,6 @@ svc_disable() {
openrc)
$STD rc-update del "$service" default 2>/dev/null || true
;;
runit)
rm -f "/var/service/$service"
;;
sysvinit)
if command -v update-rc.d &>/dev/null; then
$STD update-rc.d "$service" remove
@ -479,9 +435,6 @@ svc_start() {
openrc)
$STD rc-service "$service" start
;;
runit)
$STD sv start "$service"
;;
sysvinit)
$STD /etc/init.d/"$service" start
;;
@ -507,9 +460,6 @@ svc_stop() {
openrc)
$STD rc-service "$service" stop
;;
runit)
$STD sv stop "$service"
;;
sysvinit)
$STD /etc/init.d/"$service" stop
;;
@ -535,9 +485,6 @@ svc_restart() {
openrc)
$STD rc-service "$service" restart
;;
runit)
$STD sv restart "$service"
;;
sysvinit)
$STD /etc/init.d/"$service" restart
;;
@ -563,9 +510,6 @@ svc_status() {
openrc)
rc-service "$service" status &>/dev/null
;;
runit)
sv status "$service" | grep -q "^run:"
;;
sysvinit)
/etc/init.d/"$service" status &>/dev/null
;;
@ -846,6 +790,30 @@ EOF
# Configure SSH root access if requested
if [[ "${SSH_ROOT:-}" == "yes" ]]; then
# Ensure SSH server is installed
if [[ ! -f /etc/ssh/sshd_config ]]; then
msg_info "Installing SSH server"
case "$PKG_MANAGER" in
apt)
pkg_install openssh-server
;;
apk)
pkg_install openssh
rc-update add sshd default 2>/dev/null || true
;;
dnf | yum)
pkg_install openssh-server
;;
zypper)
pkg_install openssh
;;
emerge)
pkg_install net-misc/openssh
;;
esac
msg_ok "Installed SSH server"
fi
local sshd_config="/etc/ssh/sshd_config"
if [[ -f "$sshd_config" ]]; then
sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" "$sshd_config"