From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id E95341FF09F for ; Tue, 01 Dec 2026 15:58:52 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 26DE321738; Tue, 01 Dec 2026 15:58:31 +0100 (CET) From: =?UTF-8?q?Michael=20K=C3=B6ppl?= 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 Message-ID: <20261201145817.894414-7-m.koeppl@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261201145817.894414-1-m.koeppl@proxmox.com> References: <20261201145817.894414-1-m.koeppl@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1796137099608 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.171 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: FS2Z4B6TRGEJDQQXHC5WBTIYA3ND6MWI X-Message-ID-Hash: FS2Z4B6TRGEJDQQXHC5WBTIYA3ND6MWI X-MailFrom: m.koeppl@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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` | ` ` | Install the Ceph packages, without setting up a Ceph cluster | | `setup-zfs-storage` | ` ` | 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` | `[]` | 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 ] [--repository ] [--nic ] +# [--public-network ] [--cluster-network |''] [--osd-serial-prefix ] +# [--pool ] +# +# The n-th node gets the n-th host address of each network on , which must be left +# unconfigured (`iface 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 , 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