Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for OpenRC as an alternative to systemd #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions OpenRC/conf.d/fw-fanctrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# /etc/conf.d/fw-fanctrl

# Configuration File
#FW_FANCTRL_CONFIGFILE="%DEST_DIRECTORY%%SYSCONF_DIRECTORY%/fw-fanctrl/config.json"

# PID File
#FW_FANCTRL_PIDFILE="%DEST_DIRECTORY%/run/fw-fanctrl.pid"

# Logging
#FW_FANCTRL_LOG="%DEST_DIRECTORY%/var/log/fw-fanctrl.log"
#FW_FANCTLR_ERROR_LOG="%DEST_DIRECTORY%/var/log/fw-fanctrl.log"
23 changes: 23 additions & 0 deletions OpenRC/init.d/fw-fanctrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/sbin/openrc-run
# Copyright 2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

FW_FANCTRL_CONFIGFILE=${FW_FANCTRL_CONFIGFILE:-"/etc/fw-fanctrl/config.json"}
FW_FANCTRL_PIDFILE=${FW_FANCTRL_PIDFILE:-"/var/run/fw-fanctrl.pid")
FW_FANCTRL_LOG=${FW_FANCTRL_LOG:-"/var/log/fw-fanctrl.log"}
FW_FANCTLR_ERROR_LOG=${FW_FANCTLR_ERROR_LOG:-"/var/log/fw-fanctrl.log"}


name="fw-fanctrl daemon"
description="Fan control for frame.work Laptop 16"
command=/usr/local/bin/fw-fanctrl
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the %PREFIX_DIRECTORY% ("%PREFIX_DIRECTORY%/bin/fw-fanctrl") to ensure compatibility with users configurations and packaging solutions

command_args="--config \"${FW_FANCTRL_CONFIGFILE}\" --no-log"
required_files="${FW_FANCTRL_CONFIGFILE}"
command_background=true
pidfile="${FW_FANCTRL_PIDFILE}"
output_log="${FW_FANCTRL_LOG}"
error_log="${FW_FANCTLR_ERROR_LOG}"

depend() {
need localmount
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ this script also includes options to:
- specify an installation destination directory (`--dest-dir <installation destination directory (defaults to /)>`).
- specify an installation prefix directory (`--prefix-dir <installation prefix directory (defaults to /usr)>`).
- specify a default configuration directory (`--sysconf-dir <system configuration destination directory (defaults to /etc)>`).
- if you're using OpenRC instead of systemd use (`--openrc`).
- disable ectool installation and service activation (`--no-ectool`)
- disable post-install process (`--no-post-install`)
- disable pre-uninstall process (`--no-pre-uninstall`)
Expand Down
112 changes: 72 additions & 40 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fi

# Argument parsing
SHORT=r,d:,p:,s:,h
LONG=remove,dest-dir:,prefix-dir:,sysconf-dir:,no-ectool,no-pre-uninstall,no-post-install,help
LONG=remove,dest-dir:,prefix-dir:,sysconf-dir:,openrc,no-ectool,no-pre-uninstall,no-post-install,help
VALID_ARGS=$(getopt -a --options $SHORT --longoptions $LONG -- "$@")
if [[ $? -ne 0 ]]; then
exit 1;
Expand All @@ -21,6 +21,7 @@ SHOULD_INSTALL_ECTOOL=true
SHOULD_PRE_UNINSTALL=true
SHOULD_POST_INSTALL=true
SHOULD_REMOVE=false
INIT_SYSTEM="systemd"

eval set -- "$VALID_ARGS"
while true; do
Expand All @@ -40,6 +41,9 @@ while true; do
SYSCONF_DIR=$2
shift
;;
'--openrc')
INIT_SYSTEM="openrc"
;;
'--no-ectool')
SHOULD_INSTALL_ECTOOL=false
;;
Expand All @@ -50,7 +54,7 @@ while true; do
SHOULD_POST_INSTALL=false
;;
'--help' | '-h')
echo "Usage: $0 [--remove,-r] [--dest-dir,-d <installation destination directory (defaults to $DEST_DIR)>] [--prefix-dir,-p <installation prefix directory (defaults to $PREFIX_DIR)>] [--sysconf-dir,-s system configuration destination directory (defaults to $SYSCONF_DIR)] [--no-ectool] [--no-post-install] [--no-pre-uninstall]" 1>&2
echo "Usage: $0 [--remove,-r] [--dest-dir,-d <installation destination directory (defaults to $DEST_DIR)>] [--prefix-dir,-p <installation prefix directory (defaults to $PREFIX_DIR)>] [--sysconf-dir,-s system configuration destination directory (defaults to $SYSCONF_DIR)] [--openrc] [--no-ectool] [--no-post-install] [--no-pre-uninstall]" 1>&2
exit 0
;;
--)
Expand All @@ -61,11 +65,17 @@ while true; do
done
#

SERVICES_DIR="./services"
SERVICE_EXTENSION=".service"
if [ "${INIT_SYSTEM}" = "systemd" ]; then
SERVICES_DIR="./services"
SERVICE_EXTENSION=".service"

SERVICES="$(cd "$SERVICES_DIR" && find . -maxdepth 1 -maxdepth 1 -type f -name "*$SERVICE_EXTENSION" -exec basename {} "$SERVICE_EXTENSION" \;)"
SERVICES_SUBCONFIGS="$(cd "$SERVICES_DIR" && find . -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)"
SERVICES="$(cd "$SERVICES_DIR" && find . -maxdepth 1 -maxdepth 1 -type f -name "*$SERVICE_EXTENSION" -exec basename {} "$SERVICE_EXTENSION" \;)"
SERVICES_SUBCONFIGS="$(cd "$SERVICES_DIR" && find . -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)"
elif [ "${INIT_SYSTEM}" = "openrc" ]; then
rc-service -e fw-fanctrl
SERVICE_EXISTS=$?
RC_STATUS="$(rc-status -f ini | awk -F '=' '/fw-fanctrl/ {print $2}' | xargs)"
fi

function sanitizePath() {
local SANITIZED_PATH="$1"
Expand Down Expand Up @@ -134,42 +144,64 @@ function install() {

cp -n "./config.json" "$DEST_DIR$SYSCONF_DIR/fw-fanctrl" 2> "/dev/null" || true

# create program services based on the services present in the './services' folder
echo "creating '$DEST_DIR$PREFIX_DIR/lib/systemd/system'"
mkdir -p "$DEST_DIR$PREFIX_DIR/lib/systemd/system"
echo "creating services"
for SERVICE in $SERVICES ; do
SERVICE=$(sanitizePath "$SERVICE")
if [ "$SHOULD_PRE_UNINSTALL" = true ] && [ "$(systemctl is-active "$SERVICE")" == "active" ]; then
echo "stopping [$SERVICE]"
systemctl stop "$SERVICE"
fi
echo "creating '$DEST_DIR$PREFIX_DIR/lib/systemd/system/$SERVICE$SERVICE_EXTENSION'"
cat "$SERVICES_DIR/$SERVICE$SERVICE_EXTENSION" | sed -e "s/%PREFIX_DIRECTORY%/${PREFIX_DIR//\//\\/}/" | sed -e "s/%SYSCONF_DIRECTORY%/${SYSCONF_DIR//\//\\/}/" | tee "$DEST_DIR$PREFIX_DIR/lib/systemd/system/$SERVICE$SERVICE_EXTENSION" > "/dev/null"
done

# add program services sub-configurations based on the sub-configurations present in the './services' folder
echo "adding services sub-configurations"
for SERVICE in $SERVICES_SUBCONFIGS ; do
SERVICE=$(sanitizePath "$SERVICE")
echo "adding sub-configurations for [$SERVICE]"
SUBCONFIG_FOLDERS="$(cd "$SERVICES_DIR/$SERVICE" && find . -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)"
# ensure folders exists
mkdir -p "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE"
for SUBCONFIG_FOLDER in $SUBCONFIG_FOLDERS ; do
SUBCONFIG_FOLDER=$(sanitizePath "$SUBCONFIG_FOLDER")
echo "creating '$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG_FOLDER'"
mkdir -p "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG_FOLDER"
if [ "${INIT_SYSTEM}" = "systemd" ]; then
# create program services based on the services present in the './services' folder
echo "creating '$DEST_DIR$PREFIX_DIR/lib/systemd/system'"
mkdir -p "$DEST_DIR$PREFIX_DIR/lib/systemd/system"
echo "creating services"
for SERVICE in $SERVICES ; do
SERVICE=$(sanitizePath "$SERVICE")
if [ "$SHOULD_PRE_UNINSTALL" = true ] && [ "$(systemctl is-active "$SERVICE")" == "active" ]; then
echo "stopping [$SERVICE]"
systemctl stop "$SERVICE"
fi
echo "creating '$DEST_DIR$PREFIX_DIR/lib/systemd/system/$SERVICE$SERVICE_EXTENSION'"
cat "$SERVICES_DIR/$SERVICE$SERVICE_EXTENSION" | sed -e "s/%PREFIX_DIRECTORY%/${PREFIX_DIR//\//\\/}/" | sed -e "s/%SYSCONF_DIRECTORY%/${SYSCONF_DIR//\//\\/}/" | tee "$DEST_DIR$PREFIX_DIR/lib/systemd/system/$SERVICE$SERVICE_EXTENSION" > "/dev/null"
done
SUBCONFIGS="$(cd "$SERVICES_DIR/$SERVICE" && find . -mindepth 1 -type f)"
# add sub-configurations
for SUBCONFIG in $SUBCONFIGS ; do
SUBCONFIG=$(sanitizePath "$SUBCONFIG")
echo "adding '$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG'"
cat "$SERVICES_DIR/$SERVICE/$SUBCONFIG" | sed -e "s/%PREFIX_DIRECTORY%/${PREFIX_DIR//\//\\/}/" | tee "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG" > "/dev/null"
chmod +x "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG"

# add program services sub-configurations based on the sub-configurations present in the './services' folder
echo "adding services sub-configurations"
for SERVICE in $SERVICES_SUBCONFIGS ; do
SERVICE=$(sanitizePath "$SERVICE")
echo "adding sub-configurations for [$SERVICE]"
SUBCONFIG_FOLDERS="$(cd "$SERVICES_DIR/$SERVICE" && find . -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)"
# ensure folders exists
mkdir -p "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE"
for SUBCONFIG_FOLDER in $SUBCONFIG_FOLDERS ; do
SUBCONFIG_FOLDER=$(sanitizePath "$SUBCONFIG_FOLDER")
echo "creating '$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG_FOLDER'"
mkdir -p "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG_FOLDER"
done
SUBCONFIGS="$(cd "$SERVICES_DIR/$SERVICE" && find . -mindepth 1 -type f)"
# add sub-configurations
for SUBCONFIG in $SUBCONFIGS ; do
SUBCONFIG=$(sanitizePath "$SUBCONFIG")
echo "adding '$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG'"
cat "$SERVICES_DIR/$SERVICE/$SUBCONFIG" | sed -e "s/%PREFIX_DIRECTORY%/${PREFIX_DIR//\//\\/}/" | tee "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG" > "/dev/null"
chmod +x "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG"
done
done
done
elif [ "${INIT_SYSTEM}" = "openrc" ]; then
if ${SERVICE_EXISTS}; then
if [ "${RC_STATUS}" = "started" ]; then
echo "service is already running, stopping"
rc-service -s fw-fanctrl stop
fi
fi

echo "creating init.d file"
INIT_D_DIR="${DEST_DIR}${SYSCONF_DIR}/init.d"
CONF_D_DIR="${DEST_DIR}${SYSCONF_DIR}/conf.d"
mkdir -p "${INIT_D_DIR}"
cp "OpenRC/init.d/fw-fanctrl" "${INIT_D_DIR}"

echo "creating conf.d file"
mkdir -p "${CONF_D_DIR}"
sed -e "s/%DEST_DIRECTORY%/${DEST_DIR//\//\\/}/" "OpenRC/conf.d/fw-fanctrl" | sed -e "s/%SYSCONF_DIRECTORY%/${SYSCONF_DIR//\//\\/}/" | tee "${CONF_D_DIR}/fw-fanctrl"
cp "OpenRC/conf.d/fw-fanctrl" "${CONF_D_DIR}"

fi

if [ "$SHOULD_POST_INSTALL" = true ]; then
./post-install.sh --dest-dir "$DEST_DIR" --sysconf-dir "$SYSCONF_DIR"
fi
Expand Down
42 changes: 27 additions & 15 deletions post-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ HOME_DIR="$(eval echo "~$(logname)")"

# Argument parsing
SHORT=d:,s:,h
LONG=dest-dir:,sysconf-dir:,help
LONG=dest-dir:,sysconf-dir:,openrc,help
VALID_ARGS=$(getopt -a --options $SHORT --longoptions $LONG -- "$@")
if [[ $? -ne 0 ]]; then
exit 1;
fi

DEST_DIR="/usr"
SYSCONF_DIR="/etc"
INIT_SYSTEM="systemd"


eval set -- "$VALID_ARGS"
while true; do
Expand All @@ -30,8 +32,11 @@ while true; do
SYSCONF_DIR=$2
shift
;;
'--openrc')
INIT_SYSTEM="openrc"
;;
'--help' | '-h')
echo "Usage: $0 [--dest-dir,-d <installation destination directory (defaults to $DEST_DIR)>] [--sysconf-dir,-s system configuration destination directory (defaults to $SYSCONF_DIR)]" 1>&2
echo "Usage: $0 [--dest-dir,-d <installation destination directory (defaults to $DEST_DIR)>] [--sysconf-dir,-s system configuration destination directory (defaults to $SYSCONF_DIR)] [--openrc]" 1>&2
exit 0
;;
--)
Expand All @@ -42,11 +47,6 @@ while true; do
done
#

