Package: bfgminer Version: 5.4.2+dfsg-1 Severity: wishlist Tags: patch Hi,
Here's a patch to allow bfgminer to be started as a daemon at boot time. The only missing thing is a systemd unit file, but since systemd can use sysv init files, it can be implemented later. Basically, it creates a user named "bfgminer" (and adds it to the video group to access mining hardware) and starts bfgminer as that user, in a subdirectory located in /run. I couldn't make it work with bfgminer's chroot-dir/setuid options, so it relies on start-stop-daemon's chdir/chuid options (it's not a real chroot though, just a running directory writable by bfgminer user to contain generated binaries). The running directory is created every time before start, since /run is most often a in a tmpfs these days; also, any previously generated binary is removed before starting, to avoid problems caused by OpenCL/SDK upgrades (mentioned in upstream README files). Options are configurable from both /etc/default/bfgminer and/or /etc/bfgminer.conf. By default, it doesn't start if it's not explicitly enabled in /etc/default/bfgminer, so for people who prefer to run bfgminer manually, the whole patch should be completely transparent (minus the new configuration files and logs directory). I intentionally left a home directory (/var/lib/bfgminer) in addition to the running directory, for possible further tweaking (configuration snippets, bitcoin-cli credentials, etc). The default configuration file is almost empty (it contains brackets with nothing in between) because bfgminer chokes on a completely empty file; additionally, the init script checks for the presence of this file, since bfgminer also chokes if the configuration file doesn't exist. It *could* also check if it's readable, but such a check would be complicated to implement in the init script since it's run by root, whereas the binary will finally be run by another user; since /etc/bfgminer.conf permissions are set by the postinst script, IMHO such a test is not necessary, as it would break only if a user manually changes these permissions. It also contains a small patch against upstream sources to make the log file readable by group, as it's customary in Debian that members of "adm" group should be able to read logs (/var/log/bfgminer is installed with group "adm" and setuid group). I think I covered most questions, but of course, if you have any, feel free to ask. Regards, -- Raphaël Halimi
diff --git a/debian/bfgminer.conf b/debian/bfgminer.conf
new file mode 100644
index 0000000..2c63c08
--- /dev/null
+++ b/debian/bfgminer.conf
@@ -0,0 +1,2 @@
+{
+}
diff --git a/debian/control b/debian/control
index 8ae5693..cf7bc4e 100644
--- a/debian/control
+++ b/debian/control
@@ -24,7 +24,7 @@ Vcs-Git: git://anonscm.debian.org/pkg-bitcoin/bfgminer.git
Package: bfgminer
Architecture: any
-Depends: ${shlibs:Depends}, ${misc:Depends}
+Depends: ${shlibs:Depends}, ${misc:Depends}, adduser
Recommends: ocl-icd-libopencl1, opencl-icd
Suggests: mesa-opencl-icd
Description: multi-threaded multi-pool ASIC, FPGA and GPU bitcoin miner
diff --git a/debian/default b/debian/default
new file mode 100644
index 0000000..06ba1f2
--- /dev/null
+++ b/debian/default
@@ -0,0 +1,17 @@
+# Options for BFGMiner
+
+# Uncomment to start BFGMiner at boot time
+#START="yes"
+
+# Uncomment to also log nonces
+#LOG_NONCES="yes"
+
+# Uncomment to also log shares
+#LOG_SHARES="yes"
+
+# Custom options
+# IMPORTANT: do not put any password here, as they will appear in the command
+# line and could be seen by any user. Use /etc/bfgminer.conf instead.
+# Also, do not use options --text-only, --log-file, --noncelog or --sharelog,
+# these ones are automatically handled by the init script.
+#DAEMON_OPTS=""
diff --git a/debian/init b/debian/init
new file mode 100755
index 0000000..4bb854f
--- /dev/null
+++ b/debian/init
@@ -0,0 +1,135 @@
+#! /bin/sh
+### BEGIN INIT INFO
+# Provides: bfgminer
+# Required-Start: $remote_fs $syslog
+# Required-Stop: $remote_fs $syslog
+# Default-Start: 2 3 4 5
+# Default-Stop: 0 1 6
+# Short-Description: BFGMiner
+# Description: Multi-threaded, multi-blockchain, multi-pool ASIC, FPGA,
+# GPU and CPU miner with dynamic clocking, monitoring, and
+# fanspeed support for bitcoin.
+### END INIT INFO
+
+PATH=/sbin:/usr/sbin:/bin:/usr/bin
+DESC="BFGMiner"
+NAME=bfgminer
+DAEMON=/usr/bin/$NAME
+CONFFILE=/etc/$NAME.conf
+LOGDIR=/var/log/bfgminer
+DAEMON_ARGS="--text-only --log-file $LOGDIR/bfgminer.log"
+RUNDIR=/run/$NAME
+PIDFILE=$RUNDIR/$NAME.pid
+SCRIPTNAME=/etc/init.d/$NAME
+
+# Exit if the package is not installed
+[ -x "$DAEMON" ] || exit 0
+
+# Read configuration variable file if it is present
+[ -r /etc/default/$NAME ] && . /etc/default/$NAME
+
+# Do we really want to start ?
+[ "$START" = "yes" ] || exit 0
+
+# Secondary log files
+[ "$LOG_NONCES" = "yes" ] && DAEMON_ARGS="$DAEMON_ARGS --noncelog $LOGDIR/nonces.log"
+[ "$LOG_SHARES" = "yes" ] && DAEMON_ARGS="$DAEMON_ARGS --sharelog $LOGDIR/shares.log"
+
+# bfgminer chokes on a non-existent config file
+[ -e $CONFFILE ] && DAEMON_ARGS="$DAEMON_ARGS --config $CONFFILE"
+
+# Load the VERBOSE setting and other rcS variables
+. /lib/init/vars.sh
+
+# Define LSB log_* functions.
+# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
+# and status_of_proc is working.
+. /lib/lsb/init-functions
+
+#
+# Function that prepares the running directory
+#
+do_start_prepare() {
+ mkdir -p $RUNDIR
+ chown bfgminer:bfgminer $RUNDIR
+ rm -f $RUNDIR/*.bin
+}
+
+#
+# Function that starts the daemon/service
+#
+do_start()
+{
+ # Return
+ # 0 if daemon has been started
+ # 1 if daemon was already running
+ # 2 if daemon could not be started
+ start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
+ || return 1
+ do_start_prepare || return 2
+ start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --make-pidfile --chuid bfgminer --chdir $RUNDIR --background -- \
+ $DAEMON_ARGS $DAEMON_OPTS \
+ || return 2
+}
+
+#
+# Function that stops the daemon/service
+#
+do_stop()
+{
+ # Return
+ # 0 if daemon has been stopped
+ # 1 if daemon was already stopped
+ # 2 if daemon could not be stopped
+ # other if a failure occurred
+ start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME --remove-pidfile
+ RETVAL="$?"
+ [ "$RETVAL" = 2 ] && return 2
+ return "$RETVAL"
+}
+
+case "$1" in
+ start)
+ [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
+ do_start
+ case "$?" in
+ 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+ 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+ esac
+ ;;
+ stop)
+ [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
+ do_stop
+ case "$?" in
+ 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+ 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+ esac
+ ;;
+ status)
+ status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
+ ;;
+ restart|force-reload)
+ log_daemon_msg "Restarting $DESC" "$NAME"
+ do_stop
+ case "$?" in
+ 0|1)
+ do_start
+ case "$?" in
+ 0) log_end_msg 0 ;;
+ 1) log_end_msg 1 ;; # Old process is still running
+ *) log_end_msg 1 ;; # Failed to start
+ esac
+ ;;
+ *)
+ # Failed to stop
+ log_end_msg 1
+ ;;
+ esac
+ ;;
+ *)
+ echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
+ exit 3
+ ;;
+esac
+
+:
diff --git a/debian/logrotate b/debian/logrotate
new file mode 100644
index 0000000..074bc20
--- /dev/null
+++ b/debian/logrotate
@@ -0,0 +1,8 @@
+/var/log/bfgminer/*.log {
+ weekly
+ rotate 52
+ compress
+ copytruncate
+ missingok
+ notifempty
+}
diff --git a/debian/patches/0001-Add-group-read-permissions-on-log-file.patch b/debian/patches/0001-Add-group-read-permissions-on-log-file.patch
new file mode 100644
index 0000000..c422ff5
--- /dev/null
+++ b/debian/patches/0001-Add-group-read-permissions-on-log-file.patch
@@ -0,0 +1,20 @@
+Description: Add group read permissions on log file.
+Author: Raphaël Halimi <[email protected]>
+Origin: other
+Forwarded: not-needed
+Last-Update: 2016-05-12
+---
+This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
+diff --git a/miner.c b/miner.c
+index d84cfac..7bdb4d3 100644
+--- a/miner.c
++++ b/miner.c
+@@ -1996,7 +1996,7 @@ char *set_log_file(char *arg)
+ }
+ else
+ {
+- fd = open(arg, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
++ fd = open(arg, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
+ if (unlikely(fd == -1))
+ return "Failed to open log-file";
+ }
diff --git a/debian/patches/series b/debian/patches/series
new file mode 100644
index 0000000..f94650e
--- /dev/null
+++ b/debian/patches/series
@@ -0,0 +1 @@
+0001-Add-group-read-permissions-on-log-file.patch
diff --git a/debian/postinst b/debian/postinst
new file mode 100755
index 0000000..de22781
--- /dev/null
+++ b/debian/postinst
@@ -0,0 +1,17 @@
+#! /bin/sh -e
+
+# Create user
+adduser --quiet --system --group --home /var/lib/bfgminer bfgminer
+
+# Add user to video grooup to allow access to mining devices
+adduser --quiet bfgminer video
+
+# Permissions for config file
+chown root:bfgminer /etc/bfgminer.conf
+chmod 640 /etc/bfgminer.conf
+
+# Permissions for log directory
+chown bfgminer:adm /var/log/bfgminer
+chmod 2755 /var/log/bfgminer
+
+#DEBHELPER#
diff --git a/debian/postrm b/debian/postrm
new file mode 100755
index 0000000..d41063c
--- /dev/null
+++ b/debian/postrm
@@ -0,0 +1,12 @@
+#! /bin/sh -e
+
+# Remove run directory
+rm -rf /run/bfgminer
+
+# On purge, remove user and log directory
+if [ "$1" = "purge" ]; then
+ deluser --quiet --system --remove-home bfgminer
+ rm -rf /var/log/bfgminer
+fi
+
+#DEBHELPER#
diff --git a/debian/rules b/debian/rules
index c9e913c..0a9c0a3 100755
--- a/debian/rules
+++ b/debian/rules
@@ -25,6 +25,11 @@ override_dh_auto_install:
-mv -v $(CURDIR)/debian/bfgminer/usr/sbin/setup-vfio $(CURDIR)/debian/bfgminer/usr/lib/bfgminer/
# Wipe empty dirs if any
find $(CURDIR)/debian -type d -empty -delete -printf 'removed %p\n'
+ # Install config file
+ install -d $(CURDIR)/debian/bfgminer/etc
+ install -m 640 $(CURDIR)/debian/bfgminer.conf $(CURDIR)/debian/bfgminer/etc/
+ # Create log dir
+ install -d $(CURDIR)/debian/bfgminer/var/log/bfgminer
MAN_NAME=multi-threaded multi-pool ASIC, FPGA, GPU and CPU bitcoin miner.
bfgminer.1:
signature.asc
Description: OpenPGP digital signature

