summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Barnes <jbarnes@virtuousgeek.org>2020-01-12 19:26:20 -0800
committerJesse Barnes <jsbarnes@google.com>2020-01-14 10:15:16 -0800
commite2c6ceee7b40e344278cf541fbe3339ba699de9a (patch)
treee11b489fe3303f2ea777f8cf60da75802a74afa5
parentd09ce64804a1ce22299c1e4dfb1b5ede9a37f796 (diff)
push_kernel: add getopts and error handling
General improvements: - getopts for parsing cmdline - default boot command line - more error handling - split into functions for readability
-rwxr-xr-xpush_kernel.sh190
1 files changed, 141 insertions, 49 deletions
diff --git a/push_kernel.sh b/push_kernel.sh
index 98a5363..06ac92c 100755
--- a/push_kernel.sh
+++ b/push_kernel.sh
@@ -1,13 +1,24 @@
#!/bin/bash
+skip_modules=0
+cmdline_file=""
+target_host=""
+img_file=""
+def_cmdline=""
+partition=4
+
function usage
{
echo "Usage:"
- echo "$0 [ -s ] [ -c <cmdline_file> ] <target_host>"
- echo " -s skip module install"
+ echo "$0 [ -s ] [ -c <cmdline_file> ] -h <target_host>"
+ echo " -s update stable (KERN_A) partition"
echo " -c <cmdline_file> file containing kernel command line"
- echo " [default: ~/cmdline.txt]"
+ echo " [default: loglevel=7 root=/dev/nvme0n1p3 rootwait rw]"
echo " <target_host> target machine to update"
+ echo ""
+ echo " Updates <target_host> with the kernel and modules from"
+ echo " the current directory. Defaults to installing the kernel"
+ echo " into the KERN_B partition and marking it as 'boot once'"
exit 1
}
@@ -17,21 +28,110 @@ function error
exit 1
}
-optstr="sc:"
-skip_modules=0
-cmdline_file="~/cmdline.txt"
-target_host=""
+function default_cmdline
+{
+ def_cmdline=`mktemp /tmp/push_kernel_cmdline-XXXX`
+ cat <<EOF > $def_cmdline
+loglevel=7
+root=/dev/nvme0n1p3
+rootwait
+rw
+EOF
+}
+
+function copy_modules
+{
+ dir=`mktemp -d /tmp/push_kernel-XXXX`
+ echo -n "installing modules..."
+ make INSTALL_MOD_PATH=$dir INSTALL_MOD_STRIP=1 modules_install >& /dev/null
+ echo "done."
+ echo -n "copying modules..."
+ (cd $dir/lib/modules; rsync -a * root@$target_host:/lib/modules/)
+ echo "done."
+ if [ $? -ne 0 ] ; then
+ return -1
+ fi
+ rm -rf $dir
+ return 0
+}
+
+function build_image
+{
+ img_file=`mktemp /tmp/push_kernel_img-XXXX`
+ vbutil_kernel --pack $img_file \
+ --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
+ --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
+ --version 1 \
+ --config "$cmdline_file" \
+ --bootloader ~/bootstub.efi \
+ --vmlinuz arch/x86/boot/bzImage \
+ --arch x86_64
+ if [ ! -f $img_file ]; then
+ return -1
+ fi
+ if [ "$def_cmdline" != "" ]; then
+ rm $def_cmdline
+ fi
+ return $?
+}
+
+function copy_image
+{
+ # Setup new kernel to boot from partition B
+ echo -n "copying kernel image..."
+ scp $img_file root@$target_host:/tmp >& /dev/null
+ echo "done."
+ if [ $? -ne 0 ] ; then
+ echo "failed to copy kernel image"
+ exit 0
+ fi
+ rm $img_file
+ return 0
+}
+
+function install_image
+{
+ echo -n "installing kernel to partition $partition..."
+ ssh root@$target_host dd if=$img_file of=/dev/nvme0n1p$partition bs=4K >& /dev/null
+ echo "done."
+ if [ $? -ne 0 ] ; then
+ echo "failed to write kernel image on target"
+ exit 0
+ fi
+}
+
+function set_boot_once
+{
+ if [ $partition -eq 2 ] ; then
+ echo -n "setting boot flags on KERN_A..."
+ ssh root@$target_host cgpt add -i 2 -S 1 -T 1 -P14 /dev/nvme0n1
+ else
+ echo -n "setting boot once flag for KERN_B..."
+ ssh root@$target_host cgpt add -i 4 -S 0 -T 1 -P15 /dev/nvme0n1
+ fi
+ if [ $? -ne 0 ] ; then
+ echo "failed to set new kernel image active on target"
+ exit 0
+ fi
+ echo "done."
+}
+
+optstr="sc:h:"
while getopts $optstr opt; do
case $opt in
's')
- skip_modules=1
- echo "skipping module install"
+ partition=2
+ echo "updating stable kernel (KERN_A)"
;;
'c')
cmdline_file=$OPTARG
echo "using $cmdline_file for boot command line"
;;
+ 'h')
+ target_host=$OPTARG
+ echo "sending kernel to $target_host"
+ ;;
*)
usage
;;
@@ -42,60 +142,52 @@ if [ "$target_host" == "" ]; then
usage
fi
+if [ "$cmdline_file" == "" ]; then
+ default_cmdline
+ cmdline_file=$def_cmdline
+ echo "using default command line"
+fi
+
if [ ! -f arch/x86/boot/bzImage ] ; then
echo "run from a linux tree with the kernel built"
- exit 0
+ exit 1
fi
if [ ! -x "$(command -v vbutil_kernel)" ]; then
echo "vbutil_kernel not installed, install vboot-kernel-utils"
- exit 0
+ exit 1
fi
-# Copy modules
-if [ ! $skip_modules ]; then
- dir=`mktemp /tmp/push_kernel-XXXX`
- make INSTALL_MOD_PATH=$dir INSTALL_MOD_STRIP=1 modules_install
- (cd $dir/lib/modules; rsync -av * root@$1:/lib/modules/)
- if [ $? -ne 0 ] ; then
- echo "module copy failed"
- exit 0
- fi
- rm -rf $dir
+copy_modules
+if [ $? -ne 0 ]; then
+ echo "module copy failed"
+ exit 1
fi
-# Build kernel image
-img_file=`mktemp /tmp/push_kernel_img-XXXX`
-vbutil_kernel --pack $img_file \
- --keyblock /usr/share/vboot/devkeys/kernel.keyblock \
- --signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
- --version 1 \
- --config "$cmdline_file" \
- --bootloader ~/bootstub.efi \
- --vmlinuz arch/x86/boot/bzImage \
- --arch x86_64
-
-if [ $? -ne 0 ] ; then
- echo "failed to build kernel image"
- exit 0
+build_image
+if [ $? -ne 0 ]; then
+ echo "image build failed"
+ exit 1
fi
-# Setup new kernel to boot from partition B
-scp $img_file root@$1:/tmp
-if [ $? -ne 0 ] ; then
- echo "failed to copy kernel image"
- exit 0
+copy_image
+if [ $? -ne 0 ]; then
+ echo "image copy failed"
+ exit 1
fi
-rm $img_file
-ssh root@$1 dd if=$img_file of=/dev/nvme0n1p4 bs=4K
-if [ $? -ne 0 ] ; then
- echo "failed to write kernel image on target"
- exit 0
+install_image
+if [ $? -ne 0 ]; then
+ echo "image install failed"
+ exit 1
fi
-ssh root@$1 cgpt add -i 4 -S 0 -T 1 -P15 /dev/nvme0n1
-if [ $? -ne 0 ] ; then
- echo "failed to set new kernel image active on target"
- exit 0
+set_boot_once
+if [ $? -ne 0 ]; then
+ echo "failed to set kernel to boot"
+ exit 1
fi
+
+echo "success, $target_host is ready to reboot."
+exit 0
+