From 0510afa0090e53df44d4539715e2815ce4a009ab Mon Sep 17 00:00:00 2001 From: Francesco Picone Date: Tue, 13 Oct 2026 12:19:52 +0200 Subject: [PATCH] Utente opzionale e proposta di estensione LVM nel setup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - La creazione dell'utente non è più obbligatoria: lasciando vuoto il nome si usa l'utente corrente (SUDO_USER o root) - Clona lvm-volume-enlarger in /opt e, se la root è un LV ext* con spazio libero nel VG, propone l'esecuzione di enlarge.sh - Corregge il codename Ubuntu non espanso in docker.sources Co-Authored-By: Claude Opus 5.5 --- README.md | 9 ++-- scripts/setup.sh | 118 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 104 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index a3ef656..0458079 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ Questo repository contiene uno script per configurare rapidamente un server Ubuntu: - Aggiorna il sistema e installa pacchetti base - Configura repository e installa Docker (CE, CLI, containerd, buildx, compose plugin) -- Chiede il nome utente, lo crea e lo aggiunge al gruppo `docker` +- Chiede un nome utente da creare (opzionale: lasciando vuoto usa l'utente corrente) e lo aggiunge al gruppo `docker` +- Scarica [lvm-volume-enlarger](https://repository.pyconetwork.it/francesco/lvm-volume-enlarger.git) e, se il volume LVM di root può essere allargato, propone di eseguire lo script - Esegue una post-installazione tramite `make` - Clona il repo Portainer e ne esegue il deploy via Docker @@ -25,14 +26,16 @@ In aggiunta, applica misure di hardening (senza SSH hardening): sudo bash scripts/setup.sh ``` -3. Inserisci il nome utente quando richiesto. Lo script: +3. Inserisci il nome utente quando richiesto, oppure premi Invio per usare l'utente corrente (quello che ha lanciato `sudo`, o `root`). Lo script: - creerà l'utente (se non esiste) e imposterà la password - - installerà Docker e aggiungerà l'utente al gruppo `docker` + - clonerà `lvm-volume-enlarger` in `/opt/lvm-volume-enlarger`; se la root è un volume LVM ext4 e il volume group ha spazio libero, chiederà se eseguire `enlarge.sh` + - installerà Docker e aggiungerà l'utente al gruppo `docker` (non necessario se l'utente è `root`) - eseguirà `make postinstall` per utilità post-installazione - clonerà `https://git.pyconetwork.it/francesco/portainer.git` in `/opt/portainer` e tenterà il deploy con `docker compose up -d` ## Note - Dopo l'aggiunta al gruppo `docker`, è necessario fare logout/login per applicare i permessi. +- Se rifiuti l'estensione del disco, puoi eseguirla in seguito con `sudo bash /opt/lvm-volume-enlarger/enlarge.sh`. Lo script usa solo lo spazio già libero nel volume group: se hai ingrandito il disco virtuale, prima vanno estesi la partizione e il PV (`growpart` + `pvresize`). - Se il repository Portainer non contiene un `docker-compose.yml/compose.yml`, lo script proverà `make deploy` se presente; altrimenti mostrerà un avviso. - Il target `postinstall` installa: `vim`, `tmux`, `net-tools`, `wget`, `jq`, `tree`, `ufw`, `fail2ban`, `software-properties-common`, `htop`, `lsof`, e abilita UFW + Fail2ban. - Il target `postinstall` installa: `vim`, `tmux`, `net-tools`, `wget`, `jq`, `tree`, `fail2ban`, `software-properties-common`, `htop`, `lsof`. diff --git a/scripts/setup.sh b/scripts/setup.sh index ce45f6e..53d0476 100644 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -2,10 +2,13 @@ set -euo pipefail # Questo script prepara un Ubuntu Server con utilità di base, -# installa Docker, chiede un utente da creare e lo aggiunge al gruppo docker, +# installa Docker, usa l'utente corrente o ne crea uno nuovo e lo aggiunge al gruppo docker, +# propone l'estensione del volume LVM di root se c'è spazio libero, # esegue i post-install tramite Make, e deploya Portainer dal repository indicato. TIMEZONE_DEFAULT="Europe/Rome" +LVM_ENLARGER_REPO="https://repository.pyconetwork.it/francesco/lvm-volume-enlarger.git" +LVM_ENLARGER_DIR="/opt/lvm-volume-enlarger" require_root() { if [ "$(id -u)" -ne 0 ]; then @@ -15,26 +18,25 @@ require_root() { } prompt_user() { - read -rp "Inserisci il nome utente da creare: " NEW_USER - if [ -z "${NEW_USER}" ]; then - echo "[ERRORE] Il nome utente non può essere vuoto." >&2 - exit 1 - fi + # Con sudo, l'utente corrente è chi ha lanciato sudo; altrimenti è root + local CURRENT_USER="${SUDO_USER:-$(id -un)}" + read -rp "Nome utente da creare (lascia vuoto per usare l'utente corrente '${CURRENT_USER}'): " TARGET_USER + TARGET_USER=${TARGET_USER:-$CURRENT_USER} read -rp "Inserisci il timezone (es. Europe/Rome) [${TIMEZONE_DEFAULT}]: " TIMEZONE TIMEZONE=${TIMEZONE:-$TIMEZONE_DEFAULT} } create_user_if_needed() { - if id -u "${NEW_USER}" >/dev/null 2>&1; then - echo "[INFO] L'utente '${NEW_USER}' esiste già, procedo." + if id -u "${TARGET_USER}" >/dev/null 2>&1; then + echo "[INFO] L'utente '${TARGET_USER}' esiste già, procedo." else - echo "[INFO] Creo l'utente '${NEW_USER}'." - useradd -m -s /bin/bash "${NEW_USER}" - echo "[INFO] Imposto la password per '${NEW_USER}'." - read -srp "Inserisci la password per ${NEW_USER}: " USER_PASS + echo "[INFO] Creo l'utente '${TARGET_USER}'." + useradd -m -s /bin/bash "${TARGET_USER}" + echo "[INFO] Imposto la password per '${TARGET_USER}'." + read -srp "Inserisci la password per ${TARGET_USER}: " USER_PASS echo - echo "${NEW_USER}:${USER_PASS}" | chpasswd + echo "${TARGET_USER}:${USER_PASS}" | chpasswd fi } @@ -55,6 +57,73 @@ install_base_packages() { apt autoremove -y --purge } +# Restituisce 0 se la root è un LV ext2/3/4 e il suo VG ha spazio libero, +# cioè se enlarge.sh (lvextend +100%FREE + resize2fs) può allargarla. +root_lvm_can_grow() { + local ROOT_SRC ROOT_FSTYPE VG_NAME VG_FREE + ROOT_SRC="$(findmnt -n -o SOURCE /)" + ROOT_FSTYPE="$(findmnt -n -o FSTYPE /)" + + if [[ "${ROOT_SRC}" != /dev/mapper/* ]]; then + echo "[INFO] Il filesystem root non è su LVM (${ROOT_SRC}), estensione non necessaria." + return 1 + fi + if ! command -v lvs >/dev/null 2>&1; then + echo "[ATTENZIONE] Strumenti LVM non disponibili, salto la verifica dello spazio libero." + return 1 + fi + case "${ROOT_FSTYPE}" in + ext2|ext3|ext4) ;; + *) + echo "[INFO] Il filesystem root è '${ROOT_FSTYPE}', non supportato da resize2fs: salto l'estensione." + return 1 + ;; + esac + + VG_NAME="$(lvs --noheadings -o vg_name "${ROOT_SRC}" 2>/dev/null | tr -d ' ')" + if [ -z "${VG_NAME}" ]; then + echo "[ATTENZIONE] Impossibile determinare il volume group di ${ROOT_SRC}." + return 1 + fi + VG_FREE="$(vgs --noheadings --units b --nosuffix -o vg_free "${VG_NAME}" 2>/dev/null | tr -d ' ')" + if [ -z "${VG_FREE}" ] || [ "${VG_FREE}" -eq 0 ]; then + echo "[INFO] Nessuno spazio libero nel volume group '${VG_NAME}', il disco è già alla dimensione massima." + return 1 + fi + + echo "[INFO] Il volume group '${VG_NAME}' ha $(numfmt --to=iec "${VG_FREE}") liberi: la root (${ROOT_SRC}) può essere allargata." + return 0 +} + +clone_and_offer_lvm_enlarge() { + echo "[INFO] Scarico lo script di estensione LVM..." + if [ ! -d "${LVM_ENLARGER_DIR}" ]; then + git clone "${LVM_ENLARGER_REPO}" "${LVM_ENLARGER_DIR}" || { + echo "[ATTENZIONE] Impossibile clonare ${LVM_ENLARGER_REPO}, salto l'estensione LVM." + return 0 + } + else + ( + cd "${LVM_ENLARGER_DIR}" && git pull --rebase + ) || echo "[ATTENZIONE] Impossibile aggiornare ${LVM_ENLARGER_DIR}, uso la versione presente." + fi + + if ! root_lvm_can_grow; then + return 0 + fi + + local ANSWER + read -rp "Vuoi eseguire ora ${LVM_ENLARGER_DIR}/enlarge.sh per allargare il disco? [s/N]: " ANSWER + case "${ANSWER}" in + [sSyY]*) + bash "${LVM_ENLARGER_DIR}/enlarge.sh" || echo "[ATTENZIONE] L'estensione LVM non è andata a buon fine." + ;; + *) + echo "[INFO] Estensione saltata. Puoi eseguirla in seguito con: sudo bash ${LVM_ENLARGER_DIR}/enlarge.sh" + ;; + esac +} + install_docker() { echo "[INFO] Preparo repository Docker e installo pacchetti Docker..." apt update @@ -63,7 +132,7 @@ install_docker() { curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc chmod a+r /etc/apt/keyrings/docker.asc - tee /etc/apt/sources.list.d/docker.sources <<'EOF' + tee /etc/apt/sources.list.d/docker.sources <