Skip to content

Commit 183845d

Browse files
authored
Merge pull request #18 from Zile995/dev
hooks: remove: Switch to PreTransaction
2 parents 7bab80e + 702c8d3 commit 183845d

File tree

10 files changed

+157
-116
lines changed

10 files changed

+157
-116
lines changed

booster-um

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ main() {
4242
;;
4343
-p)
4444
# This argument is used in libalpm remove hook
45-
# Remove UKI files and other leftovers for removed kernels
46-
_post_transaction_remove || exit 0
45+
# Remove orphaned EFI entries
46+
_remove_orphaned_efi_entries
4747
;;
4848
-C)
4949
# Cleanup
@@ -65,17 +65,9 @@ main() {
6565
# The initramfs will be generated by the booster hook script
6666
# UKI file will not be signed if it already exists in the sbctl database
6767
shift
68-
always_sign=0
6968
_generate_fallback_initramfs "$1" "$2"
7069
_generate_uki "$1" "$2"
7170
;;
72-
-U)
73-
# It will always regenerate initramfs
74-
# This argument is used in libalpm hook
75-
# Regenerate UKI files for all installed kernels
76-
always_sign=0
77-
_regenerate_all
78-
;;
7971
*)
8072
echo -e "${R}==>${NC} Unknown parameter ${R}$arg${NC} passed." >&2
8173
show_help && exit 1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Trigger]
2+
Type = Path
3+
Operation = Remove
4+
Target = usr/bin/booster-um
5+
Target = usr/lib/modules/*/vmlinuz
6+
7+
[Action]
8+
Description = Removing UKI files...
9+
When = PreTransaction
10+
Exec = /usr/share/libalpm/scripts/booster-um-remove
11+
NeedsTargets

libalpm/hooks/91-booster-um-remove.hook renamed to libalpm/hooks/91-booster-um-efi-remove.hook

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Operation = Remove
44
Target = usr/lib/modules/*/vmlinuz
55

66
[Action]
7-
Description = Removing UKI leftovers...
7+
Description = Removing orphaned EFI entries...
88
When = PostTransaction
99
Exec = /usr/bin/booster-um -p
10-
NeedsTargets
10+
NeedsTargets

libalpm/scripts/booster-um-install

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ if [ "${sign_all}" -eq "1" ]; then
3939
exit
4040
fi
4141

42+
# Sbctl hook should sign the UKI file if it already exists in database
43+
export ALWAYS_SIGN=1
44+
4245
# Generate UKI files for all kernels
4346
if [ "${all}" -eq "1" ]; then
44-
booster-um -U
47+
booster-um -G
4548
exit
4649
fi
4750

