Move my card scripts here - start with freescale imx6

Signed-off-by: Andreas Müller <schnitzeltony@gmail.com>
This commit is contained in:
Andreas Müller
2018-09-20 19:06:28 +02:00
parent 366f18f9bb
commit a5cbc56520
3 changed files with 227 additions and 0 deletions

View File

@@ -0,0 +1,124 @@
#! /bin/bash
# write-card.sh
# (c) Copyright 2013-2018 Andreas Müller <schnitzeltony@gmail.com>
# Licensed under terms of GPLv2
#
# This script writes image to sdcard and aligns rootfs partition to max size.
SelectRootfs() {
# OE environment found?
if [ -z $BUILDDIR ]; then
echo "The environment variable BUILDDIR is not set. It is usually set before running bitbake."
exit 1
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
TMPDIR=`echo $tmp_dir | sed -e s/'TMPDIR='/''/g`
fi
done
for BuildPath in ${TMPDIR}-*; do
for i in `find ${BuildPath}/deploy/images/${MACHINE} -name *.sdcard -o -name *.wic.gz | sort` ; do
iCount=`expr $iCount + 1`
RootFileNameArr[${iCount}]=$i
strSelection="$strSelection $iCount "`basename $i`
done
done
# were files found?
if [ $iCount -eq 0 ]; then
echo "No rootfs files found in ${TMPDIR}-\*"
exit 1
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
echo Cancel selected 1
return 1
fi
RootFsFile=${RootFileNameArr[$menuitem]}
echo
}
run_user() {
if [ -z $DevicePath ]; then
# DevicePath for memory card
SelectCardDevice || exit 1
fi
if [ -z $RootFsFile ]; then
# select rootfs
SelectRootfs || exit 1
fi
RootParams="$DevicePath $RootFsFile"
}
run_root() {
# device node valid?
if [ ! -b $DevicePath ] ; then
echo "$DevicePath is not a valid block device!"
exit 1
fi
# rootfs valid?
if [ ! -e $RootFsFile ] ; then
echo "$RootFsFile can not be found!"
exit 1
fi
IMAGEDIR=$(dirname $RootFsFile)
# check if the card is currently mounted
MOUNTSTR=$(mount | grep $DevicePath)
if [ -n "$MOUNTSTR" ] ; then
echo -e "\n$DevicePath is currenly mounted. Needs unmounting..."
umount -f ${DevicePath}?*
fi
# rootfs write/resize to card fit
time(
echo "Writing $RootFsFile to $DevicePath..."
if echo $RootFsFile | grep -q '.wic.gz'; then
gunzip -c $RootFsFile | dd of=$DevicePath bs=1024K
else
dd of=$DevicePath if=$RootFsFile bs=1024K
fi
sync
echo "Resizing ${DevicePath}2..."
parted -s $DevicePath -- resizepart 2 -0
resize2fs "${DevicePath}2"
)
}
. `dirname $0`/machine.inc
. `dirname $0`/../tools.inc
if [ -z $MACHINE ]; then
MACHINE=$DEFAULT_MACHINE
fi
DevicePath=$1
RootFsFile=$2
# On the 1st call: run user
# After the 2nd call: run root
RootParams='$DevicePath $RootFsFile'
chk_root&&run_root

View File

@@ -0,0 +1,4 @@
# default settings
DEFAULT_MACHINE='*imx*'
DEFAULT_KERNEL_IMAGE_TYPE=uImage
KERNEL_MAJOR_VERSION=4

View File

@@ -0,0 +1,99 @@
#! /bin/bash
# tools.inc
# (c) Copyright 2013-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() {
strSelection=""
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
strSelection="$strSelection $iCount $dev"
fi
done
if [ $iCount -eq 0 ]; then
echo 'No removable devices found!'
exit 1
fi
dialog --title 'Select card device'\
--menu 'Move using [UP] [DOWN],[Enter] to select' 10 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
echo 'Cancel selected SelectCardDevice()'
return 1
fi
DevicePath=${DevicePathArr[$menuitem]}
}
# chk_root() tests if we are running as root. If not, it calls run_user and then
# asks operator how to log on as root. Then the main script is started as root
chk_root() {
# we are not already root?
if [ ! $( id -u ) -eq 0 ]; then
# do all non root operations
run_user
# abort in case run_user was performed without success
if [ $? -ne 0 ] ; then
return 1
fi
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
echo 'Cancel selected chk_root()'
return 1
fi
clear
if [ "x$1" = "x" ] ; then
echo "All data currenly stored on $DevicePath will be overwritten!!"
else
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
return 1 # sice we're 'execing' above, we wont reach this exit
# unless something goes wrong.
fi
}