public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Michael Köppl" <m.koeppl@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH test-tools 6/8] instance: add a builtin hook to set up a Ceph cluster
Date: Tue,  1 Dec 2026 15:58:15 +0100	[thread overview]
Message-ID: <20261201145817.894414-7-m.koeppl@proxmox.com> (raw)
In-Reply-To: <20261201145817.894414-1-m.koeppl@proxmox.com>

The setup-ceph builtin installs Ceph on all nodes in parallel,
configures the Ceph networks on a spare NIC, and creates a monitor,
a manager and OSDs on every node, optionally with a pool and an RBD
storage. It looks up all OSD disks and addresses before changing
anything, so a missing disk or a bad network fails early.

To fit test nodes, the OSD memory target is lowered to 1 GiB, and the
pool is created with 32 placement groups instead of 128, which would
leave the cluster in HEALTH_WARN.

Signed-off-by: Michael Köppl <m.koeppl@proxmox.com>
---
 README.md             |  27 +++++
 scripts/setup-ceph.sh | 234 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 261 insertions(+)
 create mode 100755 scripts/setup-ceph.sh

diff --git a/README.md b/README.md
index fe727133..47907f5e 100644
--- a/README.md
+++ b/README.md
@@ -196,6 +196,7 @@ naming a script, followed by its arguments, which are passed through the shell:
 | `install-ceph` | `<version> <repository>` | Install the Ceph packages, without setting up a Ceph cluster |
 | `setup-zfs-storage` | `<disk-serial> <name>` | Create a ZFS pool on the additional disk with the given serial and add it as a storage of the same name |
 | `setup-frr` | | Install and enable FRR |