libalpm/scripts/booster-um-remove

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
kernels=()
6+
7+
while read -r line; do
8+
case "$line" in
9+
usr/bin/booster-um)
10+
booster-um -R
11+
exit
12+
;;
13+
usr/lib/modules/*/vmlinuz)
14+
kernels+=("/${line%/vmlinuz}")
15+
;;
16+
esac
17+
done
18+
19+
for kernel in "${kernels[@]}"; do
20+
if ! pacman -Qqo -- "${kernel}/pkgbase" > /dev/null 2>&1; then
21+
# if pkgbase does not belong to any package then skip this kernel
22+
continue
23+
fi
24+
read -r pkgbase < "${kernel}/pkgbase"
25+
26+
# Remove the UKI file
27+
export REMOVE_EFI=1; booster-um -r "$pkgbase"
28+
done

scripts/config

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
set -e
44

55
# Disable sbsign by default
6-
declare -g sbsign=0
6+
declare -g SBSIGN=1
77

88
# A value of 0 means that the booster-um config is valid
9-
declare -g config_valid=0
9+
declare -g CONFIG_VALID=0
1010

1111
# Array with initramfs configs
1212
declare -A initramfs_config
@@ -17,13 +17,13 @@ declare -g BOOSTER_CONFIG=/etc/booster.yaml
1717

1818
# Checks if booster-um.yaml config is valid
1919
_check_config() {
20-
[[ ! -e $CONFIG ]] && config_valid=$((1-$?)) && return
20+
[[ ! -e $CONFIG ]] && CONFIG_VALID=$((1-$?)) && return
2121

2222
if [[ ! -s $CONFIG ]]; then
23-
config_valid=1
23+
CONFIG_VALID=1
2424
else
25-
config_valid=$(yq --exit-status "$CONFIG" &>/dev/null; echo "$?")
26-
if [[ $config_valid -ne 0 ]]; then
25+
CONFIG_VALID=$(yq --exit-status "$CONFIG" &>/dev/null; echo "$?")
26+
if [[ $CONFIG_VALID -ne 0 ]]; then
2727
echo -e "${R}==>${NC} ${Y}$CONFIG${NC} config ${R}is not valid${NC}. ${Y}Default${NC} settings are used."
2828
fi
2929
fi
@@ -32,53 +32,53 @@ _check_config() {
3232
}
3333

3434
_set_signing_tool() {
35-
[[ $config_valid -eq 1 ]] && return
36-
if _command_exists sbsign && yq '.sbsign == true' "$CONFIG" | grep -Fqwx -- 'true'; then sbsign=1; fi
35+
[[ $CONFIG_VALID -eq 1 ]] && return
36+
if _command_exists sbsign && yq '.sbsign == true' "$CONFIG" | grep -Fqwx -- 'true'; then SBSIGN=0; fi
3737
}
3838

3939
# Checks if the creation of universal images is enabled
4040
_is_universal() {
41-
[[ $config_valid -eq 1 ]] && return 1
41+
[[ $CONFIG_VALID -eq 1 ]] && return 1
4242
yq '.universal == true' "$BOOSTER_CONFIG" | grep -Fqwx -- 'true'
4343
}
4444

4545
# Checks if the booster-um should always generate a fallback UKI
4646
_should_generate_fallback() {
47-
[[ $config_valid -eq 1 ]] && return 1
47+
[[ $CONFIG_VALID -eq 1 ]] && return 1
4848
if yq '.generate_fallback == true' "$CONFIG" | grep -Fqwx -- 'true' && ! _is_universal; then return; fi
4949
false
5050
}
5151

5252
# Checks if the booster-um should always sign generated UKI
5353
_should_sign_uki() {
54-
[[ $config_valid -eq 1 ]] && return
54+
[[ $CONFIG_VALID -eq 1 ]] && return
5555
if ! yq '.sign_uki == false' "$CONFIG" | grep -Fqwx -- 'true'; then return; fi
5656
false
5757
}
5858

5959
# Checks if the booster-um should remove leftovers (vmlinuz and initramfs)
6060
_should_remove_leftovers() {
61-
[[ $config_valid -eq 1 ]] && return
61+
[[ $CONFIG_VALID -eq 1 ]] && return
6262
if ! yq '.remove_leftovers == false' "$CONFIG" | grep -Fqwx -- 'true'; then return; fi
6363
false
6464
}
6565

6666
# Checks if the booster-um should create EFI entry
6767
_should_create_efi_entry() {
68-
[[ $config_valid -eq 1 ]] && return 1
68+
[[ $CONFIG_VALID -eq 1 ]] && return 1
6969
if yq '.efistub == true' "$CONFIG" | grep -Fqwx -- 'true'; then return; fi
7070
false
7171
}
7272

7373
# Checks if the booster-um should create EFI entry
7474
_should_preserve_boot_order() {
75-
[[ $config_valid -eq 1 ]] && return
75+
[[ $CONFIG_VALID -eq 1 ]] && return
7676
if ! yq '.efistub_config.preserve_boot_order == false' "$CONFIG" | grep -Fqwx -- 'true'; then return; fi
7777
false
7878
}
7979

8080
_cmdline_per_kernel() {
81-
[[ $config_valid -eq 1 ]] && return 1
81+
[[ $CONFIG_VALID -eq 1 ]] && return 1
8282
if yq '.cmdline_per_kernel == true' "$CONFIG" | grep -Fqwx -- 'true'; then return; fi
8383
false
8484
}
@@ -118,7 +118,7 @@ _get_splash_path() {
118118
local default_splash=/usr/share/systemd/bootctl/splash-arch.bmp
119119

120120
# If the config file doesn't exist or is not valid, use the default arch linux splash
121-
if [[ $config_valid -eq 1 ]] && _is_bmp "$default_splash"; then
121+
if [[ $CONFIG_VALID -eq 1 ]] && _is_bmp "$default_splash"; then
122122
echo "$default_splash" && return
123123
fi
124124

scripts/efistub

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ declare -g ESP
99
# EFI dir path of the directory where the UKI files are located. Example: esp/EFI/Linux
1010
declare -g EFI_DIR
1111

12+
# ESP partition UUID
13+
declare -g ESP_PARTUUID
14+
1215
# EFI System Partition mount point. Examples: /boot, /efi, /boot/efi
1316
declare -g ESP_MOUNT
1417

@@ -44,6 +47,8 @@ _set_efi_path() {
4447
ESP="$(findmnt --real -nreo source "${ESP_MOUNT}")"
4548
fi
4649

50+
ESP_PARTUUID="$(partx -g -o UUID "${ESP}")"
51+
4752
# Trigger any potential automounts
4853
stat -- /boot /efi "$EFI_DIR" &>/dev/null || :
4954
}
@@ -52,8 +57,7 @@ _set_efi_path() {
5257
_efi_entry_exists() {
5358
local loader_path esp_partuuid
5459
loader_path="$1"
55-
esp_partuuid="$(partx -g -o UUID "${ESP}")"
56-
efibootmgr | grep -w "$esp_partuuid" | grep -Fwq -e "${loader_path}" -e "${loader_path^^}"
60+
efibootmgr | grep -w "$ESP_PARTUUID" | grep -Fwq -e "${loader_path}" -e "${loader_path^^}"
5761
}
5862

5963
# Extracts the pkgbase from known UKI file path
@@ -68,7 +72,7 @@ _get_pkgbase_from_path() {
6872
}
6973

7074
# Formats the efi path to be readable by efibootmgr
71-
_get_formatted_efi_path() {
75+
_get_loader_path() {
7276
local efi
7377
efi="$1"
7478

@@ -79,6 +83,25 @@ _get_formatted_efi_path() {
7983
echo "$efi_path"
8084
}
8185

86+
# Checks if loader file exists
87+
_loader_file_exists() {
88+
local loader_path uki_name uki_path
89+
loader_path="$1"
90+
91+
# Remove string prefix, before last \ char
92+
# \EFI\LINUX\ARCH-LINUX.EFI -> ARCH-LINUX.EFI
93+
uki_name="${loader_path##*\\}"
94+
95+
# Lowercase name
96+
uki_name="${uki_name,,}"
97+
98+
# Set the UKI file path
99+
uki_path="${EFI_DIR}/${uki_name}"
100+
101+
# Check if the UKI file exists
102+
[[ -f $uki_path ]]
103+
}
104+
82105
# Returns the EFI entry label
83106
_get_label() {
84107
local pkgbase="$1"
@@ -115,7 +138,7 @@ _create_efi_entry() {
115138
uki="$1"
116139

117140
# Set loader path with backslashes
118-
loader_path="$(_get_formatted_efi_path "$uki")"
141+
loader_path="$(_get_loader_path "$uki")"
119142

120143
# Return if the entry exists
121144
if _efi_entry_exists "$loader_path"; then return; fi
@@ -160,7 +183,7 @@ _remove_efi_entry() {
160183
uki="$1"
161184

162185
# Set loader path with backslashes
163-
loader_path="$(_get_formatted_efi_path "$uki")"
186+
loader_path="$(_get_loader_path "$uki")"
164187

165188
# Find all known labels
166189
readarray -t labels < <(efibootmgr | \
@@ -181,3 +204,44 @@ _remove_efi_entry() {
181204
efibootmgr -b "$boot_num" -B > /dev/null
182205
done
183206
}
207+
208+
# Removes orphaned EFI entries
209+
_remove_orphaned_efi_entries() {
210+
# Return if efibootmgr doesn't exist
211+
if ! _command_exists efibootmgr; then exit; fi
212+
213+
local search="${EFI_DIR#$ESP_MOUNT}/arch-"
214+
search="${search//\//\\}"
215+
216+
# Get all uniqe loader paths
217+
readarray -t loader_paths < <(efibootmgr | \
218+
grep -w "$ESP_PARTUUID" | grep -Fi "$search" | \
219+
sed 's/.*(\(.*\))/\1/' | uniq -u)
220+
221+
for loader_path in "${loader_paths[@]}"; do
222+
if ! _loader_file_exists "$loader_path"; then
223+
# Get the boot number
224+
readarray -t boot_nums < <(efibootmgr | \
225+
grep -Fw "$loader_path" | \
226+
awk '{ print $1 }' | \
227+
sed -r 's/0+([0-9]+)/\1/g;s/Boot//;s/\*//')
228+
229+
# Get the labels
230+
readarray -t labels < <(efibootmgr | \
231+
grep -Fw "$loader_path" | \
232+
sed 's/.*\*[[:blank:]]\(.*\)[[:blank:]]\(.*\)/\1/')
233+
234+
for label in "${labels[@]}"; do
235+
echo -e "${Y} ->${NC} Removing EFI entry ${Y}${label}${NC}..."
236+
done
237+
238+
# Remove the EFI entries
239+
for boot_num in "${boot_nums[@]}"; do
240+
efibootmgr -b "$boot_num" -B > /dev/null
241+
done
242+
fi
243+
done
244+
245+
# One shot function, just exit
246+
exit
247+
}

