From 2955b2b7fe9f6a1dca2cff798972f0a658131dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= Date: Fri, 12 Jul 2019 15:16:44 +0200 Subject: [PATCH] pve-efiboot-tool: add and use list file helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit in preparation of adding another file for custom kernels with the same semantics Signed-off-by: Fabian Grünbichler --- bin/pve-efiboot-tool | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/bin/pve-efiboot-tool b/bin/pve-efiboot-tool index abaaf00..31ba9c1 100755 --- a/bin/pve-efiboot-tool +++ b/bin/pve-efiboot-tool @@ -4,6 +4,35 @@ set -e . /usr/share/pve-kernel-helper/scripts/functions +_add_entry_to_list_file() { + file="$1" + entry="$2" + + if [ -e "$file" ]; then + cp "$file" "$file.new" + fi + echo "$entry" >> "$file.new" + sort -uo "$file.new" "$file.new" + mv "$file.new" "$file" +} + +_remove_entry_from_list_file() { + file="$1" + entry="$2" + + # guard against removing whole file by accident! + if [ -z "$entry" ]; then + echo "cannot remove empty entry from '$file'." + return + fi + + if [ -e "$file" ]; then + grep -vFx "$entry" "$file" > "$file.new" || true + mv "$file.new" "$file" + else + echo "'$file' does not exist.." + fi +} _get_partition_info() { if [ ! -e "$1" ]; then @@ -130,12 +159,7 @@ init() { umount "$part" echo "Adding '$part' to list of synced ESPs.." - if [ -e "$ESP_LIST" ]; then - cp "$ESP_LIST" "$ESP_LIST.new" - fi - echo "$UUID" >> "$ESP_LIST.new" - sort -uo "$ESP_LIST.new" "$ESP_LIST.new" - mv "$ESP_LIST.new" "$ESP_LIST" + _add_entry_to_list_file "$ESP_LIST" "$UUID" echo "Refreshing kernels and initrds.." refresh