Add/modify functions for enhanced error-handling/logging + fix spaces in variables

Signed-off-by: Andreas Müller <schnitzeltony@gmail.com>
This commit is contained in:
Andreas Müller
2018-10-02 12:45:28 +02:00
parent ca5f873b08
commit 62ce7912fa
4 changed files with 65 additions and 59 deletions

View File

@@ -201,8 +201,10 @@ RunUserStartRoot() {
# prepare passing params to root-run - make sure they have same
# signature as loaded at the end of this file
RootParams="$DevicePath $DeployedFile $DeployFileDir $Machine $FindString $KernelImageType"
# replace spaces - otherwise param are realigned
for setting in "$DevicePath" "$DeployedFile" "$DeployFileDir" "$Machine" "$FindString" "$KernelImageType"; do
RootParams="$RootParams `echo $setting | sed 's: :%20:g'`"
done
# dialog's gui is done here
clear
echo
@@ -261,19 +263,19 @@ StartCardKernelWrite() {
RootCardKernelWriteCallback
}
# check common prerequisites
CheckPrerequisite "dialog"
CheckPrerequisite "lsblk"
# For user call the first two params can be set optionally - the first
# target/source dialogs won't be displayed then
DevicePath=$1
DeployedFile=$2
# reintoduce spaces
DevicePath="`echo $1 | sed 's:%20: :g'`"
DeployedFile="`echo $2 | sed 's:%20: :g'`"
# For root call all params are mandatory
if [ $( id -u ) -eq 0 ]; then
DeployFileDir=$3
Machine=$4
FindString=$5
KernelImageType=$6
DeployFileDir="`echo $3 | sed 's:%20: :g'`"
Machine="`echo $4 | sed 's:%20: :g'`"
FindString="`echo $5 | sed 's:%20: :g'`"
KernelImageType="`echo $6 | sed 's:%20: :g'`"
fi

View File

@@ -37,12 +37,35 @@ base_tempdir=`dirname "$base_tempdir"`
# ErrorOut outputs error text in first param and exits script
ErrorOut() {
clear
echo "${style_red}${style_bold}${1}${style_normal}"
echo
echo -e "${style_red}${style_bold}${1}${style_normal}"
echo
exit -1
}
# ExecEx executes a command, checks if it succeeds and ouputs messages
# It expectes the following parameters:
# 1. Command
# 2. Message on start
# 3. Message on successful end
# 4. Message on error end
ExecEx() {
if [ -n "${2}" ] ; then
echo -e "${style_blue}${2}${style_normal}"
fi
if eval ${1} ; then
if [ -n "${3}" ] ; then
echo -e "${style_green}${3}${style_normal}"
fi
else
if [ -n "${4}" ] ; then
ErrorOut "${4}"
else
ErrorOut "An error occured!"
fi
fi
}
# CheckPrerequisite checks if required hosttool is found on host. In case not
# it outputs a message and exits script.
CheckPrerequisite() {

View File

@@ -17,19 +17,16 @@ RootCardWriteCallback() {
# rootfs write/resize to card fit
time(
# write
echo "Write $DeployedFile to $DevicePath..."
StartMessage="\nWrite $DeployedFile to $DevicePath..."
if echo $DeployedFile | grep -q '.wic.gz'; then
gunzip -c $DeployedFile | dd of=$DevicePath bs=1024K
ExecEx "gunzip -c $DeployedFile | dd of=$DevicePath bs=1024K" "$StartMessage" "Write done." "Write failed!"
else
dd of=$DevicePath if=$DeployedFile bs=1024K
ExecEx "dd of=$DevicePath if=$DeployedFile bs=1024K" "$StartMessage" "Write done." "Write failed!"
fi
sync
echo "${style_green}Write done.${style_normal}"
# sync
ExecEx "sync" "\nSync..." "Sync done." "Sync failed!"
# resize
echo "Resize ${DevicePath}2..."
parted -s $DevicePath -- resizepart 2 -0
resize2fs "${DevicePath}2"
echo "${style_green}Resize done.${style_normal}"
ExecEx "parted -s $DevicePath -- resizepart 2 -0 && resize2fs ${DevicePath}2" "\nResize ${DevicePath}2..." "Reszize done." "Resize failed!"
)
}
@@ -37,51 +34,38 @@ RootCardWriteCallback() {
RootCardKernelWriteCallback() {
tmpdir=`mktemp -d`
# mount boot partition
ExecEx "mount ${DevicePath}1 $tmpdir" "\nMount boot partition ${DevicePath}1 to $tmpdir..." "Mount done" "Mount failed"
# initial kernel
mount ${DevicePath}1 $tmpdir || exit 1
echo "Write initial kernel and devicetrees to boot partition..."
rm -f $tmpdir/${KernelImageType}*
cp -f $DeployedFile $tmpdir/$KernelImageType
ExecEx "rm -f $tmpdir/${KernelImageType}*" "\nRemove old kernels..." "Remove done" "Remove failed"
ExecEx "cp $DeployedFile $tmpdir/$KernelImageType" "\nCopy new kernel $KernelImageType..." "Copy done" "Copy failed"
# devicetrees
rm -f $tmpdir/*.dtb
ExecEx "rm -f $tmpdir/*.dtb" "\nRemove old devicetrees..." "Remove done" "Remove failed"
for dtb in `find ${DeployFileDir} -name "${KernelImageType}*.dtb" -type l`; do
dtbname=`basename $dtb | sed 's:'${KernelImageType}'-::'`
cp -f $dtb $tmpdir/${dtbname}
ExecEx "cp $dtb $tmpdir/${dtbname}" "Copy $dtb -> $tmpdir/${dtbname}..." "Copy done" "Copy failed"
done
echo "${style_green}Write done.${style_normal}"
# unmount boot partition
ExecEx "sleep 1 && umount ${DevicePath}1" "\nUnmount boot partition..." "Unmount done" "Unmount failed"
echo "Unmount boot partition..."
sleep 1
umount ${DevicePath}1 || exit 1
echo "${style_green}Unmount done.${style_normal}"
# rootfs/boot
mount ${DevicePath}2 $tmpdir || exit 1
echo "Write kernel to rootfs..."
rm -f $tmpdir/boot/${KernelImageType}*
# mount rootfs
ExecEx "mount ${DevicePath}2 $tmpdir" "\nMount rootfs ${DevicePath}2 to $tmpdir..." "Mount done" "Mount failed"
# rootfs/boot kernel
ExecEx "rm -f $tmpdir/boot/${KernelImageType}*" "\nRemove old kernels..." "Remove done" "Remove failed"
KernelWithAbiName=`basename $DeployedFile | sed -e 's:-abiversion-::'`
cp $DeployedFile $tmpdir/boot/$KernelWithAbiName
ln -sf $KernelWithAbiName $tmpdir/boot/$KernelImageType
echo "${style_green}Write done.${style_normal}"
ExecEx "cp $DeployedFile $tmpdir/boot/$KernelImageType" "\nCopy new kernel to /boot/$KernelImageType..." "Copy done" "Copy failed"
ExecEx "ln -sf $KernelWithAbiName $tmpdir/boot/$KernelImageType" "\nLink kernel to /boot/$KernelImageType -> $KernelWithAbiName..." "Copy done" "Copy failed"
# rootfs/lib/modules
echo "Write modules to rootfs..."
kernel_abi_ver=`echo $KernelWithAbiName | sed 's:'${KernelImageType}'::g'`
for modules in `find ${DeployFileDir} -name "modules-${Machine}.tgz"`; do
tar xvzf ${modules} -C $tmpdir/
ExecEx "tar xvzf ${modules} -C $tmpdir/" "\nUnpack kernel modules..." "Unpack done" "Unpack failed"
done
echo "${style_green}Write done.${style_normal}"
# run depmod (stolen from dempodwrapper)
sys_map=`realpath ${DeployFileDir}/../../../pkgdata/${Machine}/kernel-depmod/System.map-$kernel_abi_ver`
echo "Run depmod on modules..."
depmod -a -b $tmpdir -F "$sys_map" "$kernel_abi_ver"
echo "${style_green}Run done.${style_normal}"
ExecEx "depmod -a -b $tmpdir -F $sys_map $kernel_abi_ver" "\nRun depmod on modules..." "Run done" "Run failed"
# unmount boot partition
ExecEx "sleep 1 && umount ${DevicePath}2" "\nUnmount rootfs..." "Unmount done" "Unmount failed"
# cleanup
echo "Unmount rootfs partition..."
umount ${DevicePath}2 || exit 1
echo "${style_green}Unmount done.${style_normal}"
rm -rf $tmpdir
}

View File

@@ -17,14 +17,11 @@ RootCardWriteCallback() {
# rootfs write/resize to card fit
time(
# write
echo "Write $DeployedFile to $DevicePath..."
dd of=$DevicePath if=$DeployedFile bs=1024K
sync
echo "${style_green}Write done.${style_normal}"
echo "Resize ${DevicePath}2..."
parted -s $DevicePath -- resizepart 2 -0
resize2fs "${DevicePath}2"
echo "${style_green}Resize done.${style_normal}"
ExecEx "dd of=$DevicePath if=$DeployedFile bs=1024K" "\nWrite $DeployedFile to $DevicePath..." "Write done." "Write failed!"
# sync
ExecEx "sync" "\nSync..." "Sync done." "Sync failed!"
# resize
ExecEx "parted -s $DevicePath -- resizepart 2 -0 && resize2fs ${DevicePath}2" "\nResize ${DevicePath}2..." "Reszize done." "Resize failed!"
)
}