+| `setup-ceph` | `[<options>]` | Set up a hyper-converged Ceph cluster. Only works as a cluster hook, see [Cluster hooks](#cluster-hooks) |
 
 Setup hooks for assets are also provided with the asset configuration via a JSON configuration file,
 located at `$CONFIG_PATH`. This allows to use the same hook script for multiple assets.
@@ -245,6 +246,32 @@ address. The first node is the one the cluster was created on.
 }
 ```
 
+The builtin `setup-ceph` sets up a hyper-converged Ceph cluster, with a monitor, a manager and OSDs
+on every node:
+
+```ron
+post_cluster: [
+    Builtin("setup-ceph --pool rbd"),
+],
+```
+
+The nodes need an unconfigured second NIC for the Ceph networks (an entry in `additional_nics`
+without `cidr`) and disks for the OSDs (`additional_disks` with the serials `ceph0`, `ceph1`, ...).
+With the default pool size of 3, at least three nodes need OSDs for the cluster to become healthy.
+The options of `setup-ceph` and their defaults are:
+  - `--version`: Ceph release to install, for example `tentacle` (the default release of `pveceph`)
+  - `--repository`: repository to install Ceph from, as accepted by `pveceph install`
+    (`no-subscription`)
+  - `--nic`: the NIC for the Ceph networks (`ens19`)
+  - `--public-network`: Ceph public network, in which the n-th node gets the n-th host address
+    (`10.250.240.0/24`)
+  - `--cluster-network`: Ceph cluster network for replication, addressed like the public network,
+    or `''` to replicate over the public network (`10.250.241.0/24`)
+  - `--osd-serial-prefix`: OSDs are created on the disks whose serial starts with this prefix
+    (`ceph`)
+  - `--pool`: name of a replicated pool to create together with an RBD storage of the same name
+    (none)
+
 Clusters formed manually (`setup --no-cluster` followed by `create-cluster`) do not run
 `post_cluster` hooks. Once such a cluster is formed, `run-script --cluster` runs a script once for
 it, with the same `$CONFIG_PATH`. As with other `run-script` calls, the script path is relative to
diff --git a/scripts/setup-ceph.sh b/scripts/setup-ceph.sh
new file mode 100755
index 00000000..df67a6d3
--- /dev/null
+++ b/scripts/setup-ceph.sh
@@ -0,0 +1,234 @@
+#!/bin/bash
+# Set up a hyper-converged Ceph cluster on all nodes of a PVE cluster, with a monitor, a manager
+# and OSDs on every node. It needs all nodes at once, so it is meant as a post_cluster hook.
+#
+# Usage: setup-ceph.sh [--version <release>] [--repository <repository>] [--nic <nic>]
+#            [--public-network <cidr>] [--cluster-network <cidr>|''] [--osd-serial-prefix <prefix>]
+#            [--pool <name>]
+#
+# The n-th node gets the n-th host address of each network on <nic>, which must be left
+# unconfigured (`iface <nic> inet manual`). OSDs are created on all disks whose serial starts with
+# the prefix. An empty cluster network replicates over the public network.
+
+set -euo pipefail
+
+VERSION=""
+REPOSITORY=no-subscription
+NIC=ens19
+PUBLIC_NETWORK=10.250.240.0/24
+CLUSTER_NETWORK=10.250.241.0/24
+OSD_SERIAL_PREFIX=ceph
+POOL=""
+
+# Lower the OSD memory target from the default of 4 GiB, which would leave the nodes without memory
+# for anything else.
+OSD_MEMORY_TARGET=1073741824
+
+die() {
+    echo "$*" >&2
+    exit 1
+}
+
+log() {
+    echo "Ceph: $*"
+}
+
+while (($#)); do
+    case $1 in
+        --version) VERSION=${2?"missing value for $1"} ;;
+        --repository) REPOSITORY=${2?"missing value for $1"} ;;
+        --nic) NIC=${2?"missing value for $1"} ;;
+        --public-network) PUBLIC_NETWORK=${2?"missing value for $1"} ;;
+        --cluster-network) CLUSTER_NETWORK=${2?"missing value for $1"} ;;
+        --osd-serial-prefix) OSD_SERIAL_PREFIX=${2?"missing value for $1"} ;;
+        --pool) POOL=${2?"missing value for $1"} ;;
+        *) die "unknown option '$1'" ;;
+    esac
+    shift 2
+done
+
+# Every serial starts with an empty prefix, so the root disk would become an OSD.
+[[ -n $OSD_SERIAL_PREFIX ]] || die "--osd-serial-prefix must not be empty"
+
+SSH_KEY=$(jq -r '.ssh_private_keyfile' "$CONFIG_PATH")
+SSH_USER=$(jq -r '.ssh_user' "$CONFIG_PATH")
+mapfile -t NAMES < <(jq -r '.nodes[].name' "$CONFIG_PATH")
+mapfile -t HOSTS < <(jq -r '.nodes[].host' "$CONFIG_PATH")
+# A failing jq in the process substitution would not stop the script, so check the result.
+((${#HOSTS[@]} > 0)) || die "no cluster nodes in $CONFIG_PATH"
+
+on_node() {
+    local host=$1
+    shift
+    ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$SSH_USER@$host" "$@"
+}
+
+# Prints the n-th host address of the network <cidr>, with the prefix length of the network.
+nth_host() {
+    local cidr=$1 n=$2 octet prefix base mask address
+    [[ $cidr =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/([0-9]+)$ ]] ||
+        die "'$cidr' is not an IPv4 network in CIDR notation"
+    base=0
+    for octet in "${BASH_REMATCH[@]:1:4}"; do
+        ((octet <= 255)) || die "'$cidr' is not an IPv4 network"
+        base=$(((base << 8) | octet))
+    done
+    prefix=${BASH_REMATCH[5]}
+    ((prefix >= 1 && prefix <= 30)) ||
+        die "network '$cidr' must have a prefix length between 1 and 30"
+    mask=$(((1 << (32 - prefix)) - 1))
+    (((base & mask) == 0)) || die "'$cidr' is not a network address"
+    ((n > 0 && n < mask)) || die "network '$cidr' has no host address number $n"
+    address=$((base + n))
+    printf '%d.%d.%d.%d/%d\n' $((address >> 24 & 255)) $((address >> 16 & 255)) \
+        $((address >> 8 & 255)) $((address & 255)) "$prefix"
+}
+
+# Polls `ceph status` on the first node until the jq filter holds. Arguments after the filter are
+# passed to jq.
+wait_for_status() {
+    local what=$1 timeout=$2 filter=$3 deadline=$((SECONDS + $2)) status=""
+    shift 3
+    until status=$(on_node "${HOSTS[0]}" ceph status --format json 2>/dev/null) &&
+        jq -e "$@" "$filter" <<<"$status" >/dev/null; do
+        if ((SECONDS >= deadline)); then
+            die "timed out after ${timeout}s waiting for $what, last health:" \
+                "$(jq -r '.health.status // "unknown"' <<<"$status" 2>/dev/null || echo unknown)"
+        fi
+        sleep 5
+    done
+}
+
+# At least $osds OSDs, all up and in, and all placement groups clean.
+# shellcheck disable=SC2016 # $o and $osds are jq variables
+READY='.osdmap as $o
+    | $o.num_osds >= $osds and $o.num_up_osds == $o.num_osds and $o.num_in_osds == $o.num_osds
+    and ([.pgmap.pgs_by_state[]?
+            | select(.state_name | IN("active+clean", "active+clean+scrubbing",
+                "active+clean+scrubbing+deep"))
+            | .count] | add // 0) == .pgmap.num_pgs'
+
+# Look up all disks and addresses first, so that a missing one fails the setup before anything on
+# the nodes is changed.
+OSD_NODES=()
+OSD_DEVICES=()
+for i in "${!HOSTS[@]}"; do
+    disks=$(on_node "${HOSTS[i]}" lsblk --nodeps --noheadings --raw --output SERIAL,PATH)
+    while read -r dev; do
+        OSD_NODES+=("$i")
+        OSD_DEVICES+=("$dev")
+    done < <(awk -v prefix="$OSD_SERIAL_PREFIX" \
+        'index($1, prefix) == 1 && $2 ~ "^/dev/" { print $2 }' <<<"$disks")
+done
+((${#OSD_DEVICES[@]} > 0)) ||
+    die "no disks with a serial starting with '$OSD_SERIAL_PREFIX' to create OSDs on"
+
+PUBLIC_ADDRESSES=()
+CLUSTER_ADDRESSES=()
+for i in "${!HOSTS[@]}"; do
+    PUBLIC_ADDRESSES[i]=$(nth_host "$PUBLIC_NETWORK" $((i + 1)))
+    [[ -z $CLUSTER_NETWORK ]] || CLUSTER_ADDRESSES[i]=$(nth_host "$CLUSTER_NETWORK" $((i + 1)))
+done
+
+install_ceph() {
+    on_node "${HOSTS[$1]}" \
+        "yes | pveceph install --repository $REPOSITORY${VERSION:+ --version $VERSION}" 2>&1 |
+        sed -u "s/^/[${NAMES[$1]}] /"
+}
+
+# Every node downloads the packages on its own, so install them on all nodes at once.
+log "installing on all nodes"
+pids=()
+for i in "${!HOSTS[@]}"; do
+    install_ceph "$i" &
+    pids+=($!)
+done
+failed=0
+for pid in "${pids[@]}"; do
+    wait "$pid" || failed=1
+done
+((failed == 0)) || die "installing Ceph failed"
+
+for i in "${!HOSTS[@]}"; do
+    log "configuring $NIC on node ${NAMES[i]}"
+    on_node "${HOSTS[i]}" bash -s -- "$NIC" "${PUBLIC_ADDRESSES[i]}" \
+        ${CLUSTER_ADDRESSES[i]:+"${CLUSTER_ADDRESSES[i]}"} <<'EOF'
+set -e
+nic=$1
+shift
+if ! grep -qx "iface $nic inet manual" /etc/network/interfaces; then
+    echo "$nic is not an unconfigured interface in /etc/network/interfaces" >&2
+    exit 1
+fi
+stanza="auto $nic\niface $nic inet static"
+for cidr; do
+    stanza+="\n\taddress $cidr"
+done
+sed -i -e "/^auto $nic\$/d" -e "s|^iface $nic inet manual\$|$stanza|" /etc/network/interfaces
+ifreload -a
+EOF
+done
+
+for i in "${!HOSTS[@]}"; do
+    others=()
+    for j in "${!HOSTS[@]}"; do
+        ((i == j)) && continue
+        others+=("${PUBLIC_ADDRESSES[j]%/*}")
+        [[ -z ${CLUSTER_ADDRESSES[j]:-} ]] || others+=("${CLUSTER_ADDRESSES[j]%/*}")
+    done
+    on_node "${HOSTS[i]}" bash -s -- "${others[@]}" <<'EOF'
+for address; do
+    for attempt in $(seq 30); do
+        ping -c 1 -W 1 "$address" >/dev/null && break
+        if [ "$attempt" -eq 30 ]; then
+            echo "cannot reach $address from $(hostname)" >&2
+            exit 1
+        fi
+        sleep 1
+    done
+done
+EOF
+done
+
+first=${HOSTS[0]}
+
+log "initializing on node ${NAMES[0]}"
+on_node "$first" pveceph init --network "$PUBLIC_NETWORK" \
+    ${CLUSTER_NETWORK:+--cluster-network "$CLUSTER_NETWORK"}
+
+for i in "${!HOSTS[@]}"; do
+    log "creating monitor on node ${NAMES[i]}"
+    on_node "${HOSTS[i]}" pveceph mon create
+    # shellcheck disable=SC2016 # $node is a jq variable
+    wait_for_status "monitor '${NAMES[i]}' to join the quorum" 120 \
+        'any(.quorum_names[]?; . == $node)' --arg node "${NAMES[i]}"
+done
+
+# The first monitor already came with a manager.
+for i in "${!HOSTS[@]}"; do
+    ((i == 0)) && continue
+    log "creating manager on node ${NAMES[i]}"
+    on_node "${HOSTS[i]}" pveceph mgr create
+done
+
+on_node "$first" ceph config set osd osd_memory_target "$OSD_MEMORY_TARGET"
+
+for k in "${!OSD_DEVICES[@]}"; do
+    i=${OSD_NODES[k]}
+    log "creating OSD on node ${NAMES[i]} on ${OSD_DEVICES[k]}"
+    on_node "${HOSTS[i]}" pveceph osd create "${OSD_DEVICES[k]}"
+done
+
+wait_for_status "${#OSD_DEVICES[@]} OSDs to be up and in and all placement groups to be clean" \
+    600 "$READY" --argjson osds "${#OSD_DEVICES[@]}"
+
+if [[ -n $POOL ]]; then
+    log "creating pool '$POOL' with an RBD storage"
+    # The default of 128 placement groups is more than the autoscaler recommends for the few OSDs
+    # of a test cluster, which would leave the cluster in HEALTH_WARN.
+    on_node "$first" pveceph pool create "$POOL" --add_storages 1 --pg_num 32
+    wait_for_status "the placement groups of pool '$POOL' to be clean" \
+        600 "$READY" --argjson osds "${#OSD_DEVICES[@]}"
+fi
+
+log "ready with ${#HOSTS[@]} monitors and ${#OSD_DEVICES[@]} OSDs"
-- 
2.47.3





  parent reply	other threads:[~2026-12-01 14:58 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 14:58 [PATCH e2e-tests/test-tools 0/8] instance: rework hooks and set up Ceph as a cluster hook Michael Köppl
2026-12-01 14:58 ` [PATCH test-tools 1/8] readme: drop token fields from the hook config example Michael Köppl
2026-12-01 14:58 ` [PATCH test-tools 2/8] instance: ship generic hook scripts Michael Köppl
2026-12-01 14:58 ` [PATCH test-tools 3/8] instance: distinguish builtin hooks from hook files Michael Köppl
2026-12-01 14:58 ` [PATCH test-tools 4/8] instance: add hooks that run once a cluster is formed Michael Köppl
2026-12-01 14:58 ` [PATCH test-tools 5/8] instance: cli: run a script once for a whole cluster Michael Köppl
2026-12-01 14:58 ` Michael Köppl [this message]
2026-12-01 14:58 ` [PATCH e2e-tests 7/8] plans: use the hook scripts shipped with proxmox-test-instance Michael Köppl
2026-12-01 14:58 ` [PATCH e2e-tests 8/8] scripts: drop the scripts now shipped by proxmox-test-instance Michael Köppl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201145817.894414-7-m.koeppl@proxmox.com \
    --to=m.koeppl@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal