diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d25f79e --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +# Author: Bandie +# Copyright: Bandie, 2019, GNU-GPLv3 +all: + @echo "Nothing to make. Please run \"make install\"." + @exit 0 +install: + @echo "Installing grub-ownership..." + install -vDm 755 etc/grub.d/000_ownership -t "$(PREFIX)/etc/grub.d/" + @echo "Appending not existent config keys..." + ./tools/grub_append_cfgkeys -i $(PREFIX)/etc/default/grub + @echo "DONE!" + +uninstall: + @echo "Remove grub-ownership..." + rm $(PREFIX)/etc/grub.d/000_ownership + @echo "Removing entries from config file..." + ./tools/grub_append_cfgkeys -r $(PREFIX)/etc/default/grub + @echo "DONE!" diff --git a/README.md b/README.md index 3e05024..7395fb5 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,37 @@ # grub-ownership -Show an owner when grub is booting up +Show an owner when grub is booting up. + +## Installation + +``` +sudo make install +# OR with an autotools like PREFIX: +sudo PREFIX=/another/root make install +``` + + +## Configuration + +Edit the file `/etc/default/grub`. + +This is an example config + +``` +# Name and mail address of owner +OWNER="Alex Alexsen " + +# Amounts of seconds for how long the owner information is displayed. It is skippable via ESC. +OWNER_TIMEOUT=10 +``` + + + +## Uninstall + +``` +sudo make uninstall +# OR with an autotools like PREFIX: +sudo PREFIX=/another/root make uninstall +``` + + diff --git a/build/etc/default/grub b/build/etc/default/grub new file mode 100644 index 0000000..07ff988 --- /dev/null +++ b/build/etc/default/grub @@ -0,0 +1,6 @@ + +# Name and mail address of owner +#OWNER="John Doe " + +# Amounts of seconds for how long the owner information is displayed. It is skippable via ESC. Default: 5 +#OWNER_TIMEOUT=5 diff --git a/build/etc/grub.d/000_ownership b/build/etc/grub.d/000_ownership new file mode 100755 index 0000000..408e434 --- /dev/null +++ b/build/etc/grub.d/000_ownership @@ -0,0 +1,51 @@ +#!/bin/sh +# Author: Bandie +# Copyright: Bandie, 2019, GNU-GPLv3 + +set -e + +## Get params from /etc/default/grub +OWNER=$(cat /etc/default/grub | egrep "^OWNER=.*$" | sed "s/.*=\(.*\)/\1/;s/\"//g;s/'//g") +OWNER_TIMEOUT=$(cat /etc/default/grub | egrep "^OWNER_TIMEOUT=.*$" | sed "s/.*=\(.*\)/\1/;s/\"//g;s/'//g") + + +## Check if OWNER is set +if [ -z "$OWNER" ]; then + echo "/etc/default/grub: No OWNER set." >&2 + exit 0 +fi + +## Notify user about ownership +echo "Show ==> $OWNER <== as owner" >&2 + + +## Generate spaces in case if the owner has a very short name +for ((i=${#OWNER}; i<6; i++)); do + NSP="$NSP " +done + +## Generate border around the owner message +B="##########" +for ((i=6; i<${#OWNER}; i++)); do + B="${B}#" + SPACE="$SPACE " +done + + +## Check if OWNER_TIMEOUT is set. If not, default 5 +if ! [[ $OWNER_TIMEOUT =~ ^[0-9]+$ ]]; then + OWNER_TIMEOUT=5 + echo "/etc/default/grub: No OWNER_TIMEOUT set. Using default: 5 seconds." >&2 +fi + +## Write it into the grub.cfg +cat << EOF +echo "" +echo "$B" +echo "# Owner: ${SPACE}#" +echo "# $OWNER $NSP#" +echo "$B" +echo "" +echo "" +sleep -i $OWNER_TIMEOUT +EOF diff --git a/etc/grub.d/000_ownership b/etc/grub.d/000_ownership new file mode 100755 index 0000000..408e434 --- /dev/null +++ b/etc/grub.d/000_ownership @@ -0,0 +1,51 @@ +#!/bin/sh +# Author: Bandie +# Copyright: Bandie, 2019, GNU-GPLv3 + +set -e + +## Get params from /etc/default/grub +OWNER=$(cat /etc/default/grub | egrep "^OWNER=.*$" | sed "s/.*=\(.*\)/\1/;s/\"//g;s/'//g") +OWNER_TIMEOUT=$(cat /etc/default/grub | egrep "^OWNER_TIMEOUT=.*$" | sed "s/.*=\(.*\)/\1/;s/\"//g;s/'//g") + + +## Check if OWNER is set +if [ -z "$OWNER" ]; then + echo "/etc/default/grub: No OWNER set." >&2 + exit 0 +fi + +## Notify user about ownership +echo "Show ==> $OWNER <== as owner" >&2 + + +## Generate spaces in case if the owner has a very short name +for ((i=${#OWNER}; i<6; i++)); do + NSP="$NSP " +done + +## Generate border around the owner message +B="##########" +for ((i=6; i<${#OWNER}; i++)); do + B="${B}#" + SPACE="$SPACE " +done + + +## Check if OWNER_TIMEOUT is set. If not, default 5 +if ! [[ $OWNER_TIMEOUT =~ ^[0-9]+$ ]]; then + OWNER_TIMEOUT=5 + echo "/etc/default/grub: No OWNER_TIMEOUT set. Using default: 5 seconds." >&2 +fi + +## Write it into the grub.cfg +cat << EOF +echo "" +echo "$B" +echo "# Owner: ${SPACE}#" +echo "# $OWNER $NSP#" +echo "$B" +echo "" +echo "" +sleep -i $OWNER_TIMEOUT +EOF diff --git a/tools/grub_append_cfgkeys b/tools/grub_append_cfgkeys new file mode 100755 index 0000000..5fd48a6 --- /dev/null +++ b/tools/grub_append_cfgkeys @@ -0,0 +1,82 @@ +#!/bin/bash +# Author: Bandie +# Copyright: Bandie, 2019, GNU-GPLv3 + +CONFIG="/etc/default/grub" +TMPCFG="grub_tmp" +CFGKEYS=( + + ## 'COMMENT' + ## 'KEY' + ## 'EXAMPLE_VALUE' + + 'Name and mail address of owner' + 'OWNER' + '"John Doe "' + + 'Amounts of seconds for how long the owner information is displayed. It is skippable via ESC. Default: 5' + 'OWNER_TIMEOUT' + '5' +) + + +function testPerms(){ + if ! [ -e "$CONFIG" ]; then + echo "$0: ERROR: $CONFIG does not exist!" + exit 1 + fi + if ! [ -w "$CONFIG" ]; then + echo "$0: ERROR: Permission denied for file $CONFIG" + exit 1 + fi +} + + +function append(){ + testPerms + + for ((i=0; i<${#CFGKEYS[@]}; i+=3)); do + cat "$CONFIG" | grep "${CFGKEYS[(($i+1))]}" >> /dev/null + if [ $? -ne 0 ]; then + echo "Adding ${CFGKEYS[(($i+1))]} to $CONFIG..." + echo "" >> $CONFIG + echo "# ${CFGKEYS[$i]}" >> $CONFIG + echo "#${CFGKEYS[(($i+1))]}=${CFGKEYS[(($i+2))]}" >> $CONFIG + fi + + done +} + +function remove(){ + testPerms + + for ((i=0; i<${#CFGKEYS[@]}; i+=3)); do + echo "Removing ${CFGKEYS[(($i+1))]} from $CONFIG..." + sed "$!N;/${CFGKEYS[$i]}/!P;D" "$CONFIG" | sed "/.*${CFGKEYS[(($i+1))]}=.*/d" > "$TMPCFG" + cp "$TMPCFG" "$CONFIG" + done + rm $TMPCFG +} + +function h(){ + echo "$0: Usage:" + echo "$0 -i [ CONFIGFILE ] - Install config keys to config file. Configfile optional." + echo "$0 -r [ CONFIGFILE ] - Remove config keys from config file. Configfile optional." +} + +if [ -n "$2" ]; then + CONFIG="$2" +fi + +case $1 in +"-i") + append + ;; + +"-r") + remove + ;; + +*) + h +esac