Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit 1768c26

Browse files
committed
Create directories before copying or writing files
I accidentally did not notice this, because on my systems the directories were already created from my previous script. Also add a test case checking the individual commands and the setup command itself, so that does not happen again. Fixes #2
1 parent 943fe8e commit 1768c26

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

sicherboot

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,12 @@ install_kernel() {
129129
local image_dir="$BOOT_EFI_DIR/$relative_image_dir"
130130
local relative_image="$relative_image_dir/linux.efi"
131131
local image="$image_dir/linux.efi"
132-
local conf="$BOOT_EFI_DIR/loader/entries/$MACHINE_ID-$version.conf"
132+
local conf_dir="$BOOT_EFI_DIR/loader/entries"
133+
local conf="$conf_dir/$MACHINE_ID-$version.conf"
133134

135+
mkdir -p "$conf_dir"
136+
mkdir -p "$image_dir"
134137
_create_loader_entry "$conf" "$relative_image" "$version"
135-
[ -d "$image_dir" ] || mkdir -p "$image_dir"
136138
build_image "$BOOT_DIR/vmlinuz-$version" \
137139
"$BOOT_DIR/initrd.img-$version" \
138140
"$image"
@@ -220,8 +222,10 @@ ENROLL_KEYS_HELP="Usage: sicherboot enroll-keys
220222
"
221223
enroll_keys() {
222224
local relative_image="$MACHINE_ID/KeyTool.efi"
225+
local image_dir="$BOOT_EFI_DIR/$MACHINE_ID"
223226
local image="$BOOT_EFI_DIR/$relative_image"
224-
local conf="$BOOT_EFI_DIR/loader/entries/$MACHINE_ID-keytool.conf"
227+
local conf_dir="$BOOT_EFI_DIR/loader/entries"
228+
local conf="$conf_dir/$MACHINE_ID-keytool.conf"
225229
local key_dir_rel="Keys/$MACHINE_ID"
226230
local key_dir="$BOOT_EFI_DIR/$key_dir_rel"
227231

@@ -241,6 +245,9 @@ enroll_keys() {
241245
fi
242246
fi
243247

248+
mkdir -p "$image_dir"
249+
mkdir -p "$conf_dir"
250+
244251
cp "$KEYTOOL" "$image"
245252
sign_image "$image"
246253

tests/sicherboot.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Test configuration file
2+
BOOT_DIR=$PWD/tmp/
3+
BOOT_EFI_DIR=$PWD/tmp/efi
4+
KEY_HOME=$PWD/tmp/keys
5+
6+
# Hack: Specify a temporary machine-id (overrides os-release, as included later)
7+
MACHINE_ID=machine-id
8+
9+
# Hack: We need to export EFI_ARCH for use in the tests
10+
echo "EFI_ARCH=$EFI_ARCH" > $PWD/tmp/config

tests/test-setup

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/bin/sh -e
2+
set -e
3+
4+
cd "$(dirname "$(readlink -f "$0")")"
5+
6+
testsuccess() {
7+
printf "%-90s" "Testing $*..."
8+
printf "=====================================================\n" >> $PWD/tmp/out.log
9+
printf " Output of command $*\n" >> $PWD/tmp/out.log
10+
printf "=====================================================\n" >> $PWD/tmp/out.log
11+
if ! "$@" >>$PWD/tmp/out.log 2>&1; then
12+
printf "FAIL\n"
13+
printf "Output so far:\n" >&2
14+
cat $PWD/tmp/out.log
15+
exit 1
16+
else
17+
printf "PASS\n"
18+
fi
19+
}
20+
21+
export SICHERBOOT_CONFIGURATION_FILE_INTERNAL=$PWD/sicherboot.conf
22+
export PATH="$PWD/tmp:$PATH"
23+
24+
if test -e tmp; then
25+
rm -rf tmp
26+
fi
27+
28+
29+
mkdir tmp
30+
31+
######################### TEST KEY GENERATION ##############################
32+
testsuccess fakeroot ../sicherboot generate-keys
33+
34+
. tmp/config
35+
36+
testsuccess test -e tmp/keys/PK.auth
37+
testsuccess test -e tmp/keys/db.auth
38+
testsuccess test -e tmp/keys/KEK.auth
39+
40+
######################### TEST KEY ENROLLMENT ###############################
41+
42+
testsuccess fakeroot ../sicherboot enroll-keys
43+
44+
testsuccess sbverify --cert tmp/keys/db.crt tmp/efi/machine-id/KeyTool.efi
45+
testsuccess test -e tmp/efi/loader/entries/machine-id-keytool.conf
46+
47+
############################# TEST BOOTLOADER INSTALLATION ##################
48+
49+
# Mock bootctl
50+
cat >> $PWD/tmp/bootctl << EOF
51+
#!/bin/sh
52+
install -D /usr/lib/systemd/boot/efi/systemd-boot${EFI_ARCH}.efi $PWD/tmp/efi/EFI/systemd/systemd-boot${EFI_ARCH}.efi
53+
EOF
54+
55+
chmod +x $PWD/tmp/bootctl
56+
57+
testsuccess fakeroot ../sicherboot bootctl install
58+
testsuccess test -e tmp/efi/EFI/systemd/systemd-boot${EFI_ARCH}.efi
59+
testsuccess sbverify --cert tmp/keys/db.crt tmp/efi/EFI/systemd/systemd-boot${EFI_ARCH}.efi
60+
61+
62+
############################## TEST KERNEL INSTALLATION ######################
63+
cp /usr/lib/systemd/boot/efi/linux${EFI_ARCH}.efi.stub tmp/vmlinuz-kernel-version
64+
touch tmp/initrd.img-kernel-version
65+
66+
testsuccess fakeroot ../sicherboot install-kernel kernel-version
67+
testsuccess test -e tmp/efi/machine-id/kernel-version/linux.efi
68+
testsuccess test -e tmp/efi/loader/entries/machine-id-kernel-version.conf
69+
70+
71+
testsuccess sbverify --cert tmp/keys/db.crt tmp/efi/machine-id/kernel-version/linux.efi
72+
73+
74+
############################# TEST THE REAL SETUP COMMAND ######################
75+
76+
# Mock uname
77+
cat >> $PWD/tmp/uname << EOF
78+
#!/bin/sh
79+
echo kernel-version
80+
EOF
81+
82+
chmod +x $PWD/tmp/uname
83+
84+
85+
rm -r tmp/efi
86+
rm -r tmp/keys
87+
88+
testsuccess test ! -e tmp/efi
89+
testsuccess test ! -e tmp/keys
90+
91+
testsuccess sh -c 'yes | fakeroot ../sicherboot setup'
92+
93+
testsuccess sbverify --cert tmp/keys/db.crt tmp/efi/EFI/systemd/systemd-boot${EFI_ARCH}.efi
94+
testsuccess sbverify --cert tmp/keys/db.crt tmp/efi/machine-id/kernel-version/linux.efi
95+
testsuccess test -e tmp/efi/loader/entries/machine-id-kernel-version.conf

0 commit comments

Comments
 (0)