beaglebone/tools/bb_sd_prep.sh

87 lines
2 KiB
Bash
Executable file

#!/bin/bash
# cleanup
# trap ctrl-c and call ctrl_c()
trap cleanup INT
function cleanup() {
umount -f /tmp/sdcard.boot.$$ >/dev/null 2>&1
umount -f /tmp/sdcard.root.$$ >/dev/null 2>&1
echo "Aborted."
exit 1
}
if [[ $EUID -ne 0 ]]; then
echo "ERROR: this script must be run as root (i.e. using \`sudo'.)"
exit 1
fi
PATH=${PATH}:/sbin:/usr/sbin
DEV="$1"
IMGDIR="$2"
if [ -z "$IMGDIR" ]; then
IMGDIR=/home/dburr/devel/poky/build/tmp/deploy/images/beaglebone
fi
if [ -z "$DEV" -o -z "$IMGDIR" ]; then
echo "USAGE: bb_sd_prep.sh device build-dir"
exit 1
fi
if [ ! -b "$DEV" ]; then
echo "ERROR: no such device: $DEV"
exit 1
fi
if [ ! -d "$IMGDIR" -o ! -f "$IMGDIR/MLO-beaglebone" ]; then
echo "ERROR: $IMGDIR does not appear to be a build directory"
exit 1
fi
pushd "$IMGDIR" >/dev/null 2>&1
echo "Unmoounting all partitions on $DEV:"
PARTS="`df | grep ^$DEV | awk '{ print $1 }'`"
if [ ! -z "$PARTS" ]; then
for PART in $PARTS; do
echo "...$PART"
umount "$PART"
done
fi
echo "...done"
echo "Preparing partition table on $DEV:"
dd if=/dev/zero of=$DEV bs=512 count=10 >/dev/null 2>&1
echo -e "n\np\n1\n\n4095\nn\np\n2\n\n\nt\n1\ne\na\n1\nw\n" | fdisk $DEV >/dev/null 2>&1
echo "...done"
echo "Formatting partition on $DEV:"
mkfs.vfat -F 16 -n "boot" ${DEV}1
mkfs.ext2 -j -L "root" ${DEV}2
echo "...done"
echo "Mounting partition on $DEV:"
mkdir -p /tmp/sdcard.boot.$$
mkdir -p /tmp/sdcard.root.$$
mount -t vfat "${DEV}1" /tmp/sdcard.boot.$$
mount -t ext4 "${DEV}2" /tmp/sdcard.root.$$
echo "...done"
echo "Copying bootloader..."
cp MLO-beaglebone /mnt/sdcard.boot.$$/MLO
cp u-boot-beaglebone.img /mnt/sdcard.boot.$$/u-boot.img
echo "...done"
echo "Creating root filesystem..."
tar x -C /tmp/sdcard.root.$$ -f core-image-*-beaglebone.tar.bz2
echo "...done"
echo "unmounting SD card..."
sync;sync;sync
umount /tmp/sdcard.boot.$$
umount /tmp/sdcard.root.$$
echo "...done"
rmdir /tmp/sdcard.boot.$$
rmdir /tmp/sdcard.root.$$
echo "Yocto has been installed and is ready to be booted."