Rewrite of card scripts completely

Reduce the card specific code to what's needed.

Signed-off-by: Andreas Müller <schnitzeltony@gmail.com>
This commit is contained in:
Andreas Müller
2018-10-02 09:58:24 +02:00
parent 3cc5a3b448
commit ca5f873b08
18 changed files with 773 additions and 541 deletions

View File

@@ -0,0 +1,279 @@
#! /bin/bash
# card-helpers.inc
# (c) Copyright 2018 Andreas Müller <schnitzeltony@gmail.com>
# Licensed under terms of GPLv2
#
# This script contains some helper functions and can be sourced by other
# scripts
# SelectCardDevice() creates a dialog to select one of all available removable
# devices. The path to the selected device is stored in the variale DevicePath.
SelectCardDevice() {
if [ -z "$DevicePath" ]; then
iCount=0
for dev in /dev/sd[a-z] ; do
DeviceFile=`basename $dev`
# we are only interested in removable devices
if [ `cat /sys/block/$DeviceFile/removable` = '1' ] && [ `cat /sys/block/$DeviceFile/size` -gt "0" ]; then
iCount=`expr $iCount + 1`
DevicePathArr[${iCount}]=$dev
# pretify display
secondline=
display=
IFS=$'\n'
for line in `lsblk --raw --noheadings --output NAME,LABEL,MODEL $dev` ; do
# a bug in lsblk?
line=`echo $line | sed 's:\\\x20: :g'`
if [ "x$secondline" = "x" ]; then
# first line - it is for device
secondline="1"
display="$line"
else
if [ "x$secondline" = "x1" ]; then
secondline="2"
# second line - header + partition
display="$display / partitions: $line"
else
# second line - separator + partition
display="$display + $line"
fi
fi
done
display=`echo $display | sed 's: : :g'`
display=`echo $display | sed 's: : :g'`
display=`echo $display | sed 's: : :g'`
unset IFS
menuitems+=( "$iCount" "$display" )
fi
done
if [ $iCount -eq 0 ]; then
ErrorOut 'No removable devices found!'
fi
dialog --title 'Select card device'\
--menu 'Move using [UP] [DOWN],[Enter] to select' 10 100 $iCount\
"${menuitems[@]}"\
2>/tmp/menuitem.$$
# get OK/Cancel
sel=$?
# get selected menuitem
menuitem=`cat /tmp/menuitem.$$`
rm -f /tmp/menuitem.$$
# Cancel Button or <ESC>
if [ $sel -eq 1 -o $sel -eq 255 ] ; then
ErrorOut 'Cancel selected at SelectCardDevice().'
fi
DevicePath=${DevicePathArr[$menuitem]}
fi
}
# SelectDeployedFile() opens a dialog to select file containing rootfs/kernel/...
# It expects one parameter which contains find parameters e.g for raspberrypi:
# '-name *.rpi-sdimg -type l'.
# Output variables:
# DeployedFile: The file selected
# DeployFileDir: Path of the file selected
SelectDeployedFile() {
if [ -z "$DeployedFile" ]; then
# OE environment found?
if [ -z "$BUILDDIR" ]; then
ErrorOut "The environment variable BUILDDIR is not set. It is usually set before running bitbake."
fi
iCount=0
strSelection=
for grep_result in `grep -h TMPDIR $BUILDDIR/conf/*.conf | sed -e s/' '/''/g -e s/'\"'/''/g`; do
# exclude comments
tmp_dir=`echo $grep_result | grep '^TMPDIR='`
if [ ! -z "$tmp_dir" ]; then
OeTmpDir=`echo $tmp_dir | sed -e s/'TMPDIR='/''/g`
fi
done
for BuildPath in ${OeTmpDir}-*; do
for i in `find ${BuildPath}/deploy/images/${Machine} $FindString | sort` ; do
iCount=`expr $iCount + 1`
RootFileNameArr[${iCount}]=$i
strSelection="$strSelection $iCount "`basename $i`
done
done
# were files found?
if [ $iCount -eq 0 ]; then
ErrorOut "No files found in ${OeTmpDir}-\* matching $FindString"
fi
dialog --title 'Select rootfs'\
--menu 'Move using [UP] [DOWN],[Enter] to select' 30 100 $iCount\
${strSelection}\
2>/tmp/menuitem.$$
# get OK/Cancel
sel=$?
# get selected menuitem
menuitem=`cat /tmp/menuitem.$$`
rm -f /tmp/menuitem.$$
# Cancel Button or <ESC>
if [ $sel -eq 1 -o $sel -eq 255 ] ; then
ErrorOut "Cancel selected at SelectDeployedFile()."
fi
DeployedFile=${RootFileNameArr[$menuitem]}
fi
DeployFileDir=`dirname $DeployedFile`
# Now that we know the file, exact_machine can be set without wildcards
Machine=`basename $DeployFileDir`
}
SelectSuSudo() {
# Select su/sudo
dialog --title 'Select how you want to logon as root'\
--menu 'Move using [UP] [DOWN],[Enter] to select' 10 100 2 1 su 2 sudo \
2>/tmp/menuitem.$$
# get OK/Cancel
sel=$?
# get selected menuitem
menuitem=`cat /tmp/menuitem.$$`
rm -f /tmp/menuitem.$$
# Cancel Button or <ESC>
if [ $sel -eq 1 -o $sel -eq 255 ] ; then
ErrorOut "Cancel selected at SelectSuSudo()."
fi
}
SelectInOut() {
# DevicePath for target card
SelectCardDevice
# select rootfs source
SelectDeployedFile
}
# EnsureUnmount() tests if a device partition is mounted. If so partition is
# unmounted. Device name is expected in first parameter.
EnsureUnmount() {
# check if the card is currently mounted
MOUNTSTR=$(mount | grep $1)
if [ -n "$MOUNTSTR" ] ; then
echo -e "\n$1 is mounted. Unmounting..."
umount -f ${1}?*
echo "${style_green}Unmount done${style_normal}"
fi
}
# PrepareWrite performs actions required before data write can start:
# * Check if source and destination locations are valid
# * Extract source path to variable DeployFileDir
# * eventually unmount destination
# The following variables are expected:
# * DevicePath: destination block-device e.g '/dev/sdc'
# * DeployedFile (optional): source file (rootfs/kernel/..)
PrepareWrite() {
# device node valid?
if [ ! -b "${DevicePath}" ] ; then
ErrorOut "$DevicePath is not a valid block device!"
fi
if [ -n "${DeployedFile}" ] ; then
# rootfs/kernel valid?
if [ ! -e "${DeployedFile}" ] ; then
ErrorOut "$DeployedFile can not be found!"
fi
fi
EnsureUnmount $DevicePath
}
# RunUserStartRoot() tests if we are running as root. If not, it calls SelectInOut. It expects
# SelectInOut to leave the required parameters for root call in variable RootParams.
# After this it asks user how to log on as root. Then the main script is started
# as root.
RunUserStartRoot() {
# we are not already root?
if [ ! $( id -u ) -eq 0 ]; then
SelectInOut
SelectSuSudo
# 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"
# dialog's gui is done here
clear
echo
if [ "x$1" = "x" ] ; then
# No message text passed: use standard
echo "${style_bold}All data currenly stored on $DevicePath will be overwritten!${style_normal}"
else
# Use message text passed
echo $1 | sed "s|\%DevicePath\%|$DevicePath|"
fi
if [ $menuitem -eq 1 ]; then
echo -e "\nEnter valid root password if you are sure you want to continue"
# Call this prog as root
exec su -c "${0} $RootParams"
else
echo -e "\nEnter valid sudo password if you are sure you want to continue"
sudo -k
# Call this prog as root
exec sudo ${0} $RootParams
fi
# sice we're 'execing' above, we wont reach this exit unless something
# goes wrong.
ErrorOut "RunUserStartRoot() should not have reached here!"
fi
}
StartCardWrite() {
# If not set on call - get defaults from machine.inc
if [ -z "$Machine" ]; then
Machine="$DEFAULT_MACHINE_FAMILY"
fi
if [ -z "$FindString" ]; then
FindString="$DEFAULT_FIND_ROOTFS"
fi
RunUserStartRoot
PrepareWrite
RootCardWriteCallback
}
StartCardKernelWrite() {
# If not set on call - get defaults from machine.inc
if [ -z "$Machine" ]; then
Machine="$DEFAULT_MACHINE_FAMILY"
fi
if [ -z "$FindString" ]; then
FindString="$DEFAULT_FIND_KERNEL"
fi
if [ -z "$KernelImageType" ]; then
KernelImageType="$DEFAULT_KERNEL_IMAGE_TYPE"
fi
RunUserStartRoot "${style_bold}The kernel images on %DevicePath% will be overwritten!${style_normal}"
PrepareWrite
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
# For root call all params are mandatory
if [ $( id -u ) -eq 0 ]; then
DeployFileDir=$3
Machine=$4
FindString=$5
KernelImageType=$6
fi

View File

@@ -0,0 +1,54 @@
#! /bin/bash
# common-helpers.inc
# (c) Copyright 2018 Andreas Müller <schnitzeltony@gmail.com>
# Licensed under terms of GPLv2
#
# This script contains some helper functions and can be sourced by other
# scripts
# set terminal styles
# are we on terrminal?
if [ -t 1 ] ; then
# tput available?
if [ ! "x`which tput 2>/dev/null`" = "x" ] ; then
# supports colors ?
ncolors=`tput colors`
if [ -n "$ncolors" -a $ncolors -ge 8 ] ; then
style_bold="`tput bold`"
style_underline="`tput smul`"
style_standout="`tput smso`"
style_normal="`tput sgr0`"
style_black="`tput setaf 0`"
style_red="`tput setaf 1`"
style_green="`tput setaf 2`"
style_yellow="`tput setaf 3`"
style_blue="`tput setaf 4`"
style_magenta="`tput setaf 5`"
style_cyan="`tput setaf 6`"
style_white="`tput setaf 7`"
fi
fi
fi
# base tempdir
base_tempdir=`mktemp -u`
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
exit -1
}
# CheckPrerequisite checks if required hosttool is found on host. In case not
# it outputs a message and exits script.
CheckPrerequisite() {
if [ "x`which $1 2>/dev/null`" = "x" ] ; then
ErrorOut "You need to install $1 first!"
fi
}

