#!/sbin/openrc-run # shellcheck shell=bash ## bees variables rundir="/run/bees" logdir="/var/log/bees" mnt="$rundir/mnt/${fsuuid}" device="/dev/disk/by-uuid/${fsuuid}" beeshome="${mnt}/.beeshome" hashfile="${beeshome}/beeshash.dat" export BEESSTATUS="${statusfile:-${rundir}/${fsuuid}.status}" # optional variables can be set in conf.d : "${logfile:="${logdir}/${RC_SVCNAME}.log"}" : "${loglevel:=5}" : "${bees_args:="-m 3"}" : "${scheduler:="idle"}" : "${SSD_IONICELEVEL:=3}" : "${SSD_NICELEVEL:=19}" ## OpenRC name="${RC_SVCNAME}" description="Best-Effort Extent-Same, a btrfs dedupe agent" command="/usr/libexec/bees" command_args="-v ${loglevel} ${bees_args} \"${mnt}\" " command_background="true" extra_started_commands="suspend resume" description_suspend="Suspends bees without unloading it." description_resume="Resumes normal operation." start_stop_daemon_args="--scheduler ${scheduler} --stdout ${logfile} --stderr ${logfile}" pidfile="/run/${RC_SVCNAME}.pid" retry=${GRACEFUL_TIMEOUT:-120} depend() { use lvm device-mapper localmount } checkconfig() { if [ -z $hashfile ]; then eerror "hashsize not set!" return 1 fi if [ ! -b "${device}" ]; then eerror "${device} not found!" return 1 fi if [ ! "$(blkid -s TYPE -o value "${device}")" == "btrfs" ]; then eerror "${device} is not a btrfs filesystem" return 1 fi; if [ ! -z "${logfile}" ]; then checkpath -f -m 0600 -o root:root "${logfile}" fi } start_pre() { ebegin "Config check" checkconfig || return 1 if findmnt "${mnt}" >/dev/null ; then eerror "${mnt} is already mounted. Aborting." return 1 fi checkpath -d -m 0700 -o root:root "${logdir}" checkpath -d -m 0700 -o root:root "${rundir}" checkpath -d -m 0700 -o root:root "${rundir}/mnt" checkpath -d -m 0700 -o root:root "${mnt}" ebegin "Preparing ${fsuuid} mountpoint" mount -osubvolid=5 "${device}" "${mnt}" || return 1 if [ ! -d "${beeshome}" ]; then btrfs subvolume create "${beeshome}" || return 1 checkpath -d -m 0700 -o root:root "${beeshome}" else # btrfs subvolume show "${beeshome}" > /dev/null || return 1 checkpath -d -m 0700 -o root:root "${beeshome}" fi checkpath -f -m 0600 -o root:root "${hashfile}" old_hashsize="$(du -b "${hashfile}" | cut -f1)" if [ "${old_hashsize}" == "0" ] ; then truncate -s "${hashsize}" "${hashfile}" fi if [ "${hashsize}" != "${old_hashsize}" ] ; then eerror "Hash table size mismatch! Expected ${hashsize}b but found ${old_hashsize}b" return 1 fi pushd "${mnt}" >/dev/null eend $? } stop_pre() { # make sure bees is not suspended before stopping. start-stop-daemon --signal SIGCONT --pidfile "${pidfile}" return $? } stop_post() { ebegin "Unmounting ${fsuuid}" umount "${mnt}" eend $? } suspend() { # Suspending bees is useful to avoid a 'btrfs send' bug # that exists with some kernels. # See https://zygo.github.io/bees/btrfs-kernel.md ebegin "Suspending ${RC_SVCNAME}" start-stop-daemon --signal SIGSTOP --pidfile "${pidfile}" eend $? } resume() { ebegin "Resuming ${RC_SVCNAME}" start-stop-daemon --signal SIGCONT --pidfile "${pidfile}" eend $? }