Skip to content

Commit

Permalink
Merge pull request #34 from sysrich/fixtabs
Browse files Browse the repository at this point in the history
Fix indentations
  • Loading branch information
sysrich authored Jun 19, 2024
2 parents 48f0be2 + f4202e2 commit c53a8b4
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 366 deletions.
28 changes: 14 additions & 14 deletions usr/bin/tik
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ log "[START] $0"

# Check for debug mode
if [[ $1 == "--debug" ]]; then
debug=1
debug=1
fi

# Read configuration files, /usr first, then /etc
Expand All @@ -30,22 +30,22 @@ fi

# Check essential paths exist
if [ ! -d "${TIK_IMG_DIR}" ]; then
error "${TIK_IMG_DIR} does not exist"
error "${TIK_IMG_DIR} does not exist"
fi

cleanup() {
retval=$?
log "[STOP][${retval}] $0"
if [ "${debug}" == "1" ]; then
zenity --timeout 5 --info --no-wrap --text="<b>Test Succeeded:</b>\n\nHave a nice day!"
elif [ "${retval}" == "0" ]; then
zenity --timeout 5 --info --no-wrap --title="Installation Complete!" --text="${TIK_OS_NAME} has been installed.\n\n<b>System is rebooting</b>"
systemctl reboot
else
zenity --error --no-wrap --title="Installation Failed" --text="Please file a bug report at <tt>${TIK_BUG_URL}</tt>\n\nPlease include the <tt>tik.log</tt> file\nIt can be found on the IGNITION partition on this USB Stick\n\n<b>System is shutting down</b>"
cp -a ${tik_log} /ignition
systemctl poweroff
fi
retval=$?
log "[STOP][${retval}] $0"
if [ "${debug}" == "1" ]; then
zenity --timeout 5 --info --no-wrap --text="<b>Test Succeeded:</b>\n\nHave a nice day!"
elif [ "${retval}" == "0" ]; then
zenity --timeout 5 --info --no-wrap --title="Installation Complete!" --text="${TIK_OS_NAME} has been installed.\n\n<b>System is rebooting</b>"
systemctl reboot
else
zenity --error --no-wrap --title="Installation Failed" --text="Please file a bug report at <tt>${TIK_BUG_URL}</tt>\n\nPlease include the <tt>tik.log</tt> file\nIt can be found on the IGNITION partition on this USB Stick\n\n<b>System is shutting down</b>"
cp -a ${tik_log} /ignition
systemctl poweroff
fi
}
trap cleanup EXIT

Expand Down
128 changes: 64 additions & 64 deletions usr/lib/tik/lib/tik-functions
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,75 @@
# SPDX-FileCopyrightText: Copyright 2023-2024 Richard Brown

log(){
echo "[${tik_module}][$(date +"%Y%m%d-%T")][LOG] $*" 1>&2
echo "[${tik_module}][$(date +"%Y%m%d-%T")][LOG] $*" 1>&2
}

warn() {
echo "[${tik_module}][$(date +"%Y%m%d-%T")][WARN] $*" 1>&2
d --warning --text="$*"
echo "[${tik_module}][$(date +"%Y%m%d-%T")][WARN] $*" 1>&2
d --warning --text="$*"
}

error() {
echo "[${tik_module}][$(date +"%Y%m%d-%T")][ERROR] $*" 1>&2
d --error --text "$*"
exit 1
echo "[${tik_module}][$(date +"%Y%m%d-%T")][ERROR] $*" 1>&2
d --error --text "$*"
exit 1
}

d(){
while true
do
retval=0
result="$(zenity "$@")" || retval=$?
log "[zenity][${retval}][${result}] $@"
case $retval in
0)
return 0
;;
1|255)
zenity --question --text="Do you really want to quit?" && exit 1
;;
esac
done
while true
do
retval=0
result="$(zenity "$@")" || retval=$?
log "[zenity][${retval}][${result}] $@"
case $retval in
0)
return 0
;;
1|255)
zenity --question --text="Do you really want to quit?" && exit 1
;;
esac
done
}

# variant of privileged run (prun) function that doesn't require the pkexec call to return 0
prun-opt() {
if [ "${debug}" == "1" ]; then
log "[pkexec-noexec] $@"
else
retval=0
pkexec "$@"
retval=$?
log "[pkexec][${retval}] $@"
fi
if [ "${debug}" == "1" ]; then
log "[pkexec-noexec] $@"
else
retval=0
pkexec "$@"
retval=$?
log "[pkexec][${retval}] $@"
fi
}

