Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
andsens committed Jan 29, 2025
1 parent b6a3dbd commit f5e6f58
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Release

on:
push:
tags: ['v*']

jobs:
test:
uses: ./.github/workflows/test.yaml
secrets: inherit
release:
needs: [test]
permissions:
contents: write
runs-on: ubuntu-latest
name: Create GitHub release
steps:
- uses: orbit-online/upkg-release@v1
with:
paths: bin LICENSE README.md
21 changes: 21 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Test

on:
push:
branches: ['*']
tags: ['!v*']
workflow_call: {}

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: orbit-online/upkg-install@v1
- name: Run ShellCheck
uses: ludeeus/action-shellcheck@master
env:
SHELLCHECK_OPTS: -x
with:
ignore_paths: .upkg
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.upkg
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 Orbit Online A/S

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# EFI Bootentry

Manage entries in the EFI bootmenu
88 changes: 88 additions & 0 deletions bin/efi-bootentry
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env bash
# shellcheck source-path=..
set -eo pipefail; shopt -s inherit_errexit
PKGROOT=$(realpath "$(dirname "$(realpath "${BASH_SOURCE[0]}")")/..")
source "$PKGROOT/.upkg/records.sh/records.sh"

main() {
DOC="efi-bootentry - Add and remove EFI boot entries
Usage:
efi-bootentry add FILEPATH LABEL
efi-bootentry remove FILEPATH
"
# docopt parser below, refresh this parser with `docopt.sh efi-bootentry`
# shellcheck disable=2016,2086,2317,1090,1091,2034
docopt() { local v='2.0.2'; source \
"$PKGROOT/.upkg/docopt-lib-v$v/docopt-lib.sh" "$v" || { ret=$?;printf -- "exit \
%d\n" "$ret";exit "$ret";};set -e;trimmed_doc=${DOC:0:121};usage=${DOC:48:73}
digest=d7247;options=();node_0(){ value FILEPATH a;};node_1(){ value LABEL a;}
node_2(){ switch add a:add;};node_3(){ switch remove a:remove;};node_4(){
sequence 2 0 1;};node_5(){ sequence 3 0;};node_6(){ choice 4 5;};cat <<<' \
docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2;printf "%s\n" \
"${DOC:48:73}" >&2;exit 1;}';local varnames=(FILEPATH LABEL add remove) varname
for varname in "${varnames[@]}"; do unset "var_$varname";done;parse 6 "$@"
local p=${DOCOPT_PREFIX:-''};for varname in "${varnames[@]}"; do unset \
"$p$varname";done;eval $p'FILEPATH=${var_FILEPATH:-};'$p'LABEL=${var_LABEL:-};'\
$p'add=${var_add:-false};'$p'remove=${var_remove:-false};';local docopt_i=1;[[ \
$BASH_VERSION =~ ^4.3 ]] && docopt_i=2;for ((;docopt_i>0;docopt_i--)); do for \
varname in "${varnames[@]}"; do declare -p "$p$varname";done;done;}
# docopt parser above, complete command for generating this parser is `docopt.sh --library='"$PKGROOT/.upkg/docopt-lib-v$v/docopt-lib.sh"' efi-bootentry`
eval "$(docopt "$@")"

"$PKGROOT/.upkg/.bin/checkdeps" efibootmgr lsblk findmnt

local efi_path
efi_path=$(dirname "$FILEPATH")
local efi_device
until efi_device=$(findmnt -o SOURCE -n "$efi_path"); do
efi_path=$(dirname "$efi_path")
if [[ $efi_path = / ]]; then
printf "Unable to find device path for '%s'\n" "$FILEPATH" >&2
return 1
fi
done
local efi_parttypename
efi_parttypename=$(lsblk -noparttypename "$efi_device")
if [[ $efi_parttypename != 'EFI System' ]]; then
printf "Expected the partition type of '%s' to be 'EFI System', not '%s'\n" "$efi_device" "$efi_parttypename" >&2
return 1
fi
local efi_partuuid
efi_partuuid=$(lsblk -nopartuuid "$efi_device")

local entry_path
entry_path=${FILEPATH#"$efi_path"}
entry_path=${entry_path//'/'/"\\"}

local bootnum
# shellcheck disable=SC2154
if $add; then
if bootnum=$(get_bootnum "$efi_partuuid" "$entry_path"); then
printf "'%s' is already present in the EFI bootmenu at index %s\n" "$FILEPATH" "$bootnum" >&2
else
efibootmgr -q -c -d "$efi_device" -L "$LABEL" -l "$entry_path"
printf "Added the EFI bootmenu for '%s'\n" "$FILEPATH" >&2
fi
elif $remove; then
local removed=false
while bootnum=$(get_bootnum "$efi_partuuid" "$entry_path"); do
efibootmgr -q -B -b "$bootnum"
printf "Removed EFI bootmenu entry at index %s\n" "$bootnum" >&2
removed=true
done
$removed || printf "'%s' is not present in the EFI bootmenu\n" "$FILEPATH" >&2
else
return 1
fi
}

get_bootnum() {
local efi_partuuid=$1 entry_path=$2 bootnum
# shellcheck disable=SC1003
bootnum=$(efibootmgr | grep -im1 "GPT,$efi_partuuid,.*)/File(${entry_path//"\\"/'\\'})$" | cut -f1 -d ' ') || return 1
bootnum=${bootnum#'Boot'}
bootnum=${bootnum%'*'}
printf "%s\n" "$bootnum"
}

main "$@"
18 changes: 18 additions & 0 deletions upkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "efi-bootentry",
"dependencies": [
{
"name": "docopt-lib-v2.0.2",
"tar": "https://github.com/andsens/docopt.sh/releases/download/v2.0.2/docopt-lib.sh.tar.gz",
"sha256": "d6997858e7f2470aa602fdd1e443d89b4e2084245b485e4b7924b0f388ec401e"
},
{
"tar": "https://github.com/orbit-online/checkdeps/releases/download/v2.0.0/checkdeps.tar.gz",
"sha256": "0b7ebb5e60163ccbd4966527f72c0e30f8cb183de6057ddad72b4e6b83ae1637"
},
{
"tar": "https://github.com/orbit-online/records.sh/releases/download/v1.0.2/records.sh.tar.gz",
"sha256": "201977ecc5fc9069d8eff12ba6adc9ce1286ba66c9aeee19184e26185cc6ef63"
}
]
}

0 comments on commit f5e6f58

Please sign in to comment.