scripts/generate

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ _generate_uki() {
5151
local uki_path="${EFI_DIR}/arch-${pkgbase}"
5252

5353
local -a sign_args=()
54-
if [[ $sbsign -eq 1 ]] && _should_sign_uki; then
54+
if [[ $SBSIGN -eq 0 ]] && _should_sign_uki; then
5555
local pcr_banks="$(_get_sbsign_property pcr_banks)"
5656
local pcr_private_key="$(_get_sbsign_property pcr_private_key)"
5757
local pcr_public_key="$(_get_sbsign_property pcr_public_key)"
@@ -92,7 +92,7 @@ _generate_uki() {
9292
done
9393

9494
# Remove the leftovers (vmlinuz, booster initramfs and other files)
95-
_remove_leftovers "$pkgbase" 1
95+
_remove_leftovers "$pkgbase"
9696
}
9797

9898
# Generates UKI file for specified kernel package name
@@ -157,7 +157,7 @@ _generate_initramfs() {
157157
# Generate initramfs
158158
if ! booster build --force --kernel-version "$uname" "$initramfs"; then
159159
echo -e "${R}==>${NC} Unable to generate initramfs for ${R}${pkgbase}${NC}..."
160-
_remove_leftovers "$pkgbase" 1
160+
_remove_leftovers "$pkgbase"
161161
return 1
162162
fi
163163

0 commit comments

Comments
 (0)