refactor: ♻️ start rewrite

BREAKING CHANGE: everything
This commit is contained in:
Adrian Wannenmacher 2021-02-10 21:32:05 +01:00
parent 82634e5853
commit 7cba17357b
Signed by: tfld
GPG Key ID: 19D986ECB1E492D5
8 changed files with 90 additions and 105 deletions

View File

@ -1,9 +1 @@
# snap-pac-uki
Pacman hooks creating unified kernel images for snapshots created by snap-pac.
## Limits
- only works on the "root" snap-pac config
- puts all UKIs into `/boot/EFI/Linux/`
- kernel parameters are extracted from a file called `/boot/loader/entries/00-arch.conf`
- expects that the default subvolume is mounted via kernel command line
- expects that the default subvolume is called `@`

View File

@ -1,12 +0,0 @@
[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = *
[Action]
Description = Performing pre UKI creation...
When = PreTransaction
Exec = /usr/share/libalpm/scripts/snap-pac-uki_create.fish
AbortOnFail

View File

@ -1,11 +0,0 @@
[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = *
[Action]
Description = Performing post UKI creation...
When = PostTransaction
Exec = /usr/share/libalpm/scripts/snap-pac-uki_create.fish

View File

@ -1,11 +0,0 @@
[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = *
[Action]
Description = Performing UKI cleanup...
When = PostTransaction
Exec = /usr/share/libalpm/scripts/snap-pac-uki_cleanup.fish

View File

@ -1,4 +0,0 @@
#!/bin/fish
cp ./hooks/* /usr/share/libalpm/hooks/
cp ./scripts/* /usr/share/libalpm/scripts/

View File

@ -1,17 +0,0 @@
#!/bin/fish
# get snapshots
set -l config "root"
set -l snapshot_ids (snapper -c $config --jsonout ls | jq ".$config | .[] | .number")
# get current ukis
set -l ukis (ls /boot/EFI/Linux/ | grep '^snap-pac-uki-[1-9][0-9]*\.efi$' | grep -o "[1-9][0-9]*")
for x in $ukis
if not contains $x $snapshot_ids
set -l path "/boot/EFI/Linux/snap-pac-uki-$x.efi"
rm $path
echo "==> removed UKI: $path"
end
end

View File

@ -1,46 +1,103 @@
#!/bin/fish
# get id and type of last snapshot
set -l config "root"
set -l snapshot (snapper -c $config --jsonout ls | jq -c ".$config | last")
set -l id (echo $snapshot | jq ".number")
function error -d "prints an error and terminates the program" -a error
echo $error
if not contains (echo $snapshot | jq -r ".type") "pre" "post"
echo "==> last snapshot type is neither 'pre' nor 'post'; no UKI creation"
exit
exit 1
end
function checkdeps -d "check for depenencies"
set -l deps "jq" "snapper"
# check if UKI already exists
set -l ukis (ls /boot/EFI/Linux/ | grep '^snap-pac-uki-[1-9][0-9]*\.efi$' | grep -o "[1-9][0-9]*")
if contains $id $ukis
echo "==> UKI already exists; no UKI creation"
exit
for dep in $deps
which $dep >/dev/null 2>/dev/null
if test $status != "0"
error "didn't find depenecy: $dep"
end
end
end
function load_config -d "loads the config from a config file" -a cfg
# set default values
set -g BUILD_PATH "/tmp/snap-pac-uki"
set -g EFI_DIR "/efi/EFI/Linux/"
set -g SNAPPER_CONFIG "root"
set -g SNAPSHOT_SUBVOL "@snapshots"
# prepare uki creation
set -l buildpath "/tmp/snap-pac-uki/$id"
mkdir -p $buildpath
rm -rf "$buildpath/*"
cd $buildpath
# load file
set -l cfg (jq -c <$cfg)
sed "s/BUILD_ID=.*/BUILD_ID=\"Snapshot $id\"/" </usr/lib/os-release >./os-release
# parse config
set -l cfgs "build_path" "efi_dir" "kernel_cmd" "fallback_cmd" "snapper_config" "snapshot_subvol"
for c in $cfgs
echo $cfg | jq -e ".$c" >/dev/null
grep options /boot/loader/entries/00-arch.conf | \
string sub -s 9 | \
sed ':a;N;$!ba;s/\n/ /g' | \
sed "s/subvol=@/subvol=@snapshots\\/$id\\/snapshot/" >./kernel-parameters
switch $status
case "0"
set -g (echo $c | sed "s/./\U&/g") (echo $cfg | jq -r ".$c")
case "4"
error "failed to parse option: $c"
end
end
# check for mandatory options
set -l mandatory_cfgs "kernel_cmd" "fallback_cmd"
for mc in $mandatory_cfgs
set -gq (echo $mc | sed "s/./\U&/g")
if test $status != 0
error "missing config parameter: $mc"
end
end
end
function get_last_snapshot -d "reads the last snapper snapshot id"
set -g SNAPSHOT_ID (snapper -c $SNAPPER_CONFIG --jsonout ls | jq ".$SNAPPER_CONFIG | last | .number")
echo $SNAPSHOT_ID
end
function create_uki -d "creates a new uki" -a fallback
# prepare directory
set -l bp "$BUILD_PATH/$SNAPSHOT_ID"
mkdir -p $bp
rm -rf "$bp/*"
# os-release
sed "s/BUILD_ID=.*/BUILD_ID=\"Snapshot $SNAPSHOT_ID\"/" </usr/lib/os-release >"$bp/os-release"
# kernel cmd
echo $KERNEL_CMD | sed "s/SNAPSHOT/$SNAPSHOT_SUBVOL\\/$SNAPSHOT_ID\\/snapshot/" >"$bp/kernel-cmd"
# create uki
set -l ukipath "/boot/EFI/Linux/snap-pac-uki-$id.efi"
objcopy \
--add-section .osrel="./os-release" --change-section-vma .osrel=0x20000 \
--add-section .cmdline="./kernel-parameters" --change-section-vma .cmdline=0x30000 \
--add-section .osrel="$bp/os-release" --change-section-vma .osrel=0x20000 \
--add-section .cmdline="$bp/kernel-cmd" --change-section-vma .cmdline=0x30000 \
--add-section .splash="/usr/share/systemd/bootctl/splash-arch.bmp" --change-section-vma .splash=0x40000 \
--add-section .linux="/boot/vmlinuz-linux" --change-section-vma .linux=0x2000000 \
--add-section .initrd="/boot/initramfs-linux.img" --change-section-vma .initrd=0x3000000 \
"/usr/lib/systemd/boot/efi/linuxx64.efi.stub" $ukipath
echo "==> created UKI: $ukipath"
--add-section .initrd="/boot/initramfs-linux$fallback.img" --change-section-vma .initrd=0x3000000 \
"/usr/lib/systemd/boot/efi/linuxx64.efi.stub" "$bp/arch-linux-$SNAPSHOT_ID$fallback.efi"
end
function copy_current -d "copies new ukis to efi"
# copy to "current"
cp "$BUILD_PATH/$SNAPSHOT_ID/arch-linux-$SNAPSHOT_ID.efi" "$EFI_DIR/arch-linux-current.efi"
cp "$BUILD_PATH/$SNAPSHOT_ID/arch-linux-$SNAPSHOT_ID-fallback.efi" "$EFI_DIR/arch-linux-fallback.efi"
# copy to "snapshots"
cp "$BUILD_PATH/$SNAPSHOT_ID/arch-linux-$SNAPSHOT_ID.efi" $EFI_DIR
end
function main -d "the main function"
checkdeps
load_config "/etc/snap-pac-uki.json"
get_last_snapshot
create_uki
create_uki "-fallback"
copy_current
exit 0
end
main

View File

@ -1,9 +0,0 @@
#!/bin/fish
for x in (ls ./hooks)
rm "/usr/share/libalpm/hooks/$x"
end
for x in (ls ./scripts)
rm "/usr/share/libalpm/scripts/$x"
end