refactor: ♻️ start another rewrite with much smaller scope
This commit is contained in:
parent
333b8d06fe
commit
4e7e835bd9
@ -1,12 +0,0 @@
|
||||
[Trigger]
|
||||
Operation = Upgrade
|
||||
Operation = Install
|
||||
Operation = Remove
|
||||
Type = Package
|
||||
Target = *
|
||||
|
||||
[Action]
|
||||
Description = Performing pre UKI creation...
|
||||
When = PreTransaction
|
||||
Exec = /usr/local/bin/snap-pac-uki-create.fish
|
||||
AbortOnFail
|
@ -1,11 +0,0 @@
|
||||
[Trigger]
|
||||
Operation = Upgrade
|
||||
Operation = Install
|
||||
Operation = Remove
|
||||
Type = Package
|
||||
Target = *
|
||||
|
||||
[Action]
|
||||
Description = Performing post UKI creation...
|
||||
When = PostTransaction
|
||||
Exec = /usr/local/bin/snap-pac-uki-create.fish
|
@ -1,117 +0,0 @@
|
||||
#!/bin/fish
|
||||
|
||||
function error -d "prints an error and terminates the program" -a error
|
||||
echo $error
|
||||
|
||||
exit 1
|
||||
end
|
||||
|
||||
function checkdeps -d "check for depenencies"
|
||||
set -l deps "jq" "snapper" "efibootmgr" "rg"
|
||||
|
||||
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 EFI_PATH '\EFI\Linux'
|
||||
set -g SNAPPER_CONFIG "root"
|
||||
set -g SNAPSHOT_SUBVOL "@snapshots"
|
||||
set -g ROOT_SUBVOL "@"
|
||||
|
||||
# load file
|
||||
set -l cfg (jq -c <$cfg)
|
||||
|
||||
# parse config
|
||||
set -l cfgs "build_path" "efi_dir" "efi_dev" "efi_part" "efi_path" "kernel_cmd" "fallback_cmd" "snapper_config" "snapshot_subvol" "root_subvol"
|
||||
for c in $cfgs
|
||||
echo $cfg | jq -e ".$c" >/dev/null
|
||||
|
||||
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" "efi_dev" "efi_part"
|
||||
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")
|
||||
end
|
||||
|
||||
function create_uki -d "creates a new uki"
|
||||
# prepare directory
|
||||
set -l bp "$BUILD_PATH/$SNAPSHOT_ID"
|
||||
mkdir -p $bp
|
||||
rm -rf "$bp/*"
|
||||
|
||||
# os-release
|
||||
cp /usr/lib/os-release "$bp/os-release"
|
||||
sed "s/BUILD_ID=.*/BUILD_ID=fallback/" </usr/lib/os-release | sed "s/PRETTY_NAME=\"\(.*\)\"/PRETTY_NAME=\"\1 (Fallback)\"/" >"$bp/os-release-fallback"
|
||||
sed "s/BUILD_ID=.*/BUILD_ID=\"snapshot $SNAPSHOT_ID\"/" </usr/lib/os-release | sed "s/PRETTY_NAME=\"\(.*\)\"/PRETTY_NAME=\"\1 (Snapshot $SNAPSHOT_ID)\"/" >"$bp/os-release-snapshot"
|
||||
|
||||
# kernel cmd
|
||||
echo $KERNEL_CMD | sed "s/SNAPSHOT/$ROOT_SUBVOL/" >"$bp/kernel-cmd"
|
||||
echo $FALLBACK_CMD | sed "s/SNAPSHOT/$ROOT_SUBVOL/" >"$bp/kernel-cmd-fallback"
|
||||
echo $KERNEL_CMD | sed "s/SNAPSHOT/$SNAPSHOT_SUBVOL\\/$SNAPSHOT_ID\\/snapshot/" >"$bp/kernel-cmd-snapshot"
|
||||
|
||||
# create uki
|
||||
objcopy \
|
||||
--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" "$bp/arch-linux.efi"
|
||||
objcopy \
|
||||
--add-section .osrel="$bp/os-release-fallback" --change-section-vma .osrel=0x20000 \
|
||||
--add-section .cmdline="$bp/kernel-cmd-fallback" --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-fallback.img" --change-section-vma .initrd=0x3000000 \
|
||||
"/usr/lib/systemd/boot/efi/linuxx64.efi.stub" "$bp/arch-linux-fallback.efi"
|
||||
objcopy \
|
||||
--add-section .osrel="$bp/os-release-snapshot" --change-section-vma .osrel=0x20000 \
|
||||
--add-section .cmdline="$bp/kernel-cmd-snapshot" --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" "$bp/arch-linux-$SNAPSHOT_ID.efi"
|
||||
end
|
||||
|
||||
function add_efi_entry -d "adds a boot entry to efi"
|
||||
efibootmgr -C -d $EFI_DEV -p $EFI_PART -l "$EFI_PATH\arch-linux-$SNAPSHOT_ID.efi" -L (rg "PRETTY_NAME" "$BUILD_PATH/$SNAPSHOT_ID/os-release-snapshot" | sed "s/PRETTY_NAME=\"\(.*\)\"/\1/") -q
|
||||
end
|
||||
|
||||
function main -d "the main function"
|
||||
checkdeps
|
||||
load_config "/etc/snap-pac-uki.json"
|
||||
|
||||
get_last_snapshot
|
||||
create_uki
|
||||
|
||||
cp $BUILD_PATH/$SNAPSHOT_ID/*.efi "$EFI_DIR"
|
||||
add_efi_entry
|
||||
|
||||
exit 0
|
||||
end
|
||||
|
||||
main
|
119
src/snap-pac-uki.fish
Executable file
119
src/snap-pac-uki.fish
Executable file
@ -0,0 +1,119 @@
|
||||
#!/bin/fish
|
||||
|
||||
function error -d "prints an error and exits" -a error
|
||||
echo $error 1>&2
|
||||
exit 1
|
||||
end
|
||||
|
||||
function check_deps -d "checks if all dependencies are available"
|
||||
set -l dependencies \
|
||||
"efibootmgr" \
|
||||
"jq" \
|
||||
"sed" \
|
||||
"snapper" \
|
||||
|
||||
for dep in $dependencies
|
||||
which $dep >/dev/null 2>/dev/null
|
||||
|
||||
if test $status != "0"
|
||||
error "missing depenency: $dep"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function config -d "sets some configuration variables"
|
||||
set -g CFG_BUILD_DIR "/tmp/snap-pac-uki"
|
||||
set -g CFG_DEFAULT_SUBVOL "@"
|
||||
set -g CFG_EFI_DEV "/dev/nvme0n1"
|
||||
set -g CFG_EFI_PATH "\EFI\Linux"
|
||||
set -g CFG_EFI_PART "1"
|
||||
set -g CFG_SNAPSHOT_CONFIG "root"
|
||||
set -g CFG_SNAPSHOT_PATH "/.snapshots"
|
||||
set -g CFG_SNAPSHOT_SUBVOL "@snapshots"
|
||||
set -g CFG_UKI_DIR "/efi/EFI/Linux"
|
||||
end
|
||||
|
||||
function find_tasks -d "finds out what the program needs to do"
|
||||
set -ge TASK_UKI
|
||||
|
||||
for id in (snapper -c $CFG_SNAPSHOT_CONFIG --jsonout ls | jq ".$CFG_SNAPSHOT_CONFIG [] | .number")
|
||||
if test "$id" = "0"
|
||||
continue
|
||||
end
|
||||
|
||||
if test -f "$CFG_UKI_DIR/arch-linux-$id.efi"
|
||||
continue
|
||||
end
|
||||
|
||||
set -ga TASK_UKI $id
|
||||
end
|
||||
end
|
||||
|
||||
function create_uki -d "creates a new uki" -a variant id
|
||||
switch $variant
|
||||
case "rolling"
|
||||
set bd "$CFG_BUILD_DIR/current"
|
||||
case "fallback"
|
||||
set bd "$CFG_BUILD_DIR/current"
|
||||
set fallback "-fallback"
|
||||
case "snapshot"
|
||||
set bd "$CFG_BUILD_DIR/$id"
|
||||
set snid "-$id"
|
||||
set prefix "$CFG_SNAPSHOT_PATH/$id/snapshot"
|
||||
case "*"
|
||||
error "unknown uki variant: $variant"
|
||||
end
|
||||
|
||||
objcopy \
|
||||
--add-section .osrel="$bd/os-release$fallback" --change-section-vma .osrel=0x20000 \
|
||||
--add-section .cmdline="$bd/kernel-cmd$fallback" --change-section-vma .cmdline=0x30000 \
|
||||
--add-section .splash="$prefix/usr/share/systemd/bootctl/splash-arch.bmp" --change-section-vma .splash=0x40000 \
|
||||
--add-section .linux="$prefix/boot/vmlinuz-linux" --change-section-vma .linux=0x2000000 \
|
||||
--add-section .initrd="$prefix/boot/initramfs-linux$fallback.img" --change-section-vma .initrd=0x3000000 \
|
||||
"$prefix/usr/lib/systemd/boot/efi/linuxx64.efi.stub" "$bp/arch-linux$fallback$snid.efi"
|
||||
end
|
||||
|
||||
function add_efi_entry -d "adds an efi boot entry for a snapshot uki" -a id
|
||||
efibootmgr -q -C -d $CFG_EFI_DEV -p $CFG_EFI_PART \
|
||||
-l "$EFI_PATH\arch-linux-$id.efi" \
|
||||
-L "Arch Linux (Snapshot $id)"
|
||||
end
|
||||
|
||||
function create_snapshot_uki -d "creates an uki for a snapshot" -a id
|
||||
# create build directory
|
||||
set bd "$CFG_BUILD_DIR/$id"
|
||||
if test -f $bd
|
||||
error "build dir for snapshot $id is a file"
|
||||
else if test -d $bd
|
||||
rm -rf "$bd/*"
|
||||
else
|
||||
mkdir $bd
|
||||
end
|
||||
|
||||
# prepare files
|
||||
sed "s/BUILD_ID=.*/BUILD_ID=\"snapshot $id\"/;s/PRETTY_NAME=\"\(.*\)\"/PRETTY_NAME=\"\1 (Snapshot $id)\"" <"$CFG_SNAPSHOT_PATH/$id/snapshot/usr/lib/os-release" >"$bd/os-release"
|
||||
sed "s/SNAPSHOT/$CFG_SNAPSHOT_SUBVOL/" <"$CFG_SNAPSHOT_PATH/$id/snapshot/etc/snap-pac-uki/kernel-cmd" >"$bd/kernel-cmd"
|
||||
|
||||
# create uki
|
||||
create_uki snapshot $id
|
||||
add_efi_entry $id
|
||||
end
|
||||
|
||||
function main
|
||||
# prepare for execution
|
||||
check_deps
|
||||
config
|
||||
|
||||
switch $argv[1]
|
||||
case "snapshots"
|
||||
find_tasks
|
||||
for id in $TASK_UKI
|
||||
create_snapshot_uki $id
|
||||
end
|
||||
|
||||
case "*"
|
||||
error "unknown command"
|
||||
end
|
||||
end
|
||||
|
||||
main $argv
|
Loading…
Reference in New Issue
Block a user