Dir cleanup.

This commit is contained in:
Andrew Schott 2024-07-02 04:10:36 -05:00
parent 281503ff2e
commit 37ae0c4645
4 changed files with 0 additions and 0 deletions

12
kexec/kexec-latest.sh Executable file
View file

@ -0,0 +1,12 @@
#!/bin/bash
set -x
NEWEST_KERNEL_VERSION="$(
rpm --query --queryformat='%{VERSION}-%{RELEASE}.%{ARCH}\n' kernel \
| sort --version-sort --reverse \
| head --lines=1
)"
kexec --load --append="`cat /proc/cmdline`" \
--initrd="/boot/initramfs-$NEWEST_KERNEL_VERSION.img" \
"/boot/vmlinuz-$NEWEST_KERNEL_VERSION"

115
kexec/kexec-reboot.sh Executable file
View file

@ -0,0 +1,115 @@
#!/bin/sh
set -e
[ -n "$BASH" ] && set -o pipefail
set -u
# defaults
wait=10
detach_wait=3
usage() {
cat << EOF
Usage: kreboot [option ...] [kernel|boot-index]
Options are:
--wait, -w N wait N seconds before reboot (default $wait)
--nowait don't wait, reboot immediately
--detach Schedule reboot and return. Suitable to remote reboot.
--help, -h show this help message
--kargs, k additional kernel arguments
Example:
$0 0 Boot kernel with index 0
EOF
exit $1
}
uerr() {
echo "ERROR: $*" 2>&1
echo
usage 1
}
# Check that all programs we need are available, exit if not
# (relying on 'set -e' and hash returning non-zero for errors)
hash grubby kexec systemctl
# Process options and arguments
target=''
kargs=''
while test $# -gt 0; do
case $1 in
-w|--wait)
if [ "$#" -lt 2 ]; then
uerr "Option $1 requires an argument"
fi
wait=$2
if ! printf "%f" $wait > /dev/null 2>&1; then
uerr "Option $1 argument not a number"
fi
shift
;;
--nowait)
wait=0
;;
--detach)
detach=yes
;;
--detach_wait)
if [ "$#" -lt 2 ]; then
uerr "Option $1 requires an argument"
fi
detach_wait=$2
if ! printf "%f" $detach_wait > /dev/null 2>&1; then
uerr "Option $1 argument not a number"
fi
shift
;;
-h|--help)
usage 0
;;
-k|--kargs)
kargs=$2
shift
;;
-*)
uerr "Unrecognized option: $1"
;;
*)
if [ -n "$target" ]; then
uerr "Extra arguments: $*"
fi
target=$1
;;
esac
shift
done
if [ -z "$target" ]; then
if ! target=$(grubby --default-kernel); then
# grubby prints error to stdout
echo "grubby: $target" 1>&2
exit 1
fi
fi
if ! info=$(grubby --info=$target); then
# grubby prints error to stdout
echo "grubby: $info" 1>&2
exit 1
fi
eval $(echo "$info" | sed "s|^\(\S*\)=\([^'\"].*\)$|\1='\2'|")
echo Booting $title...
if [ "$wait" -ne 0 ]; then
echo Press Ctrl-C within $wait seconds to cancel
sleep $wait
fi
kexec -l "$kernel" --initrd="$initrd" --command-line="root=$root $args $kargs"
if [ -z "detach" ]; then
systemctl kexec
else
setsid bash -c "sleep $detach_wait; systemctl kexec" &>/dev/null < /dev/null &
fi
`

37
kexec/kexec.py Normal file
View file

@ -0,0 +1,37 @@
#!/usr/bin/env python
# From http://code.activestate.com/recipes/491277-kexec-the-newest-linux-kernel/
import sys
import os
import subprocess
import fnmatch
import rpm
def newest_kernel_and_initrd():
ts = rpm.TransactionSet()
p = max(ts.dbMatch('name', 'kernel'))
k = [ x for x in p['filenames']
if fnmatch.fnmatch(x, '/boot/vmlinuz-*') ][0]
#The initrd is not owned by the kernel rpm but generated in the %post script
i = '/boot/initrd-%s.img' % k[len('/boot/vmlinuz-'):]
return (k, i)
def kexec(kernel, initrd, cmdline=None):
if cmdline == None:
cmdline = file('/proc/cmdline').read()[:-1] # strip newline
r = subprocess.call(['/sbin/kexec', '-l', kernel,
'--initrd=%s' % initrd,
'--command-line=%s' % cmdline])
if r != 0:
raise Exception('kexec returned %d' % r)
def program_name():
return os.path.basename(sys.argv[0])
def main():
(k, i) = newest_kernel_and_initrd()
sys.stderr.write('%s: loading ("%s", "%s")\n' % (program_name(), k, i))
kexec(k, i)
sys.stderr.write('%s: reboot to load new kernel\n' % program_name())
if __name__ == '__main__':
main()

16
kexec/kexec.sh Executable file
View file

@ -0,0 +1,16 @@
#!/bin/bash
# Force root
[ `whoami` = root ] || { sudo "$0" "$@"; exit $?; }
latestkernel=`ls -t /boot/vmlinuz-* | sed "s/\/boot\/vmlinuz-//g" | head -n1`
echo "kernel current: $(uname -r)"
echo "kernel target: ${latestkernel}"
echo ""
echo "Arming kexec..."
#kexec -l /boot/vmlinuz-${latestkernel} --initrd=/boot/initramfs-${latestkernel}.img --append="`cat /proc/cmdline`"
kexec -l /boot/vmlinuz-${latestkernel} --initrd=/boot/initramfs-${latestkernel}.img --reuse-cmdline
echo ""
read -p "Press [Enter] key to start new kernel..."
#systemctl start kexec.target
kexec -e