Mise en place d’une swap par le réseau en nbd

Dans le cadre d’une configuration d’un réseau de 130 postes en diskless Ubuntu 14.04  bootant à partir d’un serveur NFS et PXE en Debian Wheezy, voici comment j’ai mis en place un serveur swap via le protocol NBD en m’inspirant très largement du projet ltsp.

1. Côté serveur :
Debian 7, ip 192.168.8.169

Installation du serveur NBD :

apt-get install nbd-serveur

Fichier de configuration /etc/nbd-server/conf.d/swap.conf :

[swap]
 exportname = /tmp/nbd-swap/%s
 prerun = nbdswapd %s
 postrun = rm -f %s

Créer le fichier /usr/bin/nbdswapd (largement inspiré mais modifié de celui de base fourni par le paquet lstp-server-standalone)

# Generates a swap file to be exported with nbd-server.
 #
 # When called with no parameters, it assumes that it was ran from inetd,
 # and it launches nbd-server in order to serve it.
 # An inetd configuration line like the following is needed in that case:
 # 9572 stream tcp nowait nobody /usr/sbin/tcpd /usr/sbin/nbdswapd
 #
 # When called with one parameter, it assumes that it was ran from nbd-server,
 # so it just creates the specified swap file and exits.
 # The nbd-server configuration section is expected to look similar to this:
 # [swap]
 # exportname = /tmp/nbd-swap/%s
 # prerun = nbdswapd %s
 # postrun = rm -f %s
 # Fail on error, to notify nbd-server that the swap file wasn't created.
 set -e
 # Default sparse swapfile size, in MB
 SIZE=512
 # Default to running mkswap
 RUN_MKSWAP=true
 # Allow overriding the defaults from a configuration file
 if [ -f /etc/ltsp/nbdswapd.conf ]; then
 . /etc/ltsp/nbdswapd.conf
 fi
 # Abort if liveimg
 if grep -q "liveimg" /proc/cmdline; then
 exit 1
 fi
 SWAP="$1"
 SWAPDIR=/tmp/nbd-swap
 test -d "$SWAPDIR" || mkdir -p "$SWAPDIR"
 # generate the swap file
 dd if=/dev/zero of="$SWAP" bs=1M count=0 seek="$SIZE" 2> /dev/null
 chmod 600 "$SWAP"
 if [ "$RUN_MKSWAP" = "true" ]; then
 mkswap "$SWAP" > /dev/null
 fi

On le rend exécutable :

chmod +x /usr/bin/nbdswapd

La partie serveur est terminée. Par défaut, les fichiers swap se trouverons dans /tmp/nbd-swap/ip.du.client et feront une taille de 512 mb. Si vous désirez des autres paramètres, il suffira d’adapter le fichier /usr/bin/nbdswapd

2. Côté client

Installer le client nbd :

apt-get install nbd-client

Configuration du fichier client ( /etc/nbd-client ) , modif en rouge :

# If you don't want to reconfigure this package after installing, uncomment
 # the following line:
 #AUTO_GEN="n"
 # If you don't want the init script to kill nbd-client devices that aren't
 # specified in this configuration file, set the following to "false":
 KILLALL="false"
 # Note that any statical settings in this file will be preserved
 # regardless of the setting of AUTO_GEN, so its use is only recommended
 # if you set things in a dynamical way (e.g., through a database)
 #
 # Name of the first used nbd /dev-entry:
 NBD_DEVICE[0]=/dev/nbd0
 #
 # Type; s=swap, f=filesystem (with entry in /etc/fstab), r=raw (no other setup
 # than to run the client)
 NBD_TYPE[0]=s
 #
 # The host on which the nbd-server process is running
 NBD_HOST[0]=192.168.8.169
 # (IP du serveur)
 # The port on which this client needs to connect. Optional for
 # new-style exports (which use a single port, and export names).
 NBD_PORT[0]=
 #
 # The name of the export. Required for new-style exports.
 NBD_NAME[0]=swap
 #
 # Any extra parameters you would want to specify
 NBD_EXTRA[0]=-persist
 # The second networked block device could look like:
 # NBD_DEVICE[1]=/dev/nbd1
 # NBD_TYPE[1]="f"
 # NBD_HOST[1]="localhost"
 # NBD_NAME[1]="disk1"
 #
 # You can add as many as you want, but don't skip any number in the variable
 # names, or the initscript will fail.

Il faut également ajouter ceci dans le fichier /etc/fstab (afin que le reboot / shutdown se passe correctement ) :

/dev/nbd0   none     swap    sw          0    0

Un reboot plus tard, tout devrais fonctionner…