SERVICES_DIR="./services"
SERVICE_EXTENSION=".service"

SERVICES="$(cd "$SERVICES_DIR" && find . -maxdepth 1 -maxdepth 1 -type f -name "*$SERVICE_EXTENSION" -exec basename {} "$SERVICE_EXTENSION" \;)"

function sanitizePath() {
local SANITIZED_PATH="$1"
local SANITIZED_PATH=${SANITIZED_PATH//..\//}
Expand All @@ -64,11 +64,23 @@ function move_legacy() {
move_legacy

echo "enabling services"
systemctl daemon-reload
for SERVICE in $SERVICES ; do
SERVICE=$(sanitizePath "$SERVICE")
echo "enabling [$SERVICE]"
systemctl enable "$SERVICE"
echo "starting [$SERVICE]"
systemctl start "$SERVICE"
done
if [ "${INIT_SYSTEM}" = "systemd" ]; then
SERVICES_DIR="./services"
SERVICE_EXTENSION=".service"

SERVICES="$(cd "$SERVICES_DIR" && find . -maxdepth 1 -maxdepth 1 -type f -name "*$SERVICE_EXTENSION" -exec basename {} "$SERVICE_EXTENSION" \;)"

systemctl daemon-reload
for SERVICE in $SERVICES ; do
SERVICE=$(sanitizePath "$SERVICE")
echo "enabling [$SERVICE]"
systemctl enable "$SERVICE"
echo "starting [$SERVICE]"
systemctl start "$SERVICE"
done
elif [ "${INIT_SYSTEM}" = "openrc" ]; then
echo "enabling fw-fanctrl"
rc-update add fw-fanctrl default
echo "starting fw-fanctrl"
rc-service fw-fanctrl start
fi
41 changes: 26 additions & 15 deletions pre-uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ if [ "$EUID" -ne 0 ]
fi

HOME_DIR="$(eval echo "~$(logname)")"
INIT_SYSTEM="systemd"

# Argument parsing
SHORT=h
LONG=help
LONG=help,openrc
VALID_ARGS=$(getopt -a --options $SHORT --longoptions $LONG -- "$@")
if [[ $? -ne 0 ]]; then
exit 1;
Expand All @@ -19,6 +20,9 @@ fi
eval set -- "$VALID_ARGS"
while true; do
case "$1" in
'--openrc')
INIT_SYSTEM="openrc"
;;
'--help' | '-h')
echo "Usage: $0" 1>&2
exit 0
Expand All @@ -31,11 +35,6 @@ while true; do
done
#

SERVICES_DIR="./services"
SERVICE_EXTENSION=".service"

SERVICES="$(cd "$SERVICES_DIR" && find . -maxdepth 1 -maxdepth 1 -type f -name "*$SERVICE_EXTENSION" -exec basename {} "$SERVICE_EXTENSION" \;)"

function sanitizePath() {
local SANITIZED_PATH="$1"
local SANITIZED_PATH=${SANITIZED_PATH//..\//}
Expand All @@ -44,12 +43,24 @@ function sanitizePath() {
echo "$SANITIZED_PATH"
}

echo "disabling services"
systemctl daemon-reload
for SERVICE in $SERVICES ; do
SERVICE=$(sanitizePath "$SERVICE")
echo "stopping [$SERVICE]"
systemctl stop "$SERVICE" 2> "/dev/null" || true
echo "disabling [$SERVICE]"
systemctl disable "$SERVICE" 2> "/dev/null" || true
done
if [ "${INIT_SYSTEM}" = "systemd" ]; then
SERVICES_DIR="./services"
SERVICE_EXTENSION=".service"

SERVICES="$(cd "$SERVICES_DIR" && find . -maxdepth 1 -maxdepth 1 -type f -name "*$SERVICE_EXTENSION" -exec basename {} "$SERVICE_EXTENSION" \;)"

echo "disabling services"
systemctl daemon-reload
for SERVICE in $SERVICES ; do
SERVICE=$(sanitizePath "$SERVICE")
echo "stopping [$SERVICE]"
systemctl stop "$SERVICE" 2> "/dev/null" || true
echo "disabling [$SERVICE]"
systemctl disable "$SERVICE" 2> "/dev/null" || true
done
elif [ "${INIT_SYSTEM}" = "openrc" ]; then
echo "stopping fw-fanctrl"
rc-service fw-fanctrl stop
echo "diabling fw-fanctrl"
rc-update del fw-fanctrl default
fi