Utente opzionale e proposta di estensione LVM nel setup

- 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 <noreply@anthropic.com>
This commit is contained in:
2026-10-13 12:19:52 +02:00
parent 87d6ffe8a0
commit 0510afa009
2 changed files with 104 additions and 23 deletions

View File

@@ -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`.

View File

@@ -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 <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
@@ -77,10 +146,14 @@ EOF
}
add_user_to_docker_group() {
echo "[INFO] Aggiungo l'utente '${NEW_USER}' al gruppo docker..."
if [ "${TARGET_USER}" = "root" ]; then
echo "[INFO] L'utente è root, non serve aggiungerlo al gruppo docker."
return 0
fi
echo "[INFO] Aggiungo l'utente '${TARGET_USER}' al gruppo docker..."
groupadd -f docker
usermod -aG docker "${NEW_USER}"
echo "[INFO] Ricorda: per applicare il gruppo docker, '${NEW_USER}' deve fare logout/login."
usermod -aG docker "${TARGET_USER}"
echo "[INFO] Ricorda: per applicare il gruppo docker, '${TARGET_USER}' deve fare logout/login."
}
run_make_postinstall() {
@@ -117,7 +190,7 @@ clone_and_deploy_portainer() {
fi
)
chown -R "${NEW_USER}":"${NEW_USER}" "${PORTAINER_DIR}"
chown -R "${TARGET_USER}":"$(id -gn "${TARGET_USER}")" "${PORTAINER_DIR}"
}
main() {
@@ -126,15 +199,20 @@ main() {
create_user_if_needed
configure_timezone
install_base_packages
clone_and_offer_lvm_enlarge
install_docker
add_user_to_docker_group
run_make_postinstall
clone_and_deploy_portainer
echo "[FATTO] Setup completato. Docker è installato e Portainer è stato deployato (se possibile)."
echo "- Utente: ${NEW_USER} (aggiunto al gruppo docker)"
echo "- Timezone: ${TIMEZONE}"
if [ "${TARGET_USER}" = "root" ]; then
echo "- Utente: root"
else
echo "- Utente: ${TARGET_USER} (aggiunto al gruppo docker)"
echo "- Ricorda di eseguire logout/login per applicare i gruppi."
fi
echo "- Timezone: ${TIMEZONE}"
}
main "$@"