Files
init.sh/modules/conf_ceph.sh

173 lines
6.5 KiB
Bash

# ------------------------------------------------------------------------------
# Configure machine for ceph (or samba / NFS) mount
# This file is part of the init.sh project
# Copyright (c) 2019-2025 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# ------------------------------------------------------------------------------
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# https://opensource.org/licenses/BSD-3-Clause
# ------------------------------------------------------------------------------
# Variable:
# * CEPH_SRV_NAMES: hosts names of ceph servers
# * CEPHIP_srv: with "srv" being a ceph server hostname, its corresponding IP
# * CEPH_MOUNTS: list of mounts to create
# * CEPH_MP_mount: mount point for the given "mount"
# * SHARED_HOME: Set at yes if homedir is a directory of the ceph mount (to be removed)
# * SMBSRV: Fallback samba server on unsupported architectures (not doing
# anything if undeclared)
# * NFSSRV: Fallback NFS server on unsupported architectures (not doing
# anything if undeclared)
# If both SMBSRV and NFSSRV are set on unsupported hardware, Samba will have a
# higher priority.
# ------------------------------------------------------------------------------
export VER_conf_ceph="1.0.2"
export DEP_conf_ceph=""
conf_ceph()
{
local success=undef
# Determine the type of installation
if [[ $SYS_ARCH == "x86_64" || $SYS_ARCH == "i386" ]]; then
export CEPH_STATUS=ceph
elif [[ -n $SMBSRV ]]; then
export CEPH_STATUS=smb
elif [[ -n $NFSSRV ]]; then
export CEPH_STATUS=nfs
else
export CEPH_STATUS=none
fi
if [[ $CEPH_STATUS == ceph ]]; then
# Install ceph package
pkginst ceph-common
# hosts files required for Ceph bootstrap when DNS not yet started
if ! grep -q "^# Ceph" /etc/hosts; then
prnt I "Adding server list to /etc/hosts"
backup_dist /etc/hosts
tag_file /etc/hosts
echo >> /etc/hosts
echo "# Ceph servers:" >> /etc/hosts
for srv in $CEPH_SRV_NAMES; do
local line
line="$(eval echo \$CEPHIP_$srv) $srv.$REALM $srv"
prnt m " - Adding line $line to /etc/hosts"
echo "$line" >> /etc/hosts
unset line
done
else
prnt W "Ceph servers already in /etc/hosts, nothing to do"
fi
backup_dist /etc/fstab
prnt I "Adding ceph entries to /etc/fstab"
tag_file /etc/fstab
echo >> /etc/fstab
local srvlist=${CEPH_SRV_NAMES/ /,}
prnt I "Fetching secret $CEPH_SECRET..."
local secret
secret=$(fetch_secret "$CEPH_SECRET")
if ! grep -q "$srvlist" /etc/fstab; then
echo "# Ceph :" >> /etc/fstab
for mnt in $CEPH_MOUNTS; do
mkdir -pv "$mnt"
echo "$srvlist:/ $(eval echo \$CEPH_MP_$mnt) ceph defaults,_netdev,name=admin,secret=$secret,id=$mnt 0 0" >> /etc/fstab
done
else
prnt W "Ceph entry already in /etc/fstab, nothing to do"
fi
unset srvlist secret
success=yes
elif [[ $CEPH_STATUS == smb ]]; then
pkginst smbclient
backup_dist /etc/fstab
prnt I "Adding Samba entries to /etc/fstab"
echo >> /etc/fstab
tag_file /etc/fstab
if ! grep -q "$SMBSRV" /etc/fstab; then
echo "# Samba:" >> /etc/fstab
for mnt in $CEPH_MOUNTS; do
echo "//$SMBSRV/$mnt $(eval echo \$CEPH_MP_$mnt) cifs defaults,_netdev,username=root,password= 0 0" >> /etc/fstab
done
else
prnt W "Samba entry already in /etc/fstab, nothing to do"
fi
success=yes
elif [[ $CEPH_STATUS == nfs ]]; then
tag_file /etc/fstab
# To be implemented
elif [[ $CEPH_STATUS == none ]]; then
prnt W "No alternative set for unsuported hardware, nothing will be done."
return 0
else
prnt E "Ceph status not understood, something is wrong."
return 1
fi
if [[ $success == yes ]]; then
# Create some mount binds for convenience
# TODO: That part should be a different module with own configuration
if grep -q "^/srv/ceph/share" /etc/fstab; then
echo "/srv/ceph/share /share none defaults,_netdev,bind 0 0" >> /etc/fstab
if [[ $SHARED_HOME == 1 ]]; then
echo "/srv/ceph/share/home /home none defaults,_netdev,bind 0 0" >> /etc/fstab
fi
fi
else
prnt E "Failed creating original mount, not adding binded ones"
fi
# Mount Ceph volumes if required
prnt I "Mounting ceph volumes"
for mnt in $CEPH_MOUNTS; do
if ! mountpoint -q "$(eval echo \$CEPH_MP_$mnt)"; then
mount -v "$(eval echo \$CEPH_MP_$mnt)" ||
prnt W "Error while mounting CEPH filesystem (check CEPH logs), ignoring"
fi
done
}
precheck_conf_ceph()
{
if [[ $SYS_ARCH == "x86_64" || $SYS_ARCH == "i386" ]]; then
prnt I "Installing ceph client package..."
if [[ -n $CEPH_SRV_NAMES ]]; then
prnt I "We will use the following CEPH servers:"
for srv in $CEPH_SRV_NAMES; do
if [[ -z "$(eval echo \$CEPHIP_$srv)" ]]; then
prnt E "The IP for CEPH server \"$srv\" is not declared."
die 180
else
prnt m " * $srv with IP $(eval echo \$CEPHIP_$srv)"
fi
done
if [[ -z $CEPH_SECRET ]]; then
prnt E "CEPH secret key is not declared, can't continue!"
prnt I "If you don't want to put a CEPH secret var in configuration file,"
prnt m "you need to export it temporarily in your environment, using the"
prnt m "\"CEPH_SECRET\" variable."
die 181
elif ! check_secret $CEPH_SECRET; then
prnt E "The declared $CEPH_SECRET is not accessible."
die 183
fi
if [[ -z $CEPH_MOUNTS ]]; then
prnt E "No CEPH mounts declared, despite reachable servers."
die 182
fi
else
prnt E "No CEPH server declared!"
die 182
fi
else
prnt W "System incompatible with ceph, falling back to Samba or NFS..."
fi
}
export -f conf_ceph
export -f precheck_conf_ceph