View File

@@ -0,0 +1,95 @@
#! /bin/bash
# machine-imx.inc
# (c) Copyright 2018 Andreas Müller <schnitzeltony@gmail.com>
# Licensed under terms of GPLv2
#
# This script contains settings and callbacks for NXP imx boards
# default settings
DEFAULT_MACHINE_FAMILY='*imx*'
DEFAULT_FIND_ROOTFS='-name *.sdcard -o -name *.wic.gz -type l'
DEFAULT_KERNEL_IMAGE_TYPE='uImage'
DEFAULT_FIND_KERNEL="-name ${DEFAULT_KERNEL_IMAGE_TYPE}-abiversion-* -type l"
# callback for card-write
RootCardWriteCallback() {
# rootfs write/resize to card fit
time(
# write
echo "Write $DeployedFile to $DevicePath..."
if echo $DeployedFile | grep -q '.wic.gz'; then
gunzip -c $DeployedFile | dd of=$DevicePath bs=1024K
else
dd of=$DevicePath if=$DeployedFile bs=1024K
fi
sync
echo "${style_green}Write done.${style_normal}"
# resize
echo "Resize ${DevicePath}2..."
parted -s $DevicePath -- resizepart 2 -0
resize2fs "${DevicePath}2"
echo "${style_green}Resize done.${style_normal}"
)
}
# callback for card-kernel-write
RootCardKernelWriteCallback() {
tmpdir=`mktemp -d`
# 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
# devicetrees
rm -f $tmpdir/*.dtb
for dtb in `find ${DeployFileDir} -name "${KernelImageType}*.dtb" -type l`; do
dtbname=`basename $dtb | sed 's:'${KernelImageType}'-::'`
cp -f $dtb $tmpdir/${dtbname}
done
echo "${style_green}Write done.${style_normal}"
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}*
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}"
# 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/
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}"
# cleanup
echo "Unmount rootfs partition..."
umount ${DevicePath}2 || exit 1
echo "${style_green}Unmount done.${style_normal}"
rm -rf $tmpdir
}
CheckPrerequisite "time"
CheckPrerequisite "gunzip"
CheckPrerequisite "dd"
CheckPrerequisite "parted"
CheckPrerequisite "resize2fs"
CheckPrerequisite "depmod"

View File

@@ -0,0 +1,103 @@
#! /bin/bash
# machine-raspberrypi.inc
# (c) Copyright 2018 Andreas Müller <schnitzeltony@gmail.com>
# Licensed under terms of GPLv2
#
# This script contains settings and callbacks fir raspberrypi boards
# default settings
DEFAULT_MACHINE_FAMILY='raspberrypi*'
DEFAULT_FIND_ROOTFS='-name *.rpi-sdimg -type l'
DEFAULT_KERNEL_IMAGE_TYPE='Image'
DEFAULT_FIND_KERNEL="-name ${DEFAULT_KERNEL_IMAGE_TYPE}-abiversion-* -type l"
# callback for card-write
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}"
)
}
# callback for card-kernel-write
RootCardKernelWriteCallback() {
tmpdir=`mktemp -d`
# initial kernel / devicetrees
mount ${DevicePath}1 $tmpdir || exit 1
echo "Write initial kernel and devicetrees to boot partition..."
# kernel (REVISIT for aarch64)
cp -f $DeployedFile $tmpdir/kernel7.img
# devicetrees
rm -f $tmpdir/*.dtb
for dtb in `find ${DeployFileDir} -name "bcm27*.dtb"`; do
cp -f $dtb $tmpdir/
done
echo "${style_green}Write done.${style_normal}"
# devicetree overlays
echo "Write devicetree-overlays to boot partition..."
rm -f $tmpdir/overlays/*.dtbo
for dtbo in `find ${DeployFileDir} -name "*.dtbo"`; do
bname=`basename $dtbo`
if ! echo "${bname}" | grep -q 'Image-'; then
cp -f ${dtbo} $tmpdir/overlays/
fi
done
echo "${style_green}Write done.${style_normal}"
# TODO bootfiles?
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}*
KernelFileName=`basename $DeployedFile`
cp $DeployedFile $tmpdir/boot/
ln -sf $KernelFileName $tmpdir/boot/$KernelImageType
echo "${style_green}Write done.${style_normal}"
# rootfs/lib/modules
echo "Write modules to rootfs..."
kernel_abi_ver=`echo $KernelFileName | sed 's:'${KernelImageType}'-::g'`
rm -rf $tmpdir/lib/modules/$kernel_abi_ver
for modules in `find ${DeployFileDir} -name "modules-${Machine}.tgz"`; do
tar xvzf ${modules} -C $tmpdir/
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}"
# cleanup
echo "Unmount rootfs partition..."
umount ${DevicePath}2 || exit 1
echo "${style_green}Unmount done.${style_normal}"
rm -rf $tmpdir
}
CheckPrerequisite "time"
CheckPrerequisite "gunzip"
CheckPrerequisite "dd"
CheckPrerequisite "parted"
CheckPrerequisite "resize2fs"
CheckPrerequisite "depmod"

View File

@@ -0,0 +1,118 @@
#! /bin/bash
# machine-ti-old-omap.inc
# (c) Copyright 2018 Andreas Müller <schnitzeltony@gmail.com>
# Licensed under terms of GPLv2
#
# This script contains settings and callbacks fir raspberrypi boards
# default settings
DEFAULT_MACHINE_FAMILY=''
DEFAULT_FIND_ROOTFS='-name *.rootfs.tar.bz2 -type l'
DEFAULT_KERNEL_IMAGE_TYPE='Image'
DEFAULT_FIND_KERNEL="-name ${DEFAULT_KERNEL_IMAGE_TYPE}-abiversion-* -type l"
# callback for card-write
RootCardWriteCallback() {
tmpdir=`mktemp -d`
# bootloaders and environment
echo "Writing bootloaders and environment to boot partition"
mount ${DevicePath}1 $tmpdir || exit 1
rm -rf $tmpdir/*
if [ -e ${DeployFileDir}/MLO ] ; then
cp ${DeployFileDir}/MLO $tmpdir/MLO
fi
cp ${DeployFileDir}/u-boot.img $tmpdir/u-boot.img
if [ -e ${DeployFileDir}/uEnv.txt ] ; then
cp ${DeployFileDir}/uEnv.txt $tmpdir
fi
sleep 1
umount ${DevicePath}1 || exit 1
# rootfs
time(
mount ${DevicePath}2 $tmpdir || exit 1
rm -rf $tmpdir/*
cd $tmpdir
tar xvjf $DeployedFile
cd ..
umount ${DevicePath}2 || exit 1
)
rm -rf $tmpdir
}
# callback for card-kernel-write
RootCardKernelWriteCallback() {
# TODO
tmpdir=`mktemp -d`
# initial kernel / devicetrees
mount ${DevicePath}1 $tmpdir || exit 1
echo "Write initial kernel and devicetrees to boot partition..."
# kernel (REVISIT for aarch64)
cp -f $DeployedFile $tmpdir/kernel7.img
# devicetrees
rm -f $tmpdir/*.dtb
for dtb in `find ${DeployFileDir} -name "bcm27*.dtb"`; do
cp -f $dtb $tmpdir/
done
echo "${style_green}Write done.${style_normal}"
# devicetree overlays
echo "Write devicetree-overlays to boot partition..."
rm -f $tmpdir/overlays/*.dtbo
for dtbo in `find ${DeployFileDir} -name "*.dtbo"`; do
bname=`basename $dtbo`
if ! echo "${bname}" | grep -q 'Image-'; then
cp -f ${dtbo} $tmpdir/overlays/
fi
done
echo "${style_green}Write done.${style_normal}"
# TODO bootfiles?
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}*
KernelFileName=`basename $DeployedFile`
cp $DeployedFile $tmpdir/boot/
ln -sf $KernelFileName $tmpdir/boot/$KernelImageType
echo "${style_green}Write done.${style_normal}"
# rootfs/lib/modules
echo "Write modules to rootfs..."
kernel_abi_ver=`echo $KernelFileName | sed 's:'${KernelImageType}'-::g'`
rm -rf $tmpdir/lib/modules/$kernel_abi_ver
for modules in `find ${DeployFileDir} -name "modules-${Machine}.tgz"`; do
tar xvzf ${modules} -C $tmpdir/
done
echo "${style_green}Write done.${style_normal}"
# run depmod (stolen from dempodwrapper)
sys_map=`realpath ${OeTmpDir}/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}"
# cleanup
echo "Unmount rootfs partition..."
umount ${DevicePath}2 || exit 1
echo "${style_green}Unmount done.${style_normal}"
rm -rf $tmpdir
}
CheckPrerequisite "time"
CheckPrerequisite "gunzip"
CheckPrerequisite "dd"
CheckPrerequisite "parted"
CheckPrerequisite "resize2fs"
CheckPrerequisite "depmod"