# Most commonly used prun function, which requires the called command to work
prun() {
prun-opt "$@"
if [ "${retval}" != "0" ]; then
error "Command <tt>$@</tt> FAILED"
fi
prun-opt "$@"
if [ "${retval}" != "0" ]; then
error "Command <tt>$@</tt> FAILED"
fi
}

get_persistent_device_from_unix_node() {
local unix_device=$1
local schema=$2
local node
local persistent_name
node=$(basename "${unix_device}")
for persistent_name in /dev/disk/"${schema}"/*; do
if [ "$(basename "$(readlink "${persistent_name}")")" = "${node}" ];then
if [[ ${persistent_name} =~ ^/dev/disk/"${schema}"/nvme-eui ]]; then
# Filter out nvme-eui nodes as they are not descriptive to the user
continue
fi
echo "${persistent_name}"
return
fi
done
warn "Could not find <tt>${schema}</tt> representation of <tt>${node}</tt>. Using original device <tt>${unix_device}</tt>"
echo "${unix_device}"
local unix_device=$1
local schema=$2
local node
local persistent_name
node=$(basename "${unix_device}")
for persistent_name in /dev/disk/"${schema}"/*; do
if [ "$(basename "$(readlink "${persistent_name}")")" = "${node}" ];then
if [[ ${persistent_name} =~ ^/dev/disk/"${schema}"/nvme-eui ]]; then
# Filter out nvme-eui nodes as they are not descriptive to the user
continue
fi
echo "${persistent_name}"
return
fi
done
warn "Could not find <tt>${schema}</tt> representation of <tt>${node}</tt>. Using original device <tt>${unix_device}</tt>"
echo "${unix_device}"
}

get_disk() {
Expand Down Expand Up @@ -300,17 +300,17 @@ dump_image() {
}

set_boot_target() {
if [ "${debug}" == "1" ]; then
log "[debug] Not setting EFI boot target"
else
# Cleanup any existing openSUSE boot entries
prun-opt /usr/sbin/efibootmgr -B -L "openSUSE Boot Manager"
prun /usr/sbin/efibootmgr -O
# Currently assuming Aeon-like partition layout and shim name. This function will need extra intelligence to probe partitions for other image layouts
prun /usr/sbin/efibootmgr -c -L "openSUSE Boot Manager" -d ${TIK_INSTALL_DEVICE} -l "\EFI\systemd\shim.efi" -p 2
# Log to show the resulting eficonfig
log "[efibootmgr] $(prun /usr/sbin/efibootmgr)"
fi
if [ "${debug}" == "1" ]; then
log "[debug] Not setting EFI boot target"
else
# Cleanup any existing openSUSE boot entries
prun-opt /usr/sbin/efibootmgr -B -L "openSUSE Boot Manager"
prun /usr/sbin/efibootmgr -O
# Currently assuming Aeon-like partition layout and shim name. This function will need extra intelligence to probe partitions for other image layouts
prun /usr/sbin/efibootmgr -c -L "openSUSE Boot Manager" -d ${TIK_INSTALL_DEVICE} -l "\EFI\systemd\shim.efi" -p 2
# Log to show the resulting eficonfig
log "[efibootmgr] $(prun /usr/sbin/efibootmgr)"
fi
}

load_modules() {
Expand All @@ -323,10 +323,10 @@ fi
if [ -n "$(ls -A $module_dir)" ]; then
for f in $module_dir/*
do
tik_module="$f"
log "[START] $module_dir/$f"
. $f
log "[STOP] $module_dir/$f"
tik_module="$f"
log "[START] $module_dir/$f"
. $f
log "[STOP] $module_dir/$f"
done
fi
tik_module="tik"
Expand Down
92 changes: 46 additions & 46 deletions usr/lib/tik/modules/post/10-encrypt
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,61 @@
# SPDX-FileCopyrightText: Copyright 2024 Richard Brown

generate_recoveryKey() {
modhex=('c' 'b' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'n' 'r' 't' 'u' 'v')
mapfile -t raw_key < <(hexdump -v --format '1/1 "%u\n"' -n 32 /dev/random)
[ "${#raw_key[@]}" = 32 ]
key=""
for ((i=0;i<"${#raw_key[@]}";++i)); do
[ "$i" -gt 0 ] && [ "$((i%4))" -eq 0 ] && key="$key-"
c="${raw_key[i]}"
key="$key${modhex[$((c>>4))]}${modhex[$((c&15))]}"
done
modhex=('c' 'b' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'n' 'r' 't' 'u' 'v')
mapfile -t raw_key < <(hexdump -v --format '1/1 "%u\n"' -n 32 /dev/random)
[ "${#raw_key[@]}" = 32 ]
key=""
for ((i=0;i<"${#raw_key[@]}";++i)); do
[ "$i" -gt 0 ] && [ "$((i%4))" -eq 0 ] && key="$key-"
c="${raw_key[i]}"
key="$key${modhex[$((c>>4))]}${modhex[$((c&15))]}"
done
}

display_recoveryKey() {
log "Displaying Recovery Key"
zenity --width=500 --height=500 --no-wrap --warning --title="Encryption Recovery Key" --text="A secret recovery key has been generated for your Aeon installation:\n\n <b><big>$key</big></b>\n\nPlease save this secret recovery key at a secure location\nIt may be used to regain access to your system if the other credentials have been lost or forgotten\nThe recovery key can be used in place of a password whenever authentication is requested\n\nYou may optionally scan the recovery key off screen:\n<span face='monospace'>$(qrencode $key -t UTF8i)</span>"
log "Recovery Key Dialogue Dismissed"
log "Displaying Recovery Key"
zenity --width=500 --height=500 --no-wrap --warning --title="Encryption Recovery Key" --text="A secret recovery key has been generated for your Aeon installation:\n\n <b><big>$key</big></b>\n\nPlease save this secret recovery key at a secure location\nIt may be used to regain access to your system if the other credentials have been lost or forgotten\nThe recovery key can be used in place of a password whenever authentication is requested\n\nYou may optionally scan the recovery key off screen:\n<span face='monospace'>$(qrencode $key -t UTF8i)</span>"
log "Recovery Key Dialogue Dismissed"
}

encrypt_disk() {
probe_partitions $TIK_INSTALL_DEVICE "btrfs" "/usr/lib/os-release"
if [ -z "${probedpart}" ]; then
error "ENCRYPTION FAILED: New Installation NOT FOUND"
fi
prun /usr/bin/mkdir ${mig_dir}/mnt
prun /usr/bin/mount -o compress=zstd:1 ${probedpart} ${mig_dir}/mnt
prun /usr/bin/systemd-repart --pretty 0 --root ${mig_dir}/mnt --dry-run=0 ${probedpart}
prun /usr/bin/mount -o compress=zstd:1,subvol=/@/var ${probedpart} ${mig_dir}/mnt/var
prun /lib/systemd/systemd-growfs ${mig_dir}/mnt/var
prun /usr/bin/umount ${mig_dir}/mnt/var
prun /usr/sbin/btrfs filesystem resize -32m /mnt
prun /usr/bin/umount ${mig_dir}/mnt
prun /usr/sbin/cryptsetup reencrypt --force-password --encrypt --reduce-device-size 32m ${probedpart} cr_root <<<"$key"
echo '{"type":"systemd-recovery","keyslots":["0"]}' | prun /usr/sbin/cryptsetup token import "${probedpart}"
prun /usr/sbin/btrfs rescue fix-device-size /dev/mapper/cr_root
prun /usr/bin/mount -o compress=zstd:1 /dev/mapper/cr_root ${mig_dir}/mnt
prun /usr/bin/mount -o compress=zstd:1,subvol=/@/var /dev/mapper/cr_root ${mig_dir}/mnt/var
etcmountcmd=$(cat ${mig_dir}/mnt/etc/fstab | grep "overlay /etc" | sed 's/\/sysroot\//${mig_dir}\/mnt\//g' | sed 's/\/work-etc.*/\/work-etc ${mig_dir}\/mnt\/etc\//' | sed 's/overlay \/etc overlay/\/usr\/bin\/mount -t overlay overlay -o/')
eval prun "$etcmountcmd"
echo "cr_root ${probedpart} none x-initrd.attach" | prun tee ${mig_dir}/mnt/etc/crypttab
probe_partitions $TIK_INSTALL_DEVICE "vfat" "/EFI/systemd/shim.efi"
prun /usr/bin/mount ${probedpart} ${mig_dir}/mnt/boot/efi
prun /usr/bin/mount -o compress=zstd:1,subvol=/@/.snapshots /dev/mapper/cr_root ${mig_dir}/mnt/.snapshots
prun /usr/bin/mount -t tmpfs -o size=10m tmpfs "${mig_dir}/mnt/run"
prun /usr/bin/mount -t tmpfs -o size=10m tmpfs "${mig_dir}/mnt/tmp"
for i in proc dev sys; do
prun /usr/bin/mount --bind "/$i" "${mig_dir}/mnt/$i"
done
prun /usr/bin/chroot ${mig_dir}/mnt <<EOT
probe_partitions $TIK_INSTALL_DEVICE "btrfs" "/usr/lib/os-release"
if [ -z "${probedpart}" ]; then
error "ENCRYPTION FAILED: New Installation NOT FOUND"
fi
prun /usr/bin/mkdir ${mig_dir}/mnt
prun /usr/bin/mount -o compress=zstd:1 ${probedpart} ${mig_dir}/mnt
prun /usr/bin/systemd-repart --pretty 0 --root ${mig_dir}/mnt --dry-run=0 ${probedpart}
prun /usr/bin/mount -o compress=zstd:1,subvol=/@/var ${probedpart} ${mig_dir}/mnt/var
prun /lib/systemd/systemd-growfs ${mig_dir}/mnt/var
prun /usr/bin/umount ${mig_dir}/mnt/var
prun /usr/sbin/btrfs filesystem resize -32m /mnt
prun /usr/bin/umount ${mig_dir}/mnt
prun /usr/sbin/cryptsetup reencrypt --force-password --encrypt --reduce-device-size 32m ${probedpart} cr_root <<<"$key"
echo '{"type":"systemd-recovery","keyslots":["0"]}' | prun /usr/sbin/cryptsetup token import "${probedpart}"
prun /usr/sbin/btrfs rescue fix-device-size /dev/mapper/cr_root
prun /usr/bin/mount -o compress=zstd:1 /dev/mapper/cr_root ${mig_dir}/mnt
prun /usr/bin/mount -o compress=zstd:1,subvol=/@/var /dev/mapper/cr_root ${mig_dir}/mnt/var
etcmountcmd=$(cat ${mig_dir}/mnt/etc/fstab | grep "overlay /etc" | sed 's/\/sysroot\//${mig_dir}\/mnt\//g' | sed 's/\/work-etc.*/\/work-etc ${mig_dir}\/mnt\/etc\//' | sed 's/overlay \/etc overlay/\/usr\/bin\/mount -t overlay overlay -o/')
eval prun "$etcmountcmd"
echo "cr_root ${probedpart} none x-initrd.attach" | prun tee ${mig_dir}/mnt/etc/crypttab
probe_partitions $TIK_INSTALL_DEVICE "vfat" "/EFI/systemd/shim.efi"
prun /usr/bin/mount ${probedpart} ${mig_dir}/mnt/boot/efi
prun /usr/bin/mount -o compress=zstd:1,subvol=/@/.snapshots /dev/mapper/cr_root ${mig_dir}/mnt/.snapshots
prun /usr/bin/mount -t tmpfs -o size=10m tmpfs "${mig_dir}/mnt/run"
prun /usr/bin/mount -t tmpfs -o size=10m tmpfs "${mig_dir}/mnt/tmp"
for i in proc dev sys; do
prun /usr/bin/mount --bind "/$i" "${mig_dir}/mnt/$i"
done
prun /usr/bin/chroot ${mig_dir}/mnt <<EOT
sdbootutil mkinitrd
umount /etc
EOT
for i in proc dev sys run tmp 'boot/efi' etc var '.snapshots'; do
prun /usr/bin/umount "${mig_dir}/mnt/$i"
done
prun /usr/bin/umount ${mig_dir}/mnt
prun /usr/bin/rmdir ${mig_dir}/mnt
for i in proc dev sys run tmp 'boot/efi' etc var '.snapshots'; do
prun /usr/bin/umount "${mig_dir}/mnt/$i"
done
prun /usr/bin/umount ${mig_dir}/mnt
prun /usr/bin/rmdir ${mig_dir}/mnt
}

generate_recoveryKey
Expand Down
Loading

0 comments on commit c53a8b4

Please sign